radicale_plugins/plugins/radicale_openauth/__init__.py

44 lines
1.3 KiB
Python

from radicale.auth import BaseAuth
import urllib.request
import json
import hashlib
import requests
import logging
from radicale.log import logger
class Auth(BaseAuth):
def get_server(self):
return self.configuration.get("auth", "server")
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
res1 = requests.post(self.get_server() + "/api/login?type=username&uid=" + user)
data1 = res1.json()
if "error" in data1:
return ""
salt = data1["salt"].encode()
# Check password
id = self.configuration.get("auth", "client_id")
secret = self.configuration.get("auth", "client_secret")
password = hashlib.sha512(salt + password.encode()).hexdigest()
res2 = requests.post(self.get_server() + "/api/internal/password", params={
"client_id": id, "client_secret": secret}, json={"uid": user, "password": password})
data2 = res2.json()
if "success" in data2 and data2["success"] is True:
return user
return ""