sns

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

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

Go to latest
Published: May 23, 2023 License: MIT Imports: 12 Imported by: 11 modules, 8 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Build Status GoDoc

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/robbiet480/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!")
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

Examples

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) SignatureAlgorithm

func (payload *Payload) SignatureAlgorithm() x509.SignatureAlgorithm

SignatureAlgorithm returns properly Algorithm for AWS Signature Version.

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

Example
var notificationPayload sns.Payload
err := json.Unmarshal([]byte(notificationJson), &notificationPayload)
if err != nil {
	fmt.Print(err)
}
subscriptionResponse, err := notificationPayload.Subscribe()
if err != nil {
	fmt.Println("Error when subscribing!", err)
}
fmt.Printf("subscriptionResponse %+v", subscriptionResponse)

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

Example
var notificationPayload sns.Payload
err := json.Unmarshal([]byte(notificationJson), &notificationPayload)
if err != nil {
	fmt.Print(err)
}
unsubscriptionResponse, err := notificationPayload.Unsubscribe()
if err != nil {
	fmt.Println("Error when unsubscribing!", err)
}
fmt.Printf("unsubscriptionResponse %+v", unsubscriptionResponse)

func (*Payload) VerifyPayload

func (payload *Payload) VerifyPayload() error

VerifyPayload will verify that a payload came from SNS

Example
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)
} else {
	fmt.Print("Payload is valid!")
}

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