Skip to content

Add authentication via Openid connect#29

Merged
emanuelelaface merged 18 commits intomasterfrom
openid-connect
Mar 3, 2025
Merged

Add authentication via Openid connect#29
emanuelelaface merged 18 commits intomasterfrom
openid-connect

Conversation

@beenje
Copy link
Copy Markdown
Collaborator

@beenje beenje commented Mar 3, 2025

Added authentication via OpenID Connect (Keycloak) both for the web UI and API.
This is enabled via the OPENID_ENABLED variable (default to False).
When enabled, login to the web UI can only be done via oidc. But it's still possible to use the old authentication for the API (via /api/v2/login). This is required until all mobile clients have been updated.

  • For the web UI, upon successful login via OpenID Connect, we save a session cookie with the user id valid for 12 hours. Users need to login every day.
  • Implementing login in Swagger UI was tricky. We don't want to have to fill the client_secret. I took inspiration on harbor that loads some custom javascript to retrieve the token from localstorage and inject it in the header.
    With some javascript, I inject a dummy token. When this token is received, the backend checks the session cookie instead to see if the user is logged in or not. Users logged in via the web UI don't have to authenticate again in Swagger UI.
    To load some extra javascript, I had to patch fastapi-versioning to disable the doc endpoint and replace it with a custom one. I vendored that plugin as it hasn't been updated in 4 years.
  • For the API, I added a new endpoint /open_id_connect to receive the OIDC code and code_verifier from the mobile clients. It allows to retrieve an access_token and id_token. The id_token is validated as requested by the standard. We can the retrieve the user info (see the flow described here). Upon success, we create a JWT token that is valid for 30 days (same as before) so that mobile clients don't have to login everyday.

This new backend was deployed at MAX IV. We updated the Android client and will deploy it soon to take advantage of the new open_id_connect endpoint.

If OPENID_ENABLED is set to False. Nothing should change.

beenje added 17 commits March 3, 2025 14:56
Project is abandonned. No update in 4 years.
Need a patch to customize the default swagger UI.
Custom doc will be added in main app.
Allow to support old authentication method for the API even when OIDC is enabled.
We need a period to support both until all clients are updated.
Allow to use the session cookie created when login via the web UI.
No need to implement login via Authorize in Swagger UI.

We load some custom javascript to inject a dummy bearer token that is required
by most API endpoints.
The session cookie is httponly and can't be retrieved from javascript.
The token is only used to force the app to check the session to see if the user is logged in or not.
@beenje beenje requested a review from emanuelelaface March 3, 2025 14:04
To use "| None", we need at least 3.10.
We use 3.11 in the docker image. No need to support below.
@emanuelelaface emanuelelaface merged commit 1036bdc into master Mar 3, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants