Documentation
¶
Overview ¶
package brief generates unforgeable tokens of cryptographically signed data with a build-in expiration. It is lighter than JWT, but more limited.
Inspired by: https://pdos.csail.mit.edu/papers/webauth:sec10.pdf
If you need something more fully-featured than arbitrary data signed and verified with an expiration date, you should look into JWT.
mint := brief.NewMint([]byte("your secret hmac key"))
...
token, err := mint.Sign([]byte("your tamper proof data"), time.Now().Add(time.Hour))
cookieValue := token.String() // serialize
...
token, err := mint.VerifyString(cookieValue)
Index ¶
- Variables
- func Decode(s string) ([]byte, error)
- func Encode(data []byte) string
- type Mint
- func (m *Mint) Generate(dataLen int, expires time.Time) (Token, error)
- func (m *Mint) GetSecret() []byte
- func (m *Mint) Sign(data []byte, expires time.Time) (Token, error)
- func (m *Mint) SignValues(data url.Values, expires time.Time) (Token, error)
- func (m *Mint) Verify(b Token) (Token, error)
- func (m *Mint) VerifyString(s string) (Token, error)
- type Token
Constants ¶
This section is empty.
Variables ¶
var ( // ErrFormat indicates that the serialized Token was malformed. ErrFormat = errors.New("format invalid") // ErrParse indicates an error in parsing a string token. ErrParse = errors.New("parsing") // ErrFieldData indicates that there was an error with the Data field. ErrFieldData = errors.New("data") // ErrFieldSignature indicates there was an error with the Signature field. ErrFieldSignature = errors.New("signature") // ErrFieldExpiry indicates there was an error with the Expiry field parsing. ErrFieldExpiry = errors.New("expiry") // ErrDecoding indicates that badly encoding data was given to parse. ErrDecoding = errors.New("decoding") // ErrVerifySignature indicates the signature did not match the data. ErrVerifySignature = errors.New("invalid signature") // ErrVerifyExpiry indicates that the Token has expired. ErrVerifyExpiry = errors.New("expired") // ErrCrypto indicates an error occurred in the underlying crypto library. ErrCrypto = errors.New("crypto") )
Functions ¶
func Decode ¶ added in v1.1.0
Decode decodes data using the same encoding scheme as Encode. This could be useful to create a new Mint with a secret that you've previously retrieved and encoded with Encode.
Types ¶
type Mint ¶
type Mint struct {
// contains filtered or unexported fields
}
Mint is used for constructing and verifying Tokens. The zero-value will generate a random secret the first time it needs to sign something, which then remains stable for its lifetime. Use NewMint to initialize the secret to something predictable.
func NewMint ¶
NewMint creates a Mint from the given secret. Any Mint instance with the same secret will product identical tokens if all parameters are the same.
func (*Mint) Generate ¶
Generate creates a signed Token whose Data is a cryptographically random byte slice of the length provided. The Token will expire at the given time.
func (*Mint) GetSecret ¶ added in v1.1.0
GetSecret returns a copy of the Mint's internal secret for creating tokens. If the secret was auto-generated (e.g. due to using a zero-value Mint), this is useful to retrieve that secret and save for later use.
func (*Mint) Sign ¶
Sign generates a valid Token which expires at the given time.
func (*Mint) SignValues ¶ added in v1.2.0
SignValues generates a Token whose Values property is set when no error occurs. Tokens created from the encoded value will also have the Value property set. Note that return token.Value property does not reference the passed url.Values, but is a deep copy.
func (*Mint) Verify ¶
Verify checks Token validity (both signature and expiry).
type Token ¶
Token is an unforgeable set of data + expiration + signature, which can be verified by the Mint which created it (or any with the same secret key).
func FromString ¶
FromString parses a serialization into a Token. A nil error indicates ONLY serialization success, NOT the validity of the Token. For that, use Mint.VerifyString.
func (Token) GetData ¶ added in v1.2.0
GetData returns the encoded Token.Values if non-nil, otherwise Token.Data.
Source Files
¶
- brief.go