TokenPayload#
authx.schema.TokenPayload #
Bases: BaseModel
A comprehensive Pydantic model for managing JSON Web Token (JWT) payloads with advanced token lifecycle handling.
Provides robust functionality for creating, validating, and manipulating authentication tokens with flexible configuration and extensive metadata support.
ATTRIBUTE | DESCRIPTION |
---|---|
jti | Unique token identifier. TYPE: |
iss | Token issuer. TYPE: |
sub | Subject (user) identifier. TYPE: |
aud | Token audience. TYPE: |
exp | Token expiration time. TYPE: |
nbf | Token not-before time. TYPE: |
iat | Token issued-at time. TYPE: |
type | Token type (access or refresh). TYPE: |
csrf | Cross-Site Request Forgery token. TYPE: |
scopes | List of token scopes. TYPE: |
fresh | Flag indicating if the token is freshly issued. TYPE: |
METHOD | DESCRIPTION |
---|---|
issued_at | Converts issued time to datetime. |
expiry_datetime | Calculates token expiration datetime. |
time_until_expiry | Calculates remaining time before token expiration. |
time_since_issued | Calculates time elapsed since token issuance. |
has_scopes | Checks if token has specific scopes. |
encode | Creates a JWT token. |
decode | Decodes and validates a JWT token. |
model_config class-attribute
instance-attribute
#
extra_dict property
#
Retrieve additional fields not defined in the base Pydantic model schema.
Provides a dictionary of extra fields with version-specific compatibility for Pydantic v1 and v2.
RETURNS | DESCRIPTION |
---|---|
dict[str, Any] | A dictionary containing additional fields beyond the model's defined schema. |
issued_at property
#
Convert the token's issued-at timestamp to a datetime object.
Transforms the issued-at (iat) claim into a standardized UTC datetime representation.
RETURNS | DESCRIPTION |
---|---|
datetime | A datetime object representing the token's issuance time. |
RAISES | DESCRIPTION |
---|---|
TypeError | If the issued-at claim is not a float, int, or datetime object. |
expiry_datetime property
#
Convert the token's expiration claim to a precise datetime object.
Transforms the expiration (exp) claim into a standardized UTC datetime representation, supporting multiple input types.
RETURNS | DESCRIPTION |
---|---|
datetime | A datetime object representing the token's expiration time. |
RAISES | DESCRIPTION |
---|---|
TypeError | If the expiration claim is not a float, int, datetime, or timedelta object. |
time_until_expiry property
#
Calculate the remaining time before the token expires.
Computes the time difference between the token's expiration datetime and the current time.
RETURNS | DESCRIPTION |
---|---|
timedelta | A timedelta object representing the remaining time until token expiration. |
time_since_issued property
#
Calculate the elapsed time since the token was issued.
Computes the time difference between the current time and the token's issued datetime.
RETURNS | DESCRIPTION |
---|---|
timedelta | A timedelta object representing the time elapsed since token issuance. |
Config #
Configuration class for Pydantic model with extra field handling.
Allows additional fields beyond the defined model schema to be included during model creation.
has_scopes #
Check if the token contains all specified scopes.
Verifies whether the token's scopes include all the requested scope values.
PARAMETER | DESCRIPTION |
---|---|
*scopes | TYPE: |
PARAMETER | DESCRIPTION |
---|---|
*scopes | Variable number of scope strings to check against the token's scopes. TYPE: |
RETURNS | DESCRIPTION |
---|---|
bool | A boolean indicating whether all specified scopes are present in the token. |
Source code in authx/schema.py
encode #
Generate a JSON Web Token (JWT) with the current payload's claims and configuration.
Creates a signed token using the specified cryptographic key and algorithm, incorporating all token metadata.
PARAMETER | DESCRIPTION |
---|---|
key | TYPE: |
algorithm | TYPE: |
ignore_errors | TYPE: |
headers | TYPE: |
data | TYPE: |
PARAMETER | DESCRIPTION |
---|---|
key | The cryptographic key used for token signing. TYPE: |
algorithm | The cryptographic algorithm for token signing. Defaults to HS256. TYPE: |
ignore_errors | Flag to suppress potential encoding errors. Defaults to True. TYPE: |
headers | Optional custom headers to include in the token. TYPE: |
data | Optional additional data to embed in the token. TYPE: |
RETURNS | DESCRIPTION |
---|---|
str | A string representing the encoded and signed JWT. |
Source code in authx/schema.py
decode classmethod
#
Decode and validate a JSON Web Token (JWT) into a TokenPayload instance.
Converts a signed token into a structured payload object, with optional verification and validation parameters.
PARAMETER | DESCRIPTION |
---|---|
token | TYPE: |
key | TYPE: |
algorithms | TYPE: |
audience | TYPE: |
issuer | TYPE: |
verify | TYPE: |
PARAMETER | DESCRIPTION |
---|---|
token | The encoded JWT string to be decoded. TYPE: |
key | The cryptographic key used for token verification. TYPE: |
algorithms | Optional list of allowed cryptographic algorithms. Defaults to HS256. TYPE: |
audience | Optional expected token audience. TYPE: |
issuer | Optional expected token issuer. TYPE: |
verify | Flag to enable or disable token verification. Defaults to True. TYPE: |
RETURNS | DESCRIPTION |
---|---|
TokenPayload | A TokenPayload instance representing the decoded token's claims. |