Claims and defaults¶
Aliases¶
The ready-made annotated types for the three time-based claims:
| Alias | Definition |
|---|---|
Exp |
Annotated[int, ExpClaim()] |
Nbf |
Annotated[int, NbfClaim()] |
Iat |
Annotated[int, IatClaim()] |
Claim markers¶
Claim
dataclass
¶
Bases: ABC
Base class for JWT claim validators.
Subclass it, set __claim_name__ and implement check(), then attach the
instance to a field with Annotated. The marker runs as an "after"
validator, so check() sees the value once the field's own type has been
applied.
Validation can be skipped per call by passing
context={'validate_claims': False} to model_validate().
Attributes:
| Name | Type | Description |
|---|---|---|
__claim_name__ |
str
|
Name of the claim, reported in the error context. |
Examples:
from typing import Annotated
@dataclass(frozen=True)
class AuthTimeClaim(Claim):
__claim_name__ = 'auth_time'
def check(self, value: Any) -> bool:
return value <= time.time()
auth_time: Annotated[int, AuthTimeClaim()]
check
abstractmethod
¶
Return whether the claim value is acceptable.
Return False for a well-formed value that fails the rule; raise
PydanticCustomError('jwt_type', ...) for a value of the wrong shape,
so callers can tell the two apart.
Source code in pydantic_jwt/claims.py
ExpClaim
dataclass
¶
Bases: Claim
Reject tokens whose expiry time has passed.
Attributes:
| Name | Type | Description |
|---|---|---|
leeway |
float
|
Seconds of clock skew to tolerate past the expiry. |
NbfClaim
dataclass
¶
Bases: Claim
Reject tokens that are not valid yet.
Attributes:
| Name | Type | Description |
|---|---|---|
leeway |
float
|
Seconds of clock skew to tolerate before the start time. |
IatClaim
dataclass
¶
Bases: Claim
Reject tokens issued in the future.
Attributes:
| Name | Type | Description |
|---|---|---|
leeway |
float
|
Seconds of clock skew to tolerate on the issuer's clock. |
IssClaim
dataclass
¶
Bases: Claim
Reject tokens that were not issued by the expected issuer.
The comparison is an exact string match.
Attributes:
| Name | Type | Description |
|---|---|---|
issuer |
str
|
The only accepted |
AudClaim
dataclass
¶
Bases: Claim
Reject tokens that are not addressed to the expected audience.
Per RFC 7519 the claim may be a single string or a list of strings; a list is accepted when it contains the expected audience.
Attributes:
| Name | Type | Description |
|---|---|---|
audience |
str
|
The audience this application answers to. |
Field defaults¶
after
¶
after(
*,
weeks: float = 0,
days: float = 0,
hours: float = 0,
minutes: float = 0,
seconds: float = 0,
milliseconds: float = 0,
) -> Any
Return a field default holding the current time plus the given duration.
The value is a default_factory, so it is recomputed for every instance.
The result is truncated to whole seconds, as JWT NumericDate requires.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weeks
|
float
|
Weeks to add. |
0
|
days
|
float
|
Days to add. |
0
|
hours
|
float
|
Hours to add. |
0
|
minutes
|
float
|
Minutes to add. |
0
|
seconds
|
float
|
Seconds to add. |
0
|
milliseconds
|
float
|
Milliseconds to add. |
0
|
Returns:
| Type | Description |
|---|---|
Any
|
A |
Source code in pydantic_jwt/claims.py
at
¶
Return a field default fixed to the given moment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
moment
|
datetime
|
The instant the claim should carry. Pass an aware |
required |
Returns:
| Type | Description |
|---|---|
Any
|
A |
Source code in pydantic_jwt/claims.py
uuid
¶
Return a field default holding a fresh UUID4, as a hyphenated string or as hex.
Typically used for jti, so every issued token carries a unique id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hex_uuid
|
bool
|
Emit the 32-character hex form without hyphens. |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
A |