Skip to content

Commit c638bb2

Browse files
author
dzleidig
committed
Do not overload x-user-token header. Create separate parameter for beaer token.
1 parent b2385cb commit c638bb2

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

test/config_test.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,28 @@ 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 eyJraWQiOiJWcmVsOE9zZ0JXaUpHeEpMeFJ4bE1UaVwvbjgyc1hwWktUaTd2UExUNFQ0TT0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJoMTBlM2hwajliNjc4bXMwOG8zbGlibHQ2IiwidG9rZW5fdXNlIjoiYWNjZXNzIiwic2NvcGUiOiJ3ZWJcL2dldCB3ZWJcL3Bvc3QiLCJhdXRoX3RpbWUiOjE1OTM3MjM1NDgsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC51cy1lYXN0LTEuYW1hem9uYXdzLmNvbVwvdXMtZWFzdC0xX1d6aEZzTGlPRyIsImV4cCI6MTU5MzcyNzE0OCwiaWF0IjoxNTkzNzIzNTQ4LCJ2ZXJzaW9uIjoyLCJqdGkiOiI4Njk5ZDEwYy05Mjg4LTQ0YmEtODIxNi01OTJjZGU3MDBhY2MiLCJjbGllbnRfaWQiOiJoMTBlM2hwajliNjc4bXMwOG8zbGlibHQ2In0.YA_yiD-x6UuBMShprUbUKuB_DO6ogCtd5srfgpJA6Ve_qsf8n19nVMmFsZBy3GxzN92P1ZXiFY99FfNPohhQtaRRhpeUkir08hgJN2bEHCJ5Ym8r9mr9mlwSG6FoiedgLaUVGwJujD9c2rcA83NEo8ayTyfCynF2AZ2pMxLHvqOYtvscGMiMzIwlZfJV301iKUVgPODJM5lpJ4iKCpOy2ByCl2_KL1uxIxgMkglpB-i7kgJc-WmYoJFoN88D89ugnEoAxNfK14N4_RyEkrLNGape9kew79nUeR6fWbVFLiGDDu25_9z-7VB-GGGk7L_Hb7YgVJ5W2FwESnkDvV1T4Q"
177+
bearer_token = (
178+
"Bearer eyJraWQiOiJWcmVsOE9zZ0JXaUpHeEpMeFJ4bE1UaVwvbjgyc1hwWktUaTd2UExUNFQ0T"
179+
"T0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJoMTBlM2hwajliNjc4bXMwOG8zbGlibHQ2IiwidG9r"
180+
"ZW5fdXNlIjoiYWNjZXNzIiwic2NvcGUiOiJ3ZWJcL2dldCB3ZWJcL3Bvc3QiLCJhdXRoX3RpbWUi"
181+
"OjE1OTM3MjM1NDgsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC51cy1lYXN0LTEuYW1hem9u"
182+
"YXdzLmNvbVwvdXMtZWFzdC0xX1d6aEZzTGlPRyIsImV4cCI6MTU5MzcyNzE0OCwiaWF0IjoxNTkz"
183+
"NzIzNTQ4LCJ2ZXJzaW9uIjoyLCJqdGkiOiI4Njk5ZDEwYy05Mjg4LTQ0YmEtODIxNi01OTJjZGU3"
184+
"MDBhY2MiLCJjbGllbnRfaWQiOiJoMTBlM2hwajliNjc4bXMwOG8zbGlibHQ2In0.YA_yiD-x6UuB"
185+
"MShprUbUKuB_DO6ogCtd5srfgpJA6Ve_qsf8n19nVMmFsZBy3GxzN92P1ZXiFY99FfNPohhQtaRR"
186+
"hpeUkir08hgJN2bEHCJ5Ym8r9mr9mlwSG6FoiedgLaUVGwJujD9c2rcA83NEo8ayTyfCynF2AZ2p"
187+
"MxLHvqOYtvscGMiMzIwlZfJV301iKUVgPODJM5lpJ4iKCpOy2ByCl2_KL1uxIxgMkglpB-i7kgJc"
188+
"-WmYoJFoN88D89ugnEoAxNfK14N4_RyEkrLNGape9kew79nUeR6fWbVFLiGDDu25_9z-7VB-GGGk"
189+
"7L_Hb7YgVJ5W2FwESnkDvV1T4Q"
190+
)
178191

