feat: switch authentication to Keycloak (OIDC)#4
Merged
Conversation
Replace local username/password login with OIDC Authorization Code + PKCE against login.keyholding.com so identity (creation, password reset, role assignment) is managed centrally instead of in this app. Local `users` rows now key on the Keycloak `sub` claim; the existing seeded admin links by email on first sign-in. Signup, admin password reset, and admin role-change UI removed — Keycloak owns these now. Realm role `library-admin` maps to app-side admin via a userinfo mapper on the Keycloak client. Matching Keycloak realm/client config is provisioned out-of-band by the keycloak-config repo. After deploy, rotate the client secret into librarian-services/oidc and force a new ECS deployment so the new task picks it up. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
✅ Terraform PlanShow plan |
There was a problem hiding this comment.
Pull request overview
This PR switches the application’s authentication from local username/password to Keycloak-backed OIDC Authorization Code + PKCE, aligning identity, password resets, and role assignment with a centralized IdP while keeping a local “mirror” user row for app-specific data.
Changes:
- Introduces OIDC configuration in the Flask app (Authlib client, login/callback/logout flow) and removes local login/signup flows.
- Updates the data model/migrations to support Keycloak linkage (
users.keycloak_sub) and makespassword_hashnullable. - Wires OIDC settings into Terraform/ECS and adds a local Keycloak service for development.
Reviewed changes
Copilot reviewed 20 out of 21 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| terraform/main/variables.tf | Adds OIDC-related Terraform variables (issuer, client_id, admin role). |
| terraform/main/modules/secrets/main.tf | Adds a Secrets Manager secret for the OIDC client secret and outputs its ARN. |
| terraform/main/modules/ecs/variables.tf | Adds ECS module inputs for OIDC env vars + secret ARN. |
| terraform/main/modules/ecs/main.tf | Injects OIDC env vars and OIDC client secret into the ECS task definition. |
| terraform/main/main.tf | Wires OIDC variables and secret ARN through to the ECS module. |
| terraform/main/envs/services/main.tf | Sets service environment OIDC issuer URL and client_id. |
| requirements.txt | Adds Authlib dependency for OIDC integration. |
| migrations/versions/d1f5a2b9c0e4_add_keycloak_sub_to_users.py | Adds keycloak_sub, makes password_hash nullable, and adds uniqueness/indexing. |
| docker-compose.yml | Adds a local Keycloak service (dev mode) with realm import volume. |
| config.py | Adds OIDC config keys sourced from environment variables. |
| app/models.py | Adds User.keycloak_sub and makes password_hash nullable. |
| app/library/routes.py | Removes “create user on the fly” behavior; uses session user_id to resolve the current user. |
| app/forms.py | Removes the signup form definitions and related validators/imports. |
| app/extensions.py | Adds an Authlib OAuth extension instance. |
| app/auth/templates/auth/signup.html | Removes legacy signup page template. |
| app/auth/templates/auth/login.html | Removes legacy login page template. |
| app/auth/routes.py | Replaces local login/signup with OIDC login, callback, logout, and user upsert logic. |
| app/admin/templates/admin/users.html | Removes role/password management UI; clarifies Keycloak ownership and keeps “delete local row”. |
| app/admin/templates/admin/dashboard.html | Updates dashboard copy to reflect Keycloak-managed identity/roles/passwords. |
| app/admin/routes.py | Removes reset-password and role-change endpoints; keeps delete behavior with updated messaging. |
| app/init.py | Registers Authlib OIDC client during app creation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+17
to
+22
| oauth.init_app(app) | ||
| oauth.register( | ||
| name="keycloak", | ||
| server_metadata_url=f"{app.config['OIDC_ISSUER_URL'].rstrip('/')}/.well-known/openid-configuration", | ||
| client_id=app.config["OIDC_CLIENT_ID"], | ||
| client_secret=app.config["OIDC_CLIENT_SECRET"], |
| @bp.route("/callback") | ||
| def callback(): | ||
| token = oauth.keycloak.authorize_access_token() | ||
| claims = token.get("userinfo") or {} |
Comment on lines
+68
to
+70
| email = claims.get("email") or f"{sub}@unknown.local" | ||
| username = claims.get("preferred_username") or email | ||
|
|
Comment on lines
+26
to
+30
| op.create_index("ix_users_keycloak_sub", "users", ["keycloak_sub"]) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| op.drop_index("ix_users_keycloak_sub", table_name="users") |
| def downgrade(): | ||
| op.drop_index("ix_users_keycloak_sub", table_name="users") | ||
| op.drop_constraint("uq_users_keycloak_sub", "users", type_="unique") | ||
| op.drop_column("users", "keycloak_sub") |
| # Stable Keycloak `sub` claim — the only identifier guaranteed not to | ||
| # change on email/username edits in the IdP. Nullable to allow the | ||
| # existing seeded admin row to be linked by email on first OIDC login. | ||
| keycloak_sub = db.Column(db.String(64), unique=True, nullable=True, index=True) |
This was referenced May 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
login.keyholding.comso identity, password reset, and role assignment are managed centrally instead of in this app.users.keycloak_sub(unique, indexed) and relaxpassword_hashto nullable. The existing seeded admin links by email on first sign-in.OIDC_ISSUER_URL/OIDC_CLIENT_IDas plain envs,OIDC_CLIENT_SECRETfrom a new Secrets Manager secret (rotated out-of-band by the keycloak-config repo).docker-compose.ymlfor dev parity.Notable decisions
sub(with email fallback) rather than email or username so admin renames in Keycloak don't break the link.password_hashkept on the model as nullable rather than dropped. Splitting "stop using" from "remove" lets us roll back without data loss.oidcSecrets Manager resource useslifecycle.ignore_changes = [secret_string]so Terraform owns the resource shell while the keycloak-config side rotates the value.Deploy sequence (after merge)
`aws secretsmanager put-secret-value --region eu-west-1 --secret-id librarian-services/oidc --secret-string '{"client_secret":"…"}'`
Test plan
🤖 Generated with Claude Code