agentauth

package
v1.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: GPL-3.0 Imports: 11 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 agentauth verifies agent API tokens (pwk_<id>_<secret>) for the MCP endpoint (docs/design/mcp.md §7, ADR-0010).

Verified tokens are cached in memory by id so a request normally costs no database read. The cache is kept honest three ways: Revoke/RevokeAll/Evict drop entries immediately in this process; the tokens_epoch setting (bumped in the same transaction as every revocation or grant change) is re-read at most every epoch interval and clears the cache when it moved, which covers changes made by other processes; and every cached entry still checks its own expiry on each use. A request that cannot read the epoch fails, and requests served from the cache while another one reads it are refused once the last successful read is more than three intervals old, so a failing or hanging database never keeps revoked tokens working.

Index

Constants

View Source
const (
	// DefaultEpochInterval is how often the tokens epoch is re-read.
	DefaultEpochInterval = 5 * time.Second
	// DefaultTouchInterval throttles last_used_at writes per token.
	DefaultTouchInterval = time.Minute
)

Variables

View Source
var ErrInvalidToken = errors.New("invalid agent token")

ErrInvalidToken is returned for malformed, unknown, wrong, expired or revoked tokens. Any other error from Verify is an internal failure.

Functions

This section is empty.

Types

type Grant

type Grant = storage.TokenGrant

Grant is a token's permission to call one tool.

type Option

type Option func(*TokenVerifier)

Option configures a TokenVerifier.

func WithClock

func WithClock(now func() time.Time) Option

WithClock sets the time source (tests).

func WithEpochInterval

func WithEpochInterval(d time.Duration) Option

WithEpochInterval sets how often the tokens epoch is re-read.

func WithLogger

func WithLogger(l zerolog.Logger) Option

WithLogger reports failed last_used_at writes.

func WithTouchInterval

func WithTouchInterval(d time.Duration) Option

WithTouchInterval sets the minimum time between last_used_at writes.

type Store

type Store interface {
	GetAPIToken(id string) (*storage.APIToken, error)
	ListAPITokens() ([]storage.APIToken, error)
	TokensEpoch() (int64, error)
	TouchAPITokenLastUsed(id string, now time.Time) error
	RevokeAPIToken(id string, now time.Time) error
	RevokeAllAPITokens(now time.Time) (int, error)
}

Store is the token persistence the verifier needs (*storage.SQLiteStore).

type TokenInfo

type TokenInfo struct {
	ID         string
	Name       string
	Scopes     []string
	Privileged bool
	Grants     []Grant
	AllTools   bool
	ExpiresAt  time.Time
}

TokenInfo is what a verified token may do. Slices are copies owned by the caller.

func (TokenInfo) HasScope

func (t TokenInfo) HasScope(scope string) bool

HasScope reports whether the token carries scope.

type TokenVerifier

type TokenVerifier struct {
	// contains filtered or unexported fields
}

TokenVerifier checks raw tokens against the store, with a cache.

func NewTokenVerifier

func NewTokenVerifier(store Store, opts ...Option) *TokenVerifier

NewTokenVerifier returns a verifier backed by store.

func (*TokenVerifier) Epoch

func (v *TokenVerifier) Epoch() int64

Epoch returns the tokens epoch as last read by Verify (0 before the first read). It changes, at most one epoch interval late, after every revocation, grant change or agent deploy level change made by any process, so callers can key caches built from verified tokens on it.

func (*TokenVerifier) Evict

func (v *TokenVerifier) Evict(id string)

Evict drops one cached token, e.g. after its grants changed.

func (*TokenVerifier) EvictAll

func (v *TokenVerifier) EvictAll()

EvictAll drops every cached token.

func (*TokenVerifier) KnownID

func (v *TokenVerifier) KnownID(id string) (bool, error)

KnownID reports whether id names a token that was active (neither revoked nor expired) when the verifier last listed the tokens. It reads no token, so callers that limit lookups of unknown ids per client address can tell a real token, which a client sharing the address must never be able to lock out, from made-up ones. The list is read on the first call and again when asked about an id it does not hold once an epoch interval has passed since the last read: a token created since is known within an interval, and made-up ids cost at most one store read an interval, however many are sent. Ids that stopped being active stay known until the next read (a lookup then fails as for any bad token). An error is a store failure.

func (*TokenVerifier) Revoke

func (v *TokenVerifier) Revoke(id string) error

Revoke revokes a token in the store (bumping the epoch for other processes) and evicts it here, so it stops working immediately.

func (*TokenVerifier) RevokeAll

func (v *TokenVerifier) RevokeAll() (int, error)

RevokeAll revokes every token and clears the cache. It returns how many tokens were still active.

func (*TokenVerifier) RevokeAllInTx

func (v *TokenVerifier) RevokeAllInTx(tx *sql.Tx) error

RevokeAllInTx revokes every token (and bumps the epoch) inside tx, a transaction on the database that holds the tokens, typically the one that resets the admin password. Call EvictAll once tx committed.

func (*TokenVerifier) Verify

func (v *TokenVerifier) Verify(raw string) (TokenInfo, error)

Verify checks a raw pwk_ token and returns what it may do. It returns ErrInvalidToken for every credential problem and a different error when the store cannot be read (the caller should answer 503, not 401).

func (*TokenVerifier) VerifyCached

func (v *TokenVerifier) VerifyCached(raw string) (info TokenInfo, ok bool, err error)

VerifyCached is Verify answered from the cache alone: ok is true only for a cached token with this secret that is still valid. Anything else (a token never verified, evicted or expired, another secret, a malformed one) is a miss, with ok false and no store access, so callers may use it for clients they limit for failed lookups. A cached token still follows revocations: the tokens epoch is re-read when due, at most once per interval across all callers (an error then is a store failure, not a miss), and last_used_at is recorded as by Verify.

Jump to

Keyboard shortcuts

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