179192
with tempfile.NamedTemporaryFile() as config_file, tempfile.NamedTemporaryFile() as key_file:
180193
with open(config_file.name, "w") as f:
181194
json.dump(
182195
{
183196
"email": "[email protected]",
184-
"token": bearer_token,
197+
"token": "foobarinvalid",
198+
"bearer_token": bearer_token,
185199
"organization_id": "transcriptic",
186200
"api_root": "http://foo:5555",
187201
"analytics": True,
@@ -201,7 +215,6 @@ def test_bearer_token(self):
201215

202216
authorization_header_value = prepared_get.headers["authorization"]
203217
self.assertEqual(authorization_header_value, bearer_token)
204-
self.assertNotIn("x-user-token", prepared_get.headers)
205218

206219
def test_malformed_bearer_token(self):
207220
"""Verify that an exception is thrown when a malformed JWT bearer token is provided"""
@@ -213,7 +226,8 @@ def test_malformed_bearer_token(self):
213226
json.dump(
214227
{
215228
"email": "[email protected]",
216-
"token": bearer_token,
229+
"token": "foobarinvalid",
230+
"bearer_token": bearer_token,
217231
"organization_id": "transcriptic",
218232
"api_root": "http://foo:5555",
219233
"analytics": True,

transcriptic/config.py

+28-16
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def __init__(
112112
feature_groups=[],
113113
rsa_key=None,
114114
session=None,
115+
bearer_token=None,
115116
):
116117
# Initialize environment args used for computing routes
117118
self.env_args = dict()
@@ -132,7 +133,7 @@ def __init__(
132133

133134
# NB: These many setattr calls update self.session.headers
134135
# cookie authentication is mutually exclusive from token authentication
135-
if cookie is not None:
136+
if cookie:
136137
if email is not None or token is not None:
137138
warnings.warn(
138139
"Cookie and token authentication is mutually "
@@ -143,9 +144,17 @@ def __init__(
143144
self.cookie = cookie
144145
self.update_session_auth(use_signature=False)
145146
else:
147+
if cookie is not None:
148+
warnings.warn(
149+
"Cookie and token authentication is mutually "
150+
"exclusive. Ignoring cookie"
151+
)
146152
self.session.headers["Cookie"] = None
147153
self.email = email
148-
self.token = token
154+
if token is not None:
155+
self.token = token
156+
if bearer_token is not None:
157+
self.bearer_token = bearer_token
149158
self.update_session_auth()
150159

151160
# Initialize feature groups
@@ -236,6 +245,20 @@ def email(self, value):
236245
self.update_headers(**{"X-User-Email": value})
237246
self.update_session_auth()
238247

248+
@property
249+
def bearer_token(self):
250+
try:
251+
return self.session.headers["Authorization"]
252+
except (NameError, KeyError):
253+
raise ValueError("Bearer token is not set.")
254+
255+
@bearer_token.setter
256+
def bearer_token(self, value):
257+
if is_valid_jwt_token(value):
258+
self.update_headers(**{"Authorization": value})
259+
else:
260+
raise ValueError("Malformed JWT Bearer Token")
261+
239262
@property
240263
def token(self):
241264
try:
@@ -244,23 +267,14 @@ def token(self):
244267
raise ValueError("token is not set.")
245268

246269
@token.setter
247-
def token(self, value: str):
270+
def token(self, value):
248271
if self.cookie is not None:
249272
warnings.warn(
250273
"Cookie and token authentication is mutually "
251274
"exclusive. Clearing cookie from headers"
252275
)
253276
self.update_headers(**{"Cookie": None})
254-
255-
if value is not None:
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")
262-
else:
263-
self.update_headers(**{"X-User-Token": value})
277+
self.update_headers(**{"X-User-Token": value})
264278

265279
@property
266280
def cookie(self):
@@ -1088,9 +1102,7 @@ def get_route(self, method, **kwargs):
10881102
raise Exception(
10891103
f"For route: {method}, argument {arg} needs " f"to be provided."
10901104
)
1091-
return route_method( # pylint: disable=no-value-for-parameter
1092-
*tuple(input_args)
1093-
)
1105+
return route_method(*tuple(input_args))
10941106

10951107
def get(self, route, **kwargs):
10961108
return self._call("get", route, **kwargs)

0 commit comments

Comments
 (0)