capability

package
v1.102.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package capability mints and verifies capability tokens: a token that is, for a few minutes, the right to one thing a service names in a grant of its own type, and nothing else.

A service hands one out as a link, so that a file reaches the machine that wants it without passing through whatever asked for it -- a model would have to repeat it byte for byte. The token is all the link's holder needs, which is why it says exactly what it grants and runs out soon.

A token is the grant and its expiry as JSON, base64url-encoded, then a dot and an HMAC-SHA256 of that, keyed with the service's key and prefixed with its domain, so that two services' tokens are never mistaken for each other's even should they ever share a key.

Index

Constants

View Source
const DefaultTtl = 10 * time.Minute

DefaultTtl is how long a token lasts: long enough to be used at once, short enough that one found later in a log or a transcript grants nothing.

View Source
const MinKeyBytes = 32

MinKeyBytes is the shortest key a signer takes.

Variables

View Source
var (
	// ErrMalformed is a token that is not one a signer could have made.
	ErrMalformed = errors.New("malformed token")
	// ErrBadSignature is a token whose signature is not the key's, in the signer's domain.
	ErrBadSignature = errors.New("bad signature")
	// ErrExpired is a token whose time has run out.
	ErrExpired = errors.New("expired token")
)

Functions

This section is empty.

Types

type Signer

type Signer[T any] struct {
	// contains filtered or unexported fields
}

Signer mints and verifies tokens of grants of type T, with one key in one domain.

func NewSigner

func NewSigner[T any](key []byte, domain string, ttl time.Duration, now func() time.Time) (*Signer[T], error)

NewSigner is a signer of the key, which is to be random and at least MinKeyBytes long, in the domain, which names the service and the use ("vvvp-google/download/v1"). A ttl of zero is DefaultTtl; a nil now is time.Now.

func (*Signer[T]) Mint

func (signer *Signer[T]) Mint(grant *T) (string, time.Time, error)

Mint is a token of the grant, which runs out the signer's TTL from now, and when that is. The grant is the caller's to have checked: the signer signs what it is given.

func (*Signer[T]) Verify

func (signer *Signer[T]) Verify(token string) (*T, error)

Verify is the grant of a token this signer's key minted in its domain and that has not run out. What the grant must hold is the caller's to check.

Jump to

Keyboard shortcuts

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