Auth (alpha)
| CLASS | DESCRIPTION |
|---|---|
JWTVerifier |
Verify JWT access tokens for a configured issuer and resource audience. |
JWTVerifier ¶
JWTVerifier(*, issuer: str, audience: str | list[str], algorithms: list[str], jwks: dict[str, Any] | None = None, jwks_uri: str | None = None, required_claims: list[str] | None = None, expected_claims: Mapping[str, str] | None = None, expected_headers: Mapping[str, str] | None = None, clock_skew_seconds: float = 60, timeout_seconds: float = 3, jwks_max_age_seconds: float = 300, unknown_kid_cooldown_seconds: float = 300)
Bases: Verifier
Verify JWT access tokens for a configured issuer and resource audience.
| PARAMETER | DESCRIPTION |
|---|---|
issuer
|
Exact trusted HTTPS issuer. Discovery must advertise this issuer.
TYPE:
|
audience
|
Accepted resource audiences; at least one must match the token. |
algorithms
|
Explicit allowlist of asymmetric signing algorithms. |
jwks
|
Static key-set snapshot. Its rotation is the application's responsibility.
TYPE:
|
jwks_uri
|
HTTPS key-set endpoint, mutually exclusive with
TYPE:
|
required_claims
|
Claims required in addition to |
expected_claims
|
Exact, case-sensitive string values required in verified claims,
for example |
expected_headers
|
Exact string values required in the signed header, for example
|
clock_skew_seconds
|
Nonnegative allowance for temporal claims, by default 60.
TYPE:
|
timeout_seconds
|
Positive discovery/key-fetch and refresh-wait budget, by default 3.
TYPE:
|
jwks_max_age_seconds
|
Positive maximum lifetime of fetched keys, by default 300.
TYPE:
|
unknown_kid_cooldown_seconds
|
Nonnegative interval between unknown-key refreshes, by default 300.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
Configuration is invalid or weakens the required verification profile. |
Examples:
1 2 3 4 5 6 7 | |
| METHOD | DESCRIPTION |
|---|---|
any_of |
Route an untrusted issuer claim only to explicitly configured verifiers. |
authorize |
Return an API Gateway authorizer response for the current request. |
cognito |
Verify resource-bound Cognito access tokens, never Cognito ID tokens. |
prefetch |
Populate an absent or expired remote key set; static keys need no I/O. |
require |
Create Event Handler middleware enforcing token validity and all scopes. |
verify |
Return verified access-token claims. |
verify_authorization_header |
Verify the JWT carried by an HTTP Authorization header. |
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/verifier.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
any_of
classmethod
¶
any_of(*verifiers: JWTVerifier) -> Verifier
Route an untrusted issuer claim only to explicitly configured verifiers.
Unknown issuers trigger no discovery. Duplicate issuer configurations are rejected. The returned verifier has the same verification, middleware, authorizer, and prefetch interface.
Examples:
1 2 | |
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/verifier.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
authorize ¶
authorize(event: dict[str, Any] | DictWrapper, *, scopes: list[str] | None = None, response_format: Literal['iam', 'simple'] = 'iam', context_claims: list[str] | None = None, on_error: Callable[[AuthError], None] | None = None) -> dict[str, Any]
Return an API Gateway authorizer response for the current request.
IAM allows require a nonempty sub and target the supplied ARN only.
Simple responses require payload version 2.0 and must also be enabled
in the Gateway deployment. Disable Gateway result caching when each
request must be verified; this method cannot change Gateway's TTL.
| PARAMETER | DESCRIPTION |
|---|---|
event
|
REST TOKEN/REQUEST or HTTP REQUEST authorizer event.
TYPE:
|
scopes
|
Every listed scope must be present in the token. |
response_format
|
Response format configured in Gateway, by default iam.
TYPE:
|
context_claims
|
Selected scalar claims to include; no claims are copied by default. |
on_error
|
Records a failure using the error's fixed reason and retryable fields. Its return value is ignored: invalid credentials still deny access, and unavailable keys still raise JWKSFetchError. Callback exceptions fail the invocation. No automatic logging is performed.
TYPE:
|
Examples:
1 2 3 | |
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/_internal/base.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
cognito
classmethod
¶
cognito(*, user_pool_id: str, client_id: str, audience: str | list[str], **options: Any) -> JWTVerifier
Verify resource-bound Cognito access tokens, never Cognito ID tokens.
Additional keyword arguments configure caching, static keys and claim
requirements in the same way as JWTVerifier.
| PARAMETER | DESCRIPTION |
|---|---|
user_pool_id
|
Cognito user pool identifier, including its Region.
TYPE:
|
client_id
|
App client identifier required in the
TYPE:
|
audience
|
Resource audience required in |
Examples:
1 2 3 4 5 | |
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/verifier.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
prefetch ¶
prefetch() -> None
Populate an absent or expired remote key set; static keys need no I/O.
| RAISES | DESCRIPTION |
|---|---|
JWKSFetchError
|
Trusted keys could not be fetched within the configured budget. |
Examples:
1 | |
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/verifier.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
require ¶
require(*, scopes: list[str] | None = None, authorize: Callable[[dict[str, Any]], bool] | None = None, on_error: Callable[[AuthErrorContext], Response] | None = None) -> AuthMiddleware
Create Event Handler middleware enforcing token validity and all scopes.
Successful verification stores claims in app.context["claims"]
while the downstream middleware and handler execute. Claims are
removed when they return or raise.
Missing/invalid tokens return 401, missing permissions return 403, and
unavailable signing keys return 503. A custom error callback replaces
the response, never execution of the protected handler.
| PARAMETER | DESCRIPTION |
|---|---|
scopes
|
Every listed scope must be present in the token. |
authorize
|
Additional policy receiving verified claims; must return True.
TYPE:
|
on_error
|
Receives status_code, headers, a fixed reason, and retryable, and returns an Event Handler Response. No automatic logging is performed.
TYPE:
|
Examples:
1 2 3 | |
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/_internal/base.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
verify ¶
verify(token: str) -> dict[str, Any]
Return verified access-token claims.
| PARAMETER | DESCRIPTION |
|---|---|
token
|
JWT access token without the
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
Claims after signature, issuer, resource, and time validation. |
| RAISES | DESCRIPTION |
|---|---|
InvalidTokenError
|
Token, key, signature, or required claims are invalid. |
JWKSFetchError
|
Current trusted keys could not be obtained. |
Examples:
1 2 | |
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/verifier.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
verify_authorization_header ¶
verify_authorization_header(value: str | None) -> dict[str, Any]
Verify the JWT carried by an HTTP Authorization header.
The scheme is case-insensitive. Missing headers, malformed values, and
schemes other than Bearer raise InvalidTokenError.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Raw HTTP Authorization header value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
Claims after token verification. |
| RAISES | DESCRIPTION |
|---|---|
InvalidTokenError
|
Header or token is missing, malformed, or invalid. |
JWKSFetchError
|
Current trusted signing keys could not be obtained. |
Examples:
1 2 | |
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/_internal/base.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
Credential-free errors raised by the Auth utility.
| CLASS | DESCRIPTION |
|---|---|
AuthError |
Base error with a fixed message that never includes credential material. |
AuthFailureReason |
Stable, credential-free reasons suitable for application logs and metrics. |
InvalidClaimsError |
A required claim is missing or a claim does not match the token profile. |
InvalidSignatureError |
The access token signature does not match the configured signing key. |
InvalidTokenError |
The bearer token could not be verified. |
JWKSFetchError |
Required signing keys could not be retrieved or refreshed. |
TokenExpiredError |
The access token has expired beyond the configured clock tolerance. |
AuthError ¶
AuthError()
Bases: Exception
Base error with a fixed message that never includes credential material.
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/exceptions.py
26 27 | |
AuthFailureReason ¶
InvalidClaimsError ¶
InvalidClaimsError()
Bases: InvalidTokenError
A required claim is missing or a claim does not match the token profile.
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/exceptions.py
26 27 | |
InvalidSignatureError ¶
InvalidSignatureError()
Bases: InvalidTokenError
The access token signature does not match the configured signing key.
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/exceptions.py
26 27 | |
InvalidTokenError ¶
InvalidTokenError()
Bases: AuthError
The bearer token could not be verified.
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/exceptions.py
26 27 | |
JWKSFetchError ¶
JWKSFetchError()
Bases: AuthError
Required signing keys could not be retrieved or refreshed.
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/exceptions.py
26 27 | |
TokenExpiredError ¶
TokenExpiredError()
Bases: InvalidTokenError
The access token has expired beyond the configured clock tolerance.
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/exceptions.py
26 27 | |
Helpers for application tests that intentionally bypass token verification.
| FUNCTION | DESCRIPTION |
|---|---|
mock_claims |
Temporarily return supplied claims without cryptography or network calls. |
mock_claims ¶
mock_claims(verifier: Verifier, claims: dict[str, Any]) -> Iterator[None]
Temporarily return supplied claims without cryptography or network calls.
This helper bypasses the verifier's security checks. Use it only in application tests; retain separate tests for real token verification.
Examples:
1 2 | |
Source code in aws_lambda_powertools/utilities/auth_alpha/jwt/testing.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |