Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/spaceone/core/auth/jwt/jwt_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@


class JWTUtil:

@staticmethod
def generate_jwk(key_type='RSA', size=2048):
def generate_jwk(key_type="RSA", size=2048):
key = jwk.JWK.generate(kty=key_type, size=size)
private_jwk = json.loads(key.export_private())
public_jwk = json.loads(key.export_public())
return private_jwk, public_jwk

@staticmethod
def encode(payload: dict, private_jwk: dict, algorithm='RS256') -> str:
def encode(payload: dict, private_jwk: dict, algorithm="RS256") -> str:
return jwt.encode(payload, key=private_jwk, algorithm=algorithm)

@staticmethod
def decode(token: str, public_jwk: dict, algorithm='RS256', options=None) -> dict:
def decode(token: str, public_jwk: dict, algorithm="RS256", options=None) -> dict:
if options is None:
options = {}

options['verify_aud'] = options.get('verify_aud', False)
options["verify_aud"] = options.get("verify_aud", False)

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

@staticmethod
def unverified_decode(token: str) -> dict:
return jwt.get_unverified_claims(token)

@staticmethod
def get_value_from_token(token: str, key: str, default: any = None) -> any:
try:
return JWTUtil.unverified_decode(token).get(key, default)
except Exception as e:
return default
2 changes: 1 addition & 1 deletion src/spaceone/core/handler/authentication_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from spaceone.core.auth.jwt import JWTAuthenticator, JWTUtil
from spaceone.core.transaction import get_transaction
from spaceone.core.handler import BaseAuthenticationHandler
from spaceone.core.error import ERROR_AUTHENTICATE_FAILURE, ERROR_REQUIRED_X_DOMAIN_ID
from spaceone.core.error import ERROR_AUTHENTICATE_FAILURE

_LOGGER = logging.getLogger(__name__)

Expand Down