Skip to main content

HOTOSM SSO authentication library for Hanko + OSM OAuth

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hotosm_auth-0.2.11.tar.gz (152.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hotosm_auth-0.2.11-py3-none-any.whl (55.4 kB view details)

Uploaded Python 3

File details

Details for the file hotosm_auth-0.2.11.tar.gz.

File metadata

  • Download URL: hotosm_auth-0.2.11.tar.gz
  • Upload date:
  • Size: 152.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.0

File hashes

Hashes for hotosm_auth-0.2.11.tar.gz
Algorithm Hash digest
SHA256 37a19edb4901b8c3d46116dbfb2cca0b525cb8b383a21c584ab43b242c953384
MD5 042dff48e6f39b88982754d5bff8cd5b
BLAKE2b-256 9d9fd88433c20bfb82bde27ad14034981ac05b49e5683b7c70bcfbdd37bec31a

See more details on using hashes here.

File details

Details for the file hotosm_auth-0.2.11-py3-none-any.whl.

File metadata

File hashes

Hashes for hotosm_auth-0.2.11-py3-none-any.whl
Algorithm Hash digest
SHA256 b3ea830e4d856aa7305a5978e0564d6ec09da3796ffe22870d1bd9042ff79083
MD5 4ac81f8b52b0122829a1e10a2f0031f3
BLAKE2b-256 670ad9306aad40f0f2e0be37c349c5ca7d02d898f87a9c776d821312a1e3ffed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page