mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-03 21:08:25 +00:00 
			
		
		
		
	Remove deprecated auth sources (#35272)
Entra ID users should use the OIDC oauth2 provider. They will still be shown if the instance has a previous Azure AD source configured. --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
		@@ -97,7 +97,7 @@ func NewAuthSource(ctx *context.Context) {
 | 
				
			|||||||
	ctx.Data["AuthSources"] = authSources
 | 
						ctx.Data["AuthSources"] = authSources
 | 
				
			||||||
	ctx.Data["SecurityProtocols"] = securityProtocols
 | 
						ctx.Data["SecurityProtocols"] = securityProtocols
 | 
				
			||||||
	ctx.Data["SMTPAuths"] = smtp.Authenticators
 | 
						ctx.Data["SMTPAuths"] = smtp.Authenticators
 | 
				
			||||||
	oauth2providers := oauth2.GetSupportedOAuth2Providers()
 | 
						oauth2providers := oauth2.GetSupportedOAuth2ProvidersWithContext(ctx)
 | 
				
			||||||
	ctx.Data["OAuth2Providers"] = oauth2providers
 | 
						ctx.Data["OAuth2Providers"] = oauth2providers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ctx.Data["SSPIAutoCreateUsers"] = true
 | 
						ctx.Data["SSPIAutoCreateUsers"] = true
 | 
				
			||||||
@@ -107,7 +107,9 @@ func NewAuthSource(ctx *context.Context) {
 | 
				
			|||||||
	ctx.Data["SSPIDefaultLanguage"] = ""
 | 
						ctx.Data["SSPIDefaultLanguage"] = ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// only the first as default
 | 
						// only the first as default
 | 
				
			||||||
 | 
						if len(oauth2providers) > 0 {
 | 
				
			||||||
		ctx.Data["oauth2_provider"] = oauth2providers[0].Name()
 | 
							ctx.Data["oauth2_provider"] = oauth2providers[0].Name()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ctx.HTML(http.StatusOK, tplAuthNew)
 | 
						ctx.HTML(http.StatusOK, tplAuthNew)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -240,7 +242,7 @@ func NewAuthSourcePost(ctx *context.Context) {
 | 
				
			|||||||
	ctx.Data["AuthSources"] = authSources
 | 
						ctx.Data["AuthSources"] = authSources
 | 
				
			||||||
	ctx.Data["SecurityProtocols"] = securityProtocols
 | 
						ctx.Data["SecurityProtocols"] = securityProtocols
 | 
				
			||||||
	ctx.Data["SMTPAuths"] = smtp.Authenticators
 | 
						ctx.Data["SMTPAuths"] = smtp.Authenticators
 | 
				
			||||||
	oauth2providers := oauth2.GetSupportedOAuth2Providers()
 | 
						oauth2providers := oauth2.GetSupportedOAuth2ProvidersWithContext(ctx)
 | 
				
			||||||
	ctx.Data["OAuth2Providers"] = oauth2providers
 | 
						ctx.Data["OAuth2Providers"] = oauth2providers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ctx.Data["SSPIAutoCreateUsers"] = true
 | 
						ctx.Data["SSPIAutoCreateUsers"] = true
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,6 +10,7 @@ import (
 | 
				
			|||||||
	"html"
 | 
						"html"
 | 
				
			||||||
	"html/template"
 | 
						"html/template"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
 | 
						"slices"
 | 
				
			||||||
	"sort"
 | 
						"sort"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"code.gitea.io/gitea/models/auth"
 | 
						"code.gitea.io/gitea/models/auth"
 | 
				
			||||||
@@ -75,6 +76,10 @@ func (p *AuthSourceProvider) IconHTML(size int) template.HTML {
 | 
				
			|||||||
// value is used to store display data
 | 
					// value is used to store display data
 | 
				
			||||||
var gothProviders = map[string]GothProvider{}
 | 
					var gothProviders = map[string]GothProvider{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func isAzureProvider(name string) bool {
 | 
				
			||||||
 | 
						return name == "azuread" || name == "microsoftonline" || name == "azureadv2"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// RegisterGothProvider registers a GothProvider
 | 
					// RegisterGothProvider registers a GothProvider
 | 
				
			||||||
func RegisterGothProvider(provider GothProvider) {
 | 
					func RegisterGothProvider(provider GothProvider) {
 | 
				
			||||||
	if _, has := gothProviders[provider.Name()]; has {
 | 
						if _, has := gothProviders[provider.Name()]; has {
 | 
				
			||||||
@@ -83,13 +88,47 @@ func RegisterGothProvider(provider GothProvider) {
 | 
				
			|||||||
	gothProviders[provider.Name()] = provider
 | 
						gothProviders[provider.Name()] = provider
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// getExistingAzureADAuthSources returns a list of Azure AD provider names that are already configured
 | 
				
			||||||
 | 
					func getExistingAzureADAuthSources(ctx context.Context) ([]string, error) {
 | 
				
			||||||
 | 
						authSources, err := db.Find[auth.Source](ctx, auth.FindSourcesOptions{
 | 
				
			||||||
 | 
							LoginType: auth.OAuth2,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var existingAzureProviders []string
 | 
				
			||||||
 | 
						for _, source := range authSources {
 | 
				
			||||||
 | 
							if oauth2Cfg, ok := source.Cfg.(*Source); ok {
 | 
				
			||||||
 | 
								if isAzureProvider(oauth2Cfg.Provider) {
 | 
				
			||||||
 | 
									existingAzureProviders = append(existingAzureProviders, oauth2Cfg.Provider)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return existingAzureProviders, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// GetSupportedOAuth2Providers returns the map of unconfigured OAuth2 providers
 | 
					// GetSupportedOAuth2Providers returns the map of unconfigured OAuth2 providers
 | 
				
			||||||
// key is used as technical name (like in the callbackURL)
 | 
					// key is used as technical name (like in the callbackURL)
 | 
				
			||||||
// values to display
 | 
					// values to display
 | 
				
			||||||
 | 
					// Note: Azure AD providers (azuread, microsoftonline, azureadv2) are filtered out
 | 
				
			||||||
 | 
					// unless they already exist in the system to encourage use of OpenID Connect
 | 
				
			||||||
func GetSupportedOAuth2Providers() []Provider {
 | 
					func GetSupportedOAuth2Providers() []Provider {
 | 
				
			||||||
 | 
						return GetSupportedOAuth2ProvidersWithContext(context.Background())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// GetSupportedOAuth2ProvidersWithContext returns the list of supported OAuth2 providers with context for filtering
 | 
				
			||||||
 | 
					func GetSupportedOAuth2ProvidersWithContext(ctx context.Context) []Provider {
 | 
				
			||||||
	providers := make([]Provider, 0, len(gothProviders))
 | 
						providers := make([]Provider, 0, len(gothProviders))
 | 
				
			||||||
 | 
						existingAzureSources, err := getExistingAzureADAuthSources(ctx)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Error("Failed to get existing OAuth2 auth sources: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, provider := range gothProviders {
 | 
						for _, provider := range gothProviders {
 | 
				
			||||||
 | 
							if isAzureProvider(provider.Name()) && !slices.Contains(existingAzureSources, provider.Name()) {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		providers = append(providers, provider)
 | 
							providers = append(providers, provider)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	sort.Slice(providers, func(i, j int) bool {
 | 
						sort.Slice(providers, func(i, j int) bool {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user