@@ -83,13 +83,55 @@ func RegisterGothProvider(provider GothProvider) {
8383	gothProviders [provider .Name ()] =  provider 
8484}
8585
86+ // hasExistingAzureADAuthSources checks if there are any existing Azure AD auth sources configured 
87+ func  hasExistingAzureADAuthSources (ctx  context.Context ) bool  {
88+ 	azureProviders  :=  map [string ]bool {
89+ 		"azuread" :         true ,
90+ 		"microsoftonline" : true ,
91+ 		"azureadv2" :       true ,
92+ 	}
93+ 
94+ 	authSources , err  :=  db .Find [auth.Source ](ctx , auth.FindSourcesOptions {
95+ 		LoginType : auth .OAuth2 ,
96+ 	})
97+ 	if  err  !=  nil  {
98+ 		return  false 
99+ 	}
100+ 
101+ 	for  _ , source  :=  range  authSources  {
102+ 		if  oauth2Cfg , ok  :=  source .Cfg .(* Source ); ok  {
103+ 			if  azureProviders [oauth2Cfg .Provider ] {
104+ 				return  true 
105+ 			}
106+ 		}
107+ 	}
108+ 	return  false 
109+ }
110+ 
86111// GetSupportedOAuth2Providers returns the map of unconfigured OAuth2 providers 
87112// key is used as technical name (like in the callbackURL) 
88113// values to display 
114+ // Note: Azure AD providers (azuread, microsoftonline, azureadv2) are filtered out 
115+ // unless they already exist in the system to encourage use of OpenID Connect 
89116func  GetSupportedOAuth2Providers () []Provider  {
117+ 	return  GetSupportedOAuth2ProvidersWithContext (context .Background ())
118+ }
119+ 
120+ // GetSupportedOAuth2ProvidersWithContext returns the list of supported OAuth2 providers with context for filtering 
121+ func  GetSupportedOAuth2ProvidersWithContext (ctx  context.Context ) []Provider  {
90122	providers  :=  make ([]Provider , 0 , len (gothProviders ))
123+ 	hasExistingAzure  :=  hasExistingAzureADAuthSources (ctx )
124+ 
125+ 	azureProviders  :=  map [string ]bool {
126+ 		"azuread" :         true ,
127+ 		"microsoftonline" : true ,
128+ 		"azureadv2" :       true ,
129+ 	}
91130
92131	for  _ , provider  :=  range  gothProviders  {
132+ 		if  azureProviders [provider .Name ()] &&  ! hasExistingAzure  {
133+ 			continue 
134+ 		}
93135		providers  =  append (providers , provider )
94136	}
95137	sort .Slice (providers , func (i , j  int ) bool  {
0 commit comments