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 instance-attribute

sub

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 = Field(default='access', description='Token type')

csrf class-attribute instance-attribute

csrf = ''

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:
    # if `self.scopes`` is None, the function will immediately return False.
    # If `self.scopes`` is not None, it will check if all elements in scopes are in `self.scopes``.
    return (
        all(s in self.scopes for s in scopes) if self.scopes is not None else False
    )

encode

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

TYPE: str

algorithm

TYPE: AlgorithmType DEFAULT: 'HS256'

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: AlgorithmType = "HS256",
    ignore_errors: bool = True,
    headers: Optional[Dict[str, Any]] = None,
) -> str:
    return create_token(
        key=key,
        algorithm=algorithm,
        uid=str(self.sub),
        jti=self.jti,
        issued=self.iat,
        # TODO: Fix type hinting for `type` Field
        # it's caused because Type is a string & what we expect is a TokenType
        # TokenType = Literal["access", "refresh"]
        # Investigate if it's possible to fix this
        type=self.type,  # type: ignore
        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=None, audience=None, issuer=None, verify=True)
PARAMETER DESCRIPTION
token

TYPE: str

key

TYPE: str

algorithms

TYPE: Optional[Sequence[AlgorithmType]] DEFAULT: None

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: Optional[Sequence[AlgorithmType]] = None,
    audience: Optional[StringOrSequence] = None,
    issuer: Optional[str] = None,
    verify: bool = True,
) -> "TokenPayload":
    if algorithms is None:  # pragma: no cover
        algorithms = ["HS256"]  # pragma: no cover
    payload = decode_token(
        token=token,
        key=key,
        algorithms=algorithms,
        audience=audience,
        issuer=issuer,
        verify=verify,
    )
    return cls.model_validate(payload)