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:
21
21
header , payload , _ = token .split ("." )
22
22
try :
23
23
# Check both header and payload are valid base64-encoded json objects
24
+ # Note that JWT are Base64URL, which might not have padding.
24
25
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
+ )
27
42
):
28
43
return False
29
44
except (binascii .Error , json .JSONDecodeError ):
Original file line number Diff line number Diff line change 4
4
import zocalo .configuration
5
5
from zocalo .util import slurm
6
6
7
+ # A sample (valid but not useful) JWT token
8
+ SAMPLE_JWT_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
9
+
7
10
8
11
@pytest .fixture
9
12
def zocalo_configuration (mocker ):
10
13
zc = mocker .MagicMock (zocalo .configuration .Configuration )
11
14
zc .slurm = {
12
15
"url" : "http://slurm.example.com:1234" ,
13
16
"user" : "foo" ,
14
- "user_token" : "sometoken" ,
17
+ "user_token" : SAMPLE_JWT_TOKEN ,
15
18
"api_version" : "v0.0.40" ,
16
19
}
17
20
return zc
@@ -229,7 +232,7 @@ def test_get_slurm_api_from_zocalo_configuration(slurm_api):
229
232
assert slurm_api .url == "http://slurm.example.com:1234"
230
233
assert slurm_api .version == "v0.0.40"
231
234
assert slurm_api .user_name == "foo"
232
- assert slurm_api .user_token == "sometoken"
235
+ assert slurm_api .user_token == SAMPLE_JWT_TOKEN
233
236
234
237
235
238
def test_get_slurm_api_user_token_external_file (tmp_path ):
You can’t perform that action at this time.
0 commit comments