Skip to content

Commit 11d68dd

Browse files
committed
Merge branch 'deprecate-azure-ad-auth' of github.com:techknowlogick/gitea into techknowlogick-deprecate-azure-ad-auth
2 parents 4b19e29 + ea89329 commit 11d68dd

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

routers/web/admin/auths.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func NewAuthSource(ctx *context.Context) {
9797
ctx.Data["AuthSources"] = authSources
9898
ctx.Data["SecurityProtocols"] = securityProtocols
9999
ctx.Data["SMTPAuths"] = smtp.Authenticators
100-
oauth2providers := oauth2.GetSupportedOAuth2Providers()
100+
oauth2providers := oauth2.GetSupportedOAuth2ProvidersWithContext(ctx)
101101
ctx.Data["OAuth2Providers"] = oauth2providers
102102

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

109109
// only the first as default
110-
ctx.Data["oauth2_provider"] = oauth2providers[0].Name()
110+
if len(oauth2providers) > 0 {
111+
ctx.Data["oauth2_provider"] = oauth2providers[0].Name()
112+
}
111113

112114
ctx.HTML(http.StatusOK, tplAuthNew)
113115
}
@@ -240,7 +242,7 @@ func NewAuthSourcePost(ctx *context.Context) {
240242
ctx.Data["AuthSources"] = authSources
241243
ctx.Data["SecurityProtocols"] = securityProtocols
242244
ctx.Data["SMTPAuths"] = smtp.Authenticators
243-
oauth2providers := oauth2.GetSupportedOAuth2Providers()
245+
oauth2providers := oauth2.GetSupportedOAuth2ProvidersWithContext(ctx)
244246
ctx.Data["OAuth2Providers"] = oauth2providers
245247

246248
ctx.Data["SSPIAutoCreateUsers"] = true

services/auth/source/oauth2/providers.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"html"
1111
"html/template"
1212
"net/url"
13+
"slices"
1314
"sort"
1415

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

79+
var azureProviders = []string{
80+
"azuread",
81+
"microsoftonline",
82+
"azureadv2",
83+
}
84+
7885
// RegisterGothProvider registers a GothProvider
7986
func RegisterGothProvider(provider GothProvider) {
8087
if _, has := gothProviders[provider.Name()]; has {
@@ -83,13 +90,44 @@ func RegisterGothProvider(provider GothProvider) {
8390
gothProviders[provider.Name()] = provider
8491
}
8592

93+
// getExistingAzureADAuthSources returns a list of Azure AD provider names that are already configured
94+
func getExistingAzureADAuthSources(ctx context.Context) []string {
95+
authSources, err := db.Find[auth.Source](ctx, auth.FindSourcesOptions{
96+
LoginType: auth.OAuth2,
97+
})
98+
if err != nil {
99+
return nil
100+
}
101+
102+
var existingAzureProviders []string
103+
for _, source := range authSources {
104+
if oauth2Cfg, ok := source.Cfg.(*Source); ok {
105+
if slices.Contains(azureProviders, oauth2Cfg.Provider) {
106+
existingAzureProviders = append(existingAzureProviders, oauth2Cfg.Provider)
107+
}
108+
}
109+
}
110+
return existingAzureProviders
111+
}
112+
86113
// GetSupportedOAuth2Providers returns the map of unconfigured OAuth2 providers
87114
// key is used as technical name (like in the callbackURL)
88115
// values to display
116+
// Note: Azure AD providers (azuread, microsoftonline, azureadv2) are filtered out
117+
// unless they already exist in the system to encourage use of OpenID Connect
89118
func GetSupportedOAuth2Providers() []Provider {
119+
return GetSupportedOAuth2ProvidersWithContext(context.Background())
120+
}
121+
122+
// GetSupportedOAuth2ProvidersWithContext returns the list of supported OAuth2 providers with context for filtering
123+
func GetSupportedOAuth2ProvidersWithContext(ctx context.Context) []Provider {
90124
providers := make([]Provider, 0, len(gothProviders))
125+
existAuthSources := getExistingAzureADAuthSources(ctx)
91126

92127
for _, provider := range gothProviders {
128+
if slices.Contains(azureProviders, provider.Name()) && !slices.Contains(existAuthSources, provider.Name()) {
129+
continue
130+
}
93131
providers = append(providers, provider)
94132
}
95133
sort.Slice(providers, func(i, j int) bool {

0 commit comments

Comments
 (0)