SherClockHolmes / webpush-go

Web Push API Encryption with VAPID support.
MIT License
312 stars 67 forks source link

illegal base64 data at input byte 'x' for decoding vapid private key #29

Closed maheshnayer closed 4 years ago

maheshnayer commented 4 years ago

Hi I'm experiencing this error while trying to send notifications My private key has a "+" in the string and the input byte location points to this char. How can I get over this? My public key also has a "+" in it. Same issue here as well Any help appreciated Thank you

juan88 commented 4 years ago

I'm having the same issue when trying to send notifications.

I have generated the key using openssl like:

I think that is throwing an error in the function getVAPIDAuthorizationHeader in vapid.go when trying to do: base64.RawURLEncoding.DecodeString(vapidPrivateKey)

Thanks!

SherClockHolmes commented 4 years ago

@juan88 Is there a reason you are not using the webpush.GenerateVAPIDKeys() method to generate your vapid keys?

juan88 commented 4 years ago

Hi!

No there was no specific reasong why. I started with a tutorial from the mozilla's blog and I created the keys in the way I posted.

Then I created a couple of keys with the function you specified and save them to a file. That is the correct way to handle it right? I mean, the keypair generated is supposed to be reused across all the notifications that you sent.

That worked for me. So I think that is an issue with the way that the keys are encoded. If you generate the keys by any other method, the function base64.RawURLEncoding.DecodeString(vapidPrivateKey) may throw that error since it uses a specific way to encode base64 strings. I've found out in the official docs from the package and the corresponding RFC.

froodian commented 4 years ago

in our organization's fork, we have made a change where we decode vapid keys with this method

func decodeVapidKey(key string) ([]byte, error) {
    bytes, err := base64.URLEncoding.DecodeString(key)
    if err == nil {
        return bytes, nil
    }
    return base64.RawURLEncoding.DecodeString(key)
}

which solves this issue for us.