Push Notification and Django problems (vapid_private_key)

Asked

Viewed 81 times

1

I was able to reproduce an example with the repository pywebpush to make the push notification. After breaking my head to understand, I was able to do it. However, when I tried to integrate with my Django project, it didn’t roll over. I believe it’s something to do with the file private_key.pem that I Gero.

Error:

...
File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/pywebpush/__init__.py", line 359, in webpush
    vv = Vapid.from_string(private_key=vapid_private_key)
  File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/py_vapid/__init__.py", line 142, in from_string
    return cls.from_der(pkey)
  File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/py_vapid/__init__.py", line 99, in from_der
    backend=default_backend())
  File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/cryptography/hazmat/primitives/serialization.py", line 28, in load_der_private_key
    return backend.load_der_private_key(data, password)
  File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/cryptography/hazmat/backends/multibackend.py", line 323, in load_der_private_key
    return b.load_der_private_key(data, password)
  File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1018, in load_der_private_key
    password,
  File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1160, in _load_key
    self._handle_key_loading_error()
  File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1229, in _handle_key_loading_error
    raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.

push py.

from pywebpush import webpush, WebPushException
from apps.user.models import User, PushNotification
from django.conf import settings
import os

def push_notification(message, user = None):
    if not user:
        all_users = PushNotification.objects.all()
        for au in all_users:
            try:
                webpush(
                    subscription_info={
                        "endpoint":au.endpoint,
                        "keys":{
                            "p256dh":au.p256dh,
                            "auth":au.auth
                        }},
                    data=message,
                    vapid_private_key=os.path.join(settings.PRIVATEKEY_DIR, 'private_key.pem'),
                    vapid_claims={
                        "sub": "mailto:[email protected]",
                    }
                )
            except WebPushException as ex:
                print("I'm sorry, Dave, but I can't do that: {}", repr(ex))
    else:
        _user = PushNotification.objects.filter(user = user)
        if _user.exists():
            _user = _user[0]
            subinfo = {
                "endpoint":_user.endpoint,
                "keys":{
                    "p256dh":_user.p256dh,
                    "auth":_user.auth
                }
            }
            print(subinfo)
            try:
                webpush(
                    subscription_info=subinfo,
                    data=message,
                    vapid_private_key=os.path.join(settings.PRIVATEKEY_DIR, 'private_key.pem'),
                    vapid_claims={
                        "sub": "mailto:[email protected]",
                    }
                )
            except WebPushException as ex:
                print("I'm sorry, Dave, but I can't do that: {}", repr(ex))

I am generating the files according to the repository they indicate: vapid

The problem is that I was able to make it work only with Python. Now, calling this same Django python, it did not roll.. I print the dictionary it generates to check if there are any errors but no, it doesn’t. It’s the same.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.