Skip to content

JWT Utilities Reference

encode_jwt_token

missil.encode_jwt_token

encode_jwt_token(claims, secret, exp, base=None, algorithm='HS256')

Create a JWT token.

PARAMETER DESCRIPTION
claims

Token user data.

TYPE: JWTClaims

secret

Secret key to sign the token.

TYPE: str

exp

Token expiration in hours.

TYPE: int

base

Token expiration base datetime, where the final datetime is given by base + exp, by default datetime.now(timezone.utc)

TYPE: datetime DEFAULT: None

algorithm

Encode algorithm, by default "HS256"

TYPE: str DEFAULT: 'HS256'

RETURNS DESCRIPTION
str

Encoded JWT token string.

Source code in missil/codec.py
def encode_jwt_token(
    claims: JWTClaims,
    secret: str,
    exp: int,
    base: datetime | None = None,
    algorithm: str = "HS256",
) -> str:
    """
    Create a JWT token.

    Parameters
    ----------
    claims : JWTClaims
        Token user data.
    secret : str
        Secret key to sign the token.
    exp : int
        Token expiration in hours.
    base : datetime, optional
        Token expiration base datetime, where the final datetime is given by
        base + exp, by default datetime.now(timezone.utc)
    algorithm : str, optional
        Encode algorithm, by default "HS256"

    Returns
    -------
    str
        Encoded JWT token string.
    """
    if base is None:
        base = datetime.now(timezone.utc)

    to_encode: dict[str, Any] = dict(claims)
    to_encode.update({"exp": base + timedelta(hours=exp)})
    return pyjwt.encode(to_encode, key=secret, algorithm=algorithm)

decode_jwt_token

missil.decode_jwt_token

decode_jwt_token(token, secret_key, algorithms='HS256')

Decode a JWT token using PyJWT.

PARAMETER DESCRIPTION
token

Token to be decoded.

TYPE: str

secret_key

Secret key to decode the signed token.

TYPE: str

algorithms

Decoding algorithm(s). See PyJWT docs for more details.

TYPE: str | list[str] DEFAULT: 'HS256'

RETURNS DESCRIPTION
JWTClaims

Decoded claims.

RAISES DESCRIPTION
TokenValidationException

The token signature has expired.

TokenValidationException

The token signature or claims are invalid.

Source code in missil/codec.py
def decode_jwt_token(
    token: str, secret_key: str, algorithms: str | list[str] = "HS256"
) -> JWTClaims:
    """
    Decode a JWT token using PyJWT.

    Parameters
    ----------
    token : str
        Token to be decoded.
    secret_key : str
        Secret key to decode the signed token.
    algorithms : str | list[str]
        Decoding algorithm(s). See PyJWT docs for more details.

    Returns
    -------
    JWTClaims
        Decoded claims.

    Raises
    ------
    TokenValidationException
        The token signature has expired.
    TokenValidationException
        The token signature or claims are invalid.
    """
    algs: list[str] = [algorithms] if isinstance(algorithms, str) else list(algorithms)
    try:
        return pyjwt.decode(token, secret_key, algorithms=algs)  # type: ignore[return-value]
    except pyjwt.ExpiredSignatureError as e:
        raise TokenValidationException(
            status.HTTP_403_FORBIDDEN, "The token signature has expired."
        ) from e
    except pyjwt.DecodeError as e:
        raise TokenValidationException(
            status.HTTP_403_FORBIDDEN, "The token signature is invalid."
        ) from e
    except pyjwt.PyJWTError as e:
        raise TokenValidationException(
            status.HTTP_403_FORBIDDEN, "The token is invalid."
        ) from e