Skip to content

Commit ebf6598

Browse files
committed
feat: Move to RFC 8188 "aes128gcm" content encoding default
Closes #97
1 parent aa66fde commit ebf6598

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

pywebpush/__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class WebPusher:
112112
valid_encodings = [
113113
# "aesgcm128", # this is draft-0, but DO NOT USE.
114114
"aesgcm", # draft-httpbis-encryption-encoding-01
115-
"aes128gcm" # draft-httpbis-encryption-encoding-04
115+
"aes128gcm" # RFC8188 Standard encoding
116116
]
117117

118118
def __init__(self, subscription_info, requests_session=None):
@@ -157,17 +157,16 @@ def _repad(self, data):
157157
"""Add base64 padding to the end of a string, if required"""
158158
return data + b"===="[:len(data) % 4]
159159

160-
def encode(self, data, content_encoding="aesgcm"):
160+
def encode(self, data, content_encoding="aes128gcm"):
161161
"""Encrypt the data.
162162
163163
:param data: A serialized block of byte data (String, JSON, bit array,
164164
etc.) Make sure that whatever you send, your client knows how
165165
to understand it.
166166
:type data: str
167167
:param content_encoding: The content_encoding type to use to encrypt
168-
the data. Defaults to draft-01 "aesgcm". Latest draft-04 is
169-
"aes128gcm", however not all clients may be able to use this
170-
format.
168+
the data. Defaults to RFC8188 "aes128gcm". The previous draft-01 is
169+
"aesgcm", however this format is now deprecated.
171170
:type content_encoding: enum("aesgcm", "aes128gcm")
172171
173172
"""
@@ -241,7 +240,7 @@ def as_curl(self, endpoint, encoded_data, headers):
241240
url=endpoint, headers="".join(header_list), data=data))
242241

