Skip to main content

Sonnet Auth

JWT/JWKS authentication and Cedar authorization for sonnet-server applications. No FastMCP dependency -- MCP auth wiring lives in each domain service.

What it provides

  • JWT/JWKS validation -- JwtCredentialValidator implements sonnet-server's CredentialValidator protocol. RS256/ES256 with TTL-based JWKS key refresh and graceful degradation on IdP outages.
  • Claim mapping -- configurable dot-path extraction from JWT claims to AuthContext fields and Cedar principal attributes. auto_map_claims mode passes all non-plumbing claims automatically.
  • Cedar policy evaluation (optional [cedar] extra) -- PolicyEngine wraps cedarpy for in-process RBAC/ABAC. check_authz() and filter_authz() are one-liner authorization for REST and MCP handlers.
  • Pluggable resource resolution -- ResourceAttributeResolver protocol for domain-specific Cedar resource attributes.
  • Settings model -- AuthSettings.resolve(env_prefix, seed) with per-field env var overrides and optional DB seeding. No global state.

Install

# JWT authentication only
pip install sonnet-auth

# JWT + Cedar authorization
pip install sonnet-auth[cedar]

Prerequisites

  • Python 3.14+
  • sonnet-server >= 0.1.9

Usage

With extensions (recommended)

Register AuthnExtension and AuthzExtension in your app factory:

from sonnet_auth import AuthnExtension, AuthzExtension

registry = create_extension_registry(
    DatabaseExtension(),
    AuthnExtension(env_prefix="MY_APP_"),
    AuthzExtension(loader=my_policy_loader),
    McpExtension(),
    RestExtension(),
)

The extensions handle all wiring: settings resolution, JWT validator registration, Cedar engine creation, DI registration.

Configuration via env vars:

MY_APP_AUTHN_ENABLED=true
MY_APP_AUTHN_JWKS_URI=https://idp.example.com/.well-known/jwks.json
MY_APP_AUTHN_ISSUER=https://idp.example.com
MY_APP_AUTHN_AUDIENCE=my-app
MY_APP_AUTHZ_ENABLED=true

Policy loading

AuthzExtension takes a PolicyLoader callable that returns (policies_text, schema_text). The consumer decides how to load:

# From files
def load_from_files():
    return (
        Path("cedar/policies.cedar").read_text(),
        Path("cedar/schema.cedarschema").read_text(),
    )

# From DB (coco-rag pattern)
def load_from_db():
    svc = get_settings_service()
    return (
        svc.get_text("authz_cedar_policies"),
        svc.get_text("authz_cedar_schema"),
    )

# Multiple policy files
def load_from_dir():
    policies = "\n".join(p.read_text() for p in Path("cedar/").glob("*.cedar"))
    schema = Path("cedar/schema.cedarschema").read_text()
    return (policies, schema)

AuthzExtension(loader=load_from_files)

DB-seeded authn (services with a settings table)

def load_seed():
    svc = get_settings_service()
    return {
        "authn_enabled": svc.get("authn_enabled"),
        "authn_config": svc.get("authn_config"),
        "authz_enabled": svc.get("authz_enabled"),
    }

AuthnExtension(env_prefix="COCO_RAG_", seed_fn=load_seed)

Authorization in handlers

from sonnet_auth import check_authz, filter_authz

# In any REST handler or MCP tool
check_authz("search", "Source", source_name)  # raises AuthzDeniedError on deny

# For list operations
permitted = filter_authz("list", "Source", source_names)

Package structure

src/sonnet_auth/
    __init__.py          # public API with lazy Cedar imports
    settings.py          # AuthSettings, AuthnConfig
    context.py           # JWT claim -> AuthContext mapping
    jwt_validator.py     # JwtCredentialValidator (JWKS)
    policy_engine.py     # Cedar PolicyEngine [cedar extra]
    authz.py             # check_authz, filter_authz [cedar extra]

License

Apache License 2.0

Download files

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

Source Distribution

sonnet_auth-0.2.0.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

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

sonnet_auth-0.2.0-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

Details for the file sonnet_auth-0.2.0.tar.gz.

File metadata

  • Download URL: sonnet_auth-0.2.0.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for sonnet_auth-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1d745c465fdb07bcff2c050a965d2acb5b98f06102bcb5028ca1ab00ff701553
MD5 ae85fd86747a6330f83f7c71d84ca1a6
BLAKE2b-256 fe909933d11ad69a707dc7aef460db4b084ad1c770d6ab6d51fbadea4fde2fa5

See more details on using hashes here.

Provenance

The following attestation bundles were made for sonnet_auth-0.2.0.tar.gz:

Publisher: publish.yml on petrarca/sonnet-server

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sonnet_auth-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: sonnet_auth-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 30.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for sonnet_auth-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04c60c20368df242fe416350a02c2f16d46f986373ae376618862c5ced4a29b0
MD5 ec7b76761b831d4f1f4770849b523b30
BLAKE2b-256 ed2ede9416b0f1b6a91f54cd2e2f612d28026ea2a4a7524e7aee522bc361f2ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for sonnet_auth-0.2.0-py3-none-any.whl:

Publisher: publish.yml on petrarca/sonnet-server

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.3.1

2 files

0.3.0

2 files

This release

0.2.0 This release

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

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