@@ -77,6 +77,13 @@ type OAuth2Configs struct {
77
77
OIDC OAuth2Config
78
78
}
79
79
80
+ func (c * OAuth2Configs ) IsZero () bool {
81
+ if c == nil {
82
+ return true
83
+ }
84
+ return c .Github == nil && c .OIDC == nil
85
+ }
86
+
80
87
const (
81
88
SignedOutErrorMessage = "You are signed out or your session has expired. Please sign in again to continue."
82
89
internalErrorMessage = "An internal error occurred. Please try again or contact the system administrator."
@@ -237,13 +244,14 @@ func ExtractAPIKey(rw http.ResponseWriter, r *http.Request, cfg ExtractAPIKeyCon
237
244
}
238
245
// Check if the OAuth token is expired
239
246
if link .OAuthExpiry .Before (now ) && ! link .OAuthExpiry .IsZero () && link .OAuthRefreshToken != "" {
240
- if cfg .OAuth2Configs == nil {
247
+ if cfg .OAuth2Configs . IsZero () {
241
248
return write (http .StatusInternalServerError , codersdk.Response {
242
249
Message : internalErrorMessage ,
243
250
Detail : fmt .Sprintf ("Unable to refresh OAuth token for login type %q. " +
244
251
"No OAuth2Configs provided. Contact an administrator to configure this login type." , key .LoginType ),
245
252
})
246
253
}
254
+
247
255
var oauthConfig OAuth2Config
248
256
switch key .LoginType {
249
257
case database .LoginTypeGithub :
@@ -256,6 +264,19 @@ func ExtractAPIKey(rw http.ResponseWriter, r *http.Request, cfg ExtractAPIKeyCon
256
264
Detail : fmt .Sprintf ("Unexpected authentication type %q." , key .LoginType ),
257
265
})
258
266
}
267
+
268
+ // It's possible for cfg.OAuth2Configs to be non-nil, but still
269
+ // missing this type. For example, if a user logged in with GitHub,
270
+ // but the administrator later removed GitHub and replaced it with
271
+ // OIDC.
272
+ if oauthConfig == nil {
273
+ return write (http .StatusInternalServerError , codersdk.Response {
274
+ Message : internalErrorMessage ,
275
+ Detail : fmt .Sprintf ("Unable to refresh OAuth token for login type %q. " +
276
+ "OAuth2Config not provided. Contact an administrator to configure this login type." , key .LoginType ),
277
+ })
278
+ }
279
+
259
280
// If it is, let's refresh it from the provided config
260
281
token , err := oauthConfig .TokenSource (r .Context (), & oauth2.Token {
261
282
AccessToken : link .OAuthAccessToken ,
0 commit comments