]> source.dussan.org Git - gitea.git/commitdiff
Fix creation OAuth2 auth source from CLI. (#14146)
authorDaniil Pankratov <mobigod0@gmail.com>
Fri, 25 Dec 2020 12:02:52 +0000 (15:02 +0300)
committerGitHub <noreply@github.com>
Fri, 25 Dec 2020 12:02:52 +0000 (20:02 +0800)
Fix #8356

models/oauth2.go
modules/auth/oauth2/oauth2.go
routers/user/auth.go

index ccf24eb35d9fd7e46352272453d3a0e6f761fc5a..21a6f995c7ebd664451817b93d30a2b8ec00a81b 100644 (file)
@@ -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)
index 2c982e1dcaa4a78057421aece4c60428304126a6..932246d06d75d4a9b28bf75a185fa9fd4138741b 100644 (file)
@@ -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"
index 893cad09beee0fe809a6e0eefa8118278dde3726..c5542456a19d6eddd023752d6744451d53ed81ea 100644 (file)
@@ -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