Deploy Caddyfile to valhalla / deploy (push) Successful in 1m23s
Chaptarr has built-in OIDC (/signin-oidc), so per the auth standard it gets Pattern A: new homelab-chaptarr OAuth2 provider, old forward_auth proxy provider removed, Caddy block reduced to internal_only + proxy. setup-books-auth.py now handles both OIDC apps generically. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
4.1 KiB
Python
104 lines
4.1 KiB
Python
# Authentik objects for the books stack (see stacks/media/BOOKS.md).
|
|
# Idempotent. Run via scripts/run-books-auth.sh (ak shell inside authentik_server).
|
|
# - forward_single proxy providers for books (CWA) / storyteller, cloned from
|
|
# homelab-sonarr and attached to the embedded outpost (Pattern B)
|
|
# - native OIDC providers for chaptarr + audiobookshelf (Pattern A)
|
|
# Writes the OIDC client ids/secrets to /tmp/books-oidc.env in the container.
|
|
from authentik.core.models import Application
|
|
from authentik.crypto.models import CertificateKeyPair
|
|
from authentik.flows.models import Flow
|
|
from authentik.outposts.models import Outpost
|
|
from authentik.providers.oauth2.models import OAuth2Provider
|
|
from authentik.providers.proxy.models import ProxyMode, ProxyProvider
|
|
|
|
AUTH_FLOW = Flow.objects.get(pk="b88daebc-c54d-4e85-ae27-8b47384d5d74")
|
|
INVALIDATION_FLOW = Flow.objects.get(pk="3259f9c5-7eab-40bf-81cb-2b0ede6eb7b3")
|
|
SIGNING_KEY = CertificateKeyPair.objects.get(pk="dc7cbd9e-c6d2-4168-b402-e7570bcb59d1")
|
|
SCOPES = [
|
|
"718abef2-504c-452c-9ff0-329eda8031c7", # openid
|
|
"19491dd8-d239-41a4-aa48-030c1471779d", # email
|
|
"ca45a8da-db62-41ba-b1c4-e669bf3c9a82", # profile
|
|
]
|
|
|
|
template = ProxyProvider.objects.get(name="homelab-sonarr")
|
|
outpost = Outpost.objects.get(managed="goauthentik.io/outposts/embedded")
|
|
|
|
FORWARD = [
|
|
# (slug, app name, host)
|
|
("books", "Calibre-Web Automated", "books.ginnoir.com"),
|
|
("storyteller", "Storyteller", "storyteller.ginnoir.com"),
|
|
]
|
|
|
|
for slug, app_name, host in FORWARD:
|
|
p, created = ProxyProvider.objects.get_or_create(
|
|
name=f"homelab-{slug}",
|
|
defaults=dict(
|
|
mode=ProxyMode.FORWARD_SINGLE,
|
|
external_host=f"https://{host}",
|
|
authorization_flow=AUTH_FLOW,
|
|
invalidation_flow=INVALIDATION_FLOW,
|
|
intercept_header_auth=template.intercept_header_auth,
|
|
access_token_validity=template.access_token_validity,
|
|
),
|
|
)
|
|
if created:
|
|
p.property_mappings.set(template.property_mappings.all())
|
|
outpost.providers.add(p)
|
|
app, _ = Application.objects.update_or_create(
|
|
slug=slug,
|
|
defaults=dict(name=app_name, provider=p, meta_launch_url=f"https://{host}"),
|
|
)
|
|
print(f"PROXY {slug}: provider pk={p.pk} created={created} app={app.slug}")
|
|
|
|
# chaptarr started on forward_auth; it has native OIDC, so drop the proxy provider.
|
|
old = ProxyProvider.objects.filter(name="homelab-chaptarr").first()
|
|
if old:
|
|
outpost.providers.remove(old)
|
|
old.delete()
|
|
print("PROXY chaptarr: removed old forward_auth provider")
|
|
|
|
outpost.save() # nudge the embedded outpost to pick up provider changes
|
|
|
|
|
|
def redirect(url):
|
|
return {"url": url, "matching_mode": "strict", "redirect_uri_type": "authorization"}
|
|
|
|
|
|
OIDC = [
|
|
# (slug, app name, host, env prefix, redirect URIs)
|
|
("chaptarr", "Chaptarr", "chaptarr.ginnoir.com", "CHAPTARR_OIDC", [
|
|
redirect("https://chaptarr.ginnoir.com/signin-oidc"),
|
|
]),
|
|
("audiobooks", "Audiobookshelf", "audiobooks.ginnoir.com", "ABS_OIDC", [
|
|
redirect("https://audiobooks.ginnoir.com/auth/openid/callback"),
|
|
redirect("https://audiobooks.ginnoir.com/auth/openid/mobile-redirect"),
|
|
redirect("audiobookshelf://oauth"),
|
|
]),
|
|
]
|
|
|
|
lines = []
|
|
for slug, app_name, host, prefix, uris in OIDC:
|
|
p, created = OAuth2Provider.objects.get_or_create(
|
|
name=f"homelab-{slug}",
|
|
defaults=dict(
|
|
client_type="confidential",
|
|
authorization_flow=AUTH_FLOW,
|
|
invalidation_flow=INVALIDATION_FLOW,
|
|
sub_mode="hashed_user_id",
|
|
signing_key=SIGNING_KEY,
|
|
grant_types=["authorization_code", "refresh_token"],
|
|
_redirect_uris=uris,
|
|
),
|
|
)
|
|
if created:
|
|
p.property_mappings.set(SCOPES)
|
|
Application.objects.update_or_create(
|
|
slug=slug,
|
|
defaults=dict(name=app_name, provider=p, meta_launch_url=f"https://{host}"),
|
|
)
|
|
print(f"OIDC {slug}: provider pk={p.pk} created={created}")
|
|
lines += [f"{prefix}_CLIENT_ID={p.client_id}", f"{prefix}_CLIENT_SECRET={p.client_secret}"]
|
|
|
|
with open("/tmp/books-oidc.env", "w") as f:
|
|
f.write("\n".join(lines) + "\n")
|