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 ¶
- type Server
- func (s *Server) AddKey(alg string) string
- func (s *Server) AddRSAKey(bits int) string
- func (s *Server) Claims(sub string, ttl time.Duration, now time.Time) map[string]any
- func (s *Server) Client() *http.Client
- func (s *Server) Close()
- func (s *Server) DiscoveryRequests() int
- func (s *Server) Issuer() string
- func (s *Server) JWKSRequests() int
- func (s *Server) SetActive(kid string)
- func (s *Server) SetDelay(d time.Duration)
- func (s *Server) SetDown(down bool)
- func (s *Server) SetIssuerOverride(iss string)
- func (s *Server) SetJWKSMaxAge(d time.Duration)
- func (s *Server) SetPublishAlg(on bool)
- func (s *Server) SetStatus(code int)
- func (s *Server) Sign(claims map[string]any) string
- func (s *Server) SignHS256WithPublicKey(kid string, claims map[string]any) string
- func (s *Server) SignNone(claims map[string]any) string
- func (s *Server) SignWith(kid string, header map[string]any, claims map[string]any) string
- func (s *Server) Unpublish(kid string)
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 ¶
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 ¶
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 ¶
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 ¶
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) 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 ¶
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) JWKSRequests ¶
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 ¶
SetActive selects the key that Sign, SignNone and SignHS256WithPublicKey use by default. It panics on an unknown kid.
func (*Server) SetDown ¶
SetDown makes every endpoint close the connection without a response, as if the IdP were unreachable.
func (*Server) SetIssuerOverride ¶
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 ¶
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 ¶
SetPublishAlg controls whether published JWKs carry the alg member. The default is true.
func (*Server) SetStatus ¶
SetStatus makes the discovery and JWKS endpoints answer with code. Zero restores normal responses.
func (*Server) Sign ¶
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 ¶
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 ¶
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 ¶
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.