This commit is contained in:
Philipp Rothmann 2022-04-23 18:49:28 +02:00
parent 6251d40055
commit b0112e05b5
6 changed files with 50 additions and 33 deletions

6
.env.sample Normal file
View file

@ -0,0 +1,6 @@
AUTHENTIK_BASEURL="http://localhost:9000/"
AUTHENTIK_TOKEN="foobar123"
WEKAN_BASEURL="http://localhost:3000"
WEKAN_USER="api"
WEKAN_PASSWORD="foobar123"

View file

@ -21,7 +21,7 @@ def test_get_user_by_username(api: Authentik):
is_active=True, is_active=True,
attributes={} attributes={}
) )
u = api.get_user(u) u = api.get_user(u)
assert not u == None assert not u == None
assert u.username == "akadmin" assert u.username == "akadmin"

View file

@ -3,8 +3,7 @@ from fastapi import FastAPI
from app.authentik.api import Authentik from app.authentik.api import Authentik
from app.authentik.models import User from app.authentik.models import User
from app.wekan.api import Wekan from app.wekan.api import Wekan
from app.settings import AuthentikSettings from app.settings import AuthentikSettings, WekanSettings
class Authentik_Hook_Model(BaseModel): class Authentik_Hook_Model(BaseModel):
@ -19,20 +18,31 @@ class Http_request(BaseModel):
path: str path: str
method: str method: str
authentikSettings = AuthentikSettings()
class Event_Controller: authentikSettings = AuthentikSettings()
wekanSettings = WekanSettings()
class EventController:
def __init__(self): def __init__(self):
try:
self.authentik = Authentik(
authentikSettings.token, authentikSettings.baseurl)
self.wekan = Wekan(wekanSettings.baseurl,
wekanSettings.user, wekanSettings.password)
except Exception as e:
raise Exception("Failed to init Api", e)
self.jobs = [] self.jobs = []
pass pass
def register_api(self, authentik: Authentik, wekan: Wekan): def register_api(self, authentik: Authentik, wekan: Wekan):
self.authentik = authentik self.authentik = authentik
self.wekan = wekan self.wekan = wekan
def handle_model_created_event(self, model: Authentik_Hook_Model): def handle_model_created_event(self, model: Authentik_Hook_Model):
user: User = self.authentik.get_user_by_pk(model.pk) user: User = self.authentik.get_user_by_pk(model.pk)
if not self.wekan.get_user(user.name): if not self.wekan.get_user(user.name):
self.wekan.create_user(username=user.username, email=user.email, password="") self.wekan.create_user(
username=user.username, email=user.email, password="")
return True return True

View file

@ -6,7 +6,8 @@ from fastapi import Depends, FastAPI, Request, BackgroundTasks
from pydantic import BaseModel from pydantic import BaseModel
from app.authentik.api import Authentik from app.authentik.api import Authentik
from app.authentik.models import User from app.authentik.models import User
from app.event_controller import Authentik_Hook_Model, Event_Controller, Http_request from app.event_controller import Authentik_Hook_Model, EventController, Http_request
from app.settings import AuthentikSettings
from .wekan.api import Wekan from .wekan.api import Wekan
import json import json
@ -21,26 +22,26 @@ async def root():
@app.post("/authentik/hook/") @app.post("/authentik/hook/")
async def hook(model: Authentik_Hook_Model, async def hook(model: Authentik_Hook_Model,
http_request: Http_request, http_request: Http_request,
event_controller: Event_Controller = Depends()): ec: EventController = Depends()):
logging.info(model) logging.info(model)
logging.info(http_request) logging.info(http_request)
if http_request.path == "/api/v3/core/users/": if http_request.path == "/api/v3/core/users/":
event_controller.handle_model_created_event(model) ec.handle_model_created_event(model)
return 200 return 200
# @app.get("/authentik/create_hook/") @app.get("/authentik/create_hook/")
# async def hook(request: Request): async def hook(request: Request):
# a = Authentik(base="http://localhost:9000/", token="foobar123") a = Authentik(base="http://localhost:9000/", token="foobar123")
# res = a.create_web_hook() res = a.create_web_hook(hook_endpoint="http://172.17.0.1:8000/authentik/hook/") # docker localhost
# logging.info(res) logging.info(res)
# @app.get("/authentik/users/create_demo_user/") @app.get("/authentik/users/create_demo_user/")
# async def create_demo_user(request: Request): async def create_demo_user(request: Request):
# a = Authentik(base="http://localhost:9000/", token="foobar123") a = Authentik(base="http://localhost:9000/", token="foobar123")
# try: try:
# user = a.create_user( user = a.create_user(
# User(username="demo", name="dmeo", email="foo@example.org")) User(username="demo", name="dmeo", email="foo@example.org"))
# except Exception as e: # TODO except Exception as e: # TODO
# return e return e
# logging.info(user) logging.info(user)
# return user.dict return user.dict

View file

@ -2,17 +2,17 @@ from pydantic import BaseSettings, Field
class WekanSettings(BaseSettings): class WekanSettings(BaseSettings):
baseurl: str baseurl: str = ""
user: str user: str = ""
password: str password: str = ""
class Config: class Config:
env_file = '.env' env_file = '.env'
env_prefix = 'WEKAN_' env_prefix = 'WEKAN_'
class AuthentikSettings(BaseSettings): class AuthentikSettings(BaseSettings):
baseurl: str baseurl: str = ""
token: str token: str = ""
class Config: class Config:
env_file = '.env' env_file = '.env'

View file

@ -1,6 +1,6 @@
from app.authentik.models import User from app.authentik.models import User
from pytest_mock import MockerFixture from pytest_mock import MockerFixture
from .event_controller import Authentik_Hook_Model, Event_Controller from .event_controller import Authentik_Hook_Model, EventController
import pytest import pytest
@ -11,7 +11,7 @@ def test_handle_model_created_event(mocker: MockerFixture):
authentik_mock = mocker.MagicMock() authentik_mock = mocker.MagicMock()
authentik_mock.get_user_by_pk.return_value = mock_user authentik_mock.get_user_by_pk.return_value = mock_user
model = Authentik_Hook_Model(pk=mock_user.pk, app="authentik_core", name=mock_user.name, model_name="user") model = Authentik_Hook_Model(pk=mock_user.pk, app="authentik_core", name=mock_user.name, model_name="user")
ec = Event_Controller() ec = EventController()
ec.register_api(authentik_mock, wekan_mock) ec.register_api(authentik_mock, wekan_mock)
ec.handle_model_created_event(model) ec.handle_model_created_event(model)
ec.authentik.get_user_by_pk.assert_called() ec.authentik.get_user_by_pk.assert_called()