usertoken

package
v0.45.2 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: MIT Imports: 10 Imported by: 1 module, 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 administrator issued. The service keeps only the hash of the token, so the identity needs no external provider.

Index

Constants

View Source
const Prefix = "mtu_"

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

Variables

This section is empty.

Functions

func Hash

func Hash(token string) string

Hash is the lowercase hex SHA-256 of token.

func New

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

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

Types

type Record

type Record struct {
	Principal string
	Scope     string
	Repos     []string
	ExpiresAt time.Time
	RevokedAt time.Time
}

Record is a stored token: who holds it, what it allows, and when it ends. Repos is empty for the scope admin.

type Store

type Store interface {
	TokenByHash(ctx context.Context, hash string) (Record, error)
}

Store finds the record of a token hash. It returns an error for an unknown hash.

type Verifier

type Verifier struct {
	Store Store
	Now   func() time.Time
	Token func(*http.Request) string
}

Verifier reads the token that Token returns, finds its record by hash, and refuses a token that is unknown, revoked or expired.

func (Verifier) Lookup

func (v Verifier) Lookup(r *http.Request) (Record, error)

Lookup returns the record of the token of r, or an error for a token that is unknown, revoked, expired or of a scope that a person may not hold.

func (Verifier) Principal

func (v Verifier) Principal(rec Record) identity.Principal

Principal is the principal of rec. A token with no end lasts an hour from now.

func (Verifier) Verify

func (v Verifier) Verify(r *http.Request) (identity.Principal, error)

Verify gives the principal of the token.

Jump to

Keyboard shortcuts

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