2022-04-29 14:13:54 +02:00
|
|
|
import structlog
|
|
|
|
from typing import List
|
2022-04-22 18:34:48 +02:00
|
|
|
from pydantic import BaseModel
|
2022-04-29 14:13:54 +02:00
|
|
|
from fastapi import FastAPI, Depends
|
2022-04-22 18:34:48 +02:00
|
|
|
from app.authentik.api import Authentik
|
|
|
|
from app.authentik.models import User
|
2022-04-29 14:56:51 +02:00
|
|
|
from app.consumer.baseConsumer import Consumer
|
|
|
|
from app.consumer.wekan.api import WekanApi
|
|
|
|
from app.consumer.wekan.main import WekanConsumer
|
2022-04-29 14:13:54 +02:00
|
|
|
from app.authentik.settings import AuthentikSettings
|
2022-04-22 18:34:48 +02:00
|
|
|
|
2022-04-29 14:13:54 +02:00
|
|
|
logging = structlog.get_logger()
|
2022-04-22 18:34:48 +02:00
|
|
|
|
|
|
|
class Authentik_Hook_Model(BaseModel):
|
|
|
|
pk: str
|
|
|
|
app: str
|
|
|
|
name: str
|
|
|
|
model_name: str
|
|
|
|
|
|
|
|
|
|
|
|
class Http_request(BaseModel):
|
|
|
|
args: dict
|
|
|
|
path: str
|
|
|
|
method: str
|
|
|
|
|
2022-03-06 15:51:27 +01:00
|
|
|
|
2022-04-23 18:49:28 +02:00
|
|
|
authentikSettings = AuthentikSettings()
|
|
|
|
|
|
|
|
class EventController:
|
2022-03-06 15:51:27 +01:00
|
|
|
|
2022-04-29 14:13:54 +02:00
|
|
|
def __init__(self, authentik_api: Authentik):
|
|
|
|
# try:
|
|
|
|
self._authentik = authentik_api
|
|
|
|
self._sinks = []
|
2022-04-29 14:56:51 +02:00
|
|
|
for sc in Consumer.__subclasses__():
|
2022-04-29 14:13:54 +02:00
|
|
|
obj = sc()
|
|
|
|
self._sinks.append(obj)
|
|
|
|
|
|
|
|
|
|
|
|
# except Exception as e:
|
|
|
|
# raise Exception("Failed to init Api", e)
|
2022-04-22 18:34:48 +02:00
|
|
|
self.jobs = []
|
2022-03-06 15:51:27 +01:00
|
|
|
pass
|
|
|
|
|
2022-04-29 14:56:51 +02:00
|
|
|
def register_api(self, authentik: Authentik, sinks: List[Consumer]):
|
2022-04-29 14:13:54 +02:00
|
|
|
self._authentik = authentik
|
|
|
|
self._sinks = sinks
|
2022-04-22 18:34:48 +02:00
|
|
|
|
|
|
|
def handle_model_created_event(self, model: Authentik_Hook_Model):
|
2022-04-29 14:13:54 +02:00
|
|
|
user: User = self._authentik.get_user_by_pk(model.pk)
|
2022-04-29 15:50:33 +02:00
|
|
|
for sink in self._sinks: # TODO this could run async
|
2022-04-29 14:13:54 +02:00
|
|
|
logging.info(f"Creating User {user.username} in {sink.__class__}")
|
2022-04-29 15:50:33 +02:00
|
|
|
try:
|
|
|
|
sink.create_user(user)
|
|
|
|
except Exception as e:
|
|
|
|
logging.error("create user", exception=str(e), sink=sink, user=user)
|
2022-04-22 18:34:48 +02:00
|
|
|
return True
|