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

Error : Web push send from python backend to fire base using VAPI keys. Unhashable type:'slice' #145

Open
ishwara-bhat opened this issue Dec 17, 2021 · 7 comments

Comments

@ishwara-bhat
Copy link

I have trouble getting pywebpush work for my python backend on gcp.

VAPI PUBLIC KEY and PVT KEY obtained from firebase dash board thourhg manual creation

#subscriptionObject comes from Angular front end. It used the VAPI PUBLIC KEY to receive the object.
I used the package versions specified in requirement.txt.
The call happens from a Flask app route.

webpush(subscription_info = subscriptionObject, data=notificationMessageObject, vapid_private_key=VAPID_PRIVATE_KEY, vapid_claims={"sub": "mailto:[email protected]"})
Error
Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python3.7/site-packages/flask_cors/extension.py", line 110, in wrapped_function return cors_after_request(app.make_response(f(*args, **kwargs))) File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise raise value File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request return self.view_functionsrule.endpoint File "/app/main.py", line 428, in notify_one_device webpush(subscription_info = subsObject, data=notificationMessageObject, vapid_private_key=VAPID_PRIVATE_KEY, vapid_claims={"sub": "mailto:[email protected]"}) File "/usr/local/lib/python3.7/site-packages/pywebpush/init.py", line 485, in webpush timeout=timeout, File "/usr/local/lib/python3.7/site-packages/pywebpush/init.py", line 305, in send encoded = self.encode(data, content_encoding) File "/usr/local/lib/python3.7/site-packages/pywebpush/init.py", line 219, in encode version=content_encoding) File "/usr/local/lib/python3.7/site-packages/http_ece/init.py", line 394, in encrypt content[i:i + chunk_size], TypeError: unhashable type: 'slice'

Please guide how I should approach this.

@cmesas
Copy link

cmesas commented Aug 23, 2022

@ishwara-bhat I have the same problem, how could you solve it?

@jrconlin
Copy link
Member

Reading the dump, it appears that the problem is that http_ece can't encrypt content.
content comes from data in self.encode(data, content_encoding)

from a very quick view, I'm going to guess that the data being passed in is not a byte array. You may need to serialize the object content into a string or byte array before passing it to webpush.encode

@ishwara-bhat
Copy link
Author

ishwara-bhat commented Aug 24, 2022 via email

@cmesas
Copy link

cmesas commented Aug 24, 2022

Reading the dump, it appears that the problem is that http_ece can't encrypt content. content comes from data in self.encode(data, content_encoding)

from a very quick view, I'm going to guess that the data being passed in is not a byte array. You may need to serialize the object content into a string or byte array before passing it to webpush.encode

An example of a key pair that I am using are the following. I am using string strings directly.

{
"subject": "mailto: [email protected]",
"publicKey": "BLR4PgElXtaeu16JlFEfVuOZkyub-Y36w1XRDYvl7-gNOKUpW-YadhR0Yv6vnrz5h4HhJG5jq0arBRuIrJdWXtQ",
"privateKey": "LqUduWL7V0F7pOrYep5oTgZJaCXLl7eRvMZMpAE6DCA"
}

Is it necessary to use PEM certificates or can I use string keys?

@jrconlin
Copy link
Member

String keys can be in either raw or DER format: https://github.com/web-push-libs/vapid/blob/main/python/py_vapid/__init__.py#L134-L147. If you provide a path, py_vapid will try to read the file as either PEM or DER formatted https://github.com/web-push-libs/vapid/blob/main/python/py_vapid/__init__.py#L134-L147

@cmesas
Copy link

cmesas commented Aug 24, 2022

Las claves de cadena pueden estar en formato raw o DER: https://github.com/web-push-libs/vapid/blob/main/python/py_vapid/__init__.py#L134-L147 . Si proporciona una ruta, py_vapid intentará leer el archivo con formato PEM o DER https://github.com/web-push-libs/vapid/blob/main/python/py_vapid/__init__.py#L134-L147

Ok, I will try to work with the private key in PEM format from the file directly.

@treygilliland
Copy link

Just ran into this same issue. Make sure your data payload is JSON string encoded and not just a raw python dictionary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants