Skip to content

jam.modules.JWTModule

JWTModule

JWTModule(
    alg: Literal[
        "HS256", "HS384", "HS512", "RS256", "RS384", "RS512"
    ] = "HS256",
    secret_key: Optional[str] = getenv(
        "JAM_JWT_SECRET_KEY", None
    ),
    public_key: Optional[str] = getenv(
        "JAM_JWT_PUBLIC_KEY", None
    ),
    private_key: Optional[str] = getenv(
        "JAM_JWT_PRIVATE_KEY", None
    ),
    expire: int = 3600,
    list: Optional[dict[str, Any]] = None,
)

Bases: BaseModule

Module for JWT auth.

Methods:

Name Description
make_payload

int | None, **data): Creating a generic payload for a token

Parameters:

Name Type Description Default
alg Literal['HS256', 'HS384', 'HS512', 'RS256', 'RS384', 'RS512', 'PS512', 'PS384', 'PS512']

Algorithm for token encryption

'HS256'
secret_key str | None

Secret key for HMAC enecryption

getenv('JAM_JWT_SECRET_KEY', None)
private_key str | None

Private key for RSA enecryption

getenv('JAM_JWT_PRIVATE_KEY', None)
public_key str | None

Public key for RSA

getenv('JAM_JWT_PUBLIC_KEY', None)
expire int

Token lifetime in seconds

3600
list dict[str, Any]

List config

None

gen_token

gen_token(**payload) -> str

Creating a new token.

Parameters:

Name Type Description Default
**payload

Payload with information

{}

Raises:

Type Description
EmptySecretKey

If the HMAC algorithm is selected, but the secret key is None

EmtpyPrivateKey

If RSA algorithm is selected, but private key None

make_payload

make_payload(
    exp: Optional[int] = None, **data
) -> dict[str, Any]

Payload maker tool.

Parameters:

Name Type Description Default
exp Optional[int]

If none exp = JWTModule.exp

None
**data

Custom data

{}

validate_payload

validate_payload(
    token: str,
    check_exp: bool = False,
    check_list: bool = True,
) -> dict[str, Any]

A method for verifying a token.

Parameters:

Name Type Description Default
token str

The token to check

required
check_exp bool

Check for expiration?

False
check_list bool

Check if there is a black/white list

True

Raises:

Type Description
ValueError

If the token is invalid.

EmptySecretKey

If the HMAC algorithm is selected, but the secret key is None.

EmtpyPublicKey

If RSA algorithm is selected, but public key None.

NotFoundSomeInPayload

If 'exp' not found in payload.

TokenLifeTimeExpired

If token has expired.

TokenNotInWhiteList

If the list type is white, but the token is not there

TokenInBlackList

If the list type is black and the token is there

Returns:

Type Description
dict[str, Any]

Payload from token