radicale_plugins/plugins/radicale_stamm_auth/__init__.py
2018-09-23 12:45:39 +02:00

45 lines
1.3 KiB
Python

from radicale.auth import BaseAuth
import urllib.request
import json
import hashlib
import requests
class Auth(BaseAuth):
def get_server(self):
return self.configuration.get("auth", "server")
def is_authenticated(self, user, password):
if user is None:
return False
res1 = requests.post(self.get_server + "/api/login?uid=" + user)
data1 = res1.json()
if "error" in data:
return False
salt = data["salt"]
id = self.configuration.get("auth", "client_id")
secret = self.configuration.get("auth", "client_secret")
password = hashlib.sha512(salt + password.encode()).hexdigest())
res2=requests.post("/api/internel/password", params = {
"client_id": id, "client_secret": secret}, json = {"uid": user, "password": password})
data2=res2.json();
if "success" in data2 and data["success"] is True:
return True
return False
def map_login_to_user(self, login):
# Get uid from username
if login is None or login is "":
return None
req_data=dict()
res=requests.post(self.get_server + "/api/login?username=" + login)
data=res.json()
if "error" in data:
return None
return data["uid"]