radicale_plugins/plugins/radicale_openauth/__init__.py

44 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
2020-09-28 16:17:28 +00:00
import logging
2018-09-23 10:45:39 +00:00
2020-09-28 16:17:28 +00:00
from radicale.log import logger
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
2020-09-28 16:17:28 +00:00
def login(self, login, password):
# Get uid from username
if login is None or login is "":
return ""
res = requests.post(self.get_server() + "/api/login?type=username&username=" + login)
data = res.json()
if "error" in data:
return ""
user = data["uid"]
# Get salt
2018-10-05 22:03:31 +00:00
res1 = requests.post(self.get_server() + "/api/login?type=username&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:
2020-09-28 16:17:28 +00:00
return ""
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
2020-09-28 16:17:28 +00:00
# Check password
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-11-06 20:34:32 +00:00
res2 = requests.post(self.get_server() + "/api/internal/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:
2020-09-28 16:17:28 +00:00
return user
return ""