Skip to content

Commit 760661f

Browse files
committed
Slurm JWT Checks: Fix tests
The previous change in a1a9e9d meant that the test data now failed.
1 parent a1a9e9d commit 760661f

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/zocalo/util/slurm/__init__.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,24 @@ def validate_is_jwt(token: str) -> bool:
2121
header, payload, _ = token.split(".")
2222
try:
2323
# Check both header and payload are valid base64-encoded json objects
24+
# Note that JWT are Base64URL, which might not have padding.
2425
if not (
25-
isinstance(json.loads(base64.b64decode(header, validate=True)), dict)
26-
and isinstance(json.loads(base64.b64decode(payload, validate=True)), dict)
26+
isinstance(
27+
json.loads(
28+
base64.urlsafe_b64decode(
29+
header + "=" * (4 - len(header) % 4)
30+
).decode()
31+
),
32+
dict,
33+
)
34+
and isinstance(
35+
json.loads(
36+
base64.urlsafe_b64decode(
37+
payload + "=" * (4 - len(payload) % 4)
38+
).decode()
39+
),
40+
dict,
41+
)
2742
):
2843
return False
2944
except (binascii.Error, json.JSONDecodeError):

tests/util/test_slurm.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
import zocalo.configuration
55
from zocalo.util import slurm
66

7+
# A sample (valid but not useful) JWT token
8+
SAMPLE_JWT_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
9+
710

811
@pytest.fixture
912
def zocalo_configuration(mocker):
1013
zc = mocker.MagicMock(zocalo.configuration.Configuration)
1114
zc.slurm = {
1215
"url": "http://slurm.example.com:1234",
1316
"user": "foo",
14-
"user_token": "sometoken",
17+
"user_token": SAMPLE_JWT_TOKEN,
1518
"api_version": "v0.0.40",
1619
}
1720
return zc
@@ -229,7 +232,7 @@ def test_get_slurm_api_from_zocalo_configuration(slurm_api):
229232
assert slurm_api.url == "http://slurm.example.com:1234"
230233
assert slurm_api.version == "v0.0.40"
231234
assert slurm_api.user_name == "foo"
232-
assert slurm_api.user_token == "sometoken"
235+
assert slurm_api.user_token == SAMPLE_JWT_TOKEN
233236

234237

235238
def test_get_slurm_api_user_token_external_file(tmp_path):

0 commit comments

Comments
 (0)