Skip to content

TokenPayload

authx.schema.TokenPayload

Bases: BaseModel

model_config class-attribute instance-attribute

model_config = ConfigDict(extra='allow')

jti class-attribute instance-attribute

jti = Field(default_factory=get_uuid)

iss class-attribute instance-attribute

iss = None

sub class-attribute instance-attribute

sub = None

aud class-attribute instance-attribute

aud = None

exp class-attribute instance-attribute

exp = None

nbf class-attribute instance-attribute

nbf = None

iat class-attribute instance-attribute

iat = Field(default_factory=lambda: int(get_now_ts()))

type class-attribute instance-attribute

type = None

csrf class-attribute instance-attribute

csrf = None

scopes class-attribute instance-attribute

scopes = None

fresh class-attribute instance-attribute

fresh = False

extra_dict property

extra_dict

issued_at property

issued_at

expiry_datetime property

expiry_datetime

time_until_expiry property

time_until_expiry

time_since_issued property

time_since_issued

has_scopes

has_scopes(*scopes)
PARAMETER DESCRIPTION
*scopes

TYPE: Sequence[str] DEFAULT: ()

Source code in authx/schema.py
def has_scopes(self, *scopes: Sequence[str]) -> bool:
    return all(s in self.scopes for s in scopes)

encode

encode(key, algorithm, ignore_errors=True, headers=None)
PARAMETER DESCRIPTION
key

TYPE: str

algorithm

TYPE: str

ignore_errors

TYPE: bool DEFAULT: True

headers

TYPE: Optional[Dict[str, Any]] DEFAULT: None

Source code in authx/schema.py
def encode(
    self,
    key: str,
    algorithm: str,
    ignore_errors: bool = True,
    headers: Optional[Dict[str, Any]] = None,
) -> str:
    return create_token(
        key=key,
        algorithm=algorithm,
        uid=self.sub,
        jti=self.jti,
        issued=self.iat,
        type=self.type,
        expiry=self.exp,
        fresh=self.fresh,
        csrf=self.csrf,
        audience=self.aud,
        issuer=self.iss,
        not_before=self.nbf,
        ignore_errors=ignore_errors,
        headers=headers,
    )

decode classmethod

decode(token, key, algorithms=['HS256'], audience=None, issuer=None, verify=True)
PARAMETER DESCRIPTION
token

TYPE: str

key

TYPE: str

algorithms

TYPE: Sequence[AlgorithmType] DEFAULT: ['HS256']

audience

TYPE: Optional[StringOrSequence] DEFAULT: None

issuer

TYPE: Optional[str] DEFAULT: None

verify

TYPE: bool DEFAULT: True

Source code in authx/schema.py
@classmethod
def decode(
    cls,
    token: str,
    key: str,
    algorithms: Sequence[AlgorithmType] = ["HS256"],
    audience: Optional[StringOrSequence] = None,
    issuer: Optional[str] = None,
    verify: bool = True,
) -> "TokenPayload":
    payload = decode_token(
        token=token,
        key=key,
        algorithms=algorithms,
        audience=audience,
        issuer=issuer,
        verify=verify,
    )
    return cls.model_validate(payload)