oauthtest

package
v1.26.2 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: 23 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package oauthtest runs a fake OpenID Provider for the tests of servers that use mcp-oauth, shaped like Dex on a private management cluster: discovery and the JWKS under /dex, served over TLS with a certificate of its own on a hostname that resolves to an RFC 1918 address of this machine. mcp-oauth reaches it as it reaches a real Dex, through its guarded dialers, so a test exercises the production validation path: signature, issuer, audience, expiry and the private-address rules.

A test using New cannot be parallel: New calls t.Setenv, and it replaces net.DefaultResolver for the rest of the test so that IssuerHost and KeysHost resolve.

Index

Constants

View Source
const (
	// IssuerHost is the hostname of the issuer.
	IssuerHost = "idp.test"
	// KeysHost is the hostname of the JWKS with [WithJWKSOnKeysHost].
	KeysHost = "keys.test"
	// ClientID is the client the environment of [IdP.Env] signs in with
	// and trusts forwarded ID tokens for.
	ClientID = "oauthtest-client"
	// Subject, Email, Name and Group are the claims a minted token carries
	// unless the caller overrides them.
	Subject = "oauthtest-subject"
	Email   = "person@idp.test"
	Name    = "Test Person"
	Group   = "oauthtest-group"
)

Variables

This section is empty.

Functions

func Settings

func Settings(t testing.TB, env map[string]string) oauthconfig.Settings

Settings sets env for the rest of t, with every other inherited OAUTH_* variable unset, and returns what oauthconfig.SettingsFromEnv reads from it.

Types

type IdP

type IdP struct {
	// Issuer is https://idp.test:<port>/dex.
	Issuer string
	// JWKSURI is the jwks_uri discovery advertises: on IssuerHost, or on
	// KeysHost with WithJWKSOnKeysHost.
	JWKSURI string
	// CAFile is the PEM file of the self-signed certificate the provider
	// serves, which is its own CA.
	CAFile string
	// contains filtered or unexported fields
}

IdP is a running fake OpenID Provider.

func New

func New(t testing.TB, opts ...Option) *IdP

New starts a provider that stops when t ends. It skips t when the machine has no RFC 1918 IPv4 address, since mcp-oauth never fetches a JWKS from loopback, or when the process proxy setting, which net/http reads once, routes the test hosts through a proxy.

func (*IdP) AccessToken

func (p *IdP) AccessToken(t testing.TB, audience []string, claims map[string]any) string

AccessToken is IdP.IDToken with the RFC 9068 typ at+jwt, the type a trusted issuer entry accepts by default.

func (*IdP) Env

func (p *IdP) Env() map[string]string

Env is the OAUTH_* environment of a server that signs people in with this provider as Dex and accepts its ID tokens forwarded for ClientID. Change the map before passing it to Settings, for example to set OAUTH_ALLOW_PRIVATE_IP_JWKS_HOSTS or OAUTH_TRUSTED_ISSUERS.

func (*IdP) IDToken

func (p *IdP) IDToken(t testing.TB, audience []string, claims map[string]any) string

IDToken mints an ID token (typ JWT) for audience, valid for 30 minutes, for Subject with the Email, Name and Group profile. claims are added last and replace any of these, registered claims included.

func (*IdP) TrustedIssuers

func (p *IdP) TrustedIssuers(t testing.TB, audiences []string, more map[string]any) string

TrustedIssuers is an OAUTH_TRUSTED_ISSUERS value with one entry for this provider: its issuer, JWKS URI and CA, and audiences. The keys of more, such as openIDProvider, acceptedTypHeaders or allowPrivateIPJWKSHosts, are added last and replace any of these.

type Option

type Option func(*options)

Option changes how New serves the provider.

func WithJWKSOnKeysHost

func WithJWKSOnKeysHost() Option

WithJWKSOnKeysHost advertises the JWKS on KeysHost, which resolves to the same private address as the issuer: a private JWKS host other than the issuer's.

Jump to

Keyboard shortcuts

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