mcpauthtest

package
v0.7.19-dev.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package mcpauthtest is a fake vendor for tests of OAuth connections: a remote MCP server that answers 401 with its resource metadata, and the authorization server behind it (metadata, consent, the token endpoint with refresh-token rotation, dynamic registration, revocation), on one TLS httptest server. Browser plays the person's browser.

Index

Constants

View Source
const GitHubClientID = "Iv1.fixtureclient"

GitHubClientID is the fake's client id.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthorizationServer added in v0.7.16

type AuthorizationServer struct {
	*httptest.Server

	// ClientID is the client it knows; ClientSecret, when set, its secret.
	ClientID     string
	ClientSecret string
	// NoProfile leaves the ID-JAG grant profile out of its metadata, and
	// NoJWTBearer the JWT bearer grant type.
	NoProfile, NoJWTBearer bool
	// Trusts is the identity provider whose ID-JAGs it takes.
	Trusts *IdentityProvider
	// RefreshToken issues a refresh token beside the access token.
	RefreshToken bool
	// Raw, when set, is the whole answer's body to a trusted assertion,
	// sent as is.
	Raw []byte

	// Requests is every token-endpoint form.
	Requests []url.Values
	// Issued is every access token it issued.
	Issued []string
	// contains filtered or unexported fields
}

AuthorizationServer is a fake MCP authorization server that takes an ID-JAG by the JWT bearer grant (RFC 7523) and answers with an access token. It trusts the ID-JAGs of one IdentityProvider and knows one client.

func NewAuthorizationServer added in v0.7.16

func NewAuthorizationServer(t *testing.T, idp *IdentityProvider, clientID string) *AuthorizationServer

NewAuthorizationServer starts the fake, trusting idp's ID-JAGs.

func (*AuthorizationServer) ResourceURL added in v0.7.16

func (a *AuthorizationServer) ResourceURL() string

ResourceURL is the MCP server this authorization server issues tokens for.

type ChatGPT

type ChatGPT struct {
	*httptest.Server

	// Subject and Email are the account the ID token names.
	Subject, Email string
	// NoPlanScope leaves chatgpt.tokens.use.direct out of the granted scopes.
	NoPlanScope bool
	// NotEligible answers the model list with OpenAI's 403.
	NotEligible bool
	// Refused refuses every refresh with RefuseCode (invalid_grant when "").
	Refused    bool
	RefuseCode string
	// IssuedClient is the client id a registration issues.
	IssuedClient string
	// AccessPrefix and RefreshPrefix start every token issued, so a test can
	// search for them.
	AccessPrefix, RefreshPrefix string
	// RevocationURL, when set, is the revocation endpoint the discovery
	// document names instead of the fake's own; NoExpiry leaves exp out
	// of the ID token.
	RevocationURL string
	NoExpiry      bool

	// Authorizations is every authorization request's query.
	Authorizations []url.Values
	// Requests is every token-endpoint form; Revocations every revocation's.
	Requests, Revocations []url.Values
	// Issued is every refresh token issued; ModelCalls every model list's
	// Authorization header.
	Issued, ModelCalls []string
	// contains filtered or unexported fields
}

ChatGPT is a fake of OpenAI's "Sign in with ChatGPT" plan usage, as its pages describe it (pkg/mcpauth/chatgpt.go): the first authorization with dynamic_agent_client registers a client and the redirect carries its id; the token endpoint exchanges codes and rotates refresh tokens for that client alone; the discovery document names the revocation endpoint; and /v1/models lists the plan's models, or answers 403 subscription_sharing_user_not_eligible. Fields set before the first request shape it.

func NewChatGPT

func NewChatGPT(t *testing.T) *ChatGPT

NewChatGPT starts the fake.

func (*ChatGPT) Flow

func (v *ChatGPT) Flow() mcpauth.ChatGPT

Flow is a mcpauth.ChatGPT pointed at the fake, with a browser that approves.

func (*ChatGPT) Live

