
com.cybersource.flex.sdk.CredentialsImpl Maven / Gradle / Ivy
package com.cybersource.flex.sdk;
import com.cybersource.flex.sdk.internal.SecureByteArrayWrapper;
import com.cybersource.flex.sdk.internal.SecurityHelper;
import java.util.Base64;
public class CredentialsImpl implements Credentials {
private final Environment environment;
private final String mid;
private final String keyId;
private final SecureByteArrayWrapper sharedSecret;
/**
* Constructor that can be used to pass all authentication information in
* one call. Successfully invoked constructor produces object that is ready
* for use (i.e. to create @see FlexService instance).
*
* @param environment environment to which credentials relate
* @param mid merchant ID
* @param keyId shared secret key ID used to identify credentials
* @param sharedSecret byte array containing raw shared secret. Note that
* the array referenced by this parameter will be overridden after it is
* used to initialize FlexService instance.
*/
CredentialsImpl(Environment environment, String mid, String keyId, char[] sharedSecret) {
this.environment = environment;
this.mid = mid;
this.keyId = keyId;
this.sharedSecret = new SecureByteArrayWrapper(decodeSharedSecret(sharedSecret));
}
@Override
public Environment getEnvironment() {
return environment;
}
public String getMid() {
return mid;
}
public String getKeyId() {
return keyId;
}
public SecureByteArrayWrapper getSharedSecret() {
return sharedSecret;
}
private static byte[] decodeSharedSecret(char[] encodedSharedSecret) {
final byte[] sharedSecret = new byte[encodedSharedSecret.length];
try {
for (int i = 0; i < sharedSecret.length; i++) {
if (encodedSharedSecret[i] > 128) {
throw new IllegalArgumentException("non ASCII character"); // todo: make it base64 alphabet verification in future
}
sharedSecret[i] = (byte) encodedSharedSecret[i];
}
return Base64.getDecoder().decode(sharedSecret);
} finally {
SecurityHelper.randomize(encodedSharedSecret);
SecurityHelper.randomize(sharedSecret);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy