apitoken

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 16 Imported by: 0 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 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

View Source
const FileName = "api-tokens.json"

FileName is the token store in the state directory.

View Source
const Prefix = "iss_"

Prefix starts every token, so a scanner (and a person) can tell what it is.

Variables

View Source
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

func DefaultPath(stateDir string) string

DefaultPath is api-tokens.json in stateDir.

func ParseTTL

func ParseTTL(s string) (time.Duration, error)

ParseTTL reads an expiry such as 30d, 12h or 90m; "" and "never" are zero.

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

func ParseScopes(s string) ([]Scope, error)

ParseScopes reads a comma-separated scope list.

func Scopes

func Scopes() []Scope

Scopes lists every scope, in order.

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

func (s *Store) Authenticate(presented string) (Token, error)

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

func (s *Store) Create(name string, scopes []Scope, ttl time.Duration) (Token, string, error)

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.

func (*Store) List

func (s *Store) List() ([]Token, error)

List returns every token, newest first.

func (*Store) Revoke

func (s *Store) Revoke(nameOrID string) (Token, error)

Revoke marks the active token with this name or ID revoked. The record is kept, so the list still shows what existed and when it was turned off.

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.

func (Token) Has

func (t Token) Has(scope Scope) bool

Has reports whether the token carries scope (admin carries every scope).

func (Token) State

func (t Token) State(now time.Time) string

State is how a token stands at a moment.

Jump to

Keyboard shortcuts

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