243242
def send(self, data=None, headers=None, ttl=0, gcm_key=None, reg_id=None,
244-
content_encoding="aesgcm", curl=False, timeout=None):
243+
content_encoding="aes128gcm", curl=False, timeout=None):
245244
"""Encode and send the data to the Push Service.
246245
247246
:param data: A serialized block of data (see encode() ).
@@ -258,7 +257,7 @@ def send(self, data=None, headers=None, ttl=0, gcm_key=None, reg_id=None,
258257
:param reg_id: registration id of the recipient. If not provided,
259258
it will be extracted from the endpoint.
260259
:type reg_id: str
261-
:param content_encoding: ECE content encoding (defaults to "aesgcm")
260+
:param content_encoding: ECE content encoding (defaults to "aes128gcm")
262261
:type content_encoding: str
263262
:param curl: Display output as `curl` command instead of sending
264263
:type curl: bool
@@ -284,8 +283,11 @@ def send(self, data=None, headers=None, ttl=0, gcm_key=None, reg_id=None,
284283
headers.update({
285284
'crypto-key': crypto_key,
286285
'content-encoding': content_encoding,
287-
'encryption': "salt=" + encoded['salt'].decode('utf8'),
288286
})
287+
if encoded.get('salt'):
288+
headers.update({
289+
'encryption': "salt=" + encoded['salt'].decode('utf8')
290+
})
289291
if gcm_key:
290292
endpoint = 'https://android.googleapis.com/gcm/send'
291293
reg_ids = []
@@ -324,7 +326,7 @@ def webpush(subscription_info,
324326
data=None,
325327
vapid_private_key=None,
326328
vapid_claims=None,
327-
content_encoding="aesgcm",
329+
content_encoding="aes128gcm",
328330
curl=False,
329331
timeout=None,
330332
ttl=0):

pywebpush/tests/test_webpush.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,10 @@ def test_send(self, mock_post):
134134
eq_(subscription_info.get('endpoint'), mock_post.call_args[0][0])
135135
pheaders = mock_post.call_args[1].get('headers')
136136
eq_(pheaders.get('ttl'), '0')
137-
ok_('encryption' in pheaders)
138137
eq_(pheaders.get('AUTHENTICATION'), headers.get('Authentication'))
139138
ckey = pheaders.get('crypto-key')
140139
ok_('pre-existing' in ckey)
141-
eq_(pheaders.get('content-encoding'), 'aesgcm')
140+
eq_(pheaders.get('content-encoding'), 'aes128gcm')
142141

143142
@patch("requests.post")
144143
def test_send_vapid(self, mock_post):
@@ -164,12 +163,11 @@ def repad(str):
164163
).decode('utf8')
165164
)
166165
ok_(subscription_info.get('endpoint').startswith(auth['aud']))
167-
ok_('encryption' in pheaders)
168166
ok_('WebPush' in pheaders.get('authorization'))
169167
ckey = pheaders.get('crypto-key')
170168
ok_('p256ecdsa=' in ckey)
171169
ok_('dh=' in ckey)
172-
eq_(pheaders.get('content-encoding'), 'aesgcm')
170+
eq_(pheaders.get('content-encoding'), 'aes128gcm')
173171

174172
@patch.object(WebPusher, "send")
175173
@patch.object(py_vapid.Vapid, "sign")
@@ -262,8 +260,7 @@ def test_send_no_headers(self, mock_post):
262260
eq_(subscription_info.get('endpoint'), mock_post.call_args[0][0])
263261
pheaders = mock_post.call_args[1].get('headers')
264262
eq_(pheaders.get('ttl'), '0')
265-
ok_('encryption' in pheaders)
266-
eq_(pheaders.get('content-encoding'), 'aesgcm')
263+
eq_(pheaders.get('content-encoding'), 'aes128gcm')
267264

268265
@patch("pywebpush.open")
269266
def test_as_curl(self, opener):
@@ -281,9 +278,8 @@ def test_as_curl(self, opener):
281278
for s in [
282279
"curl -vX POST https://example.com",
283280
"-H \"crypto-key: p256ecdsa=",
284-
"-H \"content-encoding: aesgcm\"",
281+
"-H \"content-encoding: aes128gcm\"",
285282
"-H \"authorization: WebPush ",
286-
"-H \"encryption: salt=",
287283
"-H \"ttl: 0\"",
288284
"-H \"content-length:"
289285
]:
@@ -334,11 +330,10 @@ def test_send_using_requests_session(self, mock_session):
334330
mock_session.post.call_args[0][0])
335331
pheaders = mock_session.post.call_args[1].get('headers')
336332
eq_(pheaders.get('ttl'), '0')
337-
ok_('encryption' in pheaders)
338333
eq_(pheaders.get('AUTHENTICATION'), headers.get('Authentication'))
339334
ckey = pheaders.get('crypto-key')
340335
ok_('pre-existing' in ckey)
341-
eq_(pheaders.get('content-encoding'), 'aesgcm')
336+
eq_(pheaders.get('content-encoding'), 'aes128gcm')
342337

343338

344339
class WebpushExceptionTestCase(unittest.TestCase):

setup.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33

44
from setuptools import find_packages, setup
55

6-
__version__ = "1.7.0"
6+
7+
__version__ = "1.8.0"
78

89

910
def read_from(file):
1011
reply = []
1112
with io.open(os.path.join(here, file), encoding='utf8') as f:
12-
for l in f:
13-
l = l.strip()
14-
if not l:
13+
for line in f:
14+
line = line.strip()
15+
if not line:
1516
break
16-
if l[:2] == '-r':
17-
reply += read_from(l.split(' ')[1])
17+
if line[:2] == '-r':
18+
reply += read_from(line.split(' ')[1])
1819
continue
19-
if l[0] != '#' or l[:2] != '//':
20+
if line[0] != '#' or line[:2] != '//':
2021
reply.append(l)
2122
return reply
2223

0 commit comments

Comments
 (0)