Skip to main content

jupyterhub-fastapi-adapter

License: MIT Python Version PyPI Version

A lightweight adapter for building authenticated FastAPI services that run behind JupyterHub.

The package provides:

  • OAuth authentication using JupyterHub's service OAuth flow
  • FastAPI dependencies for accessing the authenticated user
  • Automatic browser redirects to the JupyterHub login page
  • Health-check friendly authentication handling
  • Cookie- and token-based authentication support

Features

  • ✅ OAuth login flow for browser-based services
  • ✅ require_authenticated_user FastAPI dependency
  • ✅ User model with username, admin status, and groups
  • ✅ Authentication exception and exception handler
  • ✅ Supports both OAuth cookies and Authorization headers

Installation

pip install jupyterhub-fastapi-adapter

Requirements

The service must run as a JupyterHub Service and the following environment variables must be available:

Variable Description
JUPYTERHUB_API_URL URL of the JupyterHub API
JUPYTERHUB_API_TOKEN Service API token
JUPYTERHUB_SERVICE_PREFIX Service prefix assigned by JupyterHub

Basic Usage

Create a FastAPI application:

"""
Minimal FastAPI JupyterHub managed service.
Shows the authenticated user's information as JSON.

Uses jupyterhub.services.auth.HubOAuth to identify the logged-in user
from JupyterHub's OAuth cookie.
"""

import os

from fastapi import Depends, FastAPI
from jupyterhub.utils import url_path_join
from jupyterhub_fastapi_adapter import (
    AuthenticationRequired,
    User,
    authentication_required_handler,
    oauth_callback,
    require_authenticated_user,
)

JUPYTERHUB_SERVICE_PREFIX = os.environ["JUPYTERHUB_SERVICE_PREFIX"]

app = FastAPI()

# Register exception handler
app.exception_handler(AuthenticationRequired)(authentication_required_handler)

# Register OAuth callback route
app.get(url_path_join(JUPYTERHUB_SERVICE_PREFIX, "oauth_callback"))(oauth_callback)


@app.get(JUPYTERHUB_SERVICE_PREFIX)
async def index(user: User = Depends(require_authenticated_user)):
    """Return authenticated user information."""
    return user


@app.get(url_path_join(JUPYTERHUB_SERVICE_PREFIX, "hello"))
async def hello(user: User = Depends(require_authenticated_user)):
    return {"message": f"Hello, {user.username}!"}

When an unauthenticated browser visits the service, they are automatically redirected to the JupyterHub OAuth login flow.

After successful authentication, JupyterHub redirects the user back to the service and authentication is handled using an OAuth cookie.


Authentication Dependency

Use the provided dependency to require authentication:

from fastapi import Depends

from jupyterhub_fastapi_adapter.dependencies import require_authenticated_user


@app.get("/protected")
async def protected(user=Depends(require_authenticated_user)):
    return {"hello": user.username}

The dependency returns a User object:

class User(BaseModel):
    username: str
    admin: bool
    groups: list[str]

OAuth Flow

The package implements the standard JupyterHub service OAuth flow:

  1. A user accesses a protected endpoint.
  2. If no valid authentication is present, AuthenticationRequired is raised.
  3. The exception handler redirects the browser to JupyterHub's OAuth endpoint.
  4. JupyterHub authenticates the user.
  5. The OAuth callback exchanges the authorization code for an access token.
  6. The access token is stored in a cookie.
  7. Future requests authenticate using that cookie.

For API clients, bearer tokens supplied via the Authorization header are also supported.


Health Checks

Requests that do not advertise Accept: text/html receive a simple 200 OK response instead of an OAuth redirect when authentication is required.

This allows JupyterHub and other infrastructure to perform health checks without triggering the login flow.


API

hub_oauth

  • AuthenticationRequired
  • authentication_required_handler()
  • oauth_callback()
  • get_token_from_request()

dependencies

  • require_authenticated_user()
  • get_current_user()
  • User

License

MIT

Metadata

Release files for jupyterhub-fastapi-adapter 0.1.0

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

Source distribution (sdist)

Source distribution for jupyterhub-fastapi-adapter 0.1.0
File Size Uploaded Provenance
jupyterhub_fastapi_adapter-0.1.0.tar.gz 7.2 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for jupyterhub-fastapi-adapter 0.1.0
File Interpreter ABI Platform
jupyterhub_fastapi_adapter-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.2 kB

Release files / jupyterhub_fastapi_adapter-0.1.0.tar.gz

Download URL jupyterhub_fastapi_adapter-0.1.0.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
62010e04123dd75e0d6b3d6abd625e3f5886c6e37a779101822ef2486abd7f6f
BLAKE2b-256 checksum
How to use checksums
032165b6214e0a38a6163397db786d2cef726a4a120a442d8a8201c8f5471945
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.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.

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 Jul 30, 2026.

Transparency log

Release files / jupyterhub_fastapi_adapter-0.1.0-py3-none-any.whl

Download URL jupyterhub_fastapi_adapter-0.1.0-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4526c9999670590ba41c279d40a143c728e9f3f91dd300b3fcbed9a0fccc4fe0
BLAKE2b-256 checksum
How to use checksums
e12f8e9beedd0b7949938ec13bf86fd803e3f7747c9b69c8bccac2e8a4200bd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.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.

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 Jul 30, 2026.

Transparency log

Security for jupyterhub-fastapi-adapter 0.1.0

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

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for jupyterhub_fastapi_adapter-0.1.0.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 Jul 30, 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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