@@ -466,6 +466,70 @@ private void saveCredentials(ClientApplication app, ClientApplication actualApp)
466466 }
467467 upsertCredential (cred , filter , app .getId (), endpoint .toString (), update );
468468 }
469+ List <ClientAppCredential > credentials2Delete = getCredentialsToDelete (app , actualApp );
470+ deleteCredentials (app , credentials2Delete );
471+ }
472+
473+ List <ClientAppCredential > getCredentialsToDelete (ClientApplication desiredApp , ClientApplication actualApp ) {
474+ if (actualApp == null || actualApp .getCredentials () == null || actualApp .getCredentials ().isEmpty ()) return Collections .emptyList ();
475+ if (desiredApp == null || desiredApp .getCredentials () == null || desiredApp .getCredentials ().isEmpty ()) return Collections .emptyList ();
476+
477+ Set <String > desiredCredentialKeys = new HashSet <>();
478+ for (ClientAppCredential credential : desiredApp .getCredentials ()) {
479+ desiredCredentialKeys .add (getCredentialComparisonKey (credential ));
480+ }
481+ List <ClientAppCredential > credentials2Delete = new ArrayList <>();
482+ for (ClientAppCredential existingCredential : actualApp .getCredentials ()) {
483+ if (!desiredCredentialKeys .contains (getCredentialComparisonKey (existingCredential ))) {
484+ credentials2Delete .add (existingCredential );
485+ }
486+ }
487+ return credentials2Delete ;
488+ }
489+
490+ String getCredentialComparisonKey (ClientAppCredential credential ) {
491+ return credential .getCredentialType () + "|" + getCredentialIdentifier (credential );
492+ }
493+
494+ private String getCredentialIdentifier (ClientAppCredential credential ) {
495+ if (credential instanceof OAuth ) return ((OAuth ) credential ).getClientId ();
496+ if (credential instanceof ExtClients ) return ((ExtClients ) credential ).getClientId ();
497+ if (credential instanceof APIKey ) return ((APIKey ) credential ).getApiKey ();
498+ return credential .getId ();
499+ }
500+
501+ private void deleteCredentials (ClientApplication desiredApp , List <ClientAppCredential > credentials2Delete ) throws AppException {
502+ if (credentials2Delete == null || credentials2Delete .isEmpty ()) return ;
503+ for (ClientAppCredential credential : credentials2Delete ) {
504+ deleteCredential (desiredApp , credential );
505+ }
506+ }
507+
508+ private void deleteCredential (ClientApplication desiredApp , ClientAppCredential credential ) throws AppException {
509+ try {
510+ String endpoint = getCredentialDeleteEndpoint (credential );
511+ URI uri = new URIBuilder (cmd .getAPIManagerURL ()).setPath (cmd .getApiBasepath () + APPLICATIONS + "/" + desiredApp .getId () + "/" + endpoint ).build ();
512+ RestAPICall request = new DELRequest (uri );
513+ try (CloseableHttpResponse httpResponse = (CloseableHttpResponse ) request .execute ()) {
514+ int statusCode = httpResponse .getStatusLine ().getStatusCode ();
515+ if (statusCode != 204 ) {
516+ LOG .error ("Error deleting application credential. Response-Code: {}" , statusCode );
517+ Utils .logPayload (LOG , httpResponse );
518+ throw new AppException ("Error deleting application credential. Response-Code: " + statusCode , ErrorCode .API_MANAGER_COMMUNICATION );
519+ }
520+ applicationsCredentialCache .remove (desiredApp .getId () + "|" + credential .getCredentialType ());
521+ LOG .info ("Credential type: {} for application: {} successfully deleted" , credential .getCredentialType (), desiredApp .getName ());
522+ }
523+ } catch (Exception e ) {
524+ throw new AppException ("Error deleting application credential. Error: " + e .getMessage (), ErrorCode .API_MANAGER_COMMUNICATION , e );
525+ }
526+ }
527+
528+ String getCredentialDeleteEndpoint (ClientAppCredential credential ) throws AppException {
529+ if (credential instanceof OAuth ) return "oauth/" + ((OAuth ) credential ).getClientId ();
530+ if (credential instanceof ExtClients ) return "extclients/" + credential .getId ();
531+ if (credential instanceof APIKey ) return "apikeys/" + ((APIKey ) credential ).getApiKey ();
532+ throw new AppException ("Unsupported credential for deletion: " + credential .getClass ().getName (), ErrorCode .UNXPECTED_ERROR );
469533 }
470534
471535 public void copyClientAppCredential (ClientAppCredential cred , ClientAppCredential target , boolean copySecret ) {
@@ -492,6 +556,7 @@ public void upsertCredential(ClientAppCredential cred, FilterProvider filter, St
492556 Utils .logPayload (LOG , httpResponse );
493557 throw new AppException (ERROR_CREATING_APPLICATION_RESPONSE_CODE + statusCode , ErrorCode .API_MANAGER_COMMUNICATION );
494558 }
559+ applicationsCredentialCache .remove (appId + "|" + cred .getCredentialType ());
495560 }
496561 } catch (Exception e ) {
497562 throw new AppException (ERROR_CREATING_APPLICATION_ERROR , ErrorCode .CANT_CREATE_API_PROXY , e );
0 commit comments