gosigner

package
v0.0.0-...-fae07df Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2016 License: MIT, MIT Imports: 9 Imported by: 0

README

Go Signer

A simple url signer written in go.

##Signatures A signature must contain a nonce, timestamp and signature. The keys can be configured in the options.

The signature is composed as follows:

Method+(parameters sorted alphabetically including nonce and timestamp)

example:

POSTsomeparametersconcatedalphabeticallyNONCE1451852213

##Nonce check function You can pass a nonce check function. You'll have to store the used nonces yourself and implement the CheckNonceFunc to check if the nonce exists.

func CheckNonce(n string) error

##Example Usage

var secret = "secret"
var options = gosigner.Options{
	NonceParam:     "nonce",
	TimestampParam: "timestamp",
	SignatureParam: "signature",
	MaxLife:        900, //max life in seconds
}

h := hmac.New(sha1.New, []byte(secret))
signer :=  gosigner.New(h, options)

r, _ := http.NewRequest("GET", "http://google.com?foo=bar", nil)
signer.Sign(r)

if err := signer.IsValid(r); err != nil {
	//signature is invalid
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NonceChecker

type NonceChecker func(n string) error

NonceChecker is a func type to check if a nonce is valid

type Options

type Options struct {
	NonceParam     string
	TimestampParam string
	SignatureParam string
	CheckNonceFunc NonceChecker
	MaxLife        int64
}

Options for the Signer

type Signer

type Signer struct {
	// contains filtered or unexported fields
}

Signer type to create signatures

func New

func New(h hash.Hash, o Options) *Signer

New creates a new Signer instance

func (*Signer) GenerateNonce

func (s *Signer) GenerateNonce(timestamp int64, args ...interface{}) string

GenerateNonce creates a new nonce based on a timestamp and a number of arguments

func (*Signer) IsValid

func (s *Signer) IsValid(r *http.Request) error

IsValid checks if a request has a valid signature

func (*Signer) Sign

func (s *Signer) Sign(r *http.Request)

Sign signs a request with a nonce and timestamp

func (*Signer) Signature

func (s *Signer) Signature(r *http.Request) string

Signature returns a generated signature for a request. it omits the signature query parameter if present

type SignerMiddleware

type SignerMiddleware struct {
	// contains filtered or unexported fields
}

func NewMidddleware

func NewMidddleware(s *Signer, errorHandler func(w http.ResponseWriter, err error)) *SignerMiddleware

func (*SignerMiddleware) Handler

func (s *SignerMiddleware) Handler(next http.Handler) http.Handler

Jump to

Keyboard shortcuts

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