credential

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Overview

Request authorization: verify the bearer, then ask mcp-auth for a credential-access assertion so revocation applies on the next request.

the resource-server SDK (auth-client/go/mcpauth).

For each request: verify the bearer token (signature, issuer, audience, expiry, scopes), then ask mcp-auth for a credential-access assertion. mcp-auth re-checks the resource client, the subject token and that the login session is still active, so revocation takes effect on the next request. Nothing is cached across requests. The assertion proves approval only; it contains no credentials.

Package credential is the shared credential gateway. It runs from the same image as the per-server gateway, in its own mode, as one workload for every server that opts in: mcp-auth sends the browser to its sign-in page for a resource, it validates and keeps the person's header values in memory, and on later requests it verifies the OAuth bearer against mcp-auth and adds those headers before forwarding to the upstream.

The sign-in half of the gateway. mcp-auth treats it as a plain OAuth 2.0 identity provider for one resource: it sends the browser to /login/authorize, later exchanges the code at /login/token and reads an opaque subject from /login/userinfo.

The person's header values go from the browser to this service only. They are validated at the upstream, kept in the gateway's memory, and never reach mcp-auth, which sees nothing but the opaque subject.

Package session binds upstream MCP session ids to the authenticated user and service.

The gateway returns "<upstream-id>.<tag>" to the client, where tag is an HMAC over (issuer, subject, service, upstream-id) under a deployment secret. A later request is accepted only if the tag verifies for its own verified identity and service, so a session id cannot be replayed by another user or against another service. The binding is stateless: it survives restarts and works across replicas that share the key.

Index

Constants

View Source
const (
	// AssertionType is the RFC 8693 token type of a credential-access assertion.
	AssertionType = "urn:mcp-auth:token-type:credential-access-assertion"
	// AssertionJWTType is the JOSE typ of the assertion.
	AssertionJWTType = "credential-access+jwt"
)

Variables

View Source
var (
	ErrRejected    = errors.New("upstream credential validation failed")
	ErrUnavailable = errors.New("upstream validation unavailable")
)

ErrRejected means the upstream refused the credentials. ErrUnavailable means the upstream could not be asked. Neither carries upstream or credential text.

Functions

func LoginClientID

func LoginClientID(serviceID string) string

LoginClientID is the OAuth client id mcp-auth presents to a service's sign-in endpoints. The connector entry for the service must use it.

func NewSubject

func NewSubject() (string, error)

NewSubject returns an unguessable subject for one sign-in.

func Run

func Run(path string) error

Run serves the credential gateway described by the config file at path until the process is told to stop.

func SubjectPrefix

func SubjectPrefix(serviceID string) string

SubjectPrefix is what mcp-auth puts before the opaque subject the service's sign-in returns. The login method for the service must use it as its subject_prefix, because the gateway stores values under the prefixed subject.

Types

type Authorizer

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

Authorizer authorises requests for every configured service.

func NewAuthorizer

func NewAuthorizer(cfg *Config, metadataURL func(Service) string) *Authorizer

NewAuthorizer builds an Authorizer. metadataURL returns each service's protected-resource metadata URL.

func (*Authorizer) Authorize

func (a *Authorizer) Authorize(ctx context.Context, serviceID, authorization string) (Result, *Failure)

Authorize authorises one request for a service.

type Binder

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

Binder wraps and unwraps session ids.

func NewBinder

func NewBinder(key []byte) *Binder

NewBinder returns a Binder using key.

func (*Binder) Unwrap

func (b *Binder) Unwrap(issuer, subject, service, presented string) (string, bool)

Unwrap returns the upstream id when presented belongs to this identity and service, or "" and false.

func (*Binder) Wrap

func (b *Binder) Wrap(issuer, subject, service, upstreamID string) string

Wrap returns the id to give the client, or "" if the upstream id is unusable.

type Config

type Config struct {
	Listen                string
	Issuer                string
	TokenEndpoint         string
	JWKSURI               string
	AllowInsecureHTTP     bool
	ASTimeout             time.Duration
	PublicPathPrefix      string
	SessionKey            []byte
	FormTTL               time.Duration
	FormMaxEntries        int
	MaxRequestBytes       int64
	ConnectTimeout        time.Duration
	ResponseHeaderTimeout time.Duration
	Services              []Service
}

