radicale_plugins/plugins/radicale_openauth/__init__.py

45 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
2018-09-23 10:45:39 +00:00
import requests
2017-12-05 09:32:45 +00:00
class Auth(BaseAuth):
2018-09-23 10:45:39 +00:00
def get_server(self):
return self.configuration.get("auth", "server")
2017-12-05 09:32:45 +00:00
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
2018-09-23 10:45:39 +00:00
2018-09-23 11:07:26 +00:00
res1 = requests.post(self.get_server() + "/api/login?uid=" + user)
2018-09-23 10:45:39 +00:00
data1 = res1.json()
2018-09-23 11:08:43 +00:00
if "error" in data1:
2017-12-05 09:32:45 +00:00
return False
2018-09-23 10:45:39 +00:00
2018-09-23 11:13:53 +00:00
salt = data1["salt"].encode()
2018-09-23 10:45:39 +00:00
id = self.configuration.get("auth", "client_id")
secret = self.configuration.get("auth", "client_secret")
2018-09-23 11:05:29 +00:00
password = hashlib.sha512(salt + password.encode()).hexdigest()
2018-09-23 11:07:26 +00:00
res2 = requests.post(self.get_server() + "/api/internel/password", params={
2018-09-23 11:05:29 +00:00
"client_id": id, "client_secret": secret}, json={"uid": user, "password": password})
data2 = res2.json()
2018-09-23 10:45:39 +00:00
2018-09-23 11:08:43 +00:00
if "success" in data2 and data2["success"] is True:
2018-09-23 10:45:39 +00:00
return True
return False
2017-12-05 09:32:45 +00:00
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
2018-09-23 11:05:29 +00:00
req_data = dict()
2018-09-23 11:07:26 +00:00
res = requests.post(self.get_server() + "/api/login?username=" + login)
2018-09-23 11:05:29 +00:00
data = res.json()
2017-12-05 19:55:18 +00:00
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"]