integrate/app/main.py

69 lines
1.7 KiB
Python
Raw Normal View History

2022-03-06 15:29:00 +01:00
import logging
import structlog
from unicodedata import name
from urllib.error import HTTPError
2022-03-06 15:51:27 +01:00
from fastapi import Depends, FastAPI, Request
2022-03-06 15:29:00 +01:00
from pydantic import BaseModel
2022-03-04 18:51:33 +01:00
2022-03-06 15:29:00 +01:00
from app.authentik.api import Authentik
from app.authentik.models import User
2022-03-06 15:51:27 +01:00
from app.event_controller import Event_Controller
2022-03-03 16:36:08 +01:00
from .wekan.api import Wekan
import json
2022-03-02 16:10:17 +01:00
2022-03-06 15:29:00 +01:00
logging = structlog.get_logger()
2022-03-06 15:51:27 +01:00
class Authentik_Hook_Model(BaseModel):
2022-03-06 15:29:00 +01:00
pk: str
app: str
name: str
model_name: str
2022-03-06 15:51:27 +01:00
2022-03-06 15:29:00 +01:00
class Http_request(BaseModel):
args: dict
path: str
method: str
2022-03-06 15:51:27 +01:00
2022-03-02 16:10:17 +01:00
app = FastAPI()
2022-03-06 15:51:27 +01:00
2022-03-02 16:10:17 +01:00
@app.get("/")
async def root():
2022-03-03 16:36:08 +01:00
return {'message': 'Hello World'}
2022-03-06 15:29:00 +01:00
@app.post("/authentik/hook/")
2022-03-06 15:51:27 +01:00
async def hook(model: Authentik_Hook_Model,
http_request: Http_request,
event_controller: Event_Controller = Depends(Event_Controller)):
2022-03-04 18:51:33 +01:00
# print(await request.body())
2022-03-06 15:29:00 +01:00
logging.info(model)
logging.info(http_request)
2022-03-06 15:51:27 +01:00
if http_request.path == "/api/v3/core/users/":
event_controller.handle_model_created_event(model)
2022-03-06 15:29:00 +01:00
return 200
2022-03-03 16:36:08 +01:00
# model_created = json.loads(r['body'].split("model_created: ")[1])["model"]
2022-03-04 18:51:33 +01:00
# hook wekan.create_user(model_created["pk"])
2022-03-06 15:51:27 +01:00
2022-03-06 15:29:00 +01:00
@app.get("/authentik/create_hook/")
2022-03-04 18:51:33 +01:00
async def hook(request: Request):
2022-03-06 15:29:00 +01:00
a = Authentik(base="http://localhost:9000/", token="foobar123")
2022-03-06 15:51:27 +01:00
res = a.create_web_hook()
2022-03-06 15:29:00 +01:00
logging.info(res)
2022-03-06 15:51:27 +01:00
2022-03-06 15:29:00 +01:00
@app.get("/authentik/users/create_demo_user/")
async def create_demo_user(request: Request):
a = Authentik(base="http://localhost:9000/", token="foobar123")
try:
2022-03-06 15:51:27 +01:00
user = a.create_user(
User(username="demo", name="dmeo", email="foo@example.org"))
except Exception as e: # TODO
2022-03-06 15:29:00 +01:00
return e
logging.info(user)
2022-03-06 15:51:27 +01:00
return user.dict