diff --git a/api/notifications.go b/api/notifications.go index dff1977765b31b98297feaa75827e1b2edc71fd6..2aaed501ebcda3eb7bf5d09939c009574cc9639a 100644 --- a/api/notifications.go +++ b/api/notifications.go @@ -7,7 +7,15 @@ package api -import jww "github.com/spf13/jwalterweatherman" +import ( + "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" + "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/elixxir/crypto/hash" + "gitlab.com/xx_network/crypto/signature/rsa" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/id/ephemeral" +) // RegisterForNotifications allows a client to register for push // notifications. @@ -17,22 +25,29 @@ import jww "github.com/spf13/jwalterweatherman" // risk to the user. func (c *Client) RegisterForNotifications(token []byte) error { jww.INFO.Printf("RegisterForNotifications(%s)", token) - // // Pull the host from the manage - // notificationBotHost, ok := cl.receptionManager.Comms.GetHost(&id.NotificationBot) - // if !ok { - // return errors.New("Failed to retrieve host for notification bot") - // } - - // // Send the register message - // _, err := cl.receptionManager.Comms.RegisterForNotifications(notificationBotHost, - // &mixmessages.NotificationToken{ - // Token: notificationToken, - // }) - // if err != nil { - // err := errors.Errorf( - // "RegisterForNotifications: Unable to register for notifications! %s", err) - // return err - // } + // Pull the host from the manage + notificationBotHost, ok := c.comms.GetHost(&id.NotificationBot) + if !ok { + return errors.New("RegisterForNotifications: Failed to retrieve host for notification bot") + } + intermediaryReceptionID, sig, err := c.getIidAndSig() + if err != nil { + return err + } + // Send the register message + _, err = c.comms.RegisterForNotifications(notificationBotHost, + &mixmessages.NotificationRegisterRequest{ + Token: token, + IntermediaryId: intermediaryReceptionID, + TransmissionRsa: rsa.CreatePublicKeyPem(c.GetUser().TransmissionRSA.GetPublic()), + TransmissionRsaSig: sig, + TransmissionSalt: c.GetUser().TransmissionSalt, + }) + if err != nil { + err := errors.Errorf( + "RegisterForNotifications: Unable to register for notifications! %s", err) + return err + } return nil } @@ -40,19 +55,46 @@ func (c *Client) RegisterForNotifications(token []byte) error { // UnregisterForNotifications turns of notifications for this client func (c *Client) UnregisterForNotifications() error { jww.INFO.Printf("UnregisterForNotifications()") - // // Pull the host from the manage - // notificationBotHost, ok := cl.receptionManager.Comms.GetHost(&id.NotificationBot) - // if !ok { - // return errors.New("Failed to retrieve host for notification bot") - // } - - // // Send the unregister message - // _, err := cl.receptionManager.Comms.UnregisterForNotifications(notificationBotHost) - // if err != nil { - // err := errors.Errorf( - // "RegisterForNotifications: Unable to register for notifications! %s", err) - // return err - // } + // Pull the host from the manage + notificationBotHost, ok := c.comms.GetHost(&id.NotificationBot) + if !ok { + return errors.New("Failed to retrieve host for notification bot") + } + intermediaryReceptionID, sig, err := c.getIidAndSig() + if err != nil { + return err + } + // Send the unregister message + _, err = c.comms.UnregisterForNotifications(notificationBotHost, &mixmessages.NotificationUnregisterRequest{ + IntermediaryId: intermediaryReceptionID, + IIDTransmissionRsaSig: sig, + }) + if err != nil { + err := errors.Errorf( + "RegisterForNotifications: Unable to register for notifications! %s", err) + return err + } return nil } + +func (c *Client) getIidAndSig() ([]byte, []byte, error) { + intermediaryReceptionID, err := ephemeral.GetIntermediaryId(c.GetUser().ReceptionID) + if err != nil { + return nil, nil, errors.WithMessage(err, "RegisterForNotifications: Failed to form intermediary ID") + } + h, err := hash.NewCMixHash() + if err != nil { + return nil, nil, errors.WithMessage(err, "RegisterForNotifications: Failed to create cmix hash") + } + _, err = h.Write(intermediaryReceptionID) + if err != nil { + return nil, nil, errors.WithMessage(err, "RegisterForNotifications: Failed to write intermediary ID to hash") + } + + sig, err := rsa.Sign(c.rng.GetStream(), c.GetUser().TransmissionRSA, hash.CMixHash, h.Sum(nil), nil) + if err != nil { + return nil, nil, errors.WithMessage(err, "RegisterForNotifications: Failed to sign intermediary ID") + } + return intermediaryReceptionID, sig, nil +}