Files
homekeeper/api/app/main.py
T
friessn 6c9cd67a08 Rebuild auth on Keycloak OIDC, fix rootless Ansible deploy
Replaces the single shared HTTP Basic service-account credential (which
caused a production outage from a username mismatch) with per-user login:
Keycloak (already running on this VM for gcnm, now also fronted on
auth.friessn.de with its own "homekeeper" realm) authenticates the user
once via the landing page, FastAPI verifies the OIDC id_token and mints
its own signed session JWT as a cookie, and both Shiny apps forward that
per-session token as a Bearer credential instead of a static shared one.
Authorization is a simple ALLOWED_USERS allowlist; the old auth.users
table and bcrypt seeding are gone entirely.

Also carries forward the in-progress rootless Podman/Quadlet migration
(gitea, homekeeper, podman roles) and fixes a pre-existing bug where
each role's handlers were malformed inside tasks/main.yml instead of
their own handlers/main.yml, which broke ansible-playbook entirely.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FbiCdckkTX2HyAkyi1R39d
2026-07-13 06:31:36 +00:00

48 lines
1.4 KiB
Python

import os
from contextlib import asynccontextmanager
from fastapi import FastAPI
from app.routers import (
apiaries,
auth,
colonies,
inspections,
varroa,
harvests,
actions,
queens,
checklists,
lists,
)
@asynccontextmanager
async def lifespan(app: FastAPI):
from app.database import init_db
init_db()
yield
app = FastAPI(
title="Beekeeper API",
version="1.0.0",
root_path=os.getenv("ROOT_PATH", ""),
lifespan=lifespan,
)
app.include_router(auth.router, prefix="/v1/auth", tags=["auth"])
app.include_router(apiaries.router, prefix="/v1/apiaries", tags=["apiaries"])
app.include_router(colonies.router, prefix="/v1/colonies", tags=["colonies"])
app.include_router(inspections.router, prefix="/v1/inspections", tags=["inspections"])
app.include_router(varroa.router, prefix="/v1/varroa", tags=["varroa"])
app.include_router(harvests.router, prefix="/v1/harvests", tags=["harvests"])
app.include_router(actions.router, prefix="/v1/actions", tags=["actions"])
app.include_router(queens.router, prefix="/v1/queens", tags=["queens"])
app.include_router(checklists.router, prefix="/v1/checklists", tags=["checklists"])
app.include_router(lists.router, prefix="/v1/lists", tags=["lists"])
@app.get("/health")
def health():
return {"status": "ok"}