11package com .axway .apim .adapter .client .apps ;
22
33import java .util .ArrayList ;
4+ import java .util .Arrays ;
5+ import java .util .Collections ;
6+ import java .util .LinkedHashSet ;
47import java .util .List ;
8+ import java .util .Set ;
59import java .util .regex .Matcher ;
610import java .util .regex .Pattern ;
711
@@ -30,6 +34,7 @@ public class ClientAppFilter implements CustomPropertiesFilter {
3034 private static final Logger LOG = LoggerFactory .getLogger (ClientAppFilter .class );
3135 public static final String FIELD = "field" ;
3236 public static final String VALUE = "value" ;
37+ public static final List <String > SUPPORTED_CREDENTIAL_TYPES = Collections .unmodifiableList (Arrays .asList ("oauth" , "apikeys" , "extclients" ));
3338
3439 boolean includeQuota ;
3540
@@ -45,6 +50,11 @@ public class ClientAppFilter implements CustomPropertiesFilter {
4550
4651 private String redirectUrl ;
4752
53+ /**
54+ * Credential types (oauth, apikeys, extclients), of which an application must have at least one. Null or empty means no restriction.
55+ */
56+ private Set <String > credentialTypes ;
57+
4858 private String applicationName ;
4959
5060 private String state ;
@@ -189,6 +199,18 @@ public void setCredential(String credential) {
189199 this .credential = credential ;
190200 }
191201
202+ public Set <String > getCredentialTypes () {
203+ return credentialTypes ;
204+ }
205+
206+ public void setCredentialTypes (Set <String > credentialTypes ) {
207+ this .credentialTypes = credentialTypes ;
208+ }
209+
210+ private boolean hasCredentialTypeFilter () {
211+ return credentialTypes != null && !credentialTypes .isEmpty ();
212+ }
213+
192214 public String getRedirectUrl () {
193215 return redirectUrl ;
194216 }
@@ -249,14 +271,16 @@ public String toString() {
249271 }
250272
251273 public boolean filter (ClientApplication app ) {
252- if (this .getCredential () == null && this .getRedirectUrl () == null && this .getApiName () == null ) { // Nothing given to filter out.
274+ if (this .getCredential () == null && this .getRedirectUrl () == null && this .getApiName () == null && ! hasCredentialTypeFilter () ) { // Nothing given to filter out.
253275 return false ;
254276 }
255- if (this .getCredential () != null || this .getRedirectUrl () != null ) {
277+ if (this .getCredential () != null || this .getRedirectUrl () != null || hasCredentialTypeFilter () ) {
256278 // Filter this app, as it doesn't have any credentials
257279 if (app .getCredentials () == null ) return true ;
258280 boolean match = false ;
259281 for (ClientAppCredential cred : app .getCredentials ()) {
282+ // Ignore credentials of a type, which is not requested
283+ if (hasCredentialTypeFilter () && !credentialTypes .contains (cred .getCredentialType ())) continue ;
260284 switch (cred .getCredentialType ()) {
261285 case "oauth" :
262286 match = filterCredential (((OAuth ) cred ).getClientId (), ((OAuth ) cred ).getRedirectUrls (), app .getName ());
@@ -353,6 +377,8 @@ public static class Builder {
353377
354378 private String redirectUrl ;
355379
380+ private Set <String > credentialTypes ;
381+
356382 private boolean includeOauthResources ;
357383
358384 private String apiName ;
@@ -382,6 +408,7 @@ public ClientAppFilter build() {
382408 filter .setIncludeAPIAccess (this .includeAPIAccess );
383409 filter .setCredential (this .credential );
384410 filter .setRedirectUrl (this .redirectUrl );
411+ filter .setCredentialTypes (this .credentialTypes );
385412 filter .setIncludeOauthResources (this .includeOauthResources );
386413 filter .setCustomProperties (this .customProperties );
387414 filter .setApiName (this .apiName );
@@ -455,6 +482,28 @@ public Builder hasRedirectUrl(String redirectUrl) {
455482 return this ;
456483 }
457484
485+ /**
486+ * Restricts the applications to those having at least one credential of the given type(s).
487+ *
488+ * @param credentialTypes comma separated list of credential types: oauth, apikeys, extclients (case-insensitive). Null or empty means no restriction.
489+ * @return the builder
490+ * @throws AppException if an unsupported credential type is given
491+ */
492+ public Builder hasCredentialType (String credentialTypes ) throws AppException {
493+ if (StringUtils .isBlank (credentialTypes )) return this ;
494+ Set <String > types = new LinkedHashSet <>();
495+ for (String type : credentialTypes .split ("," )) {
496+ String normalized = type .trim ().toLowerCase ();
497+ if (normalized .isEmpty ()) continue ;
498+ if (!SUPPORTED_CREDENTIAL_TYPES .contains (normalized )) {
499+ throw new AppException ("Unsupported credential type: '" + type .trim () + "'. Supported types are: " + String .join (", " , SUPPORTED_CREDENTIAL_TYPES ), ErrorCode .INVALID_PARAMETER );
500+ }
501+ types .add (normalized );
502+ }
503+ this .credentialTypes = types .isEmpty () ? null : types ;
504+ return this ;
505+ }
506+
458507 public Builder includeQuotas (boolean includeQuota ) {
459508 this .includeQuota = includeQuota ;
460509 return this ;
0 commit comments