I am writing this python code to create RSA private and public keys. Then create a JWK from the private key, then sign the claims with that JWK.
#!/usr/bin/env python
import time
from jose import jwk
from jose import jws
from cryptography.hazmat.primitives import serialization as crypto_serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.backends import default_backend as crypto_default_backend
key = rsa.generate_private_key(backend=crypto_default_backend(), public_exponent=65537, key_size=2048)
private_key = key.private_bytes(crypto_serialization.Encoding.PEM, crypto_serialization.PrivateFormat.PKCS8, crypto_serialization.NoEncryption())
public_key = key.public_key().public_bytes(crypto_serialization.Encoding.OpenSSH, crypto_serialization.PublicFormat.OpenSSH)
key = jwk.construct(private_key, 'RS256')
print 'key.to_dict() = {}'.format(key.to_dict())
claims = {
'iss': 'https://e97b8a9d672e4ce4845ec6947cd66ef6-sb.baas.nintendo.com',
'sub': 'fdfdc610f849726e',
'aud': '20c875ad0d4bfc94',
'iat': time.time() - 20,
'exp': time.time() + 20,
'jti': '807443d3-3b27-4bf9-8e3e-e3f90e1ea055',
'typ': 'id_token'
}
print 'About to sign'
signed = jws.sign(claims, key, algorithm='RS256')
When I run it, it fails at the signing step:
key.to_dict() = {
'e': 'AQAB',
'kty': 'RSA',
'alg': 'RS256',
'n': 'uJ1_BLAH_BLAH_BLAH_veQ',
'q': '8Sa_BLAH_BLAH_BLAH_-Hs',
'p': 'w_t_BLAH_BLAH_BLAH_p5s',
'qi': 'OGz_BLAH_BLAH_BLAH_91U',
'dq': 'D2n_BLAH_BLAH_BLAH_5FM',
'dp': 'pDi_BLAH_BLAH_BLAH_J2k',
'd': 'oV0_BLAH_BLAH_BLAH_VLQ'
}
About to sign
Traceback (most recent call last):
File "./my_file.py", line 56, in <module>
signed = jws.sign(claims, key, algorithm='RS256')
File "my-virtual-env/lib/python2.7/site-packages/jose/jws.py", line 47, in sign
signed_output = _sign_header_and_claims(encoded_header, encoded_payload, algorithm, key)
File "my-virtual-env/lib/python2.7/site-packages/jose/jws.py", line 168, in _sign_header_and_claims
raise JWSError(e)
jose.exceptions.JWSError: Unable to parse an RSA_JWK from key: <jose.backends.pycrypto_backend.RSAKey object at 0x101761190>
What is causing this error and how can I make this work??
Using RS256, you need to give the private key to
jws.sign(), but not with the JWK format.Therefore, just replace
by
And it will work correctly.
Finally, the whole source is:
The result is something like: