39
39
errInvalidAtHash = errors .New ("access token hash does not match value in ID token" )
40
40
)
41
41
42
+ type contextKey int
43
+
44
+ var issuerURLKey contextKey
45
+
42
46
// ClientContext returns a new Context that carries the provided HTTP client.
43
47
//
44
48
// This method sets the same context key used by the golang.org/x/oauth2 package,
@@ -65,6 +69,25 @@ func cloneContext(ctx context.Context) context.Context {
65
69
return cp
66
70
}
67
71
72
+ // InsecureIssuerURLContext allows discovery to work when the issuer_url reported
73
+ // by upstream is mismatched with the discovery URL. This is meant for integration
74
+ // with off-spec providers such as Azure.
75
+ //
76
+ // discoveryBaseURL := "https://login.microsoftonline.com/organizations/v2.0"
77
+ // issuerURL := "https://login.microsoftonline.com/my-tenantid/v2.0"
78
+ //
79
+ // ctx := oidc.InsecureIssuerURLContext(parentContext, issuerURL)
80
+ //
81
+ // // Provider will be discovered with the discoveryBaseURL, but use issuerURL
82
+ // // for future issuer validation.
83
+ // provider, err := oidc.NewProvider(ctx, discoveryBaseURL)
84
+ //
85
+ // This is insecure because validating the correct issuer is critical for multi-tenant
86
+ // proivders. Any overrides here MUST be carefully reviewed.
87
+ func InsecureIssuerURLContext (ctx context.Context , issuerURL string ) context.Context {
88
+ return context .WithValue (ctx , issuerURLKey , issuerURL )
89
+ }
90
+
68
91
func doRequest (ctx context.Context , req * http.Request ) (* http.Response , error ) {
69
92
client := http .DefaultClient
70
93
if c , ok := ctx .Value (oauth2 .HTTPClient ).(* http.Client ); ok {
@@ -142,7 +165,11 @@ func NewProvider(ctx context.Context, issuer string) (*Provider, error) {
142
165
return nil , fmt .Errorf ("oidc: failed to decode provider discovery object: %v" , err )
143
166
}
144
167
145
- if p .Issuer != issuer {
168
+ issuerURL , skipIssuerValidation := ctx .Value (issuerURLKey ).(string )
169
+ if ! skipIssuerValidation {
170
+ issuerURL = issuer
171
+ }
172
+ if p .Issuer != issuerURL && ! skipIssuerValidation {
146
173
return nil , fmt .Errorf ("oidc: issuer did not match the issuer returned by provider, expected %q got %q" , issuer , p .Issuer )
147
174
}
148
175
var algs []string
@@ -152,7 +179,7 @@ func NewProvider(ctx context.Context, issuer string) (*Provider, error) {
152
179
}
153
180
}
154
181
return & Provider {
155
- issuer : p . Issuer ,
182
+ issuer : issuerURL ,
156
183
authURL : p .AuthURL ,
157
184
tokenURL : p .TokenURL ,
158
185
userInfoURL : p .UserInfoURL ,
0 commit comments