1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import json
import os
from jwt.utils import base64url_decode, force_bytes
from tests.utils import int_from_bytes
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
def decode_value(val):
decoded = base64url_decode(force_bytes(val))
return int_from_bytes(decoded, 'big')
def load_hmac_key():
with open(os.path.join(BASE_PATH, 'jwk_hmac.json'), 'r') as infile:
keyobj = json.load(infile)
return base64url_decode(force_bytes(keyobj['k']))
try:
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.backends import default_backend
from jwt.algorithms import RSAAlgorithm
has_crypto = True
except ImportError:
has_crypto = False
if has_crypto:
def load_rsa_key():
with open(os.path.join(BASE_PATH, 'jwk_rsa_key.json'), 'r') as infile:
return RSAAlgorithm.from_jwk(infile.read())
def load_rsa_pub_key():
with open(os.path.join(BASE_PATH, 'jwk_rsa_pub.json'), 'r') as infile:
return RSAAlgorithm.from_jwk(infile.read())
def load_ec_key():
with open(os.path.join(BASE_PATH, 'jwk_ec_key.json'), 'r') as infile:
keyobj = json.load(infile)
return ec.EllipticCurvePrivateNumbers(
private_value=decode_value(keyobj['d']),
public_numbers=load_ec_pub_key().public_numbers()
)
def load_ec_pub_key():
with open(os.path.join(BASE_PATH, 'jwk_ec_pub.json'), 'r') as infile:
keyobj = json.load(infile)
return ec.EllipticCurvePublicNumbers(
x=decode_value(keyobj['x']),
y=decode_value(keyobj['y']),
curve=ec.SECP521R1()
).public_key(default_backend())
|