Skip to content

Commit 32e3fd9

Browse files
authored
bug: Update vapid exp value if it's expired. (#113)
Closes #100
1 parent 2a23f45 commit 32e3fd9

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

README.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,37 @@ Encode the ``data`` for future use. On error, returns a
187187
188188
encoded_data = WebPush(subscription_info).encode(data)
189189
190+
Stand Alone Webpush
191+
-------------------
192+
193+
If you're not really into coding your own solution, there's also a
194+
"stand-alone" ``pywebpush`` command in the ./bin directory.
195+
196+
This uses two files: \* the *data* file, which contains the message to
197+
send, in whatever form you like. \* the *subscription info* file, which
198+
contains the subscription information as JSON encoded data. This is
199+
usually returned by the Push ``subscribe`` method and looks something
200+
like:
201+
202+
.. code:: json
203+
204+
{"endpoint": "https://push...",
205+
"keys": {
206+
"auth": "ab01...",
207+
"p256dh": "aa02..."
208+
}}
209+
210+
If you're interested in just testing your applications WebPush
211+
interface, you could use the Command Line:
212+
213+
.. code:: bash
214+
215+
./bin/pywebpush --data stuff_to_send.data --info subscription.info
216+
217+
which will encrypt and send the contents of ``stuff_to_send.data``.
218+
219+
See ``./bin/pywebpush --help`` for available commands and options.
220+
190221
.. |Build Status| image:: https://travis-ci.org/web-push-libs/pywebpush.svg?branch=master
191222
:target: https://travis-ci.org/web-push-libs/pywebpush
192223
.. |Requirements Status| image:: https://requires.io/github/web-push-libs/pywebpush/requirements.svg?branch=master

pywebpush/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,10 @@ def webpush(subscription_info,
405405
url = urlparse(subscription_info.get('endpoint'))
406406
aud = "{}://{}".format(url.scheme, url.netloc)
407407
vapid_claims['aud'] = aud
408-
if not vapid_claims.get('exp'):
408+
# Remember, passed structures are mutable in python.
409+
# It's possible that a previously set `exp` field is no longer valid.
410+
if (not vapid_claims.get('exp')
411+
or vapid_claims.get('exp') < int(time.time())):
409412
# encryption lives for 12 hours
410413
vapid_claims['exp'] = int(time.time()) + (12 * 60 * 60)
411414
if not vapid_private_key:

pywebpush/tests/test_webpush.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import os
44
import unittest
5+
import time
56

67
from mock import patch, Mock
78
from nose.tools import eq_, ok_, assert_is_not, assert_raises
@@ -191,6 +192,26 @@ def test_webpush_vapid_instance(self, vapid_sign, pusher_send):
191192
vapid_sign.assert_called_once_with(claims)
192193
pusher_send.assert_called_once()
193194

195+
@patch.object(WebPusher, "send")
196+
@patch.object(py_vapid.Vapid, "sign")
197+
def test_webpush_vapid_exp(self, vapid_sign, pusher_send):
198+
pusher_send.return_value.status_code = 200
199+
subscription_info = self._gen_subscription_info()
200+
data = "Mary had a little lamb"
201+
vapid_key = py_vapid.Vapid.from_string(self.vapid_key)
202+
claims = dict(sub="mailto:[email protected]",
203+
aud="https://example.com",
204+
exp=int(time.time() - 48600))
205+
webpush(
206+
subscription_info=subscription_info,
207+
data=data,
208+
vapid_private_key=vapid_key,
209+
vapid_claims=claims,
210+
)
211+
vapid_sign.assert_called_once_with(claims)
212+
pusher_send.assert_called_once()
213+
ok_(claims['exp'] > int(time.time()))
214+
194215
@patch("requests.post")
195216
def test_send_bad_vapid_no_key(self, mock_post):
196217
mock_post.return_value.status_code = 200

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from setuptools import find_packages, setup
55

66

7-
__version__ = "1.9.3"
7+
__version__ = "1.9.4"
88

99

1010
def read_from(file):

0 commit comments

Comments
 (0)