Skip to content

Commit c24920b

Browse files
authored
Merge pull request #98 from web-push-libs/feat/97
feat: Move to RFC 8188 "aes128gcm" content encoding default
2 parents 14331e4 + 24d746b commit c24920b

File tree

5 files changed

+39
-40
lines changed

5 files changed

+39
-40
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ in the `subscription_info` block.
6565
*data* - can be any serial content (string, bit array, serialized JSON, etc), but be sure that your receiving
6666
application is able to parse and understand it. (e.g. `data = "Mary had a little lamb."`)
6767

68-
*content_type* - specifies the form of Encryption to use, either `'aesgcm'` or the newer `'aes128gcm'`. NOTE that
69-
not all User Agents can decrypt `'aes128gcm'`, so the library defaults to the older form.
68+
*content_type* - specifies the form of Encryption to use, either `'aes128gcm'` or the deprecated `'aesgcm'`. NOTE that
69+
not all User Agents can decrypt `'aesgcm'`, so the library defaults to the RFC 8188 standard form.
7070

7171
*vapid_claims* - a `dict` containing the VAPID claims required for authorization (See
7272
[py_vapid](https://github.com/web-push-libs/vapid/tree/master/python) for more details). If `aud` is not specified,
@@ -120,7 +120,7 @@ can pass just `wp = WebPusher(subscription_info)`. This will return a `WebPusher
120120

121121
The following methods are available:
122122

123-
#### `.send(data, headers={}, ttl=0, gcm_key="", reg_id="", content_encoding="aesgcm", curl=False, timeout=None)`
123+
#### `.send(data, headers={}, ttl=0, gcm_key="", reg_id="", content_encoding="aes128gcm", curl=False, timeout=None)`
124124

125125
Send the data using additional parameters. On error, returns a `WebPushException`
126126

@@ -137,7 +137,7 @@ Developer Console.
137137

138138
*reg_id* Google Cloud Messaging registration ID (will be extracted from endpoint if not specified)
139139

140-
*content_encoding* ECE content encoding type (defaults to "aesgcm")
140+
*content_encoding* ECE content encoding type (defaults to "aes128gcm")
141141

142142
*curl* Do not execute the POST, but return as a `curl` command. This will write the encrypted content to a local file
143143
named `encrpypted.data`. This command is meant to be used for debugging purposes.
@@ -153,15 +153,15 @@ to send from Chrome using the old GCM mode:
153153
WebPusher(subscription_info).send(data, headers, ttl, gcm_key)
154154
```
155155

156-
#### `.encode(data, content_encoding="aesgcm")`
156+
#### `.encode(data, content_encoding="aes128gcm")`
157157

158158
Encode the `data` for future use. On error, returns a `WebPushException`
159159

160160
**Parameters**
161161

162162
*data* Binary string of data to send
163163

164-
*content_encoding* ECE content encoding type (defaults to "aesgcm")
164+
*content_encoding* ECE content encoding type (defaults to "aes128gcm")
165165

166166
**Example**
167167

README.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ etc), but be sure that your receiving application is able to parse and
6666
understand it. (e.g. ``data = "Mary had a little lamb."``)
6767

6868
*content\_type* - specifies the form of Encryption to use, either
69-
``'aesgcm'`` or the newer ``'aes128gcm'``. NOTE that not all User Agents
70-
can decrypt ``'aes128gcm'``, so the library defaults to the older form.
69+
``'aes128gcm'`` or the deprecated ``'aesgcm'``. NOTE that not all User
70+
Agents can decrypt ``'aesgcm'``, so the library defaults to the RFC 8188
71+
standard form.
7172

7273
*vapid\_claims* - a ``dict`` containing the VAPID claims required for
7374
authorization (See
@@ -129,8 +130,8 @@ object.
129130

130131
The following methods are available:
131132

132-
``.send(data, headers={}, ttl=0, gcm_key="", reg_id="", content_encoding="aesgcm", curl=False, timeout=None)``
133-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
133+
``.send(data, headers={}, ttl=0, gcm_key="", reg_id="", content_encoding="aes128gcm", curl=False, timeout=None)``
134+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
134135

135136
Send the data using additional parameters. On error, returns a
136137
``WebPushException``
@@ -150,7 +151,7 @@ system) This is the API key obtained from the Google Developer Console.
150151
*reg\_id* Google Cloud Messaging registration ID (will be extracted from
151152
endpoint if not specified)
152153

153-
*content\_encoding* ECE content encoding type (defaults to "aesgcm")
154+
*content\_encoding* ECE content encoding type (defaults to "aes128gcm")
154155

155156
*curl* Do not execute the POST, but return as a ``curl`` command. This
156157
will write the encrypted content to a local file named
@@ -168,8 +169,8 @@ to send from Chrome using the old GCM mode:
168169
169170
WebPusher(subscription_info).send(data, headers, ttl, gcm_key)
170171
171-
``.encode(data, content_encoding="aesgcm")``
172-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
172+
``.encode(data, content_encoding="aes128gcm")``
173+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
173174

174175
Encode the ``data`` for future use. On error, returns a
175176
``WebPushException``
@@ -178,7 +179,7 @@ Encode the ``data`` for future use. On error, returns a
178179

179180
*data* Binary string of data to send
180181

181-
*content\_encoding* ECE content encoding type (defaults to "aesgcm")
182+
*content\_encoding* ECE content encoding type (defaults to "aes128gcm")
182183

183184
**Example**
184185

pywebpush/__init__.py

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

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

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

244243
def send(self, data=None, headers=None, ttl=0, gcm_key=None, reg_id=None,
245-
content_encoding="aesgcm", curl=False, timeout=None):
244+
content_encoding="aes128gcm", curl=False, timeout=None):
246245
"""Encode and send the data to the Push Service.
247246
248247
:param data: A serialized block of data (see encode() ).
@@ -259,7 +258,7 @@ def send(self, data=None, headers=None, ttl=0, gcm_key=None, reg_id=None,
259258
:param reg_id: registration id of the recipient. If not provided,
260259
it will be extracted from the endpoint.
261260
:type reg_id: str
262-
:param content_encoding: ECE content encoding (defaults to "aesgcm")
261+
:param content_encoding: ECE content encoding (defaults to "aes128gcm")
263262
:type content_encoding: str
264263
:param curl: Display output as `curl` command instead of sending
265264
:type curl: bool
@@ -285,8 +284,11 @@ def send(self, data=None, headers=None, ttl=0, gcm_key=None, reg_id=None,
285284
headers.update({
286285
'crypto-key': crypto_key,
287286
'content-encoding': content_encoding,
288-
'encryption': "salt=" + encoded['salt'].decode('utf8'),
289287
})
288+
if encoded.get('salt'):
289+
headers.update({
290+
'encryption': "salt=" + encoded['salt'].decode('utf8')
291+
})
290292
if gcm_key:
291293
endpoint = 'https://android.googleapis.com/gcm/send'
292294
reg_ids = []
@@ -325,7 +327,7 @@ def webpush(subscription_info,
325327
data=None,
326328
vapid_private_key=None,
327329
vapid_claims=None,
328-
content_encoding="aesgcm",
330+
content_encoding="aes128gcm",
329331
curl=False,
330332
timeout=None,
331333
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)