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
- Variables
- type Grant
- type Option
- type Store
- type TokenInfo
- type TokenVerifier
- func (v *TokenVerifier) Epoch() int64
- func (v *TokenVerifier) Evict(id string)
- func (v *TokenVerifier) EvictAll()
- func (v *TokenVerifier) KnownID(id string) (bool, error)
- func (v *TokenVerifier) Revoke(id string) error
- func (v *TokenVerifier) RevokeAll() (int, error)
- func (v *TokenVerifier) RevokeAllInTx(tx *sql.Tx) error
- func (v *TokenVerifier) Verify(raw string) (TokenInfo, error)
- func (v *TokenVerifier) VerifyCached(raw string) (info TokenInfo, ok bool, err error)
Constants ¶
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 ¶
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 Option ¶
type Option func(*TokenVerifier)
Option configures a TokenVerifier.
func WithEpochInterval ¶
WithEpochInterval sets how often the tokens epoch is re-read.
func WithLogger ¶
WithLogger reports failed last_used_at writes.
func WithTouchInterval ¶
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.
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.