Skip to content

Commit e628da0

Browse files
committed
fix: base64 encode jwt
1 parent d2f6206 commit e628da0

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import base64
12
import json
23

34
from jwcrypto import jwk
45
from jwcrypto import jwt as jwcrypto_jwt
5-
from jwcrypto.jws import JWS
66

77

88
class JWTUtil:
@@ -54,16 +54,24 @@ def decode(token: str, public_jwk: dict, algorithm="RS256", options=None) -> dic
5454

5555
@staticmethod
5656
def unverified_decode(token: str) -> dict:
57-
# Deserialize JWS without verification
58-
jws = JWS()
59-
jws.deserialize(token, None)
57+
parts = token.split(".")
58+
if len(parts) != 3:
59+
raise ValueError("Invalid JWT token format")
6060

61-
# Parse payload from JSON string
62-
payload = jws.payload
63-
if isinstance(payload, bytes):
64-
payload = payload.decode("utf-8")
61+
# Decode payload part (base64url)
62+
payload_part = parts[1]
6563

66-
return json.loads(payload)
64+
# Handle base64url padding
65+
padding = 4 - len(payload_part) % 4
66+
if padding != 4:
67+
payload_part += "=" * padding
68+
69+
# Decode base64url
70+
payload_bytes = base64.urlsafe_b64decode(payload_part)
71+
payload_str = payload_bytes.decode("utf-8")
72+
73+
# Parse JSON
74+
return json.loads(payload_str)
6775

6876
@staticmethod
6977
def get_value_from_token(token: str, key: str, default: any = None) -> any:

0 commit comments

Comments
 (0)