Config is the validated gateway configuration.

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig reads and validates the configuration file at path.

func ParseConfig

func ParseConfig(data []byte) (*Config, error)

ParseConfig validates raw JSON configuration.

type Failure

type Failure struct {
	Status  int
	Code    string
	Message string
	Headers map[string]string
}

Failure is an authorisation outcome the gateway turns into an HTTP response.

func (*Failure) Error

func (f *Failure) Error() string

type FormField

type FormField struct {
	Name, Label string
	Secret      bool
}

FormField is one sign-in input.

type Gateway

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

Gateway implements http.Handler for every configured service.

func New

func New(cfg *Config, store *Store, transport http.RoundTripper) *Gateway

New builds the gateway. transport may be nil; tests inject one.

func (*Gateway) ServeHTTP

func (g *Gateway) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type Header struct{ Name, Field string }

Header says which stored field is sent as which upstream header.

type Identity

type Identity struct{ Issuer, Subject string }

Identity is a verified (issuer, subject) pair.

type LoginConfig

type LoginConfig struct {
	// ClientID is the OAuth client id mcp-auth presents.
	ClientID string
	// Origin is this service's public origin, for the same-origin check.
	Origin string
	// BasePath is the public path /login/* lives under.
	BasePath string
	// CallbackURL is the only redirect_uri accepted: mcp-auth's identity callback.
	CallbackURL string
	// Fields are the inputs shown, in order.
	Fields []FormField
	// ServerName, Namespace and UpstreamHost are shown on the page so the
	// person can see which server receives the values. They come from the
	// operator-derived configuration.
	ServerName, Namespace, UpstreamHost string
	// Authenticate validates submitted values at the upstream and returns the
	// opaque subject. It does not store them. It reports ErrRejected or ErrUnavailable.
	Authenticate func(ctx context.Context, values map[string]string) (string, error)
	// Commit stores values for subject. The page calls it only after this
	// submission has won the pending request.
	Commit func(subject string, values map[string]string) error
	Now    func() time.Time
}

LoginConfig describes one service's sign-in.

type LoginProvider

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

LoginProvider serves the sign-in endpoints for one service.

func NewLoginProvider

func NewLoginProvider(cfg LoginConfig) *LoginProvider

NewLoginProvider builds a provider.

func (*LoginProvider) ServeHTTP

func (p *LoginProvider) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP expects the request path relative to BasePath: /login/<endpoint>.

type ResourceClient

type ResourceClient struct {
	ClientID   string
	KeyID      string
	PrivateKey *rsa.PrivateKey
}

ResourceClient is the gateway's identity at mcp-auth for one service.

type Result

type Result struct {
	Identity      Identity
	SessionExpiry time.Time
}

Result is a successful authorisation.

type Service

type Service struct {
	ID             string
	ServerName     string
	Namespace      string
	ResourceURL    string
	ResourcePath   string
	UpstreamURL    string
	ValidationURL  string
	RequiredScopes []string
	Client         ResourceClient
	FormFields     []FormField
	Headers        []Header
}

Service is one proxied MCP backend.

type Store

type Store struct {
	TTL time.Duration
	Max int
	Now func() time.Time
	// contains filtered or unexported fields
}

Store keeps the header values a person submitted at sign-in. Values live only in process memory, are bounded in count and lifetime, and are never logged. An entry is reachable only by the exact (issuer, subject, service) it was stored under, and the subject is a random value minted per sign-in.

func (*Store) Delete

func (s *Store) Delete(issuer, subject, service string)

Delete forgets the values for an identity and service.

func (*Store) Get

func (s *Store) Get(issuer, subject, service string) (map[string]string, bool)

Get returns a copy of the live values for the identity and service.

func (*Store) Len

func (s *Store) Len() int

Len reports the number of stored entries, expired ones included until swept.

func (*Store) Put

func (s *Store) Put(issuer, subject, service string, values map[string]string)

Put stores a copy of values, replacing any earlier entry for the identity.

Jump to

Keyboard shortcuts

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