API reference
- class nameko_keycloak.auth.AuthenticationService(keycloak: KeycloakOpenID, fetch_user: Callable[[str, dict[str, Any]], Any | None], sso_cookie_prefix: str = 'nameko-keycloak')[source]
Provides a way to retrieve properly authenticated user from a request.
As we store user credentials in an external service (Keycloak), this service checks for two things:
first, validate access token found in the request
if Keycloak confirms the token is valid, look up local User
Only when the user exists in both Keycloak and local database, we consider them authenticated.
- nameko_keycloak.auth.get_token_from_request(request: Request, cookie_name: str) str | None[source]
Try to locate access token in the incoming request.
The function first reads access token from a (HttpOnly) cookie sent by a browser. If such cookie does not exist, we try a standard OAuth2 approach with token in header (sent by Oauth2 clients like Insomnia).
- class nameko_keycloak.dependencies.KeycloakProvider(*args, **kwargs)[source]
- class nameko_keycloak.fakes.FakeKeycloak[source]
Fake to be used wherever tests need to interact with Keycloak.
This class emulates a few APIs of
KeycloakOpenIDthat we use for SSO workflow.We’re working under a very important assumption here: You need to pass user’s email as
codewhen generating their token. This is obviously not true in real life where Keycloak manages generating secure tokens from one-time codes, but here it simplifies a lot.The Keycloak user database is simulated by a key-value storage where you insert an item when calling
token(), and fetch from storage when callingdecode_token()orrefresh_token().
- class nameko_keycloak.service.KeycloakSsoServiceMixin[source]
Add this to your nameko service to provide SSO authentication with Keycloak.
Expected service dependencies or class attributes:
keycloakwhich must be an instance ofKeycloakProvidersso_cookie_prefix- a string that will be used to namespace cookies (useful when there are multiple SSO-enabled apps hosted on same domain)sso_cookie_path- path part of the URL of your application, set as Path cookie attributesso_login_url- absolute URL to handler which delegates tokeycloak_login_sso()sso_token_url- absolute URL to handler which delegates tokeycloak_token_sso()sso_refresh_token_url- absolute URL to handler which delegates tokeycloak_refresh_token_sso()frontend_url- absolute URL to a user-facing web app that communicates with this backend service
- keycloak_login_sso(request: Request) Response[source]
Redirects to SSO login form configured to return back to HTTP service.
- keycloak_logout(request: Request) Response[source]
Invalidates session in Keycloak, deletes cookies and redirects to login.
Note
Keycloak logout API invalidates only refresh token, not access token. This is by design, as access tokens should be short lived anyway.
- keycloak_refresh_token_sso(request: Request) Response[source]
Generates a new access token, given a cookie with a valid refresh token.
- keycloak_token_sso(request: Request) Response[source]
Handles redirect from successful login in SSO.
The SSO passes a code query string parameter which we then use to generate a OAuth access token. We make sure that a local User exists before they are allowed to reach frontend URL. If all goes well, the access token and several other metadata are stored in cookies.