|
4 | 4 |
|
5 | 5 |
|
6 | 6 | class JWTUtil: |
7 | | - |
8 | 7 | @staticmethod |
9 | | - def generate_jwk(key_type='RSA', size=2048): |
| 8 | + def generate_jwk(key_type="RSA", size=2048): |
10 | 9 | key = jwk.JWK.generate(kty=key_type, size=size) |
11 | 10 | private_jwk = json.loads(key.export_private()) |
12 | 11 | public_jwk = json.loads(key.export_public()) |
13 | 12 | return private_jwk, public_jwk |
14 | 13 |
|
15 | 14 | @staticmethod |
16 | | - def encode(payload: dict, private_jwk: dict, algorithm='RS256') -> str: |
| 15 | + def encode(payload: dict, private_jwk: dict, algorithm="RS256") -> str: |
17 | 16 | return jwt.encode(payload, key=private_jwk, algorithm=algorithm) |
18 | 17 |
|
19 | 18 | @staticmethod |
20 | | - def decode(token: str, public_jwk: dict, algorithm='RS256', options=None) -> dict: |
| 19 | + def decode(token: str, public_jwk: dict, algorithm="RS256", options=None) -> dict: |
21 | 20 | if options is None: |
22 | 21 | options = {} |
23 | 22 |
|
24 | | - options['verify_aud'] = options.get('verify_aud', False) |
| 23 | + options["verify_aud"] = options.get("verify_aud", False) |
25 | 24 |
|
26 | 25 | return jwt.decode(token, key=public_jwk, algorithms=algorithm, options=options) |
27 | 26 |
|
28 | 27 | @staticmethod |
29 | 28 | def unverified_decode(token: str) -> dict: |
30 | 29 | return jwt.get_unverified_claims(token) |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def get_value_from_token(token: str, key: str, default: any = None) -> any: |
| 33 | + try: |
| 34 | + return JWTUtil.unverified_decode(token).get(key, default) |
| 35 | + except Exception as e: |
| 36 | + return default |
0 commit comments