Skip to content

Commit 5a94db3

Browse files
daniil-pankratovDaniil Pankratov
and
Daniil Pankratov
authored
Fix creating OAuth2 auth source from CLI (#14116)
Fix creation OAuth2 auth source from CLI. Fix #8356 Co-authored-by: Daniil Pankratov <[email protected]>
1 parent bdeccc3 commit 5a94db3

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

models/oauth2.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,18 @@ func InitOAuth2() error {
125125
if err := oauth2.Init(x); err != nil {
126126
return err
127127
}
128-
loginSources, _ := GetActiveOAuth2ProviderLoginSources()
128+
return initOAuth2LoginSources()
129+
}
129130

131+
// ResetOAuth2 clears existing OAuth2 providers and loads them from DB
132+
func ResetOAuth2() error {
133+
oauth2.ClearProviders()
134+
return initOAuth2LoginSources()
135+
}
136+
137+
// initOAuth2LoginSources is used to load and register all active OAuth2 providers
138+
func initOAuth2LoginSources() error {
139+
loginSources, _ := GetActiveOAuth2ProviderLoginSources()
130140
for _, source := range loginSources {
131141
oAuth2Config := source.OAuth2()
132142
err := oauth2.RegisterProvider(source.Name, oAuth2Config.Provider, oAuth2Config.ClientID, oAuth2Config.ClientSecret, oAuth2Config.OpenIDConnectAutoDiscoveryURL, oAuth2Config.CustomURLMapping)

modules/auth/oauth2/oauth2.go

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ func RemoveProvider(providerName string) {
119119
delete(goth.GetProviders(), providerName)
120120
}
121121

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

routers/user/auth.go

+11-2
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)