tools.py
tools
¶
Functions:
Name | Description |
---|---|
__gen_jwt__ |
Method for generating JWT token with different algorithms. |
__payload_maker__ |
Tool for making base payload. |
__validate_jwt__ |
Validate a JWT token and return the payload if valid. |
__gen_jwt__
¶
__gen_jwt__(header: dict[str, Any], payload: dict[str, Any], secret: str | None = None, private_key: str | None = None) -> str
Method for generating JWT token with different algorithms.
Example:
token = __gen_jwt__(
header={
"alg": "HS256",
"type": "jwt"
},
payload={
"id": 1
},
secret="SUPER_SECRET"
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
header
|
Dict[str, str]
|
Dict with JWT headers |
required |
payload
|
Dict[str, Any]
|
Custom JWT payload |
required |
secret
|
str | None
|
Secret key for HMAC algorithms |
None
|
private_key
|
str | None
|
Private key for RSA algorithms |
None
|
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 |
Returns:
Type | Description |
---|---|
str
|
Access/refresh token |
__payload_maker__
¶
__payload_maker__(exp: int | None, **data) -> dict[str, Any]
Tool for making base payload.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exp
|
int | None
|
Token expire |
required |
**data
|
Data for payload |
{}
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
(Dict[str, Any]) |
__validate_jwt__
¶
__validate_jwt__(token: str, check_exp: bool = False, secret: str | None = None, public_key: str | None = None) -> dict[str, Any]
Validate a JWT token and return the payload if valid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str
|
The JWT token to validate. |
required |
check_exp
|
bool
|
true to check token lifetime. |
False
|
secret
|
str | None
|
Secret key for HMAC algorithms. |
None
|
public_key
|
str | None
|
Public key for RSA algorithms. |
None
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
The payload if the token is valid. |
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. |