Browse Source

Implemented new authorization using toml

Paolo Galli 4 years ago
parent
commit
b7c5579a2f
2 changed files with 23 additions and 11 deletions
  1. 6 2
      auth.toml
  2. 17 9
      lcmlog-server

+ 6 - 2
auth.toml

@@ -1,11 +1,15 @@
+# thanks to samuelecolombo (uid 17220) for toml
+
 ["150"]
 auth=["GET 150","GET Admin","POST 150"]
 
 ["Admin"]
 auth=["GET 150","GET Admin","POST 150","POST Admin","UPDATE"]
 
-["Valhalla"] # ex 150
+# ex 150
+["Valhalla"]
 auth=["GET 150","GET Admin"]
 
-["Nirvana"] # ex admin
+# ex admin
+["Nirvana"]
 auth=["GET 150","GET Admin","POST Admin"]

+ 17 - 9
lcmlog-server

@@ -10,6 +10,7 @@ import logging
 import logging.handlers
 import hashlib
 import contextlib
+import toml
 
 
 # We log what happens every time someone connects
@@ -250,15 +251,22 @@ def method_update():
 
 # Checks if the user has the permissions to use the requested method
 def auth(user_id, method, kind):
-	
-
-
-
-#	with open(DIR + "/auth/" + kind + "/" + method) as f:
-#		for line in f:
-#			if int(line) == user_id:
-#				return
-#	raise AuthError()
+	user_type = ""
+	for file_name in ["150","Admin","Valhalla","Nirvana"]:
+		with open(DIR + "/auth/" + file_name) as f:
+			for line in f:
+				line = line.split()[0]
+				if int(line) == user_id:
+					user_type = file_name
+					break
+			if not user_type == "":
+				break
+	else:
+		raise AuthError()
+	auth_list = toml.load(DIR + "/auth/auth.toml")[user_type]["auth"]
+	if not method + " " + kind in auth_list:
+		raise AuthError()
+	return
 
 
 #------------------------------------------------------------------------------