SherClockHolmes/webpush-go

publicKey curve error

InQuartic opened this issue · 7 comments

Hi i'm getting this error in the example
Unmarshal Error: Public key is not a valid point on the curve

the script in the index.html has the public key in.
`function subscribe() {
navigator.serviceWorker.ready
.then(function(registration) {
const vapidPublicKey = 'BMj5tNfFHEwOW75bQUTHinmjhNZ6eDEveIsDjzRgM7YM8eWkU5CAQL69wFdgSWBHvB_N__X1QdN-LivIswOMtqQ';

      return registration.pushManager.subscribe({
        userVisibleOnly: true,
        applicationServerKey: urlBase64ToUint8Array(vapidPublicKey),
      });
    })
    .then(function(subscription) {
      console.log(
        JSON.stringify({
          subscription: subscription,
        })
      );
    })
    .catch(err => console.error(err));
}`

In main.go file has the info in.
`const (
subscription = "test"
vapidPublicKey = "BMj5tNfFHEwOW75bQUTHinmjhNZ6eDEveIsDjzRgM7YM8eWkU5CAQL69wFdgSWBHvB_N__X1QdN-LivIswOMtqQ"
vapidPrivateKey = "QbHMh4l_RYWF76CZJ-q-FnKq5WvmTQph_xldpq1hdKk"
)

func main() {
// Decode subscription
s := &webpush.Subscription{}
json.Unmarshal([]byte(subscription), s)

// Send Notification
_, err := webpush.SendNotification([]byte("Test"), s, &webpush.Options{
	Subscriber:      "example@example.com", // Do not include "mailto:"
	VAPIDPublicKey:  vapidPublicKey,
	VAPIDPrivateKey: vapidPrivateKey,
	TTL:             30,
})
if err != nil {
	log.Println(err)
}

}`

Any help would be appreciated, soory if this is a noob mistake.

Thanks

That's because your subscription must be like this :
const (
subscription = {"endpoint":"...","expirationTime":...,"keys":{"p256dh":"...","auth":"..."}}
vapidPublicKey = ...
)

I'm closing this for now, feel free to open another ticket if you run into more issues.

For anyone looking at this post. I used @pjserol's suggestion however I needed ticks surrounding the object inside of my brace.

@frlzjosh is your code working now? In my case solution proposed by @pjserol doesn't work. I checked code and its showing error in elleptic.unmarshall() function as sharedX is equal to nil. Can someone please help?

Just realized the problem in my code, post the resolution here in case anyone else faces the same issue 😂

# incorrect assignment 
	subscription    = `{"subscription":{"endpoint":"...","expirationTime":...,"keys":{"p256dh":"...","auth":"..."}}}`
# correct assignment 
	subscription    = `{"endpoint":"...","expirationTime":...,"keys":{"p256dh":"...","auth":"..."}}`

In example/index.html you find code like this (inside a console.log):
JSON.stringify({ subscription: subscription, })
This generates the outer shell, that starts with a "subscription" element.
To produce the assignment that is more useful on the server side, this may be used:
JSON.stringify(subscription);

What are the VAPID credentials to use in "main.go" and "index.html"? Are these generated using GeneratePairKeys() Golang function? I made it this way, and it doesn't throw any error, but I can't see the push notification. (I enabled notifications on the browser)

main.go

const (
	subscription = `{
        "endpoint": "https://fcm.googleapis.com/fcm/send/fsWo9V_2UrI:APA91bGC0HEjpqvt5wd0ggQC8SHGON4eTw5TtxqKx6b1IrMSubQ7xwEBT9NPX5nfVyBFVO3rkK4TEkkUCrihW7Vw0QTScyNtRvXrLDrHWk5LG0hCO-GavfNqvgh2OtuY_OIvy7vD6ED1",
        "expirationTime": null,
        "keys": {
            "p256dh": "BNR9wAPOEdvW2AIEICsGjRSeS4pbYtEpSgo0GFjM2z8_AAONejGpI9jXK2W52i3Nx4gYAchfcULid5doEWtnT8A",
            "auth": "vZRle8lGgwVa_A4i91fDFQ"
        }
    }`
	vapidPublicKey  = "BDTaD-bo81jd5zaFRcbPZUphh_MCUcfv84SQJaLIEz7e04qAcj7TfrC2xYOufQfgI2kNS9caKubPOFqjQQXefTw"
	vapidPrivateKey = "mWI9CzvGep3SBr1dHRuntc_sBTlcwXP_Rm19w267J3A"
)

index.html

 .then(function(registration) {
          const vapidPublicKey = 'BDTaD-bo81jd5zaFRcbPZUphh_MCUcfv84SQJaLIEz7e04qAcj7TfrC2xYOufQfgI2kNS9caKubPOFqjQQXefTw';

Thanks in advance