File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed
Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff 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 ):
Original file line number Diff line number Diff line change 44import zocalo .configuration
55from 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
912def 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
235238def test_get_slurm_api_user_token_external_file (tmp_path ):
You can’t perform that action at this time.
0 commit comments