Skip to content

Commit 92fed82

Browse files
authored
Add invalid algorithm exception check (#3399)
* Add invalid algorithm exception check We need to catch an invalid algorithm error when we decode the SSO token and raise it as OpenIDTokenInvalid. This is because we are using HS256 for our internal api key encode-decode. PBENCH-1136
1 parent a9175cd commit 92fed82

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/pbench/server/auth/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,6 @@ def token_introspect(self, token: str) -> JSON:
375375
jwt.ExpiredSignatureError,
376376
jwt.InvalidSignatureError,
377377
jwt.InvalidAudienceError,
378+
jwt.InvalidAlgorithmError,
378379
) as exc:
379380
raise OpenIDTokenInvalid() from exc

lib/pbench/test/unit/server/auth/test_auth.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def test_construct_oidc_client_succ(self, monkeypatch):
337337
)
338338

339339
def test_token_introspect_succ(self, monkeypatch, rsa_keys):
340-
"""Verify .token_introspect_offline() success path"""
340+
"""Verify .token_introspect() success path"""
341341
client_id = "us"
342342
token, expected_payload = gen_rsa_token(client_id, rsa_keys["private_key"])
343343

@@ -364,7 +364,7 @@ def test_token_introspect_succ(self, monkeypatch, rsa_keys):
364364
assert response == expected_payload
365365

366366
def test_token_introspect_exp(self, monkeypatch, rsa_keys):
367-
"""Verify .token_introspect_offline() failure via expiration"""
367+
"""Verify .token_introspect() failure via expiration"""
368368
client_id = "us"
369369
token, expected_payload = gen_rsa_token(
370370
client_id, rsa_keys["private_key"], exp=42
@@ -383,7 +383,7 @@ def test_token_introspect_exp(self, monkeypatch, rsa_keys):
383383
), f"{exc.value.__cause__}"
384384

385385
def test_token_introspect_aud(self, monkeypatch, rsa_keys):
386-
"""Verify .token_introspect_offline() failure via audience error"""
386+
"""Verify .token_introspect() failure via audience error"""
387387
client_id = "us"
388388
token, expected_payload = gen_rsa_token(client_id, rsa_keys["private_key"])
389389

@@ -397,7 +397,7 @@ def test_token_introspect_aud(self, monkeypatch, rsa_keys):
397397
assert str(exc.value.__cause__) == "Invalid audience", f"{exc.value.__cause__}"
398398

399399
def test_token_introspect_sig(self, monkeypatch, rsa_keys):
400-
"""Verify .token_introspect_offline() failure via signature error"""
400+
"""Verify .token_introspect() failure via signature error"""
401401
client_id = "us"
402402
token, expected_payload = gen_rsa_token(client_id, rsa_keys["private_key"])
403403

@@ -415,6 +415,25 @@ def test_token_introspect_sig(self, monkeypatch, rsa_keys):
415415
str(exc.value.__cause__) == "Signature verification failed"
416416
), f"{exc.value.__cause__}"
417417

418+
def test_token_introspect_alg(self, monkeypatch, rsa_keys):
419+
"""Verify .token_introspect() failure via algorithm error"""
420+
client_id = "us"
421+
422+
# Make the algorithm invalid.
423+
generated_api_key = jwt.encode(
424+
{"some_key": "some_value"}, "my_secret", algorithm="HS256"
425+
)
426+
config = mock_connection(
427+
monkeypatch, client_id, public_key=rsa_keys["public_key"]
428+
)
429+
oidc_client = OpenIDClient.construct_oidc_client(config)
430+
431+
with pytest.raises(OpenIDTokenInvalid) as exc:
432+
oidc_client.token_introspect(generated_api_key)
433+
assert (
434+
str(exc.value.__cause__) == "The specified alg value is not allowed"
435+
), f"{exc.value.__cause__}"
436+
418437

419438
@dataclass
420439
class MockRequest:

0 commit comments

Comments
 (0)