urlsigner

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2019 License: MIT Imports: 7 Imported by: 0

README

Go URL Signer

Installation:

go get github.com/ionutmilica/urlsigner

Examples:

Signing an url string
package main

import "github.com/ionutmilica/urlsigner"

func main()  {
    signer := urlsigner.New("key")
    signedUrl := signer.SignURL("https://my-app.dev?page=protected")

    // Verifying is as simple as providing the signed url:
    println(signer.VerifyURL(signedUrl))
}

Equivalent methods Sign and Verify are provided for when you want to use url.URL structs instead of strings.

Signing an url for a limited period
package main

import (
	"github.com/ionutmilica/urlsigner"
	"time"
)

func main()  {
    signer := urlsigner.New("key")
    expiration := time.Now().UTC().Add(time.Hour)
    signedUrl := signer.SignTemporaryURL("https://my-app.dev?page=protected", expiration)

    // Verifying is as simple as providing the signed url:
    println(signer.VerifyTemporaryURL(signedUrl))
}

Equivalent methods SignTemporary and VerifyTemporary are provided for when you want to use url.URL structs instead of strings.

Signing primitives
signature := urlsigner.Sign(sha256.New, "key", "payload")
isValid := urlsigner.Verify(signature, "expected-signature")
Todo:
  • Route signing
  • HTTP middleware for route signing

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Algorithm

func Algorithm(alg func() hash.Hash) func(*SignerProvider)

Algorithm allows overriding of the internal hashing algorithm

func ExpirationField

func ExpirationField(name string) func(*SignerProvider)

ExpirationField allows overriding of the internal field name for expiration

func Sign

func Sign(algorithm func() hash.Hash, key, payload string) string

Sign will create a new signature based on a key and a string payload It is required to choose a hash algorithm

func SignatureField

func SignatureField(name string) func(*SignerProvider)

SignatureField allows overriding of the internal field name for signature

func Verify

func Verify(a, b string) bool

Verify will tell if two string signatures are equal

Types

type SignerProvider

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

SignerProvider is used to sign and verify urls.

func New

func New(secretKey string, opts ...func(*SignerProvider)) *SignerProvider

New will create a new SignerProvider.

urlsigner.New("secret-key")

func (*SignerProvider) Sign

func (p *SignerProvider) Sign(u url.URL) url.URL

Sign will sign an URL object returning it updated to include the signature

func (*SignerProvider) SignTemporary

func (p *SignerProvider) SignTemporary(u url.URL, expireAt time.Time) url.URL

SignTemporary will sign an URL object for a limited period of time retuning it updated with two new query strings: signature, expiration

func (*SignerProvider) SignTemporaryURL

func (p *SignerProvider) SignTemporaryURL(rawURL string, expireAt time.Time) string

SignTemporaryURL acts like SignTemporary method but accepts the url as string instead of url.URL

func (*SignerProvider) SignURL

func (p *SignerProvider) SignURL(rawURL string) string

SignURL acts like Sign method but accepts the url as string instead of url.URL

func (*SignerProvider) Verify

func (p *SignerProvider) Verify(u url.URL) bool

Verify will check an URL object against its signature This signature should be provided by the url itself in a query string

func (*SignerProvider) VerifyTemporary

func (p *SignerProvider) VerifyTemporary(u url.URL) bool

VerifyTemporary will check an URL object against its signature and check if it's expired This signature should be provided by the url itself in a query string

func (*SignerProvider) VerifyTemporaryURL

func (p *SignerProvider) VerifyTemporaryURL(rawURL string) bool

VerifyTemporaryURL acts like VerifyTemporary method but accepts the url as string instead of url.URL

func (*SignerProvider) VerifyURL

func (p *SignerProvider) VerifyURL(rawURL string) bool

VerifyURL acts like Verify method but accepts the url as string instead of url.URL

Jump to

Keyboard shortcuts

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