A Jupyter widget that lets notebook users authenticate via OAuth2 (Authorization Code with PKCE) directly from a notebook cell.
After signing in, the access token is available in the Python kernel for making requests to protected APIs.
- Keycloak (fully tested with PKCE)
- Auth0
- Python >= 3.12
- JupyterLab >= 4.0
- ipywidgets >= 8.0, < 9
pip install ipyauth-pkceThe package installs both the Python widget and the JupyterLab extension automatically.
from ipyauth_pkce import ParamsKeycloak, Auth
params = ParamsKeycloak(
base_url='https://your-keycloak-server/auth',
realm='your-realm',
client_id='your-client-id',
redirect_uri='http://localhost:8888/callback/',
scope='openid profile',
)
session = Auth(params=params)
session # displays the widget — click "Sign In"redirect_uri must be your Jupyter server's URL followed by /callback/, and
it must be registered as an allowed redirect URI for the client at your
provider. The scope must include openid and profile. Values can also be
loaded from a dotenv file: ParamsKeycloak(dotenv_folder='.', dotenv_file='ipyauth-keycloak-myrealm.env') (the file name must start with
ipyauth-<provider>- and end with .env); explicit arguments override it.
The widget tries to open the sign-in popup as soon as it is displayed; if the browser blocks that, click "Sign In".
After authenticating:
session.access_token # the JWT access token
session.refresh_token # '' unless created with Auth(params, keep_refresh_token=True)
session.headers # {'Authorization': 'Bearer eyJ...'}
session.is_authenticated # True while there is an unexpired access token
session.logged_as # user email or name
session.expires_at # expiry timestamp
session.auth_state # 'idle', 'in_progress' (popup open), 'signed_in', 'cancelled' or 'error'
# Use with requests
import requests
r = requests.get('https://api.example.com/data', headers=session.headers)Cell output is saved into the notebook file. Don't end a cell with
session.access_token, session.refresh_token or session.headers, and don't
print them, or the token ends up in the notebook when you share or export it.
Displaying session itself or calling session.show() is safe: neither
includes tokens.
from ipyauth_pkce import ParamsAuth0, Auth
params = ParamsAuth0(
domain='your-tenant.auth0.com',
client_id='your-client-id',
redirect_uri='http://localhost:8888/callback/',
scope='openid profile',
)
session = Auth(params=params)from ipyauth_pkce import ParamsGoogle, Auth
params = ParamsGoogle(
client_id='your-client-id.apps.googleusercontent.com',
redirect_uri='http://localhost:8888/callback/',
scope='openid profile email',
)
session = Auth(params=params)Replace the provider logo shown in the widget with the logo argument: a path
to an image file (PNG, JPEG, GIF, WebP or SVG) or the raw image bytes.
session = Auth(params=params, logo='my-logo.svg')- The widget opens a popup window pointing to the OAuth provider's authorization endpoint
- The user authenticates in the popup
- The provider redirects back to
/callback/with an authorization code - The callback page sends the code back to the widget via
postMessage - The widget asks the Jupyter server to exchange the code for tokens (PKCE: a public client, no client secret)
- The widget validates the ID token, and the access token (plus the refresh token, if requested) becomes available in the Python kernel
When the token expires the widget clears it; sign in again to get a new one. There is no silent renewal. The kernel also knows the expiry time, so is_authenticated and headers stop using an expired token even if the browser tab is closed before the widget clears it. The refresh token is opt-in: by default the Jupyter server discards it from the token response, so it never reaches the browser or the kernel. With Auth(params, keep_refresh_token=True) the widget hands it to the kernel (session.refresh_token) so Python code can refresh against the provider; the widget never uses it itself.
- Authorization Code flow with PKCE (no implicit flow, no client secrets in the browser)
- Server-side token exchange via Jupyter server extension
- Tokens reach the kernel as custom comm messages, not synced widget traits, so they are never part of the widget state saved into the notebook file
- The refresh token is discarded by the Jupyter server (never stored) unless the widget is created with
keep_refresh_token=True - The saved widget state contains the signed-in user's name or email (
logged_as) and the token expiry time, but no tokens - XSRF protection on the token exchange endpoint
- Origin validation on postMessage callbacks
- Content-Security-Policy headers on the callback page
- Input validation on all OAuth parameters (SSRF protection, RFC 7636 compliance)
- OIDC ID-token validation (issuer, audience, subject, expiry, issued-at, nonce, and provider JWKS signature)
- Google bearer tokens are sent in Authorization headers rather than API query strings
The server-side token exchange decides which token endpoint to send the authorization code and PKCE verifier to — it never blindly trusts a browser-supplied URL, because that endpoint is the one place a leaked code could be redeemed by an attacker.
-
Google works with no configuration: its fixed endpoint (
https://oauth2.googleapis.com/token) is allowed by default. -
Any other provider (Keycloak, Auth0, other OIDC servers, or local development) requires a single environment variable on the Jupyter server, set before Jupyter starts:
export IPYAUTH_TOKEN_ENDPOINT="https://your-provider/realms/your-realm/protocol/openid-connect/token"
When IPYAUTH_TOKEN_ENDPOINT is set, the server uses that value and ignores
whatever the browser sends, so the token exchange always targets a trusted
endpoint. The value must be an HTTPS URL (plain http is accepted only for
localhost during development) with no credentials, query string, or fragment.
If a non-Google provider is used without IPYAUTH_TOKEN_ENDPOINT set, the
token exchange is rejected and the widget never receives an access token. The
server responds with:
Token exchange endpoint not configured. Set the IPYAUTH_TOKEN_ENDPOINT environment variable on the Jupyter server to your provider's token endpoint.
In managed or multi-user deployments this variable is set once by the server operator in the Jupyter server environment.
ipyauth_pkce/ipyauth_callback/— Jupyter server extension that serves the OAuth callback page and handles token exchangeipyauth_pkce/ipyauth_widget/— Python widget (ipywidgets VBox)ipyauth_pkce/js/— JavaScript frontend (JupyterLab extension)
Requires Node.js (24 or newer) in addition to Python. The JupyterLab extension in
ipyauth_pkce/labextension/ is a build output and is not committed: it is built
from ipyauth_pkce/js whenever the package is built or installed from source.
# Virtual environment (the dev/ scripts expect it at .venv; override with VENV=...)
python3 -m venv .venv && source .venv/bin/activate
# Editable install; also builds the JupyterLab extension
pip install -e . pytest "jupyterlab>=4,<5" jupyter-builder
cd ipyauth_pkce/js && npm ci
# JS lint and tests (from ipyauth_pkce/js)
npm run lint
npm test
# Rebuild the JupyterLab extension after changing ipyauth_pkce/js/src
npm run build
# Python tests (from the repo root)
pytest
# Build the wheel and sdist (the version comes from the latest git tag).
# An existing ipyauth_pkce/labextension is reused, so run `npm run build` first.
hatch buildTo release, publish a GitHub release with a new tag vX.Y.Z: CI runs the tests,
builds the extension and uploads version X.Y.Z to PyPI.
The JavaScript widget has its own version (ipyauth_pkce/js/package.json, and
_FRONTEND_VERSION in _widget_box.py, which must match). Bump both only when
the contract between the Python and JavaScript sides changes, such as a synced
trait being added, renamed or removed.
dev/ contains helper scripts that use Podman (the Dockerfiles also work with
Docker if you run the commands by hand). They use the .venv environment
from above (or the one set in VENV); run-test.sh additionally needs
pip install hatch-vcs hatch-jupyter-builder in it.
# Build the wheel and a JupyterLab image that installs it, then run it.
# Pass the token endpoint for non-Google providers.
dev/run-test.sh "https://keycloak.example.com/realms/my-realm/protocol/openid-connect/token"
# Fast loop: editable install with the source bind-mounted.
# --js rebuilds the labextension first; --watch rebuilds on every change.
dev/run-dev.sh "https://keycloak.example.com/realms/my-realm/protocol/openid-connect/token" --jsBoth start JupyterLab on http://localhost:8888 (override with PORT=...).
run-test.sh keeps Jupyter's token login (open the URL with ?token=... from its
output); run-dev.sh disables authentication for convenience, so only use it on
a local machine.
This project is based on ipyauth by oscar6echo, with Keycloak support from the e-ucm fork. This is a modernized version that uses the Authorization Code flow with PKCE instead of the legacy implicit flow. Tested with Python 3.12+, JupyterLab 4+, and modern versions of Keycloak that support PKCE.
