Ipyauth-pkce: authenticate to OAuth2/OIDC providers from a notebook cell

Hello everyone :slightly_smiling_face:

I wanted to share a small Jupyter widget I’ve been working on in case it’s useful to others.

One thing that kept coming up for us was needing to call protected APIs from a notebook, and not having a clean way to authenticate the user interactively. Copy-pasting tokens around works, but it gets old fast and isn’t great security-wise.

So I put together ipyauth-pkce, a widget that lets notebook users sign in via OAuth2 (Authorization Code flow with PKCE directly from a cell. You display the widget, click “Sign In”, authenticate in the popup, and the access token becomes available right there in the Python kernel:

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"

ipyauth-pkce widget in action

A few things that made it worth doing properly:

  • It uses the Authorization Code flow with PKCE (no implicit flow, no client secrets in the browser), with the token exchange done server-side via a Jupyter server extension.

  • It validates the OIDC ID token (issuer, audience, subject, expiry, nonce, provider JWKS signature) and adds XSRF protection, origin validation on the postMessage callback, and CSP headers on the callback page.

  • It works with Keycloak, Auth0, and Google.

It’s based on the original ipyauth by oscar6echo (with Keycloak support from the e-ucm fork), modernized for the PKCE flow and tested with Python 3.12+, JupyterLab 4+, and ipywidgets 8.

Installation:

pip install ipyauth-pkce

The code and docs are on GitHub if you want to take a look or try it out:

Feedback is very welcome!
Daniel.

2 Likes