Files
homelabstack/scripts/setup-books-auth.py
T
ginnoirandClaude Opus 5.5 e1fd7acce9 fix(authentik): chaptarr verified-email mapping + ginnoir-only binding
Authentik's default email scope hardcodes email_verified=false, which
Chaptarr's OIDC rejects. Chaptarr gets its own verified email mapping,
and the app is bound to the ginnoir user so the email allowlist can't
be matched by another account.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 13:52:36 -05:00

125 lines
5.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}"]
# chaptarr insists on email_verified=true, but authentik's default email
# mapping hardcodes False. Give chaptarr its own email mapping, and bind the
# app to ginnoir only so nobody can match the allowlist by editing their email.
from authentik.core.models import User
from authentik.policies.models import PolicyBinding
from authentik.providers.oauth2.models import ScopeMapping
verified, _ = ScopeMapping.objects.update_or_create(
name="homelab: OpenID 'email' (verified)",
defaults=dict(
scope_name="email",
description="Email address (marked verified)",
expression='return {"email": request.user.email, "email_verified": True}',
),
)
ch = OAuth2Provider.objects.get(name="homelab-chaptarr")
ch.property_mappings.set([SCOPES[0], SCOPES[2], verified.pk])
ch_app = Application.objects.get(slug="chaptarr")
PolicyBinding.objects.get_or_create(target=ch_app, user=User.objects.get(username="ginnoir"), defaults=dict(order=0))
print(f"OIDC chaptarr: verified-email mapping + ginnoir-only binding")
with open("/tmp/books-oidc.env", "w") as f:
f.write("\n".join(lines) + "\n")