Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Django command to generate/export a Vapid keypair #144

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: Django command to generate/export a Vapid keypair
  • Loading branch information
David-Guillot committed Dec 12, 2024
commit feb17e8071e2d8449fd1498d2d0caab300a764df
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ WEBPUSH_SETTINGS = {
```
**Replace ``"Vapid Public Key"`` and ``"Vapid Private Key"`` with your Vapid Keys. Also replace ``[email protected]`` with your email so that the push server of browser can reach to you if anything goes wrong.**

> **To know how to obtain Vapid Keys please see this [`py_vapid`](https://github.com/web-push-libs/vapid/tree/master/python) and [Google Developer Documentation](https://developers.google.com/web/fundamentals/push-notifications/subscribing-a-user#how_to_create_application_server_keys). You can obtain one easily from [web-push-codelab.glitch.me](https://web-push-codelab.glitch.me/). ``Application Server Keys`` and ``Vapid Keys`` both are same.**
**Generate a Vapid key pair**

```shell
python manage.py webpush_generate_vapid_keypair
```

Then include `webpush` in the `urls.py`

Expand Down
Empty file added webpush/management/__init__.py
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions webpush/management/commands/webpush_generate_vapid_keypair.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.core.management.base import BaseCommand

from ...vapid import get_vapid_keypair


class GenerateValidKeys(BaseCommand):
def handle(self, *args, **options):
pubkey, privkey = get_vapid_keypair()
self.stdout.write(f"Public key: {pubkey}")
self.stdout.write(f"Private key: {privkey}")
3 changes: 0 additions & 3 deletions webpush/tests.py

This file was deleted.

Empty file added webpush/tests/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions webpush/tests/test_vapid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest

from cryptography.hazmat.primitives import serialization
from py_vapid import Vapid, b64urlencode
from webpush.vapid import get_vapid_keypair


class TestGetVapidKeypair(unittest.TestCase):
def test_keypair_is_consistent(self):
# GIVEN a generated pair of public key and private key
pubkey, privkey = get_vapid_keypair()

# WHEN instantiating a new Vapid keypair from the private key
newkey = Vapid.from_raw(privkey.encode())

# THEN the public key export is the same as the one generated
self.assertEqual(
b64urlencode(
newkey.public_key.public_bytes(
serialization.Encoding.X962,
serialization.PublicFormat.UncompressedPoint,
)
),
pubkey,
)
16 changes: 16 additions & 0 deletions webpush/vapid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from cryptography.hazmat.primitives import serialization
from py_vapid import Vapid
from py_vapid.utils import num_to_bytes, b64urlencode


def get_vapid_keypair() -> tuple[str, str]:
vapid = Vapid()
vapid.generate_keys()

pubkey_export = vapid.public_key.public_bytes(
serialization.Encoding.X962,
serialization.PublicFormat.UncompressedPoint,
)
privkey_export = num_to_bytes(vapid.private_key.private_numbers().private_value, 32)

return b64urlencode(pubkey_export), b64urlencode(privkey_export)