usertoken

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 12 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 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

View Source
const Prefix = "wdu_"

Prefix starts every token, so that a credential of another kind never reaches the store.

View Source
const ScopeLand = "land"

ScopeLand is the only scope of a per-user token.

Variables

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

func Credential(r *http.Request) string

Credential is the token of the header Authorization: a Bearer token, or the password of a Basic credential.

func Hash

func Hash(tok string) string

Hash is the lowercase hex SHA-256 of tok.

func New

func New() (tok, id, hash string, err error)

New returns a token text, the ID of its record and the hash that the store keeps.

func Valid

func Valid(scope string, repos []string) bool

Valid reports whether a record may exist: the scope land on at least one repository.

Types

type Authenticator

type Authenticator struct {
	Store Store
	Now   func() time.Time
}

Authenticator finds the record of the credential by hash, and refuses a token that is unknown, revoked or expired.

func (Authenticator) Authenticate

func (a Authenticator) Authenticate(r *http.Request, repo string) (token.Claims, error)

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.

Jump to

Keyboard shortcuts

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