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,
attributes={}
)
u = api.get_user(u)
u = api.get_user(u)
assert not u == None
assert u.username == "akadmin"

View file

@ -3,8 +3,7 @@ from fastapi import FastAPI
from app.authentik.api import Authentik
from app.authentik.models import User
from app.wekan.api import Wekan
from app.settings import AuthentikSettings
from app.settings import AuthentikSettings, WekanSettings
class Authentik_Hook_Model(BaseModel):
@ -19,20 +18,31 @@ class Http_request(BaseModel):
path: str
method: str
authentikSettings = AuthentikSettings()
class Event_Controller:
authentikSettings = AuthentikSettings()
wekanSettings = WekanSettings()
class EventController:
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 = []
pass
def register_api(self, authentik: Authentik, wekan: Wekan):
self.authentik = authentik
self.authentik = authentik
self.wekan = wekan
def handle_model_created_event(self, model: Authentik_Hook_Model):
user: User = self.authentik.get_user_by_pk(model.pk)
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

View file

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

View file

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

View file

@ -1,6 +1,6 @@
from app.authentik.models import User
from pytest_mock import MockerFixture
from .event_controller import Authentik_Hook_Model, Event_Controller
from .event_controller import Authentik_Hook_Model, EventController
import pytest
@ -11,7 +11,7 @@ def test_handle_model_created_event(mocker: MockerFixture):
authentik_mock = mocker.MagicMock()
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")
ec = Event_Controller()
ec = EventController()
ec.register_api(authentik_mock, wekan_mock)
ec.handle_model_created_event(model)
ec.authentik.get_user_by_pk.assert_called()