Skip to content

Commit 205be63

Browse files
Fix creation OAuth2 auth source from CLI. (#14146)
Fix #8356
1 parent bf1441b commit 205be63

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

models/oauth2.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,18 @@ func InitOAuth2() error {
119119
if err := oauth2.Init(x); err != nil {
120120
return err
121121
}
122-
loginSources, _ := GetActiveOAuth2ProviderLoginSources()
122+
return initOAuth2LoginSources()
123+
}
123124

125+
// ResetOAuth2 clears existing OAuth2 providers and loads them from DB
126+
func ResetOAuth2() error {
127+
oauth2.ClearProviders()
128+
return initOAuth2LoginSources()
129+
}
130+
131+
// initOAuth2LoginSources is used to load and register all active OAuth2 providers
132+
func initOAuth2LoginSources() error {
133+
loginSources, _ := GetActiveOAuth2ProviderLoginSources()
124134
for _, source := range loginSources {
125135
oAuth2Config := source.OAuth2()
126136
err := oauth2.RegisterProvider(source.Name, oAuth2Config.Provider, oAuth2Config.ClientID, oAuth2Config.ClientSecret, oAuth2Config.OpenIDConnectAutoDiscoveryURL, oAuth2Config.CustomURLMapping)

modules/auth/oauth2/oauth2.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ func RemoveProvider(providerName string) {
118118
delete(goth.GetProviders(), providerName)
119119
}
120120

121+
// ClearProviders clears all OAuth2 providers from the goth lib
122+
func ClearProviders() {
123+
goth.ClearProviders()
124+
}
125+
121126
// used to create different types of goth providers
122127
func createProvider(providerName, providerType, clientID, clientSecret, openIDConnectAutoDiscoveryURL string, customURLMapping *CustomURLMapping) (goth.Provider, error) {
123128
callbackURL := setting.AppURL + "user/oauth2/" + url.PathEscape(providerName) + "/callback"

routers/user/auth.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,17 @@ func SignInOAuth(ctx *context.Context) {
570570
return
571571
}
572572

573-
err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp)
574-
if err != nil {
573+
if err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp); err != nil {
574+
if strings.Contains(err.Error(), "no provider for ") {
575+
if err = models.ResetOAuth2(); err != nil {
576+
ctx.ServerError("SignIn", err)
577+
return
578+
}
579+
if err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp); err != nil {
580+
ctx.ServerError("SignIn", err)
581+
}
582+
return
583+
}
575584
ctx.ServerError("SignIn", err)
576585
}
577586
// redirect is done in oauth2.Auth

0 commit comments

Comments
 (0)