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 #149

Open
cmesas opened this issue Aug 24, 2022 · 4 comments
Open

Error : Web push send from python backend #149

cmesas opened this issue Aug 24, 2022 · 4 comments

Comments

@cmesas
Copy link

cmesas commented Aug 24, 2022

Hello, I'm having a problem when I try to run a notification, I think I'm following the documentation to the letter.

Information

Keys generated with vpaid (python): https://github.com/web-push-libs/vapid/tree/main/python

  • The keys that I indicate are for testing...

PrivateKey [private_key.pem]
----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg4zLDHdp8DtxtcAO4
MZVGeyp2JLJyVrqaMII75nGeAC+hRANCAATW1506jo86/0Ek72wj/AYgGZ6d1R3i
wabGfNb7DlMSmIrdYbrgLLI6HgnszgCGM1ORilZ95RoW9x0ER8+h8SI2
-----END PRIVATE KEY-----

ApplicationServerKey:
BNbXnTqOjzr_QSTvbCP8BiAZnp3VHeLBpsZ81vsOUxKYit1huuAssjoeCezOAIYzU5GKVn3lGhb3HQRHz6HxIjY

Subscription (chrome client: successfully generated with the ApplicationServerKey):
{"endpoint":"https://fcm.googleapis.com/fcm/send/eN9gFAJxIeA:APA91bGRifdlYB8pDgJ9wEYNxxIrVkT5Lb2cNTvtiU55-0bFmV4KGVBuIYbk67NRPjflK4FFdA5tJBPtHVaMMV0IzLrDvBtpBy1cd5VDzrKjXbBN5rJ5NWF-RurzHaFX2iBiun_ypzCT","expirationTime":null,"keys":{"p256dh":"BD9sOH3XIEbgk1x_KvtLbCLvyYEnuQ972W4LaTUnvl_-7vB_q8IzToFKGWK_QaTtUhWBj-Tb556xPuR9uXmr6-E","auth":"X0Zy2i_2WC3tkjElaK3D2g"}}

Code
webpush(
vapid_private_key="private_key.pem",
vapid_claims={"sub": "mailto:[email protected]"},
subscription_info=subscription,
data=data
)

Error
...
return base64.urlsafe_b64decode(data + b"===="[len(data) % 4:])
File "/usr/lib/python3.8/base64.py", line 133, in urlsafe_b64decode
return b64decode(s)
File "/usr/lib/python3.8/base64.py", line 87, in b64decode
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

@jrconlin Could you tell me what I may be doing wrong?

@cmesas
Copy link
Author

cmesas commented Aug 26, 2022

@jrconlin any ideas what I may be doing wrong? I am blocked and the only solution I would have would be to integrate in node that I have seen that I have no problem.

@jrconlin
Copy link
Member

Hrm,

On my system, your private key is invalid. If that was generated by vapid, I'm not sure why it made an invalid key, but you can also create one by using openssl directly:

openssl ecparam -genkey -name prime256v1 -noout -out private_key.pem

I'm also not sure why you're seeing the incorrect padding error. I tried the application string and it worked fine.

@cmesas
Copy link
Author

cmesas commented Aug 26, 2022

Hrm,

On my system, your private key is invalid. If that was generated by vapid, I'm not sure why it made an invalid key, but you can also create one by using openssl directly:

openssl ecparam -genkey -name prime256v1 -noout -out private_key.pem

I'm also not sure why you're seeing the incorrect padding error. I tried the application string and it worked fine.

Thanks, I'll try again as you say.

@cmesas
Copy link
Author

cmesas commented Aug 29, 2022

Hrm,
On my system, your private key is invalid. If that was generated by vapid, I'm not sure why it made an invalid key, but you can also create one by using openssl directly:
openssl ecparam -genkey -name prime256v1 -noout -out private_key.pem
I'm also not sure why you're seeing the incorrect padding error. I tried the application string and it worked fine.

Thanks, I'll try again as you say.

Thanks, finally without generating the keys through the vapid library I got it to work.

I found these commands that I think could be useful for people who want to work with DER (bas64), I'll leave it here:

openssl ecparam -genkey -name prime256v1 -noout -out private_key.pem
openssl ec -in private_key.pem -pubout -outform DER|tail -c 65|base64|tr -d '=' |tr '/+' '-' >> public.key
openssl ec -in private_key.pem -outform DER|tail -c +8|head -c 32|base64|tr -d '=' |tr '/+' '
-' >> private. key

Thank @jrconlin

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

2 participants