reshut.utils
- keygen(algorithm, key_size=None)
Generates a key for the specified algorithm.
- Parameters:
algorithm (
Algorithm) – The algorithm to use.key_size (
Optional[int]) – If provided, overrides the size of the generated key(s), in bits, if supported by the algorithm. Typically you would not do this.
- Raises:
NotImplementedError – Raised when an unsupported algorithm is specified.
- Return type:
- Returns:
A JWK representing the generated key.
- tokenize(key, claims, *, audience=None, issuer=None, subject=None, expiry=None, not_before=None, issued_at=None, token_id=None)
Tokenize the provided claims, optionally accepting standard JWT claims as args and injecting them anew on top of existing claims.
- Parameters:
private_key – The private key (or secret) used for signing.
claims (
dict[str,Any]) – The claims to be tokenized.audience (
Union[str,list[str],None]) – Optionalaudclaim - a string or list of strings.issuer (
Optional[str]) – Optionalissclaim.subject (
Optional[str]) – Optionalsubclaim.expiry (
Optional[int]) – Optionalexpclaim (unix timestamp).not_before (
Optional[int]) – Optionalnbfclaim (unix timestamp).issued_at (
Optional[int]) – Optionaliatclaim (unix timestamp). If omitted and not present in claims, the current UTC time is used.token_id (
Optional[str]) – Optionaljticlaim.
- Return type:
str- Returns:
The claims encoded as a compact-serialization JWT.
- Raises:
Exception – If an error occurs while creating the token.
- validate(key, token, *, enforce=True, audience=None, issuer=None, subject=None)
Verify a token and return the contained claims.
If “standard claims” are provided as args, the function also checks that those claims match.
Other standard claims such as
nbfand are automatically enforced unlessenforce_claims=Falseis specified.- Parameters:
public_key – The public key (or secret) used for verification.
token (
str) – The compact-serialization JWT string to be validated.enforce (
bool) – Indicates that “standard claims enforcement” should be performed, for example thatnbfis not validated before the indicated time or thatexpis not in the past.audience (
Optional[str]) – Expectedaudclaim. Omit to skip validatingaudclaim.issuer (
Optional[str]) – Expectedissclaim. Omit to skip validatingissclaim.subject (
Optional[str]) – Expectedsubclaim. Omit to skip validatingsubclaim.
- Return type:
dict[str,Any]- Returns:
The decoded claims (as a
dict).- Raises:
Exception – If the token is invalid, fails standards-enforcement, or claims do not match expected values.