tokens

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tokens manages the tokens of accounts: API keys with their own scopes, pad pattern and expiry. A token never has more rights than its account: what it may do is the account's rights, cut down to its scopes and pattern, evaluated on every use (accounts.Grants.Restrict). Only a SHA-256 hash of a key is stored; the key is shown once.

Index

Constants

View Source
const (
	// KeyPrefix starts every key, so leaked keys are easy to find.
	KeyPrefix = "ep_"

	// MaxName is the longest token name, in characters.
	MaxName = 100
)
View Source
const LegacyAccount = "legacy-api"

LegacyAccount is the service account that holds the API keys of before the tokens: the keys of the former admin page "API keys" and of an APIKEY.txt. Like those keys, it may do everything.

Variables

View Source
var (
	ErrNoName         = errors.New("a name is required")
	ErrNameTooLong    = errors.New("the name is too long")
	ErrUnknownScope   = errors.New("unknown scope")
	ErrNoScopes       = errors.New("a token needs at least one scope")
	ErrForbidden      = errors.New("you may not manage tokens of this account")
	ErrEscalation     = errors.New("you cannot give a token rights you do not have")
	ErrNoAccount      = errors.New("the account does not exist or is disabled")
	ErrExpiryRequired = errors.New("tokens must expire on this server")
	ErrExpiryTooLate  = errors.New("the expiry is later than this server allows")
	ErrExpiryPast     = errors.New("the expiry is in the past")
	ErrNotActive      = errors.New("the token is revoked or expired")
	ErrInvalidLimit   = errors.New("a limit cannot be negative")
	ErrInvalidPattern = errors.New("invalid pad pattern")
	ErrLimitTooHigh   = errors.New("you cannot give a token limits above your own")
	ErrGraceTooLong   = errors.New("a replaced key cannot work longer than it would have, nor for ever")
	ErrTokenNotFound  = db.ErrTokenNotFound
	// ErrInvalid is any failure to use a key; the reason is not told.
	ErrInvalid = errors.New("invalid token")
)

Functions

func Hash

func Hash(key string) string

Hash is the stored form of a key (the same as the former API keys', so their keys keep working as tokens).

Types

type LegacyStore

type LegacyStore interface {
	Store
	db.APIKeyMethods
}

LegacyStore is what the take-over needs: the former keys and the tokens.

type Manager

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

Manager issues, rotates, revokes and checks tokens.

func For

func For(store db.DataStore, cfg *settings.Settings) *Manager

For returns the manager of a data store, made once.

func NewManager

func NewManager(store Store, acc *accounts.Manager, policy func() settings.Tokens) *Manager

NewManager returns a manager on store, with the accounts' manager and the expiry policy (read on every change, so a changed setting counts).

func (*Manager) Authenticate

func (m *Manager) Authenticate(key, ip string) (*Use, error)

Authenticate checks a key and records its use (at most every minute, with the IP). It fails with ErrInvalid for an unknown, revoked or expired key and for one whose account is gone or disabled. The rights are the account's of now, cut down to the token's.

func (*Manager) ImportKey

func (m *Manager) ImportKey(key, name string) (bool, error)

ImportKey stores an existing key (from an APIKEY.txt) as a token of the legacy-api account, unless it is one already. It reports whether it stored it.

func (*Manager) ImportKeyOnce

func (m *Manager) ImportKeyOnce(flag, name string, read func() (string, error)) (OnceResult, error)

ImportKeyOnce takes over the key read returns as a token named name, if the instance flag is not set yet, and sets it. The check, the take-over and the flag run under the accounts' write lock, which every instance of a cluster shares: of instances starting at once, only the first takes a key over; the others find the flag. A failure sets no flag, so the next start tries again. read is called only while the flag is not set.

func (*Manager) Issue

func (m *Manager) Issue(actor accounts.Actor, spec Spec) (key string, tok *db.Token, err error)

Issue makes a token and returns its key, which cannot be recovered later.

func (*Manager) List

func (m *Manager) List(actor accounts.Actor, accountID string) ([]db.Token, error)

List returns the tokens of an account ("" for all), for an actor who may see them: its own with tokens:own, all with admin:apikeys.

func (*Manager) Revoke

func (m *Manager) Revoke(actor accounts.Actor, id string) error

Revoke ends a token at once.

func (*Manager) Rotate

func (m *Manager) Rotate(actor accounts.Actor, id string, oldUntil *time.Time) (key string, tok *db.Token, err error)

Rotate makes a new key with the same rights. The old one keeps working until oldUntil (nil or past: not at all from now), at most as long as it would have. The new key lives as long as the old one was issued for, within the policy. Under the accounts' write lock, so two instances rotating the same key make one new key, not two.

func (*Manager) SetExpiry

func (m *Manager) SetExpiry(actor accounts.Actor, id string, expires *time.Time) (tok *db.Token, err error)

SetExpiry changes when an active token stops working (nil: never), e.g. to shorten or lengthen the grace period of a rotated key, within the policy. A replaced key never works longer than it would have before its rotation, and never for ever.

func (*Manager) Store

func (m *Manager) Store() db.DataStore

Store returns the data store of the tokens (for tests and tools).

func (*Manager) TakeOverAPIKeys

func (m *Manager) TakeOverAPIKeys(store LegacyStore) (int, error)

TakeOverAPIKeys makes every former API key a token of the legacy-api account with all scopes, never expiring, under the same hash: the keys keep working. A disabled key becomes a revoked token. It runs on every start and takes over only keys that are no token yet, so an interrupted take-over is finished and a revoked token is not brought back. The former keys stay in their table. It returns how many it took over.

type OnceResult

type OnceResult int

OnceResult is what ImportKeyOnce did.

const (
	// OnceNothing: no key, the step is now marked as done.
	OnceNothing OnceResult = iota
	// OnceStored: the key is now a token.
	OnceStored
	// OnceKnown: the key already was a token.
	OnceKnown
	// OnceDone: the step was done before (maybe by another instance a
	// moment ago); nothing was taken over.
	OnceDone
)

type Spec

type Spec struct {
	AccountID string
	Name      string
	// Scopes the token may use; accounts.AllScopes for all of them.
	Scopes []string
	// PadPattern limits it to matching pads; "" for all.
	PadPattern string
	// ExpiresAt nil: never (where the policy allows).
	ExpiresAt *time.Time
	Limits    map[string]int64
}

Spec is a token to issue: for which account, what it may do, how long.

type Store

type Store interface {
	db.TokenMethods
}

Store is what the manager needs of the database.

type Use

type Use struct {
	Token   *db.Token
	Account *db.Account
	Grants  accounts.Grants
}

Use is a checked key: the token, its account and what it may do.

Jump to

Keyboard shortcuts

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