urlsigner

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2024 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidSignature = errors.New("invalid signature")

ErrInvalidSignature is returned by Unsign when the provided token's signatuire is not valid.

View Source
var ErrShortToken = errors.New("token is too small to be valid")

ErrShortToken is returned by Unsign when the provided token's length is too short to be a vlaid token.

Functions

func Epoch

func Epoch(e int64) func(*Sword)

Epoch is a functional option that can be passed to New() to set the Epoch to be used.

func Timestamp

func Timestamp(s *Sword)

Timestamp is a functional option that can be passed to New() to add a timestamp to signatures.

Types

type Signer

type Signer struct {
	Secret []byte // Secret is the key used to sign the token
}

func (*Signer) Expired

func (s *Signer) Expired(data string, minutesUntilExpire int) bool

func (*Signer) GenerateTokenFromString

func (s *Signer) GenerateTokenFromString(data string) string

func (*Signer) VerifyToken

func (s *Signer) VerifyToken(token string) bool

type Sword

type Sword struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Sword is a magical Wooden Sword to be used for protection, because it's dangerous out there... Also, it is the main struct used to sign and unsign data using this package.

func New

func New(key []byte, options ...func(*Sword)) *Sword

New takes a secret key and returns a new Sword. If no Options are provided then minimal defaults will be used. NOTE: The key must be 64 bytes or less in size. If a larger key is provided it will be truncated to 64 bytes. func New(key []byte, o *Options) *Sword {

func (*Sword) Parse

func (s *Sword) Parse(t []byte) Token

Parse will parse the []byte token returned from Sign based on the Sword Options into a Token struct. For this to work corectly the Sword Options need to match that of what was used when the token was initially created.

func (*Sword) Sign

func (s *Sword) Sign(data []byte) []byte

Sign signs data and returns []byte in the format `data.signature`. Optionally add a timestamp and return in the format `data.timestamp.signature`

func (*Sword) Unsign

func (s *Sword) Unsign(token []byte) ([]byte, error)

Unsign validates a signature and if successful returns the data portion of the []byte. If unsuccessful it will return an error and nil for the data.

type Token

type Token struct {
	Payload   []byte
	Timestamp time.Time
}

Token is used to parse out a []byte token provided by Sign()

Jump to

Keyboard shortcuts

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