oidctest

package
v0.313.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 14 Imported by: 0 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 oidctest is an in-memory OpenID Connect provider for tests: an httptest server with discovery, the token endpoint (authorization code with PKCE, refresh with rotation), userinfo, JWKS and revocation, signing id_tokens with ES256. Its knobs (Tamper) make each id_token check fail on its own, so a test can show a refusal comes from the check it names.

Index

Constants

View Source
const (
	ClientID     = "muster-test-client"
	ClientSecret = "muster-test-client-secret-0123456789"
)

ClientID and ClientSecret are the one client the fake knows.

Variables

This section is empty.

Functions

func SignES256

func SignES256(key *ecdsa.PrivateKey, header, claims map[string]any) string

SignES256 signs a JWT with key: the fake's id_tokens, and a test's own hand-made ones.

Types

type Fake

type Fake struct {
	Server *httptest.Server
	// contains filtered or unexported fields
}

Fake is the provider.

func New

func New(t testing.TB) *Fake

New starts a fake provider; it is closed when the test ends.

func (*Fake) AddUser

func (f *Fake) AddUser(u User)

AddUser adds (or replaces) an account.

func (*Fake) Advance

func (f *Fake) Advance(d time.Duration)

Advance moves the clock.

func (*Fake) Authorize

func (f *Fake) Authorize(t testing.TB, authURL, sub string) url.Values

Authorize plays the browser at the authorization endpoint: it reads the authorization request muster redirected to (authURL), signs sub in, and returns the callback's query (code and state) as the provider would send the browser back with. It fails the test on a request muster should never make (wrong client, no PKCE S256, another redirect URI).

func (*Fake) Ban

func (f *Fake) Ban(sub string)

Ban marks an account banned (userinfo refuses it).

func (*Fake) Calls

func (f *Fake) Calls(path string) int

Calls is how many requests path has had.

func (*Fake) FailNext

func (f *Fake) FailNext(path string, n int)

FailNext answers the next n requests to path (e.g. "/token") with 503.

func (*Fake) Issuer

func (f *Fake) Issuer() string

Issuer is the fake's issuer (its base URL).

func (*Fake) LiveRefreshTokens

func (f *Fake) LiveRefreshTokens(sub string) int

LiveRefreshTokens is how many refresh tokens of sub the provider would still accept.

func (*Fake) Now

func (f *Fake) Now() time.Time

Now is the fake's clock; SetNow and Advance move it. The provider under test must share it (oidc.Config.Now) for exp and iat to line up.

func (*Fake) RevokeRefreshTokens

func (f *Fake) RevokeRefreshTokens(sub string)

RevokeRefreshTokens makes every refresh token of sub invalid_grant.

func (*Fake) Revoked

func (f *Fake) Revoked() []string

Revoked is the refresh tokens revoked so far.

func (*Fake) SetClock

func (f *Fake) SetClock(clock func() time.Time)

SetClock makes the fake read its time from clock (another fake's, so the two agree); Advance then has no effect.

func (*Fake) SetRoles

func (f *Fake) SetRoles(sub string, roles ...string)

SetRoles replaces an account's roles.

func (*Fake) SetTamper

func (f *Fake) SetTamper(t Tamper)

SetTamper sets what the next id_tokens look like (Tamper{} for honest ones).

type Tamper

type Tamper struct {
	Issuer     string        // "" = the fake's own issuer
	Audience   []string      // nil = the client id
	Azp        string        // "" = none
	ExpIn      time.Duration // 0 = 5 minutes; negative = already expired
	IatIn      time.Duration // 0 = now; positive = in the future
	Nonce      *string       // nil = the authorization request's nonce
	Subject    *string       // nil = the account's sub
	OtherKey   bool          // sign with a key the JWKS does not publish (same kid)
	Alg        string        // "" = ES256 (the header only; the signature stays ES256)
	NoIDToken  bool          // answer the code with no id_token at all
	AuthTime   *time.Time    // nil = now
	NoRefresh  bool          // answer the code with no refresh token
	UserinfoAs string        // "" = the token's own account; else userinfo answers for this sub
}

Tamper changes what the NEXT id_tokens look like. The zero value is an honest token.

type User

type User struct {
	Sub           string
	Email         string
	EmailVerified bool
	Username      string
	Roles         []string
	// Banned makes userinfo refuse the account's tokens (401), as a provider
	// does for a banned or deleted account.
	Banned bool
}

User is a provider account.

Jump to

Keyboard shortcuts

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