11package com .cybersource .ws .client ;
22
3+ import org .apache .commons .lang3 .StringUtils ;
34import org .apache .ws .security .WSConstants ;
45import org .apache .ws .security .WSEncryptionPart ;
56import org .apache .ws .security .WSSecurityException ;
@@ -36,7 +37,7 @@ public class SecurityUtil {
3637 private static MessageHandlerKeyStore localKeyStoreHandler = null ;
3738
3839 //mapping between IdentityName and Identity
39- private static ConcurrentHashMap <String , Identity > identities = new ConcurrentHashMap <String , Identity >();
40+ private static ConcurrentHashMap <String , Identity > identities = new ConcurrentHashMap <>();
4041
4142 // By default signature algorithm is set to null and during WSSecSignature build() Signature algorithm will set to "http://www.w3.org/2000/09/xmldsig#rsa-sha1" .
4243 private static final String SIGNATURE_ALGORITHM = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" ;
@@ -147,7 +148,7 @@ private static void readAndStoreCertificateAndPrivateKey(MerchantConfig merchant
147148 while (enumKeyStore .hasMoreElements ()) {
148149 KeyStore .PrivateKeyEntry keyEntry = null ;
149150 merchantKeyAlias = (String ) enumKeyStore .nextElement ();
150- if (merchantKeyAlias .toLowerCase (). contains (merchantConfig .getKeyAlias (). toLowerCase ())){
151+ if (merchantKeyAlias .contains (merchantConfig .getKeyAlias ())){
151152 try {
152153 keyEntry = (KeyStore .PrivateKeyEntry ) merchantKeyStore .getEntry
153154 (merchantKeyAlias , new KeyStore .PasswordProtection (merchantConfig .getKeyPassword ().toCharArray ()));
@@ -202,8 +203,8 @@ public static Document handleMessageCreation(Document signedDoc, String merchant
202203 WSSecEncrypt encrBuilder = new WSSecEncrypt ();
203204 //Set the user name to get the encryption certificate.
204205 //The public key of this certificate is used, thus no password necessary. The user name is a keystore alias usually.
205- encrBuilder . setUserInfo ( identities . get ( SERVER_ALIAS ). getKeyAlias () );
206-
206+ String serverAlias = getServerAlias ( );
207+ encrBuilder . setUserInfo ( identities . get ( serverAlias ). getKeyAlias ());
207208 /*This is to reference a public key or certificate when signing or encrypting a SOAP message.
208209 *The following valid values for these configuration items are:
209210 *IssuerSerial (default),DirectReference[BST],X509KeyIdentifier,Thumbprint,SKIKeyIdentifier,KeyValue (signature only),EncryptedKeySHA1 (encryption only)
@@ -228,8 +229,8 @@ public static Document handleMessageCreation(Document signedDoc, String merchant
228229 // encrypted using the public key of the receiver
229230 signedEncryptedDoc = encrBuilder .build (signedDoc , localKeyStoreHandler , secHeader );
230231 } catch (WSSecurityException e ) {
231- logger .log (Logger .LT_EXCEPTION , "Failed while encrypting signed requeest for , '" + merchantId + "'" + " with " + SERVER_ALIAS );
232- throw new SignEncryptException ("Failed while encrypting signed requeest for , '" + merchantId + "'" + " with " + SERVER_ALIAS , e );
232+ logger .log (Logger .LT_EXCEPTION , "Failed while encrypting signed requeest for , '" + merchantId + "'" + " with " + serverAlias );
233+ throw new SignEncryptException ("Failed while encrypting signed requeest for , '" + merchantId + "'" + " with " + serverAlias , e );
233234 }
234235 encrBuilder .prependToHeader (secHeader );
235236 return signedEncryptedDoc ;
@@ -384,7 +385,7 @@ private static void loadJavaKeystore(MerchantConfig merchantConfig, Logger logge
384385 identities .put (identity .getName (), identity );
385386 }
386387 }
387- java .security .cert .Certificate serverCert = keystore .getCertificate (SERVER_ALIAS );
388+ java .security .cert .Certificate serverCert = keystore .getCertificate (getServerAlias () );
388389 if (serverCert == null ) {
389390 throw new SignException ("Missing Server Certificate " );
390391 }
@@ -421,4 +422,23 @@ private static void loadJavaKeystore(MerchantConfig merchantConfig, Logger logge
421422 }
422423
423424 }
425+
426+ private static String getServerAlias () {
427+ String serverAlias = SERVER_ALIAS ;
428+ if (!identities .containsKey (serverAlias )) {
429+ if (identities .containsKey (serverAlias .toLowerCase ())) {
430+ serverAlias = serverAlias .toLowerCase ();
431+ } else if (identities .containsKey (serverAlias .toUpperCase ())) {
432+ serverAlias = serverAlias .toUpperCase ();
433+ } else {
434+ for (String identityKey :identities .keySet ()) {
435+ if (identityKey .equalsIgnoreCase (serverAlias )) {
436+ serverAlias = identityKey ;
437+ break ;
438+ }
439+ }
440+ }
441+ }
442+ return serverAlias ;
443+ }
424444}
0 commit comments