oidctest

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: AGPL-3.0 Imports: 20 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 provides a fake OpenID Connect identity provider for tests.

A Server serves OIDC discovery and a JWKS endpoint over TLS and signs arbitrary claims with RSA, ECDSA P-256 and Ed25519 keys. Tests can rotate keys, inject failures, delay responses and count requests, so the token verifier can be exercised without a real IdP. The package depends on the standard library only.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

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

Server is a fake OpenID Connect identity provider. All methods are safe for concurrent use.

func New

func New(t testing.TB) *Server

New starts a fake IdP for the duration of the test. It registers Close with t.Cleanup and creates one active RS256 key with 2048 bits.

func (*Server) AddKey

func (s *Server) AddKey(alg string) string

AddKey generates a new signing key and publishes it. The algorithm is RS256 (RSA with 2048 bits), ES256 (NIST P-256) or EdDSA (Ed25519). The new key becomes the active signing key. AddKey returns the key ID. It panics on an unsupported algorithm.

func (*Server) AddRSAKey

func (s *Server) AddRSAKey(bits int) string

AddRSAKey generates an RS256 key with the given modulus size, for weak-key tests. The new key is published and becomes the active signing key. It returns the key ID.

func (*Server) Claims

func (s *Server) Claims(sub string, ttl time.Duration, now time.Time) map[string]any

Claims returns a baseline claim set for subject sub, valid from now for ttl: iss is Issuer(), aud is ["project-1", "spa-client"], azp is "spa-client", iat and nbf are now, exp is now+ttl (all as Unix seconds), and jti is random. Tests may mutate the returned map.

func (*Server) Client

func (s *Server) Client() *http.Client

Client returns an HTTP client that trusts the server's TLS certificate.

func (*Server) Close

func (s *Server) Close()

Close shuts the server down. New registers it with t.Cleanup, so calling it is only needed to stop the server early.

func (*Server) DiscoveryRequests

func (s *Server) DiscoveryRequests() int

DiscoveryRequests returns the number of requests received at the discovery endpoint. Requests are counted when they arrive, including those answered with an injected status or dropped by SetDown.

func (*Server) Issuer

func (s *Server) Issuer() string

Issuer returns the server's base URL (https, without trailing slash).

func (*Server) JWKSRequests

func (s *Server) JWKSRequests() int

JWKSRequests returns the number of requests received at the JWKS endpoint. Requests are counted when they arrive, including those answered with an injected status or dropped by SetDown.

func (*Server) SetActive

func (s *Server) SetActive(kid string)

SetActive selects the key that Sign, SignNone and SignHS256WithPublicKey use by default. It panics on an unknown kid.

func (*Server) SetDelay

func (s *Server) SetDelay(d time.Duration)

SetDelay delays every response by d. Zero disables the delay.

func (*Server) SetDown

func (s *Server) SetDown(down bool)

SetDown makes every endpoint close the connection without a response, as if the IdP were unreachable.

func (*Server) SetIssuerOverride

func (s *Server) SetIssuerOverride(iss string)

SetIssuerOverride makes discovery report iss as its issuer instead of Issuer(), to test the exact-issuer check. An empty string restores the default. The jwks_uri and the other endpoints keep pointing at the real server.

func (*Server) SetJWKSMaxAge

func (s *Server) SetJWKSMaxAge(d time.Duration)

SetJWKSMaxAge sets the max-age of the Cache-Control header on JWKS responses. The default is 300 seconds; the value is truncated to whole seconds.

func (*Server) SetPublishAlg

func (s *Server) SetPublishAlg(on bool)

SetPublishAlg controls whether published JWKs carry the alg member. The default is true.

func (*Server) SetStatus

func (s *Server) SetStatus(code int)

SetStatus makes the discovery and JWKS endpoints answer with code. Zero restores normal responses.

func (*Server) Sign

func (s *Server) Sign(claims map[string]any) string

Sign signs claims with the active key and returns a compact JWS. The header is {"alg": <key alg>, "kid": <kid>, "typ": "JWT"}.

func (*Server) SignHS256WithPublicKey

func (s *Server) SignHS256WithPublicKey(kid string, claims map[string]any) string

SignHS256WithPublicKey returns a token with header {"alg": "HS256", "kid": kid, "typ": "JWT"}. The HMAC-SHA256 secret is the DER encoding of the public key of kid (x509.MarshalPKIXPublicKey), which is the classic algorithm confusion attack. An empty kid selects the active key. It panics on an unknown kid.

func (*Server) SignNone

func (s *Server) SignNone(claims map[string]any) string

SignNone returns an unsigned token with header {"alg": "none", "kid": <active kid>, "typ": "JWT"} and an empty signature. It is used for attack tests.

func (*Server) SignWith

func (s *Server) SignWith(kid string, header map[string]any, claims map[string]any) string

SignWith signs claims with the key identified by kid, or with the active key if kid is empty. The fields of header are merged over the default header; a nil value removes the field. The signature is always made with the key's own algorithm, so a header alg that differs from it yields a token whose declared algorithm does not match its signature. It panics on an unknown kid.

func (*Server) Unpublish

func (s *Server) Unpublish(kid string)

Unpublish removes the key from the JWKS. The key stays usable for signing through SignWith. It panics on an unknown kid.

Jump to

Keyboard shortcuts

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