JWT
Encode JSON Web Tokens as JWS and validate their registered claims.
from secrets import token_bytes
from oxcrypto.jose import jwt
from oxcrypto.jose.jws import JWS
secret = token_bytes(32)
token = jwt.encode({"sub": "alice", "aud": "api", "exp": 2_000_000_000}, secret, algorithm=JWS.HS256)
claims = jwt.decode(token, secret, algorithm=JWS.HS256, audience="api")
assert claims["sub"] == "alice"encode(claims, key, *, algorithm, header=None) serializes claims with
json.dumps and signs it with the JWS selector, adding
typ: JWT. decode verifies the JWS with the same algorithm checks, then
validates claims:
expandnbfare checked againstnow(default: the current time in seconds) withleewayseconds of tolerance. A token is expired oncenow >= exp + leeway.audmust containaudiencewhen the token has anaudclaim, and a token withaudis rejected when noaudienceis given.issandsubmust equalissuerandsubjectwhen those are given.- Every name in
requiredmust be present.
Failed claims raise ValueError; a bad signature or wrong algorithm raises
InvalidTag.