Documentation
¶
Overview ¶
Package httpauth protects an HTTP resource and hands each request the caller's own Google token.
A Protector accepts two kinds of bearer token, either or both:
- Google access tokens a client holds for the person (Path A, for example from Gemini Enterprise), verified by a googletoken.Authenticator.
- steward's own access tokens (Path B), issued by an authserver.Server the service mounts, which carry the person's Google access token sealed inside, or name a workload that signed in with client credentials.
A request is routed by its token's shape (steward's tokens start with seal.Prefix; a Google token never does) to one of two purser httpmw.CallerMiddleware instances, each built with Enforce and no Fallback around its authenticator. A request with a good token reaches the handler as a purser.Caller. A missing, bad or unadmitted token is a 401 whose challenge names this resource's RFC 9728 metadata. A token that could not be checked (Google, the group directory or the sealing keys unavailable) is a 503 with Retry-After and no challenge, so the client retries with the token it has rather than signing in again. Behind the purser layer, a second layer attaches, for a person, the Google token as an oauth2.TokenSource (TokenSourceFromContext) and their googletoken.Identity (IdentityFromContext), the same on both paths, so the handler can call Google APIs and GKE clusters as that person; and, for a Path B token, the authserver.Principal (PrincipalFromContext), which is all a workload has.
The Protector also serves the RFC 9728 Protected Resource Metadata document, naming the authorization server a client that is not preconfigured should sign in with: the steward authorization server when there is one, else Google.
Index ¶
Constants ¶
const WellKnownPrefix = "/.well-known/oauth-protected-resource"
WellKnownPrefix is where RFC 9728 places protected resource metadata: the resource's path is appended to it.
Variables ¶
This section is empty.
Functions ¶
func IdentityFromContext ¶
func IdentityFromContext(ctx context.Context) (id googletoken.Identity, ok bool)
IdentityFromContext returns the verified googletoken.Identity of the person behind a request that passed a Protector, on either path. ok is false outside a protected request, and for a workload.
func PrincipalFromContext ¶ added in v0.2.0
func PrincipalFromContext(ctx context.Context) (p authserver.Principal, ok bool)
PrincipalFromContext returns the authserver.Principal behind a request that passed a Protector with a steward access token (Path B), person or workload. ok is false outside a protected request and for a Google token (Path A).
func TokenSourceFromContext ¶
func TokenSourceFromContext(ctx context.Context) (ts oauth2.TokenSource, ok bool)
TokenSourceFromContext returns the caller's own Google token as an oauth2.TokenSource, for a request from a person that passed a Protector: the token they presented (Path A), or the one sealed in steward's token (Path B). It does not refresh: the client holds the refresh token and sends a fresh access token on a later request. ok is false outside a protected request, and for a workload, which has no person's token (see PrincipalFromContext).
Not for tool handlers in a stateful MCP session: the MCP SDK runs every call with the context of the session's first request, so this would return the token from initialize. Use mcpauth.TokenSource(req) there.
Types ¶
type Options ¶
type Options struct {
// Authenticator verifies and admits Google access tokens (Path A).
// At least one of Authenticator and AuthServer is required.
Authenticator *googletoken.Authenticator
// AuthServer verifies steward's own access tokens (Path B). Its
// Resource must equal Resource. With an AuthServer, the metadata names
// it as the authorization server. Its allow-list (Config.Admit) and the
// Authenticator's are separate: give them the same entries, so the same
// people get in on either path.
AuthServer *authserver.Server
// Resource is the protected resource's absolute URL, exactly as clients
// reach it, for example "https://lookout.example.com/mcp". It is the
// "resource" in the metadata, and its path places the metadata
// document. https is required, except for a loopback host. Required.
Resource string
// Scopes are advertised as scopes_supported in the metadata and in the
// challenge: the scopes a client should request. With an AuthServer
// they must be among its scopes, and empty means its scopes in the
// metadata and its DefaultScopes in the challenge (MCP clients request
// what the challenge names, so configure the AuthServer's
// DefaultScopes to have them ask for less). For Path A alone they are
// Google scopes, such as "openid", "email" and
// "https://www.googleapis.com/auth/cloud-platform". They are advice to
// the client; RequiredScopes and the Authenticator's Verifier are what
// enforce scopes.
Scopes []string
// RequiredScopes are service scopes every steward access token must
// carry. A token without one is refused with 403 and
// error="insufficient_scope", which starts a client's step-up flow.
// Google access tokens (Path A) carry no service scopes and are not
// checked against these. Each must be one of the AuthServer's scopes.
RequiredScopes []string
// ResourceName is a human-readable name for the metadata. Optional.
ResourceName string
// Logger receives purser's log lines: the rejected-proxy-assertion
// audit line, a Warn for every 503 (a token that could not be checked:
// Google, the group directory or the sealing keys unavailable) and a
// Debug for every 401. Nil means slog.Default().
Logger *slog.Logger
}
Options configures a Protector.
type Protector ¶
type Protector struct {
// contains filtered or unexported fields
}
Protector protects a resource. It is immutable after New and safe for concurrent use.
func (*Protector) GatesCredentials ¶
GatesCredentials implements httpmw.Gate: every request that reaches the protected handler presented a verified token. It is what purser's CheckBind asks before a surface binds a non-loopback address.
func (*Protector) MetadataHandler ¶
MetadataHandler serves the RFC 9728 Protected Resource Metadata document. It needs no authentication: a client reads it before it has a token. Browser-based clients may read it cross-origin.
func (*Protector) MetadataPath ¶
MetadataPath is the path at which to mount MetadataHandler.
func (*Protector) MetadataURL ¶
MetadataURL is the metadata document's absolute URL, as named in the challenge.