Repository: go-gitea/gitea
File: services/auth/source/oauth2/providers.go (Go)

Code before:
		return template.HTML(img)
	}
	return p.GothProvider.IconHTML(size)
}

// Providers contains the map of registered OAuth2 providers in Gitea (based on goth)
// key is used to map the OAuth2Provider with the goth provider type (also in AuthSource.OAuth2Config.Provider)
// value is used to store display data
var gothProviders = map[string]GothProvider{}

var azureProviders = map[string]bool{
	"azuread":         true,
	"microsoftonline": true,
	"azureadv2":       true,
}

// RegisterGothProvider registers a GothProvider
func RegisterGothProvider(provider GothProvider) {
	if _, has := gothProviders[provider.Name()]; has {
		log.Fatal("Duplicate oauth2provider type provided: %s", provider.Name())
	}
	gothProviders[provider.Name()] = provider
}

// hasExistingAzureADAuthSources checks if there are any existing Azure AD auth sources configured
func hasExistingAzureADAuthSources(ctx context.Context) bool {
	authSources, err := db.Find[auth.Source](ctx, auth.FindSourcesOptions{
		LoginType: auth.OAuth2,
	})
	if err != nil {
		return false
	}

	for _, source := range authSources {
		if oauth2Cfg, ok := source.Cfg.(*Source); ok {
			if azureProviders[oauth2Cfg.Provider] {
				return true
			}
		}
	}
	return false
}

// GetSupportedOAuth2Providers returns the map of unconfigured OAuth2 providers
// key is used as technical name (like in the callbackURL)
// 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 {
	return GetSupportedOAuth2ProvidersWithContext(context.Background())
}

Review comment (suggestion):
Maybe we should return the existed types as `[]string` but not only the `bool` and then just list the exists providers but all the 3 types.

Code after:
		)
		return template.HTML(img)
	}
	return p.GothProvider.IconHTML(size)
}

// Providers contains the map of registered OAuth2 providers in Gitea (based on goth)
// key is used to map the OAuth2Provider with the goth provider type (also in AuthSource.OAuth2Config.Provider)
// value is used to store display data
var gothProviders = map[string]GothProvider{}

func isAzureProvider(name string) bool {
	return name == "azuread" || name == "microsoftonline" || name == "azureadv2"
}

// RegisterGothProvider registers a GothProvider
func RegisterGothProvider(provider GothProvider) {
	if _, has := gothProviders[provider.Name()]; has {
		log.Fatal("Duplicate oauth2provider type provided: %s", provider.Name())
	}
	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
// key is used as technical name (like in the callbackURL)
// 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 {
	return GetSupportedOAuth2ProvidersWithContext(context.Background())
}