Skip to main content

ipyauth-pkce

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.

Demo

demo

Supported Providers

  • Keycloak (fully tested with PKCE)
  • Auth0
  • Google

Requirements

  • Python >= 3.12
  • JupyterLab >= 4.0
  • ipywidgets >= 8.0, < 9

Install

pip install ipyauth-pkce

The package installs both the Python widget and the JupyterLab extension automatically.

Usage (Keycloak)

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 / False
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)

Other Providers

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')

How It Works

  1. The widget opens a popup window pointing to the OAuth provider's authorization endpoint
  2. The user authenticates in the popup
  3. The provider redirects back to /callback/ with an authorization code
  4. The callback page sends the code back to the widget via postMessage
  5. The widget asks the Jupyter server to exchange the code for tokens (PKCE: a public client, no client secret)
  6. 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. The refresh token is opt-in: by default the Jupyter server removes 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.

Security

  • 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 stays on the Jupyter server unless the widget is created with keep_refresh_token=True
  • 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

Token endpoint configuration

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.

Architecture

  • ipyauth_pkce/ipyauth_callback/ — Jupyter server extension that serves the OAuth callback page and handles token exchange
  • ipyauth_pkce/ipyauth_widget/ — Python widget (ipywidgets VBox)
  • ipyauth_pkce/js/ — JavaScript frontend (JupyterLab extension)

Development

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 build

To 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.

Containers

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" --js

Both 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.

Credits

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.

Metadata

Release files for ipyauth-pkce 1.4.1

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

Source distribution (sdist)

Source distribution for ipyauth-pkce 1.4.1
File Size Uploaded Provenance Actions
ipyauth_pkce-1.4.1.tar.gz 272.6 kB With provenance Details for ipyauth_pkce-1.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ipyauth-pkce 1.4.1
File Interpreter ABI Platform Actions
ipyauth_pkce-1.4.1-py3-none-any.whl Python 3 none any Details for ipyauth_pkce-1.4.1-py3-none-any.whl

Total release size: 421.1 kB

Release files / ipyauth_pkce-1.4.1.tar.gz

Download URL ipyauth_pkce-1.4.1.tar.gz
Size 272.6 kB
Tags Source
SHA-256 checksum
How to use checksums
955f4b844f2492b04ef39a495f366d7a983d436d9c115cac005dafba1b536240
BLAKE2b-256 checksum
How to use checksums
9a30e6e9c6e09508620c91c8e556884dfdfe54bf171de7b054c2d8491777c117
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 18, 2026.

Transparency log

Release files / ipyauth_pkce-1.4.1-py3-none-any.whl

Download URL ipyauth_pkce-1.4.1-py3-none-any.whl
Size 148.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
af5381ac3aa27196e5e123f47dce7cede11ba928bd864d6816331b0ce8b86a25
BLAKE2b-256 checksum
How to use checksums
2da864f09506c2f7cd123050140073fe16a0279437ce0dae0ece24e21ac88d9d
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 18, 2026.

Transparency log

Security for ipyauth-pkce 1.4.1

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 ipyauth_pkce-1.4.1.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 18, 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