Help us shape the future of Go! Take the 2026 Go Developer Survey

identity

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package identity makes the server an OAuth resource server: it verifies the access tokens of MCP requests, serves the protected resource metadata that tells clients where to get them, and carries the verified caller to the code that serves the requests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MetadataHandler

func MetadataHandler(a *config.Auth) http.Handler

MetadataHandler serves the protected resource metadata (RFC 9728) for a.

func MetadataPath

func MetadataPath(resource string) string

MetadataPath returns the path of the protected resource metadata for resource, which the WWW-Authenticate challenge points to.

func RequireToken

func RequireToken(a *config.Auth, logger *slog.Logger, next http.Handler) http.Handler

RequireToken returns a handler that serves a request with next only if it carries a valid access token from a's issuer that has each of a's required scopes in its scope or scp claim. A missing or invalid token gets a 401, and a valid token without a required scope a 403. Both get a WWW-Authenticate challenge that points to the protected resource metadata and lists the advertised scopes, with error="invalid_token" for an invalid token and error="insufficient_scope" for a missing scope. A token that cannot be checked yet gets a 500 with no challenge: when the issuer's discovery or signing keys are unavailable, or when the token's key is unknown and the request skipped fetching the keys because they were last fetched less than a minute before it, since the key may be newer than that fetch. When the request skipped fetching, an expired token gets a 401 instead, whether that last fetch succeeded or failed. Response bodies are fixed ("no bearer token", "invalid token", "insufficient scope", "cannot verify access tokens"); the reason for a rejected token or a 500 goes to logger.

func WithCaller

func WithCaller(ctx context.Context, caller *Caller) context.Context

WithCaller returns a context that carries caller.

Types

type Caller

type Caller struct {
	Issuer  string
	Subject string
	// Claims are the verified claims of the caller's access token.
	Claims map[string]any
}

Caller is the verified identity behind a request, keyed by Issuer and Subject.

func CallerFrom

func CallerFrom(ctx context.Context) *Caller

CallerFrom returns the caller that ctx carries, or nil if it carries none, as when auth is not configured.

func CallerFromTokenInfo

func CallerFromTokenInfo(ti *auth.TokenInfo) *Caller

CallerFromTokenInfo returns the caller for ti, which must come from RequireToken.

func (*Caller) Scopes added in v0.0.3

func (c *Caller) Scopes(claim string) []string

Scopes returns the scopes in the caller's claim named claim, such as scope or scp, read as a space-separated string or an array of strings.

Jump to

Keyboard shortcuts

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