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
- Variables
- func Hash(key string) string
- type LegacyStore
- type Manager
- func (m *Manager) Authenticate(key, ip string) (*Use, error)
- func (m *Manager) ImportKey(key, name string) (bool, error)
- func (m *Manager) ImportKeyOnce(flag, name string, read func() (string, error)) (OnceResult, error)
- func (m *Manager) Issue(actor accounts.Actor, spec Spec) (key string, tok *db.Token, err error)
- func (m *Manager) List(actor accounts.Actor, accountID string) ([]db.Token, error)
- func (m *Manager) Revoke(actor accounts.Actor, id string) error
- func (m *Manager) Rotate(actor accounts.Actor, id string, oldUntil *time.Time) (key string, tok *db.Token, err error)
- func (m *Manager) SetExpiry(actor accounts.Actor, id string, expires *time.Time) (tok *db.Token, err error)
- func (m *Manager) Store() db.DataStore
- func (m *Manager) TakeOverAPIKeys(store LegacyStore) (int, error)
- type OnceResult
- type Spec
- type Store
- type Use
Constants ¶
const ( // KeyPrefix starts every key, so leaked keys are easy to find. KeyPrefix = "ep_" // MaxName is the longest token name, in characters. MaxName = 100 )
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 ¶
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 ¶
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 NewManager ¶
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 ¶
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 ¶
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 ¶
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) List ¶
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) 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) 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.