snoozeml.auth — Authentication, RBAC & JWT Minting#
Authentication helpers, RBAC role definitions, and HMAC JWT minting/verification for SnoozeML.
Provides zero-dependency RFC 7519 JSON Web Token (JWT) creation and verification
compatible with SnoozeML’s Rust authentication service (src/auth.rs).
- class snoozeml.auth.Role(*values)[source]#
-
Role-Based Access Control (RBAC) tier matching SnoozeML’s
src/auth.rshierarchy.Hierarchy:
READ_ONLY (1) < INFERENCE (2) < ADMIN (3).- READ_ONLY = 'read_only'#
Read-only access to
GET /v1/models.
- INFERENCE = 'inference'#
Permission to execute model inference (REST & gRPC) and read model metadata.
- ADMIN = 'admin'#
Full administrative access (load, unload, pin, unpin, upload, refresh, protected metrics).
- class snoozeml.auth.JwtAlgorithm(*values)[source]#
-
Supported HMAC signature algorithms for SnoozeML JWT authentication.
- HS256 = 'HS256'#
- HS384 = 'HS384'#
- HS512 = 'HS512'#
- classmethod from_str(value)[source]#
Parses a case-insensitive algorithm identifier into a
JwtAlgorithm.- Parameters:
value (JwtAlgorithm | str)
- Return type:
- snoozeml.auth.create_jwt_token(secret=None, *, subject='snoozeml-client', role=Role.INFERENCE, roles=None, expires_in_secs=3600, exp=None, iat=None, nbf=None, issuer=None, audience=None, algorithm=JwtAlgorithm.HS256, extra_claims=None)[source]#
Mints a signed HMAC JSON Web Token (JWT) compatible with the SnoozeML daemon.
Both the shared
secretand the generated JWT are automatically registered with SnoozeML’s log redaction filter so they never leak in client telemetry.- Parameters:
secret (str | None) – Shared HMAC secret key. If
None, readsSNOOZE_JWT_SECRETfrom the environment.subject (str) – Subject identifier claim (
sub).role (Role | str | None) – Primary RBAC role claim (
"read_only","inference", or"admin").roles (Sequence[Role | str] | None) – Optional list of RBAC roles assigned to the token (
rolesclaim).expires_in_secs (int) – Token lifetime in seconds from
nowwhenexpis not explicitly provided.exp (int | None) – Explicit expiration epoch timestamp in seconds. Overrides
expires_in_secs.iat (int | None) – Optional issued-at epoch timestamp in seconds (defaults to current UTC epoch time).
nbf (int | None) – Optional not-before epoch timestamp in seconds.
issuer (str | None) – Optional issuer claim (
iss).audience (str | None) – Optional audience claim (
aud).algorithm (JwtAlgorithm | str) – HMAC algorithm (
JwtAlgorithm.HS256,HS384, orHS512).extra_claims (Mapping[str, Any] | None) – Optional additional custom JSON claims to include in the payload.
- Returns:
Compact
header.payload.signatureJWT string ready forSnoozeClient(token=...).- Raises:
InvalidInputError – If the secret, subject, role, or expiration parameters are invalid.
- Return type:
- snoozeml.auth.mint_jwt_token(secret=None, *, subject='snoozeml-client', role=Role.INFERENCE, roles=None, expires_in_secs=3600, exp=None, iat=None, nbf=None, issuer=None, audience=None, algorithm=JwtAlgorithm.HS256, extra_claims=None)#
Mints a signed HMAC JSON Web Token (JWT) compatible with the SnoozeML daemon.
Both the shared
secretand the generated JWT are automatically registered with SnoozeML’s log redaction filter so they never leak in client telemetry.- Parameters:
secret (str | None) – Shared HMAC secret key. If
None, readsSNOOZE_JWT_SECRETfrom the environment.subject (str) – Subject identifier claim (
sub).role (Role | str | None) – Primary RBAC role claim (
"read_only","inference", or"admin").roles (Sequence[Role | str] | None) – Optional list of RBAC roles assigned to the token (
rolesclaim).expires_in_secs (int) – Token lifetime in seconds from
nowwhenexpis not explicitly provided.exp (int | None) – Explicit expiration epoch timestamp in seconds. Overrides
expires_in_secs.iat (int | None) – Optional issued-at epoch timestamp in seconds (defaults to current UTC epoch time).
nbf (int | None) – Optional not-before epoch timestamp in seconds.
issuer (str | None) – Optional issuer claim (
iss).audience (str | None) – Optional audience claim (
aud).algorithm (JwtAlgorithm | str) – HMAC algorithm (
JwtAlgorithm.HS256,HS384, orHS512).extra_claims (Mapping[str, Any] | None) – Optional additional custom JSON claims to include in the payload.
- Returns:
Compact
header.payload.signatureJWT string ready forSnoozeClient(token=...).- Raises:
InvalidInputError – If the secret, subject, role, or expiration parameters are invalid.
- Return type:
- snoozeml.auth.decode_jwt_token(token, secret=None, *, verify=True, algorithm=JwtAlgorithm.HS256, leeway_secs=60, issuer=None, audience=None)[source]#
Decodes and optionally verifies a SnoozeML HMAC JSON Web Token.
- Parameters:
token (str) – Compact
header.payload.signatureJWT string.secret (str | None) – Shared HMAC secret key (required when
verify=True; falls back toSNOOZE_JWT_SECRET).verify (bool) – Whether to verify the HMAC signature and temporal/issuer/audience claims.
algorithm (JwtAlgorithm | str) – Expected HMAC algorithm (default
HS256).leeway_secs (int) – Clock skew tolerance in seconds for
expandnbfchecks (default60).issuer (str | None) – Expected
issclaim value if issuer validation is desired.audience (str | None) – Expected
audclaim value if audience validation is desired.
- Returns:
Decoded JWT claims dictionary.
- Raises:
AuthenticationError – If the token format, signature, or claims validation fails.
InvalidInputError – If
verify=Trueand no secret is provided.
- Return type: