sns

package module
v0.0.0-...-f550388 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2022 License: MIT Imports: 12 Imported by: 0

README

go.sns

A helper library for receiving Amazon AWS SNS HTTP(S) notifications.

It provides signature validation for payloads and conveinence functions for subscribing and unsubscribing from topics.

Usage

Verifying a HTTP POST (a payload)
import (
  "encoding/json"
  "fmt"

  "github.com/cwxstat/go.sns"
)

var notificationPayload sns.Payload
err := json.Unmarshal([]byte(notificationJson), &notificationPayload)
if err != nil {
  fmt.Print(err)
}
verifyErr := notificationPayload.VerifyPayload()
if verifyErr != nil {
  fmt.Print(verifyErr)
}
fmt.Print("Payload is valid!")

Example with Labstack Echo

// Handler
func aws(c echo.Context) error {
	log.Println("aws called...")


	var notificationPayload sns.Payload
	err := json.NewDecoder(c.Request().Body).Decode(&notificationPayload)

	if err != nil {
		log.Print(err)
	}
	verifyErr := notificationPayload.VerifyPayload()
	if verifyErr != nil {
		log.Print(verifyErr)
    return c.String(http.StatusBadRequest, "")
	}

	log.Print("Payload is valid!")

	return c.String(http.StatusOK, "")
}

Subscribing to a topic
import (
  "encoding/json"
  "fmt"

  "github.com/robbiet480/go.sns"
)

// If it's a SubscriptionConfirmation or UnsubscribeConfirmation
subscriptionResponse, err := notificationPayload.Subscribe()
if err != nil {
  fmt.Println("Error when subscribing!", err)
}
fmt.Printf("subscriptionResponse %+v", subscriptionResponse)
Unsubscribing from a topic
import (
  "encoding/json"
  "fmt"

  "github.com/robbiet480/go.sns"
)

// If it's a Notification
unsubscriptionResponse, err := notificationPayload.Unsubscribe()
if err != nil {
  fmt.Println("Error when unsubscribing!", err)
}
fmt.Printf("unsubscriptionResponse %+v", unsubscriptionResponse)

Thanks

This library was based off work by lazywei, found on this Stack Overflow question and code written by syama666.

Thanks also goes to xibz for helping me work out some of the low level certificate/SHA1WithRSA stuff in this issue on aws-sdk-go.

Contributing

Fork, edit, write & run tests, submit PR, success!

Tests

Tests are written but not passing because the payload string is an example from the documentation.

License

MIT

Documentation

Overview

Package sns provides helper functions for verifying and processing Amazon AWS SNS HTTP POST payloads.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfirmSubscriptionResponse

type ConfirmSubscriptionResponse struct {
	XMLName         xml.Name `xml:"ConfirmSubscriptionResponse"`
	SubscriptionArn string   `xml:"ConfirmSubscriptionResult>SubscriptionArn"`
	RequestId       string   `xml:"ResponseMetadata>RequestId"`
}

ConfirmSubscriptionResponse contains the XML response of accessing a SubscribeURL

type Payload

type Payload struct {
	Message          string `json:"Message"`
	MessageId        string `json:"MessageId"`
	Signature        string `json:"Signature"`
	SignatureVersion string `json:"SignatureVersion"`
	SigningCertURL   string `json:"SigningCertURL"`
	SubscribeURL     string `json:"SubscribeURL"`
	Subject          string `json:"Subject"`
	Timestamp        string `json:"Timestamp"`
	Token            string `json:"Token"`
	TopicArn         string `json:"TopicArn"`
	Type             string `json:"Type"`
	UnsubscribeURL   string `json:"UnsubscribeURL"`
}

Payload contains a single POST from SNS

func (*Payload) BuildSignature

func (payload *Payload) BuildSignature() []byte

BuildSignature returns a byte array containing a signature usable for SNS verification

func (*Payload) Subscribe

func (payload *Payload) Subscribe() (ConfirmSubscriptionResponse, error)

Subscribe will use the SubscribeURL in a payload to confirm a subscription and return a ConfirmSubscriptionResponse

func (*Payload) Unsubscribe

func (payload *Payload) Unsubscribe() (UnsubscribeResponse, error)

Unsubscribe will use the UnsubscribeURL in a payload to confirm a subscription and return a UnsubscribeResponse

func (*Payload) VerifyPayload

func (payload *Payload) VerifyPayload() error

VerifyPayload will verify that a payload came from SNS

type UnsubscribeResponse

type UnsubscribeResponse struct {
	XMLName   xml.Name `xml:"UnsubscribeResponse"`
	RequestId string   `xml:"ResponseMetadata>RequestId"`
}

UnsubscribeResponse contains the XML response of accessing an UnsubscribeURL

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL