Skip to content

jwt

jwt

Quick JWT methods.

Functions:

Name Description
decode_jwt_token

Token decoding.

get_jwt_token

Quick method for generate JWT token.

verify_jwt_token

Method to verify the token.

decode_jwt_token

decode_jwt_token(token: str, secret_key: str | None = None, public_key: str | None = None) -> dict[str, Any]

Token decoding.

Parameters:

Name Type Description Default
token str

JWT token

required
secret_key str | None

Secret key for HS algs

None
public_key str | None

Public key for RS algs

None

Returns:

Type Description
dict[str, Any]

Decoded payload

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.

get_jwt_token

get_jwt_token(alg: str, payload: dict[str, Any], secret_key: str | None = None, private_key: str | None = None) -> str

Quick method for generate JWT token.

Parameters:

Name Type Description Default
alg str

Jwt algorithm

required
payload dict[str, Any]

Payload with information

required
secret_key str | None

Secret key for HS algs

None
private_key str | None

Private key for RS algs

None

Returns:

Type Description
str

New jwt token

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

ValueError

Unsupported alg

verify_jwt_token

verify_jwt_token(token: str, secret_key: str | None = None, public_key: str | None = None) -> bool

Method to verify the token.

Parameters:

Name Type Description Default
token str

JWT token

required
secret_key str | None

Secret key for HS algs

None
public_key str | None

Public key for RS algs

None

Returns:

Type Description
bool

If token is valid

Raises:

Type Description
EmptySecretKey

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

EmtpyPublicKey

If RSA algorithm is selected, but public key None.