Skip to main content

HOTOSM Auth Library - Python

FastAPI, Litestar, and Django integration for Hanko SSO authentication with OSM OAuth support.

Installation

# Core only
pip install hotosm-auth

# With FastAPI support
pip install hotosm-auth[fastapi]

# With Django support
pip install hotosm-auth[django]

# With Litestar support
pip install hotosm-auth[litestar]

Usage

FastAPI

from fastapi import FastAPI
from hotosm_auth_fastapi import setup_auth, Auth

app = FastAPI()
setup_auth(app)

@app.get("/me")
async def get_me(auth: Auth):
    return {"id": auth.user.id, "email": auth.user.email}

Django

# settings.py
INSTALLED_APPS = [..., 'hotosm_auth_django']
MIDDLEWARE = [..., 'hotosm_auth_django.HankoAuthMiddleware']

# views.py
from hotosm_auth_django import login_required

@login_required
def my_view(request):
    return JsonResponse({"user_id": request.hotosm.user.id})

Litestar

from litestar import Litestar, get
from hotosm_auth_litestar import setup_auth, Auth


@get("/me")
async def me(auth: Auth):
    return {"id": auth.user.id, "email": auth.user.email}


deps, route_handlers = setup_auth()
app = Litestar(route_handlers=[me, *route_handlers], dependencies=deps)

Configuration

Set these environment variables:

HANKO_API_URL=https://login.hotosm.org
COOKIE_SECRET=your-32-byte-secret
COOKIE_DOMAIN=.hotosm.org

# Optional: OSM OAuth
OSM_CLIENT_ID=your_osm_client_id
OSM_CLIENT_SECRET=your_osm_secret

Development

Running Tests

# Install dev dependencies
uv sync --extra dev

# Run core tests (no framework dependencies)
uv run pytest

# Run with FastAPI tests
uv run --extra fastapi pytest

# Run with Django tests
uv run --extra django pytest

# Run with Litestar tests
uv run --extra litestar pytest

# Run all tests (FastAPI + Litestar + Django)
uv run --extra fastapi --extra litestar --extra django pytest

# Run with coverage
uv run --extra fastapi --extra litestar --extra django pytest --cov=src \
  --cov-report=term-missing

Test Structure

Core tests (no framework dependencies):

  • tests/test_config.py - Configuration loading and validation
  • tests/test_crypto.py - Cookie encryption/decryption
  • tests/test_exceptions.py - Exception hierarchy
  • tests/test_models.py - HankoUser and OSMConnection models
  • tests/test_jwt_validator.py - JWT payload parsing
  • tests/test_jwt_validation.py - Full JWT validation with mocked JWKS
  • tests/test_osm_cookie.py - OSM connection cookie round-trip
  • tests/test_osm_oauth.py - OSM OAuth client with mocked HTTP

FastAPI tests (require [fastapi] extra):

  • tests/test_fastapi_setup.py - Auth dependency setup
  • tests/test_fastapi_dependencies.py - Dependency injection with mocked requests
  • tests/test_fastapi_osm_routes.py - OSM OAuth routes (login, callback, status, disconnect)
  • tests/test_admin_routes.py - Admin CRUD routes with mocked database

Litestar tests (require [litestar] extra):

  • tests/test_litestar_setup.py - Auth context and setup API
  • tests/test_litestar_dependencies.py - Dependency helpers with mocked requests
  • tests/test_litestar_osm_routes.py - OSM OAuth routes (login, callback, status, disconnect)
  • tests/test_litestar_admin_routes.py - Admin CRUD routes with mocked database

Django tests (require [django] extra):

  • tests/test_django_middleware.py - Middleware and decorators
  • tests/test_django_osm_views.py - OSM OAuth views
  • tests/test_django_admin_routes.py - Admin CRUD routes with mocked database

Test Coverage

Run with coverage report:

uv run --extra fastapi --extra litestar --extra django pytest --cov=src --cov-report=term-missing

Current coverage: 72% (127 tests)

Features

  • JWT validation for Hanko tokens (RS256 with JWKS)
  • OSM OAuth 2.0 integration
  • Encrypted httpOnly cookies for OSM connection
  • User mapping between Hanko and application users
  • FastAPI, Litestar, and Django integrations

License

AGPL-3.0

Metadata

Release files for hotosm-auth 0.2.12

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

Source distribution (sdist)

Source distribution for hotosm-auth 0.2.12
File Size Uploaded Provenance
hotosm_auth-0.2.12.tar.gz 152.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for hotosm-auth 0.2.12
File Interpreter ABI Platform
hotosm_auth-0.2.12-py3-none-any.whl Python 3 none any Details

Total release size: 207.4 kB

Release files / hotosm_auth-0.2.12.tar.gz

Download URL hotosm_auth-0.2.12.tar.gz
Size 152.0 kB
Tags Source
SHA-256 checksum
How to use checksums
89151926ff449139635013cda7c219cdd834818cbad2ecb9f886220399864126
BLAKE2b-256 checksum
How to use checksums
79ccd1933e46168219816d78b9ac59d0c2d65efeb889d75094409f6f9af46419
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Pop!_OS","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / hotosm_auth-0.2.12-py3-none-any.whl

Download URL hotosm_auth-0.2.12-py3-none-any.whl
Size 55.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a61f2634f84fe3d0ca2fb0da6fd0fdab8cadce77293453c019adf80247df7b3f
BLAKE2b-256 checksum
How to use checksums
230b742a0b78c1895307b2dcdcad9c375606670ad93cc8add56ad1be19277eaf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Pop!_OS","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for hotosm-auth 0.2.12

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

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