Re: MLE for APIs isn't working as expected

hgebreselassie
Regular Visitor

MLE for APIs isn't working as expected

I have tried to implement MLE with spring REST template and the api call seems getting some authentication error this is sample code I used 

Spoiler
HttpHeaders newHeaders = new HttpHeaders() {{

String auth = visaBasicAuthUsername + ":" + visaBasicAuthPassword;
Charset charset = StandardCharsets.UTF_8;

byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(charset));
String authHeader = "Basic " + new String(encodedAuth, charset);

set("Authorization", authHeader);

}};

//newHeaders.setBasicAuth(visaBasicAuthUsername, visaBasicAuthPassword);
newHeaders.setContentType(MediaType.APPLICATION_JSON);
newHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
newHeaders.set("keyId", keyId);

HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.sslContext(sslcontext)
.connectTimeout(Duration.ofSeconds(10))
.build();

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

String plainText = visaForeignExchangeLookUpRequest == null ? "" : mapper.writeValueAsString(visaForeignExchangeLookUpRequest);
JWEHeader.Builder headerBuilder = new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP_256, EncryptionMethod.A128GCM);

headerBuilder.keyID(keyId);
headerBuilder.type(JOSEObjectType.JOSE);
headerBuilder.customParam("iat", System.currentTimeMillis());

JWEObject jweObject = new JWEObject(headerBuilder.build(), new Payload(plainText));
RSAPublicKey publicKey = VisaCertificateUtils.loadPublicKeyFromFile(publicCertificatePath);
jweObject.encrypt(new RSAEncrypter(getRSAPublicKey(publicCertificatePath)));

HttpEntity<?> body = new HttpEntity<>(VisaRequestEncryptor.getEncryptedPayload(visaForeignExchangeLookUpRequest, keyId, mleServerPublicCertificatePath), newHeaders);

ResponseEntity<String> encryptedResponseStr = restTemplate.postForEntity("https://sandbox.api.visa.com/forexrates/v2/foreignexchangerates", body, String.class);

the response message is

Spoiler
 "{"responseStatus":{"status":400,"code":"9123","severity":"ERROR","message":"Expected input credential was not present","info":""}}" 
2 REPLIES 2
Mfaiz
Community Moderator

Re: MLE for APIs isn't working as expected

Hi @hgebreselassie! Thank you for reaching out. One of our agents will look into this and get back to you soon. Until then, if any community members know a solution, please feel free to reply to this thread.

API_Products
Visa Developer Support Specialist

Re: MLE for APIs isn't working as expected

Hi @hgebreselassie,

 

To troubleshoot and fix the error, please refer to this community forum post here




Thanks,

Diana H.



Was your question answered? Don't forget to click on "Accept as Solution" to help other devs find the answer to the same question.