Documentation
¶
Overview ¶
Package apitoken issues and checks the REST API's bearer tokens.
A token is shown once, when it is created. Only its SHA-256 is stored, in api-tokens.json in the state directory (mode 0600), never in the global config, which is meant to be shareable. A presented token is hashed and compared with every stored hash in constant time; a match that has expired or been revoked is refused, and each accepted use is recorded.
Index ¶
Constants ¶
const FileName = "api-tokens.json"
FileName is the token store in the state directory.
const Prefix = "iss_"
Prefix starts every token, so a scanner (and a person) can tell what it is.
Variables ¶
var ( ErrUnknown = errors.New("not a valid API token") ErrExpired = errors.New("this API token has expired") ErrRevoked = errors.New("this API token was revoked") )
Errors Authenticate returns. Each is a refusal; only the API's message differs.
Functions ¶
func DefaultPath ¶
DefaultPath is api-tokens.json in stateDir.
Types ¶
type Scope ¶
type Scope string
Scope is what a token may do.
const ( ScopeRead Scope = "read" ScopeSync Scope = "sync" ScopeConfigWrite Scope = "config:write" ScopeAdmin Scope = "admin" )
The scopes. Admin grants every other scope as well.
func ParseScopes ¶
ParseScopes reads a comma-separated scope list.
type Store ¶
type Store struct {
Path string
// Now is the clock; nil means time.Now.
Now func() time.Time
// contains filtered or unexported fields
}
Store is the token file.
func (*Store) Authenticate ¶
Authenticate checks a presented token. Every stored hash is compared, in constant time, whatever matches first, so the time taken does not say how many tokens exist or which one nearly matched.
func (*Store) Create ¶
Create makes a token and returns it with its secret, which is never stored and cannot be shown again. ttl zero means it never expires.
type Token ¶
type Token struct {
ID string `json:"id"`
Name string `json:"name"`
Scopes []Scope `json:"scopes"`
Hash string `json:"sha256"`
Created time.Time `json:"created"`
Expires time.Time `json:"expires,omitzero"`
LastUsed time.Time `json:"last_used,omitzero"`
Revoked time.Time `json:"revoked,omitzero"`
}
Token is one stored token: everything but the secret.