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
- Variables
- func CORS(origins []string, next http.Handler) http.Handler
- func LoopbackOrigin(origin string) bool
- type Authenticator
- type Config
- type Provider
- func (p *Provider) Close()
- func (p *Provider) IsSecretClient(clientID string) bool
- func (p *Provider) Issuer() string
- func (p *Provider) RequireBearer(next http.Handler) http.Handler
- func (p *Provider) RevokeClient(clientID string) int
- func (p *Provider) Routes(mux *http.ServeMux)
- func (p *Provider) VerifyAccessToken(token string) (*TokenInfo, error)
- type TokenInfo
Constants ¶
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 ¶
var ErrInvalidToken = errors.New("invalid or expired token")
ErrInvalidToken is returned for unknown or expired access tokens.
var Scopes = []string{"vault.read", "vault.write", "vault.delete"}
Scopes advertised in metadata and shown on the consent screen.
Functions ¶
func CORS ¶
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
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 ¶
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 ¶
NewProvider validates the configuration and builds the provider.
func (*Provider) IsSecretClient ¶ added in v1.13.0
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) RequireBearer ¶
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
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.