func (v *ChatGPT) Live(rt string) bool

Live reports whether rt is a refresh token the fake would still honour.

type ES256Signer added in v0.7.16

type ES256Signer struct {
	Key *ecdsa.PrivateKey
	Kid string
	// Signed is every signing input it signed.
	Signed [][]byte
	// contains filtered or unexported fields
}

ES256Signer signs client assertions with an in-memory P-256 key: the test stand-in for a caller's key service.

func NewES256Signer added in v0.7.16

func NewES256Signer(t *testing.T) *ES256Signer

NewES256Signer makes a signer with a fresh key.

func (*ES256Signer) Algorithm added in v0.7.16

func (s *ES256Signer) Algorithm() string

Algorithm is ES256.

func (*ES256Signer) KeyID added in v0.7.16

func (s *ES256Signer) KeyID() string

KeyID is the key's id.

func (*ES256Signer) Sign added in v0.7.16

func (s *ES256Signer) Sign(_ context.Context, input []byte) ([]byte, error)

Sign signs input, answering the JWS signature (R and S, 32 bytes each).

type GitHub

type GitHub struct {
	*httptest.Server

	// Login is the account /user names.
	Login string
	// Pending is how many polls answer authorization_pending first;
	// SlowDown answers slow_down once before them.
	Pending  int
	SlowDown bool
	// Declined answers access_denied; Expired expired_token.
	Declined, Expired bool
	// NoExpiry issues tokens without expiry or refresh token, as an OAuth
	// App whose tokens do not expire does.
	NoExpiry bool
	// AccessPrefix and RefreshPrefix start every token issued (ghu_, ghr_
	// by default), so a test can search for them.
	AccessPrefix, RefreshPrefix string

	// DeviceRequests is every device-code request's form; TokenRequests
	// every token-endpoint form.
	DeviceRequests, TokenRequests []url.Values
	// Revoked is every credential revoked; RevokeAuth the Authorization
	// header any revocation carried.
	Revoked    []string
	RevokeAuth []string
	// UserCalls is every /user call's Authorization header.
	UserCalls []string
	// Waits are the waits between polls the flow asked for.
	Waits []time.Duration
	// contains filtered or unexported fields
}

GitHub is a fake of GitHub's device flow and the endpoints a Copilot sign-in uses (pkg/mcpauth/github.go): the device code, the token endpoint's polling and its secret-free refresh (which rotates the refresh token and spends the old one), /user, and the unauthenticated credential revocation. Fields set before the first request shape it.

func NewGitHub

func NewGitHub(t *testing.T) *GitHub

NewGitHub starts the fake.

func (*GitHub) Flow

func (v *GitHub) Flow() mcpauth.GitHub

Flow is a mcpauth.GitHub pointed at the fake, polling without waiting (each wait it would make is kept in Waits).

func (*GitHub) Issue

func (v *GitHub) Issue(client string) (access, refresh string)

Issue grants a pair outside the device flow, as a sign-in kept before.

func (*GitHub) Live

func (v *GitHub) Live(rt string) bool

Live reports whether rt is a refresh token the fake would still honour, and LiveAccess whether at is an access token it still accepts.

func (*GitHub) LiveAccess

func (v *GitHub) LiveAccess(at string) bool

LiveAccess reports whether at is an access token the fake accepts.

type HuggingFace

type HuggingFace struct {

	// AppID is a public app's client id the fake also takes.
	AppID string
	// Username is the account userinfo names.
	Username string
	// NoInference grants the scopes asked but inference-api; NoRefresh
	// issues no refresh token; Rotate rotates refresh tokens; Refused
	// refuses every refresh; Revocation names a revocation endpoint;
	// NoCIMD serves no metadata document.
	NoInference, NoRefresh, Rotate, Refused, Revocation, NoCIMD bool
	// ForeignToken names a token endpoint on another host.
	ForeignToken bool
	// Requests is every token request's form; Revocations every revocation's.
	Requests, Revocations []url.Values
	// contains filtered or unexported fields
}

