2024-10-28 21:57:23 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2024-11-02 23:15:51 +00:00
|
|
|
import requests
|
|
|
|
|
2024-11-11 00:33:17 +00:00
|
|
|
from selfprivacy_api.utils import get_domain
|
2024-11-02 23:15:51 +00:00
|
|
|
from selfprivacy_api.models.user import UserDataUser
|
2024-10-28 21:57:23 +00:00
|
|
|
from selfprivacy_api.repositories.users.abstract_user_repository import (
|
|
|
|
AbstractUserRepository,
|
|
|
|
)
|
|
|
|
|
2024-11-02 23:15:51 +00:00
|
|
|
KANIDM_URL = "http://localhost:9001"
|
2024-11-11 00:33:17 +00:00
|
|
|
TEST_TOKEN = """eyJhbGciOiJFUzI1NiIsImtpZCI6IjVkNDUyNzdmZWUxY2UzZmNkMTViZDhkZjE3NTdlMjRkIn0.eyJhY2NvdW50X2lkIjoiYmZlN2MxNmEtNTY1NC00YzAxLWFkMjMtOWU2YjY4OTAxNDEwIiwidG9rZW5faWQiOiJmZTU5NzAxZS1iYzIyLTQwMzctYTEzNy1jZTIxYzBlNDhlZjciLCJsYWJlbCI6InRva2VuMiIsImV4cGlyeSI6bnVsbCwiaXNzdWVkX2F0IjoxNzMxMjgxMzM1LCJwdXJwb3NlIjoicmVhZHdyaXRlIn0.0fj0NAsUtBJWi1KVNKA4qi8EOHUUvaWNzeHbR82zbUVvWynnqm5ndLhFPG0v462qJXFTayonI9YJnkCaAE7a5w"""
|
2024-11-02 23:15:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KanidmQueryError(Exception):
|
2024-11-11 08:41:30 +00:00
|
|
|
"""Error occurred during kanidm query"""
|
|
|
|
|
|
|
|
error: str
|
2024-10-28 21:57:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KanidmUserRepository(AbstractUserRepository):
|
2024-11-02 23:15:51 +00:00
|
|
|
@staticmethod
|
2024-11-11 00:33:17 +00:00
|
|
|
def _send_query(endpoint: str, method: str = "GET", data=None):
|
2024-11-02 23:15:51 +00:00
|
|
|
request_method = getattr(requests, method.lower(), None)
|
|
|
|
|
|
|
|
try:
|
|
|
|
response = request_method(
|
2024-11-11 00:33:17 +00:00
|
|
|
f"{KANIDM_URL}/v1/{endpoint}",
|
|
|
|
json=data,
|
|
|
|
headers={
|
|
|
|
"Authorization": f"Bearer {TEST_TOKEN}",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2024-11-02 23:15:51 +00:00
|
|
|
timeout=0.8, # TODO: change timeout
|
|
|
|
)
|
|
|
|
|
|
|
|
if response.status_code != 200:
|
2024-11-11 09:04:35 +00:00
|
|
|
error_text = getattr(response, "text", "No response error was found...")
|
2024-11-11 08:41:30 +00:00
|
|
|
raise KanidmQueryError(
|
2024-11-11 09:04:35 +00:00
|
|
|
error=f"Kanidm returned {response.status_code} unexpected HTTP status code. Error: {error_text}."
|
2024-11-02 23:15:51 +00:00
|
|
|
)
|
|
|
|
json = response.json()
|
|
|
|
|
|
|
|
return json["data"]
|
|
|
|
except Exception as error:
|
2024-11-11 08:41:30 +00:00
|
|
|
raise KanidmQueryError(error=f"Kanidm request failed! Error: {str(error)}")
|
2024-11-02 23:15:51 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_user(username: str, password: str):
|
2024-11-11 00:33:17 +00:00
|
|
|
data = {
|
|
|
|
"attrs": {
|
|
|
|
"name": [username],
|
|
|
|
"displayname": [username],
|
|
|
|
"mail": [f"{username}@{get_domain()}"],
|
|
|
|
"class": ["user"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 23:15:51 +00:00
|
|
|
return KanidmUserRepository._send_query(
|
2024-11-11 00:33:17 +00:00
|
|
|
endpoint="person",
|
|
|
|
method="POST",
|
|
|
|
data=data,
|
2024-11-02 23:15:51 +00:00
|
|
|
)
|
|
|
|
|
2024-10-28 21:57:23 +00:00
|
|
|
def get_users(
|
|
|
|
exclude_primary: bool = False,
|
|
|
|
exclude_root: bool = False,
|
|
|
|
) -> list[UserDataUser]:
|
2024-11-11 00:33:17 +00:00
|
|
|
return KanidmUserRepository._send_query(endpoint="person", method="GET")
|
2024-10-28 21:57:23 +00:00
|
|
|
|
|
|
|
def delete_user(username: str) -> None:
|
|
|
|
"""Deletes an existing user"""
|
2024-11-02 23:15:51 +00:00
|
|
|
return KanidmUserRepository._send_query()
|
2024-10-28 21:57:23 +00:00
|
|
|
|
|
|
|
def update_user(username: str, password: str) -> None:
|
|
|
|
"""Updates the password of an existing user"""
|
2024-11-02 23:15:51 +00:00
|
|
|
return KanidmUserRepository._send_query()
|
2024-10-28 21:57:23 +00:00
|
|
|
|
|
|
|
def get_user_by_username(username: str) -> Optional[UserDataUser]:
|
|
|
|
"""Retrieves user data (UserDataUser) by username"""
|
2024-11-02 23:15:51 +00:00
|
|
|
return KanidmUserRepository._send_query()
|