This release is a pre-release and may not be stable for production use.
benzene-auth
Authentication middleware for Benzene Python —
Basic 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))
- Basic —
basic_auth_interception(verify, *, realm=...)decodesauthorization: Basic base64(user:pass)and callsverify;Trueauthenticates asPrincipal(username), aPrincipalis attached as-is,False/None(or a missing/malformed header) rejects withunauthorized. - Bearer/OAuth2 —
bearer_token_interception(validate, *, scheme="Bearer")reads the bearer token and callsvalidate(token) → claims | Principal | None.JwtValidatoris a ready-made validator that decodes a JWT with PyJWT (imported lazily), constrained by key, algorithms, audience, and issuer, returningNonefor any token it rejects.static_token_validator({token: principal})builds an in-memory validator for tests. - API Gateway authorizer —
api_gateway_authorizer(validate, *, principal_id_claim=...)returns a Lambdahandler(event, context=None) → dict. It pulls the token fromauthorizationToken(TOKEN authorizer) or theauthorizationheader (REQUEST authorizer), and returns an IAM policy document allowing or denyingexecute-api:Invokeonevent["methodArn"], echoing the claims undercontext.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a244fe19ea3d48c8baddfe0b35b47b8e4dab720b2e0cfa0dd3ebe9a4e2a5052
|
|
| MD5 |
8199049b191efa9c2334db09200b1081
|
|
| BLAKE2b-256 |
2a34986ff7e567952b3d9c6eb98f45ebd05d3bc6f08fab8b892a09a7bccc9385
|
Provenance
The following attestation bundles were made for benzene_auth-0.1.0b1.tar.gz:
Publisher:
release.yml on daniellepelley/benzene-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
benzene_auth-0.1.0b1.tar.gz -
Subject digest:
5a244fe19ea3d48c8baddfe0b35b47b8e4dab720b2e0cfa0dd3ebe9a4e2a5052 - Sigstore transparency entry: 2467247341
- Sigstore integration time:
-
Permalink:
daniellepelley/benzene-python@7f3c0d2abeeb3782a82a8e2621ee8b431bf02ca8 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/daniellepelley
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7f3c0d2abeeb3782a82a8e2621ee8b431bf02ca8 -
Trigger Event:
workflow_dispatch
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a14da39ab8a8f16d5225bcdc651487515bce3f0fc3b799b34a29ec6acfba8f0
|
|
| MD5 |
8caf526697de8215fc576d84c65b3403
|
|
| BLAKE2b-256 |
1bcb807e36a8a224422bd4a18a011eca9be99e0ae68f1fb7f22a19fe21f19968
|
Provenance
The following attestation bundles were made for benzene_auth-0.1.0b1-py3-none-any.whl:
Publisher:
release.yml on daniellepelley/benzene-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
benzene_auth-0.1.0b1-py3-none-any.whl -
Subject digest:
1a14da39ab8a8f16d5225bcdc651487515bce3f0fc3b799b34a29ec6acfba8f0 - Sigstore transparency entry: 2467091259
- Sigstore integration time:
-
Permalink:
daniellepelley/benzene-python@7f3c0d2abeeb3782a82a8e2621ee8b431bf02ca8 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/daniellepelley
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7f3c0d2abeeb3782a82a8e2621ee8b431bf02ca8 -
Trigger Event:
workflow_dispatch
-
Statement type: