Files
homelabstack/scripts/setup-books-auth.py
T
ginnoirandClaude Opus 5.5 72688f48ac
Deploy Caddyfile to valhalla / deploy (push) Failing after 1m37s
feat(authentik): books-stack providers + ABS OIDC client
Adds idempotent scripts/run-books-auth.sh (ak shell) that creates
forward_single proxy providers for chaptarr/books/storyteller on the
embedded outpost and the homelab-audiobooks OIDC provider. Records the
ABS client credentials in media stack.env and marks host prep done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 02:44:32 -05:00

81 lines
3.5 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 chaptarr / books (CWA) / storyteller,
# cloned from homelab-sonarr and attached to the embedded outpost
# - native OIDC provider for audiobookshelf (Pattern A)
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)
("chaptarr", "Chaptarr", "chaptarr.ginnoir.com"),
("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}")
outpost.save() # nudge the embedded outpost to pick up new providers
abs_host = "audiobooks.ginnoir.com"
abs_p, created = OAuth2Provider.objects.get_or_create(
name="homelab-audiobooks",
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=[
{"url": f"https://{abs_host}/auth/openid/callback", "matching_mode": "strict", "redirect_uri_type": "authorization"},
{"url": f"https://{abs_host}/auth/openid/mobile-redirect", "matching_mode": "strict", "redirect_uri_type": "authorization"},
{"url": "audiobookshelf://oauth", "matching_mode": "strict", "redirect_uri_type": "authorization"},
],
),
)
if created:
abs_p.property_mappings.set(SCOPES)
Application.objects.update_or_create(
slug="audiobooks",
defaults=dict(name="Audiobookshelf", provider=abs_p, meta_launch_url=f"https://{abs_host}"),
)
print(f"OIDC audiobooks: provider pk={abs_p.pk} created={created}")
with open("/tmp/books-abs-oidc.env", "w") as f:
f.write(f"ABS_OIDC_CLIENT_ID={abs_p.client_id}\nABS_OIDC_CLIENT_SECRET={abs_p.client_secret}\n")