Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions routers/web/admin/auths.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func NewAuthSource(ctx *context.Context) {
ctx.Data["AuthSources"] = authSources
ctx.Data["SecurityProtocols"] = securityProtocols
ctx.Data["SMTPAuths"] = smtp.Authenticators
oauth2providers := oauth2.GetSupportedOAuth2Providers()
oauth2providers := oauth2.GetSupportedOAuth2ProvidersWithContext(ctx)
ctx.Data["OAuth2Providers"] = oauth2providers

ctx.Data["SSPIAutoCreateUsers"] = true
Expand All @@ -107,7 +107,9 @@ func NewAuthSource(ctx *context.Context) {
ctx.Data["SSPIDefaultLanguage"] = ""

// only the first as default
ctx.Data["oauth2_provider"] = oauth2providers[0].Name()
if len(oauth2providers) > 0 {
ctx.Data["oauth2_provider"] = oauth2providers[0].Name()
}

ctx.HTML(http.StatusOK, tplAuthNew)
}
Expand Down Expand Up @@ -240,7 +242,7 @@ func NewAuthSourcePost(ctx *context.Context) {
ctx.Data["AuthSources"] = authSources
ctx.Data["SecurityProtocols"] = securityProtocols
ctx.Data["SMTPAuths"] = smtp.Authenticators
oauth2providers := oauth2.GetSupportedOAuth2Providers()
oauth2providers := oauth2.GetSupportedOAuth2ProvidersWithContext(ctx)
ctx.Data["OAuth2Providers"] = oauth2providers

ctx.Data["SSPIAutoCreateUsers"] = true
Expand Down
38 changes: 38 additions & 0 deletions services/auth/source/oauth2/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"html"
"html/template"
"net/url"
"slices"
"sort"

"code.gitea.io/gitea/models/auth"
Expand Down Expand Up @@ -75,6 +76,12 @@ func (p *AuthSourceProvider) IconHTML(size int) template.HTML {
// value is used to store display data
var gothProviders = map[string]GothProvider{}

var azureProviders = []string{
"azuread",
"microsoftonline",
"azureadv2",
}

// RegisterGothProvider registers a GothProvider
func RegisterGothProvider(provider GothProvider) {
if _, has := gothProviders[provider.Name()]; has {
Expand All @@ -83,13 +90,44 @@ func RegisterGothProvider(provider GothProvider) {
gothProviders[provider.Name()] = provider
}

// getExistingAzureADAuthSources returns a list of Azure AD provider names that are already configured
func getExistingAzureADAuthSources(ctx context.Context) []string {
authSources, err := db.Find[auth.Source](ctx, auth.FindSourcesOptions{
LoginType: auth.OAuth2,
})
if err != nil {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't silently ignore an unknown error.


var existingAzureProviders []string
for _, source := range authSources {
if oauth2Cfg, ok := source.Cfg.(*Source); ok {
if slices.Contains(azureProviders, oauth2Cfg.Provider) {
existingAzureProviders = append(existingAzureProviders, oauth2Cfg.Provider)
}
}
}
return existingAzureProviders
}

// 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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases, context.Background shouldn't be used

-> Fix context usages #35348

}

// GetSupportedOAuth2ProvidersWithContext returns the list of supported OAuth2 providers with context for filtering
func GetSupportedOAuth2ProvidersWithContext(ctx context.Context) []Provider {
providers := make([]Provider, 0, len(gothProviders))
existAuthSources := getExistingAzureADAuthSources(ctx)

for _, provider := range gothProviders {
if slices.Contains(azureProviders, provider.Name()) && !slices.Contains(existAuthSources, provider.Name()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that an gothProviders = gothProviders.Remove(existingAzureSources)?
(Although it will probably look different since the stdlib doesn't seem to support that method…)

continue
}
providers = append(providers, provider)
}
sort.Slice(providers, func(i, j int) bool {
Expand Down