decode APIM3.2 credentials - clientSecret
This commit is contained in:
parent
a3ebd99fb6
commit
5b5ce421d5
@ -14,6 +14,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import cz.trask.migration.model.ApiDefinition32;
|
||||
import cz.trask.migration.model.ApiDefinition45;
|
||||
import cz.trask.migration.model.ApiDefinition45.ApiPolicies;
|
||||
import cz.trask.migration.util.CredentialsDecoder;
|
||||
|
||||
public class ApiDefinitionMapper {
|
||||
|
||||
@ -216,7 +217,7 @@ public class ApiDefinitionMapper {
|
||||
newSec.setType(oldSec.getType());
|
||||
newSec.setTokenUrl(oldSec.getTokenUrl());
|
||||
newSec.setClientId(oldSec.getClientId());
|
||||
newSec.setClientSecret(oldSec.getClientSecret());
|
||||
newSec.setClientSecret(CredentialsDecoder.decodeCredentials(oldSec.getClientSecret(), "wso2apim32-pk.pem"));
|
||||
newSec.setUsername(oldSec.getUsername());
|
||||
newSec.setPassword(oldSec.getPassword());
|
||||
newSec.setGrantType(oldSec.getGrantType());
|
||||
|
||||
@ -17,33 +17,44 @@ import lombok.extern.log4j.Log4j2;
|
||||
@Log4j2
|
||||
public class CredentialsDecoder {
|
||||
|
||||
public static void decodeCredentials(String credentials, String pkFile) throws Exception {
|
||||
String decodedJson = new String(Base64.getDecoder().decode(credentials));
|
||||
log.debug("Decoded JSON: {}", decodedJson);
|
||||
public static String decodeCredentials(String credentials, String pkFile) {
|
||||
if (credentials == null || credentials.isEmpty()) {
|
||||
log.warn("No credentials provided to decode.");
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
String decodedJson = new String(Base64.getDecoder().decode(credentials));
|
||||
log.debug("Decoded JSON: {}", decodedJson);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Map<String, String> jsonMap = mapper.readValue(decodedJson, Map.class);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Map<String, String> jsonMap = mapper.readValue(decodedJson, Map.class);
|
||||
|
||||
String cipherBase64 = jsonMap.get("c");
|
||||
String transformation = jsonMap.get("t");
|
||||
log.debug("Used algorithm: {}", transformation);
|
||||
String cipherBase64 = jsonMap.get("c");
|
||||
String transformation = jsonMap.get("t");
|
||||
log.debug("Used algorithm: {}", transformation);
|
||||
|
||||
String privateKeyPEM = new String(Files.readAllBytes(Paths.get(pkFile)))
|
||||
.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "")
|
||||
.replaceAll("\\s+", "");
|
||||
byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyPEM);
|
||||
String privateKeyPEM = new String(Files.readAllBytes(Paths.get(pkFile)))
|
||||
.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "")
|
||||
.replaceAll("\\s+", "");
|
||||
byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyPEM);
|
||||
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
|
||||
|
||||
byte[] encryptedBytes = Base64.getDecoder().decode(cipherBase64);
|
||||
Cipher cipher = Cipher.getInstance(transformation);
|
||||
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||
byte[] encryptedBytes = Base64.getDecoder().decode(cipherBase64);
|
||||
Cipher cipher = Cipher.getInstance(transformation);
|
||||
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||
|
||||
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
|
||||
String decryptedText = new String(decryptedBytes, "UTF-8");
|
||||
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
|
||||
String decryptedText = new String(decryptedBytes, "UTF-8");
|
||||
|
||||
log.debug("Decoded credential: {}", decryptedText);
|
||||
log.debug("Decoded credential: {}", decryptedText);
|
||||
|
||||
return decryptedText;
|
||||
} catch (Exception e) {
|
||||
log.error("Error decoding credentials: ", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user