Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
bug: Trap for missing Vapid AUTH header token
Browse files Browse the repository at this point in the history
* Fix some flake8 spacing

closes #902
  • Loading branch information
jrconlin committed May 23, 2017
1 parent e6081e8 commit 2fccec6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
42 changes: 21 additions & 21 deletions autopush/tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,10 @@ def test_no_auth(self):
@inlineCallbacks
def test_bad_body(self):
resp = yield self.client.post(
self.url(router_type="webpush",
uaid=dummy_uaid.hex,
chid=str(dummy_chid)),
body="{invalid"
self.url(router_type="webpush",
uaid=dummy_uaid.hex,
chid=str(dummy_chid)),
body="{invalid"
)
self._check_error(resp, 401, 108, "Unauthorized")

Expand Down Expand Up @@ -534,9 +534,9 @@ def test_put(self, *args):

uri = self.url(router_type='test', uaid=dummy_uaid.hex)
resp = yield self.client.put(
uri,
headers={"Authorization": self.auth},
body=json.dumps(data),
uri,
headers={"Authorization": self.auth},
body=json.dumps(data),
)
payload = json.loads(resp.content)
eq_(payload, {})
Expand All @@ -556,8 +556,8 @@ def test_put_bad_auth(self, *args):
self.patch('uuid.uuid4', return_value=dummy_uaid)

resp = yield self.client.put(
self.url(router_type="test", uaid=dummy_uaid.hex),
headers={"Authorization": "Fred Smith"}
self.url(router_type="test", uaid=dummy_uaid.hex),
headers={"Authorization": "Fred Smith"}
)
self._check_error(resp, 401, 109, "Unauthorized")

Expand All @@ -566,12 +566,12 @@ def test_put_bad_arguments(self, *args):
self.patch('uuid.uuid4', return_value=dummy_chid)

resp = yield self.client.put(
self.url(router_type='foo', uaid=dummy_uaid.hex),
headers={"Authorization": self.auth},
body=json.dumps(dict(
type="test",
data=dict(token="some_token"),
))
self.url(router_type='foo', uaid=dummy_uaid.hex),
headers={"Authorization": self.auth},
body=json.dumps(dict(
type="test",
data=dict(token="some_token"),
))
)
self._check_error(resp, 400, 108, "Bad Request")

Expand Down Expand Up @@ -693,10 +693,10 @@ def test_get(self):
self.settings.message.all_channels = Mock()
self.settings.message.all_channels.return_value = (True, chids)
resp = yield self.client.get(
self.url(router_type="test",
router_token="test",
uaid=dummy_uaid.hex),
headers={"Authorization": self.auth}
self.url(router_type="test",
router_token="test",
uaid=dummy_uaid.hex),
headers={"Authorization": self.auth}
)
self.settings.message.all_channels.assert_called_with(str(dummy_uaid))
payload = json.loads(resp.content)
Expand All @@ -706,7 +706,7 @@ def test_get(self):
@inlineCallbacks
def test_get_no_uaid(self):
resp = yield self.client.get(
self.url(router_type="test", router_token="test"),
headers={"Authorization": self.auth}
self.url(router_type="test", router_token="test"),
headers={"Authorization": self.auth}
)
eq_(resp.get_status(), 410)
12 changes: 12 additions & 0 deletions autopush/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,18 @@ def test_basic_delivery_with_invalid_vapid_exp(self):
status=401)
yield self.shut_down(client)

@inlineCallbacks
def test_basic_delivery_with_invalid_vapid_auth(self):
data = str(uuid.uuid4())
client = yield self.quick_register(use_webpush=True)
vapid_info = _get_vapid()
vapid_info['auth'] = ""
yield client.send_notification(
data=data,
vapid=vapid_info,
status=401)
yield self.shut_down(client)

@inlineCallbacks
def test_basic_delivery_with_invalid_signature(self):
data = str(uuid.uuid4())
Expand Down
2 changes: 2 additions & 0 deletions autopush/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,8 @@ def websocket_format(self):
def parse_auth_header(header):
vapid_auth = {}
scheme_bits = header.split(' ', 1)
if len(scheme_bits) < 2:
raise VapidAuthException("Missing Auth Token")
scheme = scheme_bits[0].lower()
if scheme not in AUTH_SCHEMES:
return vapid_auth
Expand Down
3 changes: 2 additions & 1 deletion autopush/web/webpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ def validate_auth(self, d):
else:
public_key = d["subscription"].get("public_key")
jwt = extract_jwt(token, public_key)
except (KeyError, ValueError, InvalidSignature, TypeError):
except (KeyError, ValueError, InvalidSignature, TypeError,
VapidAuthException):
raise InvalidRequest("Invalid Authorization Header",
status_code=401, errno=109,
headers={"www-authenticate": PREF_SCHEME})
Expand Down

0 comments on commit 2fccec6

Please sign in to comment.