radicale_plugins/plugins/radicale_stamm_auth/__init__.py

39 lines
1.3 KiB
Python
Raw Normal View History

2017-12-05 09:32:45 +00:00
from radicale.auth import BaseAuth
2017-12-05 19:55:18 +00:00
import urllib.request
import json
2017-12-12 09:52:17 +00:00
import hashlib
2017-12-05 09:32:45 +00:00
class Auth(BaseAuth):
2017-12-05 19:55:18 +00:00
def generate_base_uri(self, endpoint):
2017-12-05 09:32:45 +00:00
server = self.configuration.get("auth", "server")
id = self.configuration.get("auth", "client_id")
secret = self.configuration.get("auth", "client_secret")
return "{}{}?client_id={}&client_secret={}".format(server, endpoint, id, secret)
def is_authenticated(self, user, password):
2017-12-05 19:55:18 +00:00
if user is None:
2017-12-05 09:32:45 +00:00
return False
2017-12-05 19:55:18 +00:00
main_uri = self.generate_base_uri(
2017-12-12 09:52:17 +00:00
"/client/check_pw") + "&hashed=true&uuid=" + user + "&password=" + hashlib.sha512(password.encode()).hexdigest()
2017-12-05 09:32:45 +00:00
req = urllib.request.urlopen(main_uri, data=None)
jsons = req.read()
data = json.loads(jsons)
2017-12-05 19:55:18 +00:00
print(data)
if "error" in data:
2017-12-05 09:32:45 +00:00
return False
return True
2017-12-05 19:55:18 +00:00
def map_login_to_user(self, login):
2018-03-31 23:51:48 +00:00
# Get uid from username
2017-12-05 19:55:18 +00:00
if login is None or login is "":
return None
main_uri = self.generate_base_uri(
2018-03-31 23:51:48 +00:00
"/client/uid") + "&username=" + login
2017-12-05 09:32:45 +00:00
req = urllib.request.urlopen(main_uri, data=None)
jsons = req.read()
data = json.loads(jsons)
2017-12-05 19:55:18 +00:00
print(data)
if "error" in data:
2017-12-05 09:32:45 +00:00
return None
2018-03-31 23:51:48 +00:00
return data["uid"]