Here is a quick usage example: generating a private RSA key, signing some data, then validating that signature with the
matching public key:
from jwskate import Jwk
# Let's generate a random private key, to use with alg 'RS256'.
# Based on that alg, jwskate knows it must be an RSA key.
# RSA keys can be of any size, so let's pass the requested key size as parameter
rsa_private_jwk = Jwk.generate(alg="RS256", key_size=2048)
data = b"Signing is easy!" # we will sign this
signature = rsa_private_jwk.sign(data) # done!
print(signature)
# b'-\xe89\x81\xc4\xb9.G\x11\xa6\x93/dm\xf0\xc8\x0f\xd....'
# now extract the public key, and verify the signature with it
rsa_public_jwk = rsa_private_jwk.public_jwk()
assert rsa_public_jwk.verify(data, signature)
# let's see what a `Jwk` looks like:
from collections import UserDict
assert isinstance(rsa_private_jwk, UserDict) # Jwk are UserDicts
print(rsa_private_jwk.with_usage_parameters())
The result of this print will look like this (with the random parts abbreviated to ... for display purposes only):
Now let's sign a JWT containing arbitrary claims, this time using an Elliptic Curve (EC) key:
from jwskate import Jwk, Jwt, InvalidSignature
# This time let's try to use the "ES256" alg, which is an ECDSA signature alg.
# Let's specify an arbitrary Key ID (kid).
private_jwk = Jwk.generate(alg="ES256", kid="my_key")
# Note that the appropriate key type and curve are determined automatically, based only on the `alg` value
print(private_jwk)
# {'kty': 'EC', 'crv': 'P-256', 'x': 'Ppe...', 'y': '9Si...', 'd': 'g09...', 'alg': 'ES256'}
assert private_jwk.kty == "EC"
assert private_jwk.crv == "P-256"
assert private_jwk.alg == "ES256"
# this is a private key and 'ES256' is a signature alg, so 'use' and 'key_ops' can also be deduced:
assert private_jwk.use == "sig"
assert private_jwk.key_ops == ("sign",)
# here are the claims to sign in a JWT:
claims = {"sub": "some_sub", "claim1": "value1"}
jwt = Jwt.sign(claims, private_jwk)
# that's it! we have a signed JWT.
# let's see what this looks like
assert isinstance(jwt, Jwt) # Jwt are objects
print(jwt) # they can be serialized to string with the `__str__` method
# eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzb21lX3N1YiIsImNsYWltMSI6InZhbHVlMSJ9.SBQIlGlFdwoEMViWUFsBmCsXShtOq4lnp3Im5ZVh1PFCGJFdW-dTG9qJjlFSAA_BkM5PF9u38PL7Ai9cC2_DJw
assert jwt.claims == claims # claims can be accessed as a `dict`
assert jwt.headers == {"typ": "JWT", "alg": "ES256", "kid": "my_key"} # headers too
assert jwt.sub == "some_sub" # individual claims can be accessed as attributes
assert jwt["claim1"] == "value1" # or as `dict` items (with "subscription")
assert jwt.alg == "ES256" # alg and kid headers are also accessible as attributes
assert jwt.kid == private_jwk.kid
# notice that alg and kid are automatically set with appropriate values taken from our private jwk
assert isinstance(jwt.signature, bytes) # signature is accessible too
# verifying the jwt signature is as easy as:
assert jwt.verify_signature(private_jwk.public_jwk())
# or, if you want an exception to be raised if the signature is invalid:
try:
jwt.verify(private_jwk.public_jwk())
except InvalidSignature:
print("Invalid signature!") # this will not be printed, since the signature is valid
# since our jwk contains an 'alg' parameter (here 'ES256'), the signature is automatically verified using that alg
# you could also specify an alg manually, useful for keys with no "alg" hint:
assert jwt.verify_signature(private_jwk.public_jwk(), alg="ES256")
# note that jwskate will only trust the alg(s) you provide as parameter, either part of the JWK
# or with `alg` or `algs` params, and will ignore the 'alg' that is set in the JWT, for security reasons.
For most applications, you will often sign with the same key and issuer, and with a pre-determined token lifetime.
So you can create a JwtSigner object to simplify this for you:
from jwskate import Jwk, JwtSigner
private_jwk = Jwk.generate(alg="ES256")
signer = JwtSigner(issuer="https://myissuer.com", key=private_jwk, default_lifetime=60) # those values are pre-determined
jwt = signer.sign( # so you only have to specify the variable claims when signing tokens
subject="some_sub",
audience="some_aud",
extra_claims={"custom_claim1": "value1", "custom_claim2": "value2"},
)
print(jwt.claims)
The generated JWT will include the standardized claims (iss, aud, sub, iat, exp and jti), together with the
extra_claims provided to .sign():
There are already multiple modules implementing JOSE and Json Web Crypto related specifications in Python. However, I
have been dissatisfied by all of them so far, so I decided to come up with my own module.
Not to say that those are bad libs (I actually use jwcrypto myself for jwskate unit tests), but they either don't
support some important features, lack documentation, or more generally have APIs that don't feel easy-enough,
Pythonic-enough to use. See Design below for some of the design decisions that lead to jwskate.
Design
Tokens are objects
Since JSON Web Tokens (JWT) are more and more used, JWT generation and validation must be as easy to do as possible. The
Jwt class wraps around a JWT value to allow easy access to its headers, claims and signature, and exposes methods to
easily verify the signature.
Jwt instances always represent a syntactically valid JWT. If you try to initialize one with a malformed value, you
will get a InvalidJwt exception, with an helpful error message:
jwt = Jwt(
"eyJhbGci-malformedheader.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
)
# jwskate.jwt.base.InvalidJwt: Invalid JWT header: it must be a Base64URL-encoded JSON object
Jwt may be objects, but they are easy to serialize into their representation. Use either str() or bytes()
depending on what type of value you need, or the value attribute:
If, for testing purposes, you need to fully control which headers are included in the JWT, even if they are inconsistent,
you can use Jwt.sign_arbitrary():
from jwskate import SymmetricJwk, Jwt
jwk = SymmetricJwk.from_bytes(b"T0t4llyR@nd0M", kid="symmetric_key1")
jwt = Jwt.sign_arbitrary(
headers={
"custom_header": "custom_value",
"typ": "WeirdJWT",
"kid": "R@nd0m_KID",
"alg": "WeirdAlg",
},
claims={"my": "claims"},
key=jwk,
alg="HS256",
)
print(jwt)
# eyJjdXN0b21faGVhZGVyIjoiY3VzdG9tX3ZhbHVlIiwidHlwIjoiV2VpcmRKV1QiLCJraWQiOiJSQG5kMG1fS0lEIiwiYWxnIjoiV2VpcmRBbGcifQ.eyJteSI6ImNsYWltcyJ9.bcTFqCSiVIbyJhxClgsBDIyhbvLXTOXOV55QGqo2mhw
print(jwt.headers) # you asked for inconsistent headers, you have them:
# {'custom_header': 'custom_value', 'typ': 'WeirdJWT', 'kid': 'R@nd0m_KID', 'alg': 'WeirdAlg'}
Jwk as thin wrapper around cryptography keys
Jwk keys are just thin wrappers around keys from the cryptography module, or, in the case of symmetric keys,
around bytes. But, unlike cryptography keys, they present a consistent interface for signature
creation/verification, key management, and encryption/decryption, regardless of the algorithm:
For signature generation and verification, use Jwk.sign() and Jwk.verify() respectively
For content encryption using a Key-Management alg and a symmetric Content-Encryption Key (CEK):
use Jwk.sender_key() to generate and wrap or otherwise derive a Content Encryption Key
use Jwk.encrypt() on that CEK to encrypt your data
For decryption on the recipient side:
use Jwk.recipient_key() to unwrap or otherwise derive the CEK
use Jwk.decrypt() on that CEK to decrypt the encrypted data
Here is an example usage of signature and verification:
from jwskate import Jwk, SignatureAlgs
# I'll use a symmetric key for this example, but the same applies to asymmetric keys
sig_key = Jwk.generate(alg=SignatureAlgs.HS256, kid="example_signature_key") # symmetric key
# use `Jwk.sign()` and `Jwk.verify()` to sign and verify data
data = b"Something to sign"
signature = sig_key.sign(data)
if not sig_key.verify(data, signature[:-7] + b"altered"):
print("Invalid signature!")
Here is an example of data encryption using a Key-Management alg and a Content-Encryption Key.
This example uses a Symmetric Key, but the interface is the same with private/public keys (excepted that encryption must be done with the public key).
from jwskate import Jwk, EncryptionAlgs, KeyManagementAlgs
# generate a suitabe key
key_mgmt_key = Jwk.generate(alg=KeyManagementAlgs.A256KW, kid="example_key_management_key")
# First, choose your symmetric encryption alg
enc = EncryptionAlgs.A256GCM
# Use `Jwk.sender_key()` and `Jwk.recipient_key()` to generate or otherwise derive the CEK
# This will return a 3-tuple with the clear-text CEK, the wrapped CEK and extra headers to include in the token, if any
cek, wrapped_cek, headers = key_mgmt_key.sender_key(enc=enc)
# use the clear-text CEK to encrypt your data
secret_message = b"Encryption is easy!"
ciphertext, iv, tag = cek.encrypt(secret_message, enc=enc)
# you may also include Additional Authentication Data (AAD) with the `aad` parameter to `cek.encrypt()` above
# send the ciphertext, the wrapped CEK, IV, tag, AAD (if any) and extra headers to the recipient
recipient_cek = key_mgmt_key.recipient_key(wrapped_cek, enc=enc, **headers)
assert recipient_cek == cek # the CEK is the same as the one we used to encrypt the data
cleartext = recipient_cek.decrypt(ciphertext, iv=iv, tag=tag, enc=enc) # if there is AAD, you must also include it here
assert cleartext == secret_message
All of the above is much easier if you use a JWE to encrypt your data:
from jwskate import Jwk, JweCompact, EncryptionAlgs, KeyManagementAlgs
key = Jwk.generate(alg=KeyManagementAlgs.A256GCMKW)
jwe = JweCompact.encrypt(b"This is a secret message", key=key, enc=EncryptionAlgs.A256CBC_HS512)
print(jwe)
# the resulting JWE contains everything the recipient needs to be able to decrypt your message, excepted the key of course!
assert jwe.alg == "A256GCMKW" # key-management alg is part of the headers
assert jwe.enc == "A256CBC-HS512" # as well as content-encryption alg
assert isinstance(jwe.ciphertext, bytes) # the encrypted data
assert isinstance(jwe.wrapped_cek, bytes) # the CEK, if is it encrypted (empty if it is otherwise derived)
assert isinstance(jwe.initialization_vector, bytes) # the IV used for encryption
assert isinstance(jwe.authentication_tag, bytes) # the authentication tag
assert isinstance(jwe.additional_authenticated_data) # theJWE header part is used as AAD
# on recipient side, decryption is as easy as:
assert jwe.decrypt(key) == b"This is a secret message"
The Jwk layer is so thin, that everywhere a key is required as parameter, you may pass either:
a Jwk instance,
or a Mapping (typically a dict) representing the JWK key,
a raw cryptography key instance, or a bytes if it is a symmetric key, but in that case, you must specify the alg
to use, since that is not part of the key.
As long as the provided key is suitable for the chosen algorithm, any of the above will work. Here is an example of
encrypting JWE tokens with raw cryptography keys:
from jwskate import JweCompact
secret_message = b"my_very_secret_message"
# symmetric encryption with A128GCMKW and a 128 bit `bytes` key
aes_key = b"very_secret_key!"
jwe = JweCompact.encrypt(secret_message, key=aes_key, alg="A128GCMKW", enc="A128GCM")
assert jwe.decrypt(key=aes_key) == secret_message
# or, for asymmetric encryption with ECDH-ES+A256KW and a raw `cryptography` key (e.g. X25519):
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
eckey = X25519PrivateKey.generate()
jwe = JweCompact.encrypt(secret_message, key=eckey.public_key(), alg="ECDH-ES+A256KW", enc="A256GCM")
assert jwe.decrypt(key=eckey) == secret_message
Jwk are UserDict instances
JWK are specified as JSON objects, which are parsed as dict in Python. The Jwk class in jwskate is actually a
UserDict subclass, which is very similar to a standard dict. So you can use it exactly like you would use a dict:
you can access its members, dump it back as JSON, etc. The same is true for Signed or Encrypted Json Web tokens in JSON
format. However, you cannot change the key cryptographic materials, since that would lead to unusable keys.
Unfortunately, UserDict is not natively serializable with the default JSON dump. To workaround that, you may either
use Jwk.to_json() / JwkSet.to_json() to get a JSON-serialized string, or Jwk.to_dict() / JwkSet.to_dict() to get
a standard dict, that is serializable by the standard json module.
JWA Wrappers
You can use cryptography to do the cryptographic operations that are described in
JWA, but since cryptography is a general purpose library, its usage is not
straightforward and gives you plenty of options to carefully select and combine, leaving room for mistakes, errors and
confusion. It also has a quite inconsistent API to handle the different key types and algorithms. To work around this,
jwskate comes with a set of consistent wrappers that implement the exact JWA specifications, with minimum risk of
mistakes.
Here is an example of using raw JWA wrappers:
from jwskate import A256KW, A256GCM # pick any key-management and content-encryption algs
# on sender side, you have a message to encrypt and a pre-shared key with the recipient
secret_message = b"This is a secret message"
shared_key = A256KW.generate_key() # let's generate a suitable pre-shared key for this example, using this helper
a256kw = A256KW(shared_key) # you can initialize a JWA wrapper with a key of your choice this way
a256gcm = A256GCM.with_random_key() # or you can initialize them with a randomly generated key, as a one-liner
iv = A256GCM.generate_iv() # helper method for generating Initialisation Vectors of the appropriate size
ciphertext, tag = a256gcm.encrypt(secret_message, iv=iv) # use the Content-Encryption JWA wrapper to encrypt your message
wrapped_cek = a256kw.wrap_key(a256gcm.key) # and/or use the Key-Management JWA wrapper to wrap the CEK
# on recipient side, you receive ciphertext, iv, tag and wrapped_cek,
# and you are supposed to know the shared_key and algorithms to use
cek = A256KW(shared_key).unwrap_key(wrapped_cek) # so you can unwrap the CEK
cleartext = A256GCM(cek).decrypt(ciphertext, iv=iv, auth_tag=tag) # and decrypt the ciphertext and validating the tag
assert cleartext == secret_message
For every signature verification method in jwskate, the expected signature(s) algorithm(s) must be specified. That is
to avoid a security flaw where your application accepts tokens with a weaker encryption scheme than what your security
policy mandates; or even worse, where it accepts unsigned tokens, or tokens that are symmetrically signed with an
improperly used public key, leaving your application exposed to exploitation by attackers.
To specify which signature algorithms are accepted, each signature verification method accepts, in order of preference:
an alg parameter which contains the expected algorithm, or an algs parameter which contains a list of acceptable
algorithms
the alg parameter from the signature verification Jwk, if present. This alg is the algorithm intended for use
with that key.
Note that you cannot use alg and algs at the same time. If your Jwk contains an alg parameter, and you provide
an alg or algs which does not match that value, a Warning will be emitted.