1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-25 10:04:27 +00:00

Fix creation OAuth2 auth source from CLI. (#14146)

Fix #8356
This commit is contained in:
Daniil Pankratov 2020-12-25 15:02:52 +03:00 committed by GitHub
parent bf1441b1e1
commit 205be63bc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 3 deletions

View File

@ -119,8 +119,18 @@ func InitOAuth2() error {
if err := oauth2.Init(x); err != nil { if err := oauth2.Init(x); err != nil {
return err 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 { for _, source := range loginSources {
oAuth2Config := source.OAuth2() oAuth2Config := source.OAuth2()
err := oauth2.RegisterProvider(source.Name, oAuth2Config.Provider, oAuth2Config.ClientID, oAuth2Config.ClientSecret, oAuth2Config.OpenIDConnectAutoDiscoveryURL, oAuth2Config.CustomURLMapping) err := oauth2.RegisterProvider(source.Name, oAuth2Config.Provider, oAuth2Config.ClientID, oAuth2Config.ClientSecret, oAuth2Config.OpenIDConnectAutoDiscoveryURL, oAuth2Config.CustomURLMapping)

View File

@ -118,6 +118,11 @@ func RemoveProvider(providerName string) {
delete(goth.GetProviders(), providerName) 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 // used to create different types of goth providers
func createProvider(providerName, providerType, clientID, clientSecret, openIDConnectAutoDiscoveryURL string, customURLMapping *CustomURLMapping) (goth.Provider, error) { func createProvider(providerName, providerType, clientID, clientSecret, openIDConnectAutoDiscoveryURL string, customURLMapping *CustomURLMapping) (goth.Provider, error) {
callbackURL := setting.AppURL + "user/oauth2/" + url.PathEscape(providerName) + "/callback" callbackURL := setting.AppURL + "user/oauth2/" + url.PathEscape(providerName) + "/callback"

View File

@ -570,8 +570,17 @@ func SignInOAuth(ctx *context.Context) {
return return
} }
err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp) if err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp); err != nil {
if 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) ctx.ServerError("SignIn", err)
} }
// redirect is done in oauth2.Auth // redirect is done in oauth2.Auth