airflow.providers.keycloak.auth_manager.services.token

Attributes

CLI_LOOPBACK_HOSTS

CLI_CALLBACK_PATH

log

Functions

build_keycloak_unavailable_error(error)

translate_keycloak_outage([rejected_detail])

Raise a 503 instead of a Keycloak error that is_keycloak_outage() matches.

is_cli_loopback_redirect_uri(redirect_uri)

Return whether redirect_uri is a loopback callback a command-line client listens on.

build_user_from_userinfo(client, tokens, *[, ...])

Build the Airflow user that Keycloak's UserInfo endpoint describes for tokens.

create_token_for(username, password[, ...])

create_jwt_federated_token(assertion[, ...])

Create a token from a Keycloak access token obtained outside of Airflow.

create_client_credentials_token(client_id, client_secret)

Create token using OAuth2 client_credentials grant type.

create_token_from_authorization_code(code, ...)

Create a CLI token from an authorization code obtained through /login/cli.

refresh_cli_token(token, expiration_time_in_seconds)

Create a CLI token with fresh Keycloak tokens from a CLI token that is still valid.

Module Contents

airflow.providers.keycloak.auth_manager.services.token.CLI_LOOPBACK_HOSTS[source]
airflow.providers.keycloak.auth_manager.services.token.CLI_CALLBACK_PATH = '/callback'[source]
airflow.providers.keycloak.auth_manager.services.token.log[source]
airflow.providers.keycloak.auth_manager.services.token.build_keycloak_unavailable_error(error)[source]
airflow.providers.keycloak.auth_manager.services.token.translate_keycloak_outage(rejected_detail=None)[source]

Raise a 503 instead of a Keycloak error that is_keycloak_outage() matches.

With rejected_detail, any other Keycloak error becomes a 403 with that detail.

airflow.providers.keycloak.auth_manager.services.token.is_cli_loopback_redirect_uri(redirect_uri)[source]

Return whether redirect_uri is a loopback callback a command-line client listens on.

airflow.providers.keycloak.auth_manager.services.token.build_user_from_userinfo(client, tokens, *, is_service_account=False)[source]

Build the Airflow user that Keycloak’s UserInfo endpoint describes for tokens.

airflow.providers.keycloak.auth_manager.services.token.create_token_for(username, password, expiration_time_in_seconds=conf.getint('api_auth', 'jwt_expiration_time'))[source]
airflow.providers.keycloak.auth_manager.services.token.create_jwt_federated_token(assertion, expiration_time_in_seconds=conf.getint('api_auth', 'jwt_expiration_time'))[source]

Create a token from a Keycloak access token obtained outside of Airflow.

This authentication flow accepts an access token issued by Keycloak through any Keycloak-native mechanism (e.g. a “Signed JWT - Federated” client bound to an external OIDC identity provider such as a Kubernetes ServiceAccount issuer, or AWS IAM outbound identity federation). Airflow does not obtain the token on the caller’s behalf, the caller must obtain it directly from Keycloak’s token endpoint. Airflow verifies the token and calls Keycloak’s UserInfo endpoint to validate it and retrieve user information.

The token’s signature, issuer, and audience are verified against this realm’s JWKS. The aud claim (a string or a list) must include this Airflow client’s id, which requires an Audience mapper on the federated client’s scope in Keycloak. The calling client (azp) must also appear in the jwt_federated_client_ids allow-list below – an aud match alone only proves the token was meant for Airflow, not that the issuing client has been vetted for machine auth. Any Keycloak validation error is returned as a generic 403 Invalid Keycloak assertion response.

airflow.providers.keycloak.auth_manager.services.token.create_client_credentials_token(client_id, client_secret, expiration_time_in_seconds=conf.getint('api_auth', 'jwt_expiration_time'))[source]

Create token using OAuth2 client_credentials grant type.

This authentication flow uses the provided client_id and client_secret to obtain a token for a service account. The Keycloak client must have: - Service accounts roles: ON - Client Authentication: ON (confidential client)

The service account must be configured with the appropriate roles/permissions.

Only the client Airflow is configured to use is accepted. The route this is reached from is unauthenticated, so without that restriction the credentials of any confidential client in the realm would be usable to obtain an Airflow token.

airflow.providers.keycloak.auth_manager.services.token.create_token_from_authorization_code(code, code_verifier, redirect_uri, expiration_time_in_seconds)[source]

Create a CLI token from an authorization code obtained through /login/cli.

Keycloak checks code_verifier against the challenge /login/cli forwarded, so the code is useless to anyone who intercepted it on the loopback redirect.

airflow.providers.keycloak.auth_manager.services.token.refresh_cli_token(token, expiration_time_in_seconds)[source]

Create a CLI token with fresh Keycloak tokens from a CLI token that is still valid.

The new token expires no later than the old one, so refreshing never extends a CLI session, and lasts at most expiration_time_in_seconds, so a longer-lived API token does not refresh into a CLI token outliving [api_auth] jwt_cli_expiration_time. On Airflow 3.2+ the old token is revoked; earlier versions have no token revocation, so it stays usable until it expires. Expired and revoked tokens are refused by get_user_from_token.

Was this entry helpful?