Skip to content

Commit b2385cb

Browse files
author
dzleidig
committed
Add regular expression to check JWT bearer token form and sad path test.
1 parent 79b1e4f commit b2385cb

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

test/config_test.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def test_signing(self):
174174
def test_bearer_token(self):
175175
"""Verify that the authorization header is set when a bearer token is provided"""
176176

177-
bearer_token = "Bearer myBearerToken"
177+
bearer_token = "Bearer eyJraWQiOiJWcmVsOE9zZ0JXaUpHeEpMeFJ4bE1UaVwvbjgyc1hwWktUaTd2UExUNFQ0TT0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJoMTBlM2hwajliNjc4bXMwOG8zbGlibHQ2IiwidG9rZW5fdXNlIjoiYWNjZXNzIiwic2NvcGUiOiJ3ZWJcL2dldCB3ZWJcL3Bvc3QiLCJhdXRoX3RpbWUiOjE1OTM3MjM1NDgsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC51cy1lYXN0LTEuYW1hem9uYXdzLmNvbVwvdXMtZWFzdC0xX1d6aEZzTGlPRyIsImV4cCI6MTU5MzcyNzE0OCwiaWF0IjoxNTkzNzIzNTQ4LCJ2ZXJzaW9uIjoyLCJqdGkiOiI4Njk5ZDEwYy05Mjg4LTQ0YmEtODIxNi01OTJjZGU3MDBhY2MiLCJjbGllbnRfaWQiOiJoMTBlM2hwajliNjc4bXMwOG8zbGlibHQ2In0.YA_yiD-x6UuBMShprUbUKuB_DO6ogCtd5srfgpJA6Ve_qsf8n19nVMmFsZBy3GxzN92P1ZXiFY99FfNPohhQtaRRhpeUkir08hgJN2bEHCJ5Ym8r9mr9mlwSG6FoiedgLaUVGwJujD9c2rcA83NEo8ayTyfCynF2AZ2pMxLHvqOYtvscGMiMzIwlZfJV301iKUVgPODJM5lpJ4iKCpOy2ByCl2_KL1uxIxgMkglpB-i7kgJc-WmYoJFoN88D89ugnEoAxNfK14N4_RyEkrLNGape9kew79nUeR6fWbVFLiGDDu25_9z-7VB-GGGk7L_Hb7YgVJ5W2FwESnkDvV1T4Q"
178178

179179
with tempfile.NamedTemporaryFile() as config_file, tempfile.NamedTemporaryFile() as key_file:
180180
with open(config_file.name, "w") as f:
@@ -202,3 +202,29 @@ def test_bearer_token(self):
202202
authorization_header_value = prepared_get.headers["authorization"]
203203
self.assertEqual(authorization_header_value, bearer_token)
204204
self.assertNotIn("x-user-token", prepared_get.headers)
205+
206+
def test_malformed_bearer_token(self):
207+
"""Verify that an exception is thrown when a malformed JWT bearer token is provided"""
208+
209+
bearer_token = "Bearer myBigBadBearerToken"
210+
211+
with tempfile.NamedTemporaryFile() as config_file, tempfile.NamedTemporaryFile() as key_file:
212+
with open(config_file.name, "w") as f:
213+
json.dump(
214+
{
215+
"email": "[email protected]",
216+
"token": bearer_token,
217+
"organization_id": "transcriptic",
218+
"api_root": "http://foo:5555",
219+
"analytics": True,
220+
"user_id": "ufoo2",
221+
"feature_groups": [
222+
"can_submit_autoprotocol",
223+
"can_upload_packages",
224+
],
225+
},
226+
f,
227+
)
228+
229+
with self.assertRaisesRegexp(ValueError, "Malformed JWT Bearer Token"):
230+
transcriptic.config.Connection.from_file(config_file.name)

transcriptic/config.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from . import routes
1414
from .signing import StrateosSign
15+
from .util import is_valid_jwt_token
1516
from .version import __version__
1617

1718
try:
@@ -252,9 +253,12 @@ def token(self, value: str):
252253
self.update_headers(**{"Cookie": None})
253254

254255
if value is not None:
255-
is_bearer_token = value.startswith("Bearer")
256-
if is_bearer_token:
257-
self.update_headers(**{"Authorization": value})
256+
is_bearer_auth = value.startswith("Bearer")
257+
if is_bearer_auth:
258+
if is_valid_jwt_token(value):
259+
self.update_headers(**{"Authorization": value})
260+
else:
261+
raise ValueError("Malformed JWT Bearer Token")
258262
else:
259263
self.update_headers(**{"X-User-Token": value})
260264

transcriptic/util.py

+5
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,8 @@ def makedirs(name, mode=None, exist_ok=False):
9292

9393
mode = mode if mode is not None else 0o777
9494
makedirs(name, mode, exist_ok)
95+
96+
97+
def is_valid_jwt_token(token: str):
98+
regex = r"Bearer ([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_\-\+\/=]*)"
99+
return re.fullmatch(regex, token) is not None

0 commit comments

Comments
 (0)