@@ -244,10 +244,34 @@ func (p *ProviderConfig) NewProvider(ctx context.Context) *Provider {
244244 }
245245}
246246
247+ // IssuerMismatchError is returned by [NewProvider] when the "iss" value
248+ // reported by the upstream is different than the expected value.
249+ //
250+ // Issuer mismatches can occur due to trailing slashes ("https://example.com"
251+ // vs. "https://example.com/") or represent significant misconfiguration for
252+ // multi-tenant issuers.
253+ //
254+ // Issuers must match exactly as they are also used to validate ID Tokens.
255+ //
256+ // https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata
257+ type IssuerMismatchError struct {
258+ // The value provided to this package. The expected value.
259+ Provided string
260+ // The value advertised by the discovery document.
261+ Discovered string
262+ }
263+
264+ func (e * IssuerMismatchError ) Error () string {
265+ return fmt .Sprintf ("oidc: issuer URL provided to client (%q) did not match the issuer URL returned by provider (%q)" , e .Provided , e .Discovered )
266+ }
267+
247268// NewProvider uses the OpenID Connect discovery mechanism to construct a Provider.
248269// The issuer is the URL identifier for the service. For example: "https://accounts.google.com"
249270// or "https://login.salesforce.com".
250271//
272+ // If the "iss" value returned in the discovery document doesn't match the value
273+ // provided here, [IssuerMismatchError] is returned.
274+ //
251275// OpenID Connect providers that don't implement discovery or host the discovery
252276// document at a non-spec compliant path (such as requiring a URL parameter),
253277// should use [ProviderConfig] instead.
@@ -285,7 +309,10 @@ func NewProvider(ctx context.Context, issuer string) (*Provider, error) {
285309 issuerURL = issuer
286310 }
287311 if p .Issuer != issuerURL && ! skipIssuerValidation {
288- return nil , fmt .Errorf ("oidc: issuer URL provided to client (%q) did not match the issuer URL returned by provider (%q)" , issuer , p .Issuer )
312+ return nil , & IssuerMismatchError {
313+ Provided : issuerURL ,
314+ Discovered : p .Issuer ,
315+ }
289316 }
290317 var algs []string
291318 for _ , a := range p .Algorithms {
0 commit comments