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.ApiDefinition32;
|
||||||
import cz.trask.migration.model.ApiDefinition45;
|
import cz.trask.migration.model.ApiDefinition45;
|
||||||
import cz.trask.migration.model.ApiDefinition45.ApiPolicies;
|
import cz.trask.migration.model.ApiDefinition45.ApiPolicies;
|
||||||
|
import cz.trask.migration.util.CredentialsDecoder;
|
||||||
|
|
||||||
public class ApiDefinitionMapper {
|
public class ApiDefinitionMapper {
|
||||||
|
|
||||||
@ -216,7 +217,7 @@ public class ApiDefinitionMapper {
|
|||||||
newSec.setType(oldSec.getType());
|
newSec.setType(oldSec.getType());
|
||||||
newSec.setTokenUrl(oldSec.getTokenUrl());
|
newSec.setTokenUrl(oldSec.getTokenUrl());
|
||||||
newSec.setClientId(oldSec.getClientId());
|
newSec.setClientId(oldSec.getClientId());
|
||||||
newSec.setClientSecret(oldSec.getClientSecret());
|
newSec.setClientSecret(CredentialsDecoder.decodeCredentials(oldSec.getClientSecret(), "wso2apim32-pk.pem"));
|
||||||
newSec.setUsername(oldSec.getUsername());
|
newSec.setUsername(oldSec.getUsername());
|
||||||
newSec.setPassword(oldSec.getPassword());
|
newSec.setPassword(oldSec.getPassword());
|
||||||
newSec.setGrantType(oldSec.getGrantType());
|
newSec.setGrantType(oldSec.getGrantType());
|
||||||
|
|||||||
@ -16,34 +16,45 @@ import lombok.extern.log4j.Log4j2;
|
|||||||
|
|
||||||
@Log4j2
|
@Log4j2
|
||||||
public class CredentialsDecoder {
|
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);
|
|
||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
public static String decodeCredentials(String credentials, String pkFile) {
|
||||||
Map<String, String> jsonMap = mapper.readValue(decodedJson, Map.class);
|
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);
|
||||||
|
|
||||||
String cipherBase64 = jsonMap.get("c");
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
String transformation = jsonMap.get("t");
|
Map<String, String> jsonMap = mapper.readValue(decodedJson, Map.class);
|
||||||
log.debug("Used algorithm: {}", transformation);
|
|
||||||
|
|
||||||
String privateKeyPEM = new String(Files.readAllBytes(Paths.get(pkFile)))
|
String cipherBase64 = jsonMap.get("c");
|
||||||
.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "")
|
String transformation = jsonMap.get("t");
|
||||||
.replaceAll("\\s+", "");
|
log.debug("Used algorithm: {}", transformation);
|
||||||
byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyPEM);
|
|
||||||
|
|
||||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
|
String privateKeyPEM = new String(Files.readAllBytes(Paths.get(pkFile)))
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "")
|
||||||
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
|
.replaceAll("\\s+", "");
|
||||||
|
byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyPEM);
|
||||||
|
|
||||||
byte[] encryptedBytes = Base64.getDecoder().decode(cipherBase64);
|
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
|
||||||
Cipher cipher = Cipher.getInstance(transformation);
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
|
||||||
|
|
||||||
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
|
byte[] encryptedBytes = Base64.getDecoder().decode(cipherBase64);
|
||||||
String decryptedText = new String(decryptedBytes, "UTF-8");
|
Cipher cipher = Cipher.getInstance(transformation);
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||||
|
|
||||||
log.debug("Decoded credential: {}", decryptedText);
|
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
|
||||||
|
String decryptedText = new String(decryptedBytes, "UTF-8");
|
||||||
|
|
||||||
|
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