Skip to content

Commit 9f5b8c4

Browse files
committed
Correctly pad oidc tokens
According to the JWT spec base64 padding characters are stripped. Fixes kubernetes-client#65
1 parent 789de6a commit 9f5b8c4

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

config/kube_config.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,15 @@ def _load_oid_token(self):
231231
if len(parts) != 3: # Not a valid JWT
232232
return None
233233

234+
padding = (4 - len(parts[1]) % 4) * '='
235+
234236
if PY3:
235237
jwt_attributes = json.loads(
236-
base64.b64decode(parts[1]).decode('utf-8')
238+
base64.b64decode(parts[1] + padding).decode('utf-8')
237239
)
238240
else:
239241
jwt_attributes = json.loads(
240-
base64.b64decode(parts[1] + "==")
242+
base64.b64decode(parts[1] + padding)
241243
)
242244

243245
expire = jwt_attributes.get('exp')

config/kube_config_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ def _raise_exception(st):
8787

8888
TEST_OIDC_TOKEN = "test-oidc-token"
8989
TEST_OIDC_INFO = "{\"name\": \"test\"}"
90-
TEST_OIDC_BASE = _base64(TEST_OIDC_TOKEN) + "." + _base64(TEST_OIDC_INFO)
90+
TEST_OIDC_BASE = _base64(TEST_OIDC_TOKEN).strip('=') + "." + _base64(TEST_OIDC_INFO).strip('=')
9191
TEST_OIDC_LOGIN = TEST_OIDC_BASE + "." + TEST_CLIENT_CERT_BASE64
9292
TEST_OIDC_TOKEN = "Bearer %s" % TEST_OIDC_LOGIN
9393
TEST_OIDC_EXP = "{\"name\": \"test\",\"exp\": 536457600}"
94-
TEST_OIDC_EXP_BASE = _base64(TEST_OIDC_TOKEN) + "." + _base64(TEST_OIDC_EXP)
94+
TEST_OIDC_EXP_BASE = _base64(TEST_OIDC_TOKEN).strip('=') + "." + _base64(TEST_OIDC_EXP).strip('=')
9595
TEST_OIDC_EXPIRED_LOGIN = TEST_OIDC_EXP_BASE + "." + TEST_CLIENT_CERT_BASE64
9696
TEST_OIDC_CA = _base64(TEST_CERTIFICATE_AUTH)
9797

0 commit comments

Comments
 (0)