jam.aio.modules.JWTModule
JWTModule
¶
JWTModule(
alg: Literal[
"HS256", "HS384", "HS512", "RS256", "RS384", "RS512"
] = "HS256",
secret_key: Optional[str] = None,
public_key: Optional[str] = None,
private_key: Optional[str] = None,
expire: int = 3600,
list: Optional[dict[str, Any]] = None,
)
Bases: BaseModule
Module for JWT auth.
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 |
None
|
private_key
|
str | None
|
Private key for RSA enecryption |
None
|
public_key
|
str | None
|
Public key for RSA |
None
|
expire
|
int
|
Token lifetime in seconds |
3600
|
list
|
dict[str, Any]
|
List config |
None
|
Methods:
Name | Description |
---|---|
gen_token |
Creating a new token. |
make_payload |
Payload maker tool. |
validate_payload |
A method for verifying a token. |
gen_token
async
¶
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
async
¶
make_payload(
exp: Optional[int] = None, **data: Any
) -> dict[str, Any]
Payload maker tool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exp
|
int | None
|
If none exp = JWTModule.exp |
None
|
**data
|
Any
|
Custom data |
{}
|
validate_payload
async
¶
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 |
FILE PATH: jam/aio/modules.py