From 88b1f03741bc776ce750b2f2bece3985f7825f2b Mon Sep 17 00:00:00 2001 From: Kit Cambridge Date: Fri, 15 Apr 2016 16:30:50 -0700 Subject: [PATCH] feat: Support app server keys via the HTTP interface. Closes #423. --- autopush/endpoint.py | 4 +++- autopush/tests/test_endpoint.py | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/autopush/endpoint.py b/autopush/endpoint.py index 093a4dd7..e76f4a42 100644 --- a/autopush/endpoint.py +++ b/autopush/endpoint.py @@ -616,6 +616,7 @@ def post(self, router_type="", router_token="", uaid="", chid=""): # Should this be different than websocket? self.uaid = uaid self.chid = params["channelID"] + self.app_server_key = params.get("key") if new_uaid: d = Deferred() d.addCallback(router.register, params, router_token) @@ -681,7 +682,8 @@ def _delete_uaid(self, uaid, router): def _register_channel(self, router_data=None): self.ap_settings.message.register_channel(self.uaid, self.chid) - endpoint = self.ap_settings.make_endpoint(self.uaid, self.chid) + endpoint = self.ap_settings.make_endpoint(self.uaid, self.chid, + self.app_server_key) return endpoint, router_data @cyclone.web.asynchronous diff --git a/autopush/tests/test_endpoint.py b/autopush/tests/test_endpoint.py index cb4cbeef..51b39e16 100644 --- a/autopush/tests/test_endpoint.py +++ b/autopush/tests/test_endpoint.py @@ -1615,6 +1615,46 @@ def handle_finish(value): self.reg.post(router_type="simplepush", uaid=dummy_uaid) return self.finish_deferred + @patch('uuid.uuid4', return_value=uuid.UUID(dummy_chid)) + def test_post_with_app_server_key(self, *args): + dummy_key = "RandomKeyString" + self.reg.request.body = json.dumps(dict( + type="simplepush", + key=utils.base64url_encode(dummy_key), + data={}, + )) + + def mock_encrypt(cleartext): + eq_(len(cleartext), 64) + # dummy_uaid + eq_(cleartext[0:16], + 'abad1dea00000000aabbccdd00000000'.decode('hex')) + # dummy_chid + eq_(cleartext[16:32], + 'deadbeef00000000decafbad00000000'.decode('hex')) + # sha256(dummy_key).digest() + eq_(cleartext[32:], + ('47aedd050b9e19171f0fa7b8b65ca670' + '28f0bc92cd3f2cd3682b1200ec759007').decode('hex')) + return 'abcd123' + self.fernet_mock.configure_mock(**{ + 'encrypt.side_effect': mock_encrypt, + }) + self.reg.request.headers["Authorization"] = self.auth + + def handle_finish(value): + call_args = self.reg.write.call_args + ok_(call_args is not None) + args = call_args[0] + call_arg = json.loads(args[0]) + eq_(call_arg["channelID"], dummy_chid) + eq_(call_arg["endpoint"], "http://localhost/push/v2/abcd123") + + self.finish_deferred.addCallback(handle_finish) + self.reg.request.headers["Authorization"] = self.auth + self.reg.post(router_type="simplepush", uaid=dummy_uaid) + return self.finish_deferred + @patch('uuid.uuid4', return_value=uuid.UUID(dummy_chid)) def test_put(self, *args): data = dict(token="some_token")