Documentation
¶
Overview ¶
Package usertoken authenticates a request by a per-user token that an operator issued. ward keeps only the hash of the token, so the identity needs no external provider.
Index ¶
Constants ¶
const Prefix = "wdu_"
Prefix starts every token, so that a credential of another kind never reaches the store.
const ScopeLand = "land"
ScopeLand is the only scope of a per-user token.
Variables ¶
var ( ErrNotFound = errors.New("usertoken: not found") ErrExists = errors.New("usertoken: exists") )
ErrNotFound is the answer of a store for an unknown hash or ID. ErrExists is the answer for a repeated ID or hash.
Functions ¶
func Credential ¶
Credential is the token of the header Authorization: a Bearer token, or the password of a Basic credential.
Types ¶
type Authenticator ¶
Authenticator finds the record of the credential by hash, and refuses a token that is unknown, revoked or expired.
func (Authenticator) Authenticate ¶
type Record ¶
type Record struct {
ID, Principal, Scope string
Repos []string
CreatedAt, ExpiresAt time.Time
RevokedAt time.Time
}
Record is a per-user token. The store keeps its hash and never the token. ExpiresAt and RevokedAt are zero when the token has no expiry or is not revoked.
type Store ¶
type Store interface {
// CreateUserToken stores r under hash. ErrExists when r.ID or hash exists.
CreateUserToken(ctx context.Context, hash string, r Record) error
// UserTokenByHash returns the record under hash, revoked or not, or ErrNotFound.
UserTokenByHash(ctx context.Context, hash string) (Record, error)
// UserTokens lists every record, newest first, ties by ID descending.
UserTokens(ctx context.Context) ([]Record, error)
// RevokeUserToken sets RevokedAt of id to at once. A repeat keeps the first time and is not an error.
// ErrNotFound for an unknown id.
RevokeUserToken(ctx context.Context, id string, at time.Time) error
}
Store keeps per-user tokens. hash is the lowercase hex SHA-256 of the token text.