Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

benzene-auth

Authentication middleware for Benzene PythonBasic auth, JWT/OAuth2 bearer-token validation, and an AWS API Gateway custom authorizer adapter. Depends only on benzene-core (PyJWT is an optional extra, only for the real JWT decode).

pip install benzene-auth        # middleware only
pip install benzene-auth[jwt]   # + PyJWT, for JwtValidator's real decode

Authentication is an interception concern, like the core's health endpoint: a middleware verifies the credential ahead of the message router, attaches the authenticated Principal to the context on success, and short-circuits with Result.unauthorized on failure — a middleware that does not await next() ends the pipeline, so the handler never sees an unauthenticated call. Verifiers and validators may be sync or async, and none of them raise for a bad credential.

from benzene.auth import (
    basic_auth_interception,
    bearer_token_interception,
    JwtValidator,
    api_gateway_authorizer,
    get_principal,
)


# Basic auth — verify(username, password) → bool | Principal | None.
def verify(username: str, password: str) -> bool:
    return password == secrets.get(username)


definition.middleware += [basic_auth_interception(verify, realm="orders")]

# Bearer/OAuth2 — a validator that decodes a JWT (None on any invalid token, never raises).
definition.middleware += [
    bearer_token_interception(
        JwtValidator(key=signing_secret, algorithms=("HS256",), audience="orders-api")
    )
]

# Downstream, read who the caller is:
principal = get_principal(context)  # None when unauthenticated
# AWS API Gateway custom authorizer — adapts the same validate seam into a Lambda handler
# emitting an Allow/Deny IAM policy scoped to the invoked methodArn.
handler = api_gateway_authorizer(JwtValidator(key=signing_secret))
  • Basicbasic_auth_interception(verify, *, realm=...) decodes authorization: Basic base64(user:pass) and calls verify; True authenticates as Principal(username), a Principal is attached as-is, False/None (or a missing/malformed header) rejects with unauthorized.
  • Bearer/OAuth2bearer_token_interception(validate, *, scheme="Bearer") reads the bearer token and calls validate(token) → claims | Principal | None. JwtValidator is a ready-made validator that decodes a JWT with PyJWT (imported lazily), constrained by key, algorithms, audience, and issuer, returning None for any token it rejects. static_token_validator({token: principal}) builds an in-memory validator for tests.
  • API Gateway authorizerapi_gateway_authorizer(validate, *, principal_id_claim=...) returns a Lambda handler(event, context=None) → dict. It pulls the token from authorizationToken (TOKEN authorizer) or the authorization header (REQUEST authorizer), and returns an IAM policy document allowing or denying execute-api:Invoke on event["methodArn"], echoing the claims under context.

Nothing here needs PyJWT installed to run or test: JwtValidator accepts an injected decode function and static_token_validator needs no JWT library at all. Mirrors .NET's Benzene.Auth.Basic and Benzene.Auth.OAuth2, plus Benzene.Aws.Lambda.ApiGateway.ApiGatewayCustomAuthorizer, and contributes the benzene.auth subpackage to the shared benzene namespace.

Download files

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

Source Distribution

benzene_auth-0.1.0b1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

benzene_auth-0.1.0b1-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file benzene_auth-0.1.0b1.tar.gz.

File metadata

  • Download URL: benzene_auth-0.1.0b1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for benzene_auth-0.1.0b1.tar.gz
Algorithm Hash digest
SHA256 5a244fe19ea3d48c8baddfe0b35b47b8e4dab720b2e0cfa0dd3ebe9a4e2a5052
MD5 8199049b191efa9c2334db09200b1081
BLAKE2b-256 2a34986ff7e567952b3d9c6eb98f45ebd05d3bc6f08fab8b892a09a7bccc9385

See more details on using hashes here.

Provenance

The following attestation bundles were made for benzene_auth-0.1.0b1.tar.gz:

Publisher: release.yml on daniellepelley/benzene-python

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

File details

Details for the file benzene_auth-0.1.0b1-py3-none-any.whl.

File metadata

  • Download URL: benzene_auth-0.1.0b1-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for benzene_auth-0.1.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 1a14da39ab8a8f16d5225bcdc651487515bce3f0fc3b799b34a29ec6acfba8f0
MD5 8caf526697de8215fc576d84c65b3403
BLAKE2b-256 1bcb807e36a8a224422bd4a18a011eca9be99e0ae68f1fb7f22a19fe21f19968

See more details on using hashes here.

Provenance

The following attestation bundles were made for benzene_auth-0.1.0b1-py3-none-any.whl:

Publisher: release.yml on daniellepelley/benzene-python

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

Release history Release notifications | RSS feed

This release

0.1.0b1 This release

2 files

Supported by

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