HuggingFace is a fake of Hugging Face's OAuth: discovery (with no revocation endpoint unless Revocation is set), authorization for a client metadata document it serves or a registered public client, tokens with refresh (rotated when Rotate is set), and userinfo.

func NewHuggingFace

func NewHuggingFace(t *testing.T) *HuggingFace

NewHuggingFace starts the fake.

func (*HuggingFace) CIMDURL

func (v *HuggingFace) CIMDURL() string

CIMDURL is the client metadata document the fake serves.

func (*HuggingFace) Flow

func (v *HuggingFace) Flow() mcpauth.HuggingFace

Flow is a mcpauth.HuggingFace pointed at the fake.

func (HuggingFace) Guard

func (k HuggingFace) Guard() mcpauth.Guard

Guard is an egress guard that trusts the fake.

func (HuggingFace) Handed

func (k HuggingFace) Handed(s string) bool

Handed reports whether s was issued by the fake.

func (*HuggingFace) Live

func (v *HuggingFace) Live(rt string) bool

Live reports whether rt is a refresh token the fake would still honour.

type IdentityProvider added in v0.7.16

type IdentityProvider struct {
	*httptest.Server

	// ClientID is the requesting app; ClientSecret, when set, is its secret;
	// PublicKey, when set, verifies its private_key_jwt client assertions.
	ClientID     string
	ClientSecret string
	PublicKey    *ecdsa.PublicKey
	// Allow is the admin's policy: whether an ID-JAG is issued for this
	// audience and resource. Nil allows everything.
	Allow func(audience, resource string) bool
	// IssuedTokenType, TokenType and Assertion override the answer's
	// fields, for tests of a provider that answers wrongly.
	IssuedTokenType, TokenType, Assertion string
	// Raw, when set, is the whole answer's body, sent as is.
	Raw []byte
	// EchoClientAssertion refuses a client it cannot authenticate with a
	// description that repeats the client assertion, as a careless provider
	// might.
	EchoClientAssertion bool

	// Requests is every token-endpoint form.
	Requests []url.Values
	// Issued is every ID-JAG it issued.
	Issued []string
	// contains filtered or unexported fields
}

IdentityProvider is a fake enterprise identity provider that issues Identity Assertion JWT Authorization Grants (ID-JAGs) by RFC 8693 token exchange: its metadata, and a token endpoint that takes the exchange from one requesting app, by client secret or by private_key_jwt with ES256. Fields set before the first request shape it.

func NewIdentityProvider added in v0.7.16

func NewIdentityProvider(t *testing.T, clientID string) *IdentityProvider

NewIdentityProvider starts the fake with a requesting app of the given id.

func (*IdentityProvider) Guard added in v0.7.16

func (p *IdentityProvider) Guard() mcpauth.Guard

Guard trusts the fake's certificate and its loopback address. Every httptest TLS server presents the same certificate, so this guard also reaches an AuthorizationServer started beside it.

func (*IdentityProvider) Handed added in v0.7.16

func (p *IdentityProvider) Handed(s string) bool

Handed reports whether s is an ID-JAG this provider issued.

func (*IdentityProvider) SignIn added in v0.7.16

func (p *IdentityProvider) SignIn(subject string) string

SignIn records an ID token the provider issued to a person, as their sign-in at the provider would, and returns it.

type OpenRouter

type OpenRouter struct {

	// Deny refuses the authorization in the browser.
	Deny bool
	// KeyPrefix starts every key issued (sk-or-v1-PROBE- by default).
	KeyPrefix string
	// Exchanges is every key request's body.
	Exchanges []map[string]string
	// contains filtered or unexported fields
}

OpenRouter is a fake of OpenRouter's PKCE key flow: /auth sends the browser back to the callback with a code and the state, and /api/v1/auth/keys trades the code and its verifier for an sk-or- key.

func NewOpenRouter

func NewOpenRouter(t *testing.T) *OpenRouter

NewOpenRouter starts the fake.

func (*OpenRouter) Flow

func (v *OpenRouter) Flow() mcpauth.OpenRouter

Flow is a mcpauth.OpenRouter pointed at the fake.

func (OpenRouter) Guard

func (k OpenRouter) Guard() mcpauth.Guard

Guard is an egress guard that trusts the fake.

func (OpenRouter) Handed

func (k OpenRouter) Handed(s string) bool

Handed reports whether s was issued by the fake.

type Poe

type Poe struct {
	ClientID string
	// ExpiresIn is the key's lifetime in seconds; 0 sends null (no expiry).
	ExpiresIn int
	Email     string
	// Deny refuses the authorization in the browser.
	Deny bool
	// Requests is every token request's form.
	Requests []url.Values
	// contains filtered or unexported fields
}

Poe is a fake of Sign in with Poe: /oauth/authorize for one client and the apikey:create scope, /token minting a key with ExpiresIn, and the balance route that names the account.

func NewPoe

func NewPoe(t *testing.T) *Poe

NewPoe starts the fake.

func (*Poe) Flow

func (v *Poe) Flow() mcpauth.Poe

Flow is a mcpauth.Poe pointed at the fake.

func (Poe) Guard

func (k Poe) Guard() mcpauth.Guard

Guard is an egress guard that trusts the fake.

func (Poe) Handed

func (k Poe) Handed(s string) bool

Handed reports whether s was issued by the fake.

type Vendor

type Vendor struct {
	*httptest.Server

	// DCR serves dynamic registration; CIMD advertises client metadata
	// documents; NoISS leaves the iss parameter off the redirect.
	DCR, CIMD, NoISS bool
	// RefreshPrefix starts every refresh token issued, so a test can
	// search for it; ClientSecret is what a registration issues when
	// the server takes no public client.
	RefreshPrefix string
	ClientSecret  string
	// NoPublic leaves "none" out of the token endpoint's auth methods.
	NoPublic bool
	// Scope, when set, is the WWW-Authenticate scope.
	Scope string
	// IssuerOnly is a sign-in at the issuer for a package server:
	// the authorization request must carry no resource.
	IssuerOnly bool
	// SecretAnyway issues ClientSecret to every registration, one that
	// asked to be public included, and names no auth method in the answer
	// (a vendor that makes every client confidential).
	SecretAnyway bool
	// WholeGrant revokes, with any one refresh token, every refresh token
	// issued to the same client, as Google ends an account's whole grant to
	// a client.
	WholeGrant bool

	Revoked  []string
	Requests []url.Values // every token-endpoint form
	Refused  bool         // refuse every refresh with invalid_grant
	Issued   []string     // every refresh token issued
	// Registrations is every registration request's body.
	Registrations []map[string]any
	// contains filtered or unexported fields
}

Vendor is the fake. Fields set before the first request shape it.

func New

func New(t *testing.T) *Vendor

New starts a vendor.

func (*Vendor) AddClient

func (v *Vendor) AddClient(id, secret string)

AddClient registers a client the person made themselves.

func (*Vendor) Browser

func (v *Vendor) Browser() func(authURL string)

Browser plays the person: it opens the consent address and, approving, follows the vendor's redirect to wherever it points (the loopback).

func (*Vendor) Guard

func (v *Vendor) Guard() mcpauth.Guard

Guard trusts the fake's certificate and its loopback address.

func (*Vendor) Live

func (v *Vendor) Live(rt string) bool

Live is whether a refresh token is still good at the vendor.

func (*Vendor) Redirect

func (v *Vendor) Redirect(authURL string) (string, error)

Redirect is the address the vendor would send the browser to for authURL, without following it: what a person pastes back.

func (*Vendor) ServerURL

func (v *Vendor) ServerURL() string

ServerURL is the fake MCP server's address.

Jump to

Keyboard shortcuts

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