This commit is contained in:
parent
f6597e8fae
commit
d9e68d997d
@ -1,6 +1,6 @@
|
|||||||
"""Application configuraiton."""
|
"""Application configuraiton."""
|
||||||
|
|
||||||
from datetime import timedelta
|
import logging
|
||||||
from os import environ
|
from os import environ
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@ -29,3 +29,12 @@ TEMPLATE_DIR = Path(".").absolute() / "keycloak_collective_portal" / "templates"
|
|||||||
|
|
||||||
# Theme selection
|
# Theme selection
|
||||||
APP_THEME = environ.get("APP_THEME", "default")
|
APP_THEME = environ.get("APP_THEME", "default")
|
||||||
|
|
||||||
|
# Log level
|
||||||
|
LOG_LEVEL = environ.get("APP_LOG_LEVEL", "info")
|
||||||
|
if LOG_LEVEL == "info":
|
||||||
|
APP_LOG_LEVEL = logging.INFO
|
||||||
|
elif LOG_LEVEL == "debug":
|
||||||
|
APP_LOG_LEVEL = logging.DEBUG
|
||||||
|
else:
|
||||||
|
APP_LOG_LEVEL = logging.INFO
|
||||||
|
@ -12,8 +12,10 @@ async def logged_in(request: Request):
|
|||||||
from keycloak_collective_portal.exceptions import RequiresLoginException
|
from keycloak_collective_portal.exceptions import RequiresLoginException
|
||||||
|
|
||||||
user = request.session.get("user")
|
user = request.session.get("user")
|
||||||
|
|
||||||
if not user:
|
if not user:
|
||||||
raise RequiresLoginException
|
raise RequiresLoginException
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
"""App entrypoint."""
|
"""App entrypoint."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
from fastapi import FastAPI, Request
|
from fastapi import FastAPI, Request
|
||||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
@ -8,6 +10,7 @@ from starlette.exceptions import HTTPException
|
|||||||
from starlette.middleware.sessions import SessionMiddleware
|
from starlette.middleware.sessions import SessionMiddleware
|
||||||
|
|
||||||
from keycloak_collective_portal.config import (
|
from keycloak_collective_portal.config import (
|
||||||
|
APP_LOG_LEVEL,
|
||||||
APP_SECRET_KEY,
|
APP_SECRET_KEY,
|
||||||
APP_THEME,
|
APP_THEME,
|
||||||
REDIS_DB,
|
REDIS_DB,
|
||||||
@ -28,6 +31,9 @@ from keycloak_collective_portal.routes import (
|
|||||||
root,
|
root,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
log = logging.getLogger("uvicorn")
|
||||||
|
log.setLevel(APP_LOG_LEVEL)
|
||||||
|
|
||||||
app = FastAPI(docs_url=None, redoc_url=None)
|
app = FastAPI(docs_url=None, redoc_url=None)
|
||||||
|
|
||||||
|
|
||||||
@ -60,9 +66,14 @@ app.add_middleware(SessionMiddleware, secret_key=APP_SECRET_KEY)
|
|||||||
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
||||||
|
|
||||||
app.state.oidc = init_oidc()
|
app.state.oidc = init_oidc()
|
||||||
|
log.info("Initialised OpenID Connect client (for Keycloak logins)")
|
||||||
|
|
||||||
app.state.keycloak = init_keycloak()
|
app.state.keycloak = init_keycloak()
|
||||||
|
log.info("Initialised Keycloak admin client (for Keycloak REST API)")
|
||||||
|
|
||||||
app.state.templates = Jinja2Templates(directory=TEMPLATE_DIR)
|
app.state.templates = Jinja2Templates(directory=TEMPLATE_DIR)
|
||||||
app.state.theme = APP_THEME
|
app.state.theme = APP_THEME
|
||||||
|
app.state.log = log
|
||||||
|
|
||||||
app.include_router(invite.router)
|
app.include_router(invite.router)
|
||||||
app.include_router(oidc.router)
|
app.include_router(oidc.router)
|
||||||
|
@ -22,6 +22,7 @@ async def invite_keycloak_create(
|
|||||||
username = user["preferred_username"]
|
username = user["preferred_username"]
|
||||||
|
|
||||||
new_invite = {"link": str(uuid4()), "time": str(dt.now())}
|
new_invite = {"link": str(uuid4()), "time": str(dt.now())}
|
||||||
|
request.app.state.log.info(f"Generated new invite: {new_invite}")
|
||||||
|
|
||||||
invites = await request.app.state.redis.get(username)
|
invites = await request.app.state.redis.get(username)
|
||||||
if invites:
|
if invites:
|
||||||
@ -39,10 +40,13 @@ async def invite_keycloak_delete(
|
|||||||
request: Request, user=Depends(get_user), invites=Depends(get_invites)
|
request: Request, user=Depends(get_user), invites=Depends(get_invites)
|
||||||
):
|
):
|
||||||
username = user["preferred_username"]
|
username = user["preferred_username"]
|
||||||
|
|
||||||
invite_to_delete = request.query_params.get("invite")
|
invite_to_delete = request.query_params.get("invite")
|
||||||
|
|
||||||
invites = await request.app.state.redis.get(username)
|
invites = await request.app.state.redis.get(username)
|
||||||
|
request.app.state.log.info(f"Retrieved invites: {invites}")
|
||||||
|
|
||||||
purged = [i for i in invites if i["link"] != invite_to_delete]
|
purged = [i for i in invites if i["link"] != invite_to_delete]
|
||||||
|
request.app.state.log.info(f"Purged invites: {invites}")
|
||||||
|
|
||||||
await request.app.state.redis.set(user["preferred_username"], purged)
|
await request.app.state.redis.set(user["preferred_username"], purged)
|
||||||
|
|
||||||
|
@ -82,6 +82,9 @@ def form_keycloak_register(
|
|||||||
)
|
)
|
||||||
request.app.state.keycloak.send_verify_email(user_id=user_id)
|
request.app.state.keycloak.send_verify_email(user_id=user_id)
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
|
request.app.state.log.error(
|
||||||
|
f"Keycloak user registration failed, saw: {exception}"
|
||||||
|
)
|
||||||
message = json.loads(exception.error_message).get(
|
message = json.loads(exception.error_message).get(
|
||||||
"errorMessage", "Unknown reason!"
|
"errorMessage", "Unknown reason!"
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user