diff options
author | Daniil Pankratov <mobigod0@gmail.com> | 2020-12-25 15:02:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-25 20:02:52 +0800 |
commit | 205be63bc17242973187f042d94d815107b4bab8 (patch) | |
tree | f617c0777785fd340bfcf26ba7a8779fd5d17431 | |
parent | bf1441b1e126b315bb14f2c56a2515d43f909384 (diff) | |
download | gitea-205be63bc17242973187f042d94d815107b4bab8.tar.gz gitea-205be63bc17242973187f042d94d815107b4bab8.zip |
Fix creation OAuth2 auth source from CLI. (#14146)
Fix #8356
-rw-r--r-- | models/oauth2.go | 12 | ||||
-rw-r--r-- | modules/auth/oauth2/oauth2.go | 5 | ||||
-rw-r--r-- | routers/user/auth.go | 13 |
3 files changed, 27 insertions, 3 deletions
diff --git a/models/oauth2.go b/models/oauth2.go index ccf24eb35d..21a6f995c7 100644 --- a/models/oauth2.go +++ b/models/oauth2.go @@ -119,8 +119,18 @@ func InitOAuth2() error { if err := oauth2.Init(x); err != nil { return err } - loginSources, _ := GetActiveOAuth2ProviderLoginSources() + return initOAuth2LoginSources() +} +// ResetOAuth2 clears existing OAuth2 providers and loads them from DB +func ResetOAuth2() error { + oauth2.ClearProviders() + return initOAuth2LoginSources() +} + +// initOAuth2LoginSources is used to load and register all active OAuth2 providers +func initOAuth2LoginSources() error { + loginSources, _ := GetActiveOAuth2ProviderLoginSources() for _, source := range loginSources { oAuth2Config := source.OAuth2() err := oauth2.RegisterProvider(source.Name, oAuth2Config.Provider, oAuth2Config.ClientID, oAuth2Config.ClientSecret, oAuth2Config.OpenIDConnectAutoDiscoveryURL, oAuth2Config.CustomURLMapping) diff --git a/modules/auth/oauth2/oauth2.go b/modules/auth/oauth2/oauth2.go index 2c982e1dca..932246d06d 100644 --- a/modules/auth/oauth2/oauth2.go +++ b/modules/auth/oauth2/oauth2.go @@ -118,6 +118,11 @@ func RemoveProvider(providerName string) { delete(goth.GetProviders(), providerName) } +// ClearProviders clears all OAuth2 providers from the goth lib +func ClearProviders() { + goth.ClearProviders() +} + // used to create different types of goth providers func createProvider(providerName, providerType, clientID, clientSecret, openIDConnectAutoDiscoveryURL string, customURLMapping *CustomURLMapping) (goth.Provider, error) { callbackURL := setting.AppURL + "user/oauth2/" + url.PathEscape(providerName) + "/callback" diff --git a/routers/user/auth.go b/routers/user/auth.go index 893cad09be..c5542456a1 100644 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -570,8 +570,17 @@ func SignInOAuth(ctx *context.Context) { return } - err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp) - if err != nil { + if err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp); err != nil { + if strings.Contains(err.Error(), "no provider for ") { + if err = models.ResetOAuth2(); err != nil { + ctx.ServerError("SignIn", err) + return + } + if err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp); err != nil { + ctx.ServerError("SignIn", err) + } + return + } ctx.ServerError("SignIn", err) } // redirect is done in oauth2.Auth |