web-push-libs / pywebpush

Python Webpush Data encryption library
Mozilla Public License 2.0
316 stars 54 forks source link

Error : Web push send from python backend #149

Open cmesas opened 2 years ago

cmesas commented 2 years ago

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:contact@example.com"}, 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 commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 privatekey.pem -pubout -outform DER|tail -c 65|base64|tr -d '=' |tr '/+' '-' >> public.key openssl ec -in privatekey.pem -outform DER|tail -c +8|head -c 32|base64|tr -d '=' |tr '/+' '-' >> private. key

Thank @jrconlin