aws_sns_verifier

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2021 License: GPL-3.0 Imports: 15 Imported by: 0

README

AWS SNS Verifier

Simple package for verifying the signature of the AWS SNS Message

Installation

go get github.com/Sfinks80/aws_sns_verifier

Usage

To enable debug-mode, please set the ENV variable SNS_VERIFIER_DEBUG=true

package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	sns "github.com/Sfinks80/aws_sns_verifier"
)

func main() {
    // ...
}
         
func Handler() http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        if !sns.IsValidType(r.Header.Get("x-amz-sns-message-type")) {
            fmt.Println("error invalid message type")
            return
        }
    
        body := []byte(`{
            "Type" : "SubscriptionConfirmation",
            "MessageId" : "165545c9-2a5c-472c-8df2-7ff2be2b3b1b",
            "Token" : "2336412f37f...",
            "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic",
            "Message" : "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:123456789012:MyTopic.\nTo confirm the subscription, visit the SubscribeURL included in this message.",
            "SubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:123456789012:MyTopic&Token=2336412f37...",
            "Timestamp" : "2012-04-26T20:45:04.751Z",
            "SignatureVersion" : "1",
            "Signature" : "EXAMPLEpH+...",
            "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem"
        }`)
    
        notify := &sns.Notification{}
        json.Unmarshal(body, notify)
	
        valid, _ := notify.VerifySignature("eu-central-1")
        
        fmt.Println(valid)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValidType added in v1.0.2

func IsValidType(t string) bool

Types

type Notification

type Notification struct {
	Type             string
	Subject          string
	Message          string
	MessageId        string
	Signature        string
	SignatureVersion string
	SigningCertURL   string
	SubscribeURL     string
	UnsubscribeURL   string
	TopicArn         string
	Token            string
	Timestamp        string
}

func (*Notification) VerifySignature

func (sns *Notification) VerifySignature(awsRegion string) (bool, error)

Jump to

Keyboard shortcuts

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