duck.contrib.jwt¶
JWT utilities for Duck’s auth system.
Provides token encoding, decoding, issuance, and rotation. Requires PyJWT — install with: pip install PyJWT
Package Contents¶
Functions¶
Decode and verify a signed JWT token. |
|
Encode a signed JWT token from the given payload. |
|
Retrieve the access token lifetime from Duck settings. |
|
Retrieve the JWT signing algorithm from Duck settings. |
|
Import and return the PyJWT module. |
|
Retrieve the refresh token lifetime from Duck settings. |
|
Retrieve the secret key from Duck settings. |
|
Issue both an access and a refresh token for a user. |
API¶
- exception duck.contrib.jwt.JWTError[source]¶
Bases:
ExceptionBase class for JWT-related errors.
Initialization
Initialize self. See help(type(self)) for accurate signature.
- exception duck.contrib.jwt.JWTExpired[source]¶
Bases:
duck.contrib.jwt.JWTErrorRaised when a JWT has expired.
Initialization
Initialize self. See help(type(self)) for accurate signature.
- exception duck.contrib.jwt.JWTInvalid[source]¶
Bases:
duck.contrib.jwt.JWTErrorRaised when a JWT cannot be decoded or is structurally invalid.
Initialization
Initialize self. See help(type(self)) for accurate signature.
- duck.contrib.jwt.decode_token(token: str, verify_expiry: bool = True) Dict[str, Any][source]¶
Decode and verify a signed JWT token.
- Parameters:
token – The raw JWT string to decode.
verify_expiry – Whether to verify. If True,
JWTExpiredmay be raised on expiry.
- Returns:
The decoded payload as a dict.
- Raises:
JWTExpired – If the token’s
expclaim has passed.JWTInvalid – If the token is malformed, tampered with, or the signature does not match.
- duck.contrib.jwt.encode_token(payload: dict[str, Any], token_type: str = 'access') str[source]¶
Encode a signed JWT token from the given payload.
- Parameters:
payload – Data to embed in the token (e.g.
{"user_id": 1}).token_type – Either
"access"or"refresh". Controls the default expiry whenexpires_inis not given.expires_in – Custom expiry duration. Overrides settings-based defaults.
- Returns:
A signed JWT string.
- duck.contrib.jwt.get_access_lifetime() datetime.timedelta[source]¶
Retrieve the access token lifetime from Duck settings.
Defaults to 60 minutes if
JWT_ACCESS_LIFETIMEis not set.- Returns:
Seconds representing the access token lifetime.
- duck.contrib.jwt.get_algorithm() str[source]¶
Retrieve the JWT signing algorithm from Duck settings.
Defaults to
HS256ifJWT_ALGORITHMis not set.- Returns:
The algorithm string (e.g.
"HS256").
- duck.contrib.jwt.get_jwt_lib()[source]¶
Import and return the PyJWT module.
- Returns:
The imported
jwtmodule.- Raises:
ImportError – If the
PyJWTpackage is not installed.
- duck.contrib.jwt.get_refresh_lifetime() float[source]¶
Retrieve the refresh token lifetime from Duck settings.
Defaults to 7 days in seconds if
JWT_REFRESH_LIFETIMEis not set.- Returns:
Seconds representing the refresh token lifetime.