@@ -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 
7677var  gothProviders  =  map [string ]GothProvider {}
7778
79+ var  azureProviders  =  []string {
80+ 	"azuread" ,
81+ 	"microsoftonline" ,
82+ 	"azureadv2" ,
83+ }
84+ 
7885// RegisterGothProvider registers a GothProvider 
7986func  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 
89118func  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