auth

package
v1.13.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 18 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 auth implements vellum's OAuth 2.1 authorization server and the bearer check that guards /mcp and /api.

The model: vellum is both the authorization server and the resource server. The owner holds one pre-configured confidential client (VELLUM_CLIENT_ID/VELLUM_CLIENT_SECRET); clients such as Claude may also register themselves (RFC 7591) as public clients without a secret. A public client proves nothing at /token, so approving it on the consent screen requires the owner's VELLUM_CLIENT_SECRET. Authorization-code flow with mandatory PKCE (S256), opaque in-memory tokens. No user database, no external identity provider, no persistence: tokens die with the process, clients silently re-authorize.

Index

Constants

View Source
const (
	AccessTokenTTL  = time.Hour
	AuthCodeTTL     = 10 * time.Minute
	RefreshTokenTTL = 30 * 24 * time.Hour
)

Lifetimes recycled from openclaw-mcp (production-proven), except the refresh token, extended to 30 days so a client re-authorizes far less often.

Variables

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

ErrInvalidToken is returned for unknown or expired access tokens.

View Source
var Scopes = []string{"vault.read", "vault.write", "vault.delete"}

Scopes advertised in metadata and shown on the consent screen.

Functions

func CORS

func CORS(origins []string, next http.Handler) http.Handler

CORS returns a middleware applying CORS headers for the allowed browser origins (CORS_ORIGINS) plus loopback origins. OPTIONS preflights short-circuit with 204.

func LoopbackOrigin added in v1.7.0

func LoopbackOrigin(origin string) bool

LoopbackOrigin reports whether a browser Origin points at the client's own machine (localhost, 127.0.0.1 or [::1], any port). Such origins are always CORS-allowed: the page behind them already runs on the user's machine (MCP Inspector, local dev tools), the browser sets the Origin header so a remote site cannot fake one, and authentication is still required for anything the response would reveal.

Types

type Authenticator

type Authenticator interface {
	VerifyAccessToken(token string) (*TokenInfo, error)
}

Authenticator verifies bearer tokens. It is an interface so a future team mode (identity→role mapping in vellum.yaml, PHY-119) can slot in.

type Config

type Config struct {
	Enabled      bool     // AUTH_ENABLED
	ClientID     string   // VELLUM_CLIENT_ID (default "vellum")
	ClientSecret string   // VELLUM_CLIENT_SECRET (>= 32 chars)
	IssuerURL    string   // VELLUM_ISSUER_URL — public HTTPS URL behind a proxy
	RedirectURIs []string // VELLUM_REDIRECT_URIS — empty allows any non-empty URI
	TrustProxy   bool     // TRUST_PROXY — derive client IP from X-Forwarded-For
}

Config is the auth configuration (env names per PHY-112).

type Provider

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

Provider is the single-client OAuth 2.1 provider. All state is in memory; lazy expiry on every lookup plus a 5-minute reaper sweep.

func NewProvider

func NewProvider(cfg Config) (*Provider, error)

NewProvider validates the configuration and builds the provider.

func (*Provider) Close

func (p *Provider) Close()

Close stops the background reaper.

func (*Provider) IsSecretClient added in v1.13.0

func (p *Provider) IsSecretClient(clientID string) bool

IsSecretClient reports whether clientID is the configured confidential client. Whoever uses it holds VELLUM_CLIENT_SECRET and can get a new token at any time, so revoking its tokens would not disconnect it.

func (*Provider) Issuer

func (p *Provider) Issuer() string

Issuer returns the normalized issuer URL (no trailing slash).

func (*Provider) RequireBearer

func (p *Provider) RequireBearer(next http.Handler) http.Handler

RequireBearer guards a handler with bearer token verification. 401s carry a WWW-Authenticate challenge including the resource_metadata hint so MCP clients can discover the authorization server.

func (*Provider) RevokeClient added in v1.13.0

func (p *Provider) RevokeClient(clientID string) int

RevokeClient drops every access token, refresh token and pending authorization code of a client and reports how many tokens it dropped. The registration stays: the client can come back only through the consent screen, which asks for the client secret.

func (*Provider) Routes

func (p *Provider) Routes(mux *http.ServeMux)

Routes registers the OAuth endpoints on mux. Call only when auth is enabled — without auth none of these routes exist (matching openclaw-mcp).

func (*Provider) VerifyAccessToken

func (p *Provider) VerifyAccessToken(token string) (*TokenInfo, error)

VerifyAccessToken implements Authenticator.

type TokenInfo

type TokenInfo struct {
	Token     string
	ClientID  string
	Scopes    []string
	ExpiresAt time.Time
}

TokenInfo is the server-side record of an access token.

func TokenFromContext added in v1.13.0

func TokenFromContext(ctx context.Context) (*TokenInfo, bool)

TokenFromContext returns the access token RequireBearer verified for the request, if any.

Jump to

Keyboard shortcuts

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