Skip to content

Commit 0a07629

Browse files
authored
Add workaround for py-vapid cryptography deprecation warning (#793)
Update WebPush.rst to include alternative VAPID key generation method using ecdsa library directly, addressing compatibility issues with py-vapid and newer cryptography versions. Refs: #781
1 parent caaf4cf commit 0a07629

1 file changed

Lines changed: 52 additions & 9 deletions

File tree

docs/WebPush.rst

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,70 @@ These are in addition to the instalation steps for django-push-notifications[WP]
77

88
Configure the VAPID keys
99
------------------------------
10-
- Install:
10+
11+
.. note::
12+
There is currently a known issue with the ``py-vapid`` library causing deprecation warnings with newer versions of the ``cryptography`` library. While this issue is being resolved upstream (see `py-vapid issue #105 <https://github.com/web-push-libs/vapid/issues/105>`_), we recommend using the alternative method below.
13+
14+
**Recommended Method: Generate keys using standalone script**
15+
16+
This method uses the ``ecdsa`` library directly and avoids the ``py-vapid`` compatibility issue:
17+
18+
- Install the dependency:
19+
20+
.. code-block:: bash
21+
22+
pip install ecdsa
23+
24+
- Create and run this key generation script (shout-out to `@Tobiaqs <https://gist.github.com/Tobiaqs/450a4516ae44813792b7d84028c366c0>`_ for providing this script):
1125

1226
.. code-block:: python
1327
14-
pip install py-vapid (Only for generating key)
28+
# vapid_keygen.py
29+
import base64
30+
import ecdsa
31+
32+
def generate_vapid_keypair():
33+
"""
34+
Generate a new set of encoded key-pair for VAPID
35+
"""
36+
pk = ecdsa.SigningKey.generate(curve=ecdsa.NIST256p)
37+
vk = pk.get_verifying_key()
38+
39+
return {
40+
'private_key': base64.urlsafe_b64encode(pk.to_string()).strip(b"="),
41+
'public_key': base64.urlsafe_b64encode(b"\x04" + vk.to_string()).strip(b"=")
42+
}
43+
44+
keys = generate_vapid_keypair()
1545
16-
- Generate public and private keys:
46+
print("\nPrivate key (use for WP_PRIVATE_KEY setting):\n")
47+
print(keys["private_key"].decode())
48+
print("\nPublic key (use as Application Server Key in client JavaScript):\n")
49+
print(keys["public_key"].decode())
50+
print()
51+
52+
- Run the script:
53+
54+
.. code-block:: bash
55+
56+
python vapid_keygen.py
57+
58+
The private key output should be used with the setting ``WP_PRIVATE_KEY``.
59+
The public key will be used in your client side JavaScript as the Application Server Key (see example below).
60+
61+
**Method 2: Using py-vapid (once upstream fix is released)**
62+
63+
Once the upstream issue is resolved, you can use ``py-vapid`` as originally documented:
1764

1865
.. code-block:: bash
1966
67+
pip install py-vapid
2068
vapid --gen
2169
2270
Generating private_key.pem
2371
Generating public_key.pem
2472
25-
26-
The private key generated is the file to use with the setting ``WP_PRIVATE_KEY``
27-
The public key will be used in your client side javascript, but first it must be formated as an Application Server Key
28-
29-
- Generate client public key (applicationServerKey)
73+
Then format as Application Server Key:
3074

3175
.. code-block:: bash
3276
@@ -35,7 +79,6 @@ The public key will be used in your client side javascript, but first it must be
3579
Application Server Key = <Your Public Key>
3680
3781
38-
3982
Client Side logic to ask user for permission and subscribe to WebPush
4083
------------------------------
4184
The example subscribeUser function is best called in response to a user action, such as a button click. Some browsers will deny the request otherwise.

0 commit comments

Comments
 (0)