Skip to content

Commit 868bfb9

Browse files
author
Jongmin Kim
authored
Merge pull request #137 from whdalsrnt/master
feat: add get_value_from_token method in JWTUtil
2 parents f0666c0 + 621051b commit 868bfb9

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/spaceone/core/auth/jwt/jwt_util.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,33 @@
44

55

66
class JWTUtil:
7-
87
@staticmethod
9-
def generate_jwk(key_type='RSA', size=2048):
8+
def generate_jwk(key_type="RSA", size=2048):
109
key = jwk.JWK.generate(kty=key_type, size=size)
1110
private_jwk = json.loads(key.export_private())
1211
public_jwk = json.loads(key.export_public())
1312
return private_jwk, public_jwk
1413

1514
@staticmethod
16-
def encode(payload: dict, private_jwk: dict, algorithm='RS256') -> str:
15+
def encode(payload: dict, private_jwk: dict, algorithm="RS256") -> str:
1716
return jwt.encode(payload, key=private_jwk, algorithm=algorithm)
1817

1918
@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:
2120
if options is None:
2221
options = {}
2322

24-
options['verify_aud'] = options.get('verify_aud', False)
23+
options["verify_aud"] = options.get("verify_aud", False)
2524

2625
return jwt.decode(token, key=public_jwk, algorithms=algorithm, options=options)
2726

2827
@staticmethod
2928
def unverified_decode(token: str) -> dict:
3029
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

src/spaceone/core/handler/authentication_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from spaceone.core.auth.jwt import JWTAuthenticator, JWTUtil
77
from spaceone.core.transaction import get_transaction
88
from spaceone.core.handler import BaseAuthenticationHandler
9-
from spaceone.core.error import ERROR_AUTHENTICATE_FAILURE, ERROR_REQUIRED_X_DOMAIN_ID
9+
from spaceone.core.error import ERROR_AUTHENTICATE_FAILURE
1010

1111
_LOGGER = logging.getLogger(__name__)
1212

0 commit comments

Comments
 (0)