2020import java .util .ArrayList ;
2121import java .util .List ;
2222import java .util .Map ;
23- import java .util .Objects ;
2423
2524import com .cloud .api .response .ApiResponseSerializer ;
2625import com .cloud .api .ApiDBUtils ;
2726import com .cloud .domain .Domain ;
28- import com .cloud .domain .dao .DomainDao ;
2927import com .cloud .user .Account ;
30- import com .cloud .utils .component .ComponentContext ;
3128import org .apache .cloudstack .acl .RoleType ;
3229import org .apache .cloudstack .api .APICommand ;
3330import org .apache .cloudstack .api .ApiConstants ;
4542import org .apache .cloudstack .oauth2 .OAuth2AuthManager ;
4643import org .apache .cloudstack .oauth2 .api .response .OauthProviderResponse ;
4744import org .apache .cloudstack .oauth2 .vo .OauthProviderVO ;
48- import org .apache .commons .lang .ArrayUtils ;
45+ import org .apache .commons .lang3 .ArrayUtils ;
4946
5047import javax .servlet .http .HttpServletRequest ;
5148import javax .servlet .http .HttpServletResponse ;
@@ -70,6 +67,10 @@ public class ListOAuthProvidersCmd extends BaseListCmd implements APIAuthenticat
7067 description = "List OAuth providers for a specific domain. Use -1 for global providers only." )
7168 private Long domainId ;
7269
70+ @ Parameter (name = ApiConstants .DOMAIN , type = CommandType .STRING ,
71+ description = "Domain path for domain-specific OAuth provider lookup" )
72+ private String domainPath ;
73+
7374 /////////////////////////////////////////////////////
7475 /////////////////// Accessors ///////////////////////
7576 /////////////////////////////////////////////////////
@@ -91,8 +92,6 @@ public Long getDomainId() {
9192
9293 OAuth2AuthManager _oauth2mgr ;
9394
94- DomainDao _domainDao ;
95-
9695 @ Override
9796 public long getEntityOwnerId () {
9897 return Account .Type .NORMAL .ordinal ();
@@ -114,20 +113,26 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
114113 if (ArrayUtils .isNotEmpty (providerArray )) {
115114 provider = providerArray [0 ];
116115 }
117- final String [] domainIdArray = ( String []) params . get ( ApiConstants . DOMAIN_ID );
118- if ( ArrayUtils .isNotEmpty (domainIdArray )) {
119- String domainUuid = domainIdArray [ 0 ] ;
120- if ( "-1" . equals ( domainUuid )) {
121- domainId = - 1L ; // Special case for global-only filter
122- } else {
123- Domain domain = _domainDao . findByUuid ( domainUuid );
124- if ( Objects . nonNull ( domain )) {
125- domainId = domain . getId ( );
126- }
127- }
116+
117+ boolean domainRequested = ArrayUtils .isNotEmpty (( String []) params . get ( ApiConstants . DOMAIN_ID ))
118+ || ArrayUtils . isNotEmpty (( String []) params . get ( ApiConstants . DOMAIN )) ;
119+ domainId = resolveDomainId ( params );
120+
121+ if ( domainRequested && domainId == null ) {
122+ ListResponse < OauthProviderResponse > response = new ListResponse <>( );
123+ response . setResponses ( new ArrayList <>(), 0 );
124+ response . setResponseName ( getCommandName () );
125+ setResponseObject ( response );
126+ return ApiResponseSerializer . toSerializedString ( response , responseType );
128127 }
129128
130129 List <OauthProviderVO > resultList = _oauth2mgr .listOauthProviders (provider , id , domainId );
130+ if (domainRequested && domainId != null && domainId > 0 ) {
131+ resultList .removeIf (p -> p .getDomainId () == null );
132+ } else if (!domainRequested ) {
133+ resultList .removeIf (p -> p .getDomainId () != null );
134+ }
135+
131136 List <UserOAuth2Authenticator > userOAuth2AuthenticatorPlugins = _oauth2mgr .listUserOAuth2AuthenticationProviders ();
132137 List <String > authenticatorPluginNames = new ArrayList <>();
133138 for (UserOAuth2Authenticator authenticator : userOAuth2AuthenticatorPlugins ) {
@@ -156,6 +161,10 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
156161 return ApiResponseSerializer .toSerializedString (response , responseType );
157162 }
158163
164+ private Long resolveDomainId (Map <String , Object []> params ) {
165+ return _oauth2mgr .resolveDomainId (params );
166+ }
167+
159168 @ Override
160169 public APIAuthenticationType getAPIType () {
161170 return null ;
@@ -171,9 +180,5 @@ public void setAuthenticators(List<PluggableAPIAuthenticator> authenticators) {
171180 if (_oauth2mgr == null ) {
172181 logger .error ("No suitable Pluggable Authentication Manager found for listing OAuth providers" );
173182 }
174- _domainDao = (DomainDao ) ComponentContext .getComponent (DomainDao .class );
175- if (Objects .isNull (_domainDao )) {
176- logger .error ("Could not get DomainDao component" );
177- }
178183 }
179184}
0 commit comments