Skip to main content

KeyCloakAuthenticator

Authenticates users via SSO using OIDC.

This authenticator implements a refresh mechanism, ensuring that the tokens stored in the user dict are always up-to-date (if the update is not possible, it forces a re-authentication of the user). It also allows exchanging the user token for tokens that can be used to authenticate against other (external) services.

This Authenticator is built on top of OAuthenticator and should be possible to use some of its configuration values.

Requirements

  • Jupyterhub
  • oauthenticator
  • PyJWT[crypto]
  • openssl_devel (see below)

Installation

pip install keycloakauthenticator

If you enable check_signature, you also need the openssl_devel (or equivalent in your distribution) package.

Usage

In your JupyterHub config file, set the authenticator and configure it:

# Enable the authenticator
c.JupyterHub.authenticator_class = "keycloakauthenticator.KeyCloakAuthenticator"
c.KeyCloakAuthenticator.username_claim = "cern_upn"

# URL to redirect to after logout is complete with auth provider.
c.KeyCloakAuthenticator.logout_redirect_url = "https://cern.ch/swan"
c.KeyCloakAuthenticator.oauth_callback_url = "https://swan.cern.ch/hub/oauth_callback"

# Specify the issuer url, to get all the endpoints automatically from .well-known/openid-configuration
c.KeyCloakAuthenticator.oidc_issuer = "https://auth.cern.ch/auth/realms/cern"

# If you need to set a different scope, like adding the offline option for longer lived refresh token
c.KeyCloakAuthenticator.scope = ["profile", "email", "offline_access"]
# Sync the roles from Keycloak as JupyterHub groups (required for allowed_groups and admin_groups)
c.KeyCloakAuthenticator.manage_groups = True
# Only allow users with these specific roles (or set allow_all = True to allow everyone)
c.KeyCloakAuthenticator.allowed_groups = {"swan-admins", "swan-qa", "swan-devs"}
# Specify the roles that make a user admin
c.KeyCloakAuthenticator.admin_groups = {"swan-admin"}


# If you have the roles in a non default place inside the user token, you can retrieve them
# This must return a set
def claim_roles_key(env, token):
    return set(token.get("app_roles", []))


c.KeyCloakAuthenticator.claim_roles_key = claim_roles_key

# Request access tokens for other services by passing their id's (this uses the token exchange mechanism)
c.KeyCloakAuthenticator.exchange_tokens = ["eos-service", "cernbox-service"]


# If your authenticator needs extra configurations, set them in the pre-spawn hook
def pre_spawn_hook(authenticator, spawner, auth_state):
    spawner.environment["ACCESS_TOKEN"] = auth_state["exchanged_tokens"]["eos-service"]
    spawner.environment["OAUTH_INSPECTION_ENDPOINT"] = authenticator.userdata_url.replace("https://", "")
    spawner.user_uid = auth_state["oauth_user"]["cern_uid"]
    decoded_token = authenticator._decode_token(auth_state["access_token"])
    spawner.user_roles = authenticator.claim_roles_key(authenticator, decoded_token)


c.KeyCloakAuthenticator.pre_spawn_hook = pre_spawn_hook

# Configure token signature verification
c.KeyCloakAuthenticator.check_signature = True
c.KeyCloakAuthenticator.jwt_signing_algorithms = ["HS256", "RS256"]

# Once a token is refreshed, by default jupyterhub does not trigger a refresh again (triggered when receiving any authenticated request) in `Authenticator.auth_refresh_age` seconds (default 5 minutes)
# If you want to refresh the token less often, and align the refresh to your tokens expiration, which will also trigger the update of the oAuth/OIDC token, this value can be changed:
c.KeyCloakAuthenticator.auth_refresh_age = 900  # 15 minutes

It's also necessary to configure the Client ID and secret. One way of doing this is by setting the following environment variables:

OAUTH_CLIENT_ID=my_id
OAUTH_CLIENT_SECRET=my_secret

Metadata

Release files for keycloakauthenticator 4.0.14

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for keycloakauthenticator 4.0.14
File Size Uploaded Provenance Actions
keycloakauthenticator-4.0.14.tar.gz 8.2 kB With provenance Details for keycloakauthenticator-4.0.14.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for keycloakauthenticator 4.0.14
File Interpreter ABI Platform Actions
keycloakauthenticator-4.0.14-py3-none-any.whl Python 3 none any Details for keycloakauthenticator-4.0.14-py3-none-any.whl

Total release size: 17.0 kB

Release files / keycloakauthenticator-4.0.14.tar.gz

Download URL keycloakauthenticator-4.0.14.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3391a0bf4cc50c1dd5f89ba5cd05e5a14f6fcfe149fc4ea2cdb9f8778b04df16
BLAKE2b-256 checksum
How to use checksums
0b327d1eeac29cb48cf6e6862753cd8cf98e200969b870fb824779c2ecebf0a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Release files / keycloakauthenticator-4.0.14-py3-none-any.whl

Download URL keycloakauthenticator-4.0.14-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73cbf9cf69077cbbd41b9d6b24a1a1806c422b2ab19e06616fee61c40bb6b009
BLAKE2b-256 checksum
How to use checksums
3c80af78e094428e868d87d7d44c7bc5e9a6a3d93e12424f520dcdc3882a8a4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Security for keycloakauthenticator 4.0.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for keycloakauthenticator-4.0.14.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page