API Reference
- jwt.encode(payload, key, algorithm='HS256', headers=None, json_encoder=None) str
- jwt.decode(jwt, key='', algorithms=None, options=None, audience=None, issuer=None, leeway=0) dict[str, Any]
- jwt.decode_complete(jwt, key='', algorithms=None, options=None, verify=None, detached_payload=None, audience=None, issuer=None, subject=None, leeway=0) dict[str, Any]
- class jwt.PyJWT(options: dict[str, Any] | None = None)
- class jwt.PyJWK(jwk_data: Dict[str, Any], algorithm: str | None = None)
- property algorithm_name
- Type:
str
The name of the algorithm used by the key.
- class jwt.PyJWKSet(keys: list[Dict[str, Any]])
- class jwt.PyJWKClient(uri: str, cache_keys: bool = False, max_cached_keys: int = 16, cache_jwk_set: bool = True, lifespan: int = 300, headers: Dict[str, Any] | None = None, timeout: int = 30, ssl_context: SSLContext | None = None)
Note
TODO: Finish documenting PyJWS class
- class jwt.api_jws.PyJWS(algorithms: Sequence[str] | None = None, options: dict[str, Any] | None = None)
- get_algorithm_by_name(alg_name: str) Algorithm
For a given string name, return the matching Algorithm object.
Example usage:
>>> jws_obj.get_algorithm_by_name("RS256")
- get_algorithms() list[str]
Returns a list of supported values for the ‘alg’ parameter.
- get_unverified_header(jwt: str | bytes) dict[str, Any]
Returns back the JWT header parameters as a dict()
Note: The signature is not verified so the header parameters should not be fully trusted until signature verification is complete
- register_algorithm(alg_id: str, alg_obj: Algorithm) None
Registers a new Algorithm for use when creating and verifying tokens.
- unregister_algorithm(alg_id: str) None
Unregisters an Algorithm for use when creating and verifying tokens Throws KeyError if algorithm is not registered.
Algorithms
- class jwt.algorithms.Algorithm
The interface for an algorithm used to sign and verify tokens.
- compute_hash_digest(bytestr: bytes) bytes
Compute a hash digest using the specified algorithm’s hash algorithm.
If there is no hash algorithm, raises a NotImplementedError.
- abstractmethod static from_jwk(jwk: str | Dict[str, Any]) Any
Deserializes a given key from JWK back into a key object
- abstractmethod prepare_key(key: Any) Any
Performs necessary validation and conversions on the key and returns the key value in the proper format for sign() and verify().
- abstractmethod sign(msg: bytes, key: Any) bytes
Returns a digital signature for the specified message using the specified key value.
- abstractmethod static to_jwk(key_obj, as_dict: Literal[True]) Dict[str, Any]
- abstractmethod static to_jwk(key_obj, as_dict: Literal[False] = False) str
Serializes a given key into a JWK
- abstractmethod verify(msg: bytes, key: Any, sig: bytes) bool
Verifies that the specified digital signature is valid for the specified message and key values.
Types
Warnings
- exception jwt.warnings.RemovedInPyjwt3Warning
Bases:
DeprecationWarning
Exceptions
- exception jwt.exceptions.DecodeError
Bases:
InvalidTokenError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.ExpiredSignatureError
Bases:
InvalidTokenError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.ImmatureSignatureError
Bases:
InvalidTokenError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.InvalidAlgorithmError
Bases:
InvalidTokenError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.InvalidAudienceError
Bases:
InvalidTokenError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.InvalidIssuedAtError
Bases:
InvalidTokenError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.InvalidIssuerError
Bases:
InvalidTokenError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.InvalidJTIError
Bases:
InvalidTokenError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.InvalidKeyError
Bases:
PyJWTError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.InvalidSignatureError
Bases:
DecodeError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.InvalidSubjectError
Bases:
InvalidTokenError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.InvalidTokenError
Bases:
PyJWTError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.MissingCryptographyError
Bases:
PyJWKError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.MissingRequiredClaimError(claim: str)
Bases:
InvalidTokenError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.PyJWKClientConnectionError
Bases:
PyJWKClientError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.PyJWKClientError
Bases:
PyJWTError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.PyJWKError
Bases:
PyJWTError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception jwt.exceptions.PyJWKSetError
Bases:
PyJWTError- add_note(object, /)
Exception.add_note(note) – add a note to the exception
- with_traceback(object, /)
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.