File: __init__.py

package info (click to toggle)
pyjwt 2.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 864 kB
  • sloc: python: 5,151; makefile: 141
file content (43 lines) | stat: -rw-r--r-- 1,055 bytes parent folder | download
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
import json
import os

from jwt.algorithms import has_crypto
from jwt.utils import base64url_decode

try:
    from cryptography.hazmat.primitives.asymmetric import ec
except ModuleNotFoundError:
    pass

if has_crypto:
    from jwt.algorithms import RSAAlgorithm

BASE_PATH = os.path.dirname(os.path.abspath(__file__))


def decode_value(val):
    decoded = base64url_decode(val)
    return int.from_bytes(decoded, byteorder="big")


def load_hmac_key():
    with open(os.path.join(BASE_PATH, "jwk_hmac.json")) as infile:
        keyobj = json.load(infile)

    return base64url_decode(keyobj["k"])


def load_rsa_pub_key():
    with open(os.path.join(BASE_PATH, "jwk_rsa_pub.json")) as infile:
        return RSAAlgorithm.from_jwk(infile.read())


def load_ec_pub_key_p_521():
    with open(os.path.join(BASE_PATH, "jwk_ec_pub_P-521.json")) as infile:
        keyobj = json.load(infile)

    return ec.EllipticCurvePublicNumbers(
        x=decode_value(keyobj["x"]),
        y=decode_value(keyobj["y"]),
        curve=ec.SECP521R1(),
    ).public_key()