itsdangerous

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

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

Go to latest
Published: Oct 11, 2024 License: MIT Imports: 13 Imported by: 0

README

license

go-itsdangerous

Like itsdangerous but for Go.

This is originally based on https://github.com/iromli/go-itsdangerous, but forked here as that seems unmaintained. Also the public API here has drifted significantly from that version.

Documentation

Overview

Package itsdangerous implements various functions to deal with untrusted sources. Mainly useful for web applications.

This package exists purely as a port of https://github.com/mitsuhiko/itsdangerous, where the original version is written in Python.

Index

Constants

This section is empty.

Variables

View Source
var NowFunc = time.Now

Function used to obtain the current time. Defaults to time.Now, but can be overridden eg for unit tests to simulate a different current time.

Functions

This section is empty.

Types

type HMACAlgorithm

type HMACAlgorithm struct {
	DigestMethod func() hash.Hash
}

HMACAlgorithm provides signature generation using HMACs.

func (*HMACAlgorithm) GetSignature

func (a *HMACAlgorithm) GetSignature(key []byte, value string) []byte

GetSignature returns the signature for the given key and value.

func (*HMACAlgorithm) VerifySignature

func (a *HMACAlgorithm) VerifySignature(key []byte, value string, signature []byte) bool

VerifySignature verifies the given signature matches the expected signature.

type InvalidSignatureError

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

func (InvalidSignatureError) Error

func (e InvalidSignatureError) Error() string

func (InvalidSignatureError) Unwrap

func (e InvalidSignatureError) Unwrap() error

type SignatureExpiredError

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

func (SignatureExpiredError) Error

func (e SignatureExpiredError) Error() string

type Signer

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

Signer can sign bytes and unsign it and validate the signature provided.

Salt can be used to namespace the hash, so that a signed string is only valid for a given namespace. Leaving this at the default value or re-using a salt value across different parts of your application where the same signed value in one part can mean something different in another part is a security risk.

func NewSigner

func NewSigner(secret, salt string) *Signer

NewSigner creates a new Signer with the given secret and salt. All other properties will be set to match the Python itsdangerous defaults.

func NewSignerWithOptions

func NewSignerWithOptions(secret, salt, sep, derivation string, digest func() hash.Hash, algo SigningAlgorithm) (*Signer, error)

NewSignerWithOptions creates a new Signer allowing overiding the default properties.

func (*Signer) Sign

func (s *Signer) Sign(value string) string

Sign the given string.

func (*Signer) Unsign

func (s *Signer) Unsign(signed string) (string, error)

Unsign the given string.

type SigningAlgorithm

type SigningAlgorithm interface {
	GetSignature(key []byte, value string) []byte
	VerifySignature(key []byte, value string, signature []byte) bool
}

SigningAlgorithm provides interfaces to generate and verify signature

type TimestampSigner

type TimestampSigner struct {
	Signer
}

TimestampSigner works like the regular Signer but also records the time of the signing and can be used to expire signatures.

func NewTimestampSigner

func NewTimestampSigner(secret, salt string) *TimestampSigner

NewTimestampSigner creates a new TimestampSigner with the given secret and salt. All other properties will be set to match the Python itsdangerous defaults.

func NewTimestampSignerWithOptions

func NewTimestampSignerWithOptions(secret, salt, sep, derivation string, digest func() hash.Hash, algo SigningAlgorithm) (*TimestampSigner, error)

NewTimestampSignerWithOptions creates a new TimestampSigner allowing overiding the default properties.

func (*TimestampSigner) Sign

func (s *TimestampSigner) Sign(value string) string

Sign the given string.

func (*TimestampSigner) Unsign

func (s *TimestampSigner) Unsign(value string, maxAge time.Duration) (string, error)

Unsign the given string.

type URLSafeSerializer

type URLSafeSerializer struct {
	Signer
}

func NewURLSafeSerializer

func NewURLSafeSerializer(secret, salt string) *URLSafeSerializer

func (*URLSafeSerializer) Marshal

func (s *URLSafeSerializer) Marshal(value interface{}) (string, error)

func (*URLSafeSerializer) Unmarshal

func (s *URLSafeSerializer) Unmarshal(signed string, value interface{}) error

type URLSafeTimedSerializer

type URLSafeTimedSerializer struct {
	TimestampSigner
}

func NewURLSafeTimedSerializer

func NewURLSafeTimedSerializer(secret, salt string) *URLSafeTimedSerializer

func (*URLSafeTimedSerializer) Marshal

func (s *URLSafeTimedSerializer) Marshal(value interface{}) (string, error)

func (*URLSafeTimedSerializer) Unmarshal

func (s *URLSafeTimedSerializer) Unmarshal(signed string, value interface{}, maxAge time.Duration) error

Jump to

Keyboard shortcuts

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