# HG changeset patch # User Peter Sanchez # Date 1670883408 21600 # Mon Dec 12 16:16:48 2022 -0600 # Node ID 2ccb6f8d93b43c80628a7499187254b7fca6c6b7 # Parent f6fa87105cbb3dbc198c618d0d9373712367d357 Adding (un)subscribe confirmation diff --git a/feedback.go b/feedback.go --- a/feedback.go +++ b/feedback.go @@ -9,6 +9,7 @@ "errors" "fmt" "io" + "io/ioutil" "net/http" "net/url" "reflect" @@ -68,7 +69,7 @@ UnsubscribeURL string `json:"UnsubscribeURL"` } -func (r Record) getBytesToSign() []byte { +func (r *Record) getBytesToSign() []byte { var fields []string var lines bytes.Buffer if r.Type == "Notification" { @@ -91,14 +92,14 @@ return lines.Bytes() } -func (r Record) signatureAlgorithm() x509.SignatureAlgorithm { +func (r *Record) signatureAlgorithm() x509.SignatureAlgorithm { if r.SignatureVersion == "2" { return x509.SHA256WithRSA } return x509.SHA1WithRSA } -func (r Record) verify() error { +func (r *Record) verify() error { // Get signature signature, err := base64.StdEncoding.DecodeString(r.Signature) if err != nil { @@ -147,7 +148,7 @@ func (s *Service) Feedback(c echo.Context) error { gctx := c.(*server.Context) req := c.Request() - var data Record + var data *Record err := json.NewDecoder(req.Body).Decode(&data) if err != nil { return c.JSON(http.StatusOK, err) @@ -165,7 +166,10 @@ case "Notification": // For Notification, the Message field is a json-like string var message map[string]any - json.Unmarshal([]byte(data.Message), &message) + err = json.Unmarshal([]byte(data.Message), &message) + if err != nil { + return err + } // According to some settings `eventType` might be called `notificationType` // https://docs.aws.amazon.com/ses/latest/dg/event-publishing-retrieving-sns-contents.html#event-publishing-retrieving-sns-contents-subscription-object @@ -193,11 +197,23 @@ } gctx.Server.Logger().Printf("Received %s notification", eventType) - case "SubscriptionConfirmation": - err = s.actions.Subscribe(data.Message) - gctx.Server.Logger().Printf("Received %s notification", data.Type) - case "UnsubscribeConfirmation": - err = s.actions.Unsubscribe(data.Message) + case "SubscriptionConfirmation", "UnsubscribeConfirmation": + resp, err := http.Get(data.SubscribeURL) + if err != nil { + return err + } + defer resp.Body.Close() + + _, err = ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + + if data.Type == "SubscribeConfirmation" { + err = s.actions.Subscribe(data.Message) + } else { + err = s.actions.Unsubscribe(data.Message) + } gctx.Server.Logger().Printf("Received %s notification", data.Type) default: gctx.Server.Logger().Printf("Error: Received unknown notification type: %s.", data.Type)