Merge branch 'mssl-fix' into 'master'

Remove additional HTTP call from mssl handler

Closes product-iots#128

See merge request entgra/carbon-device-mgt!202
revert-70aa11f8
Charitha Goonetilleke 5 years ago
commit 9dfe4abaac

@ -95,7 +95,7 @@ public class AuthenticationHandler extends AbstractHandler {
log.debug("Verify Cert:\n" + mdmSignature); log.debug("Verify Cert:\n" + mdmSignature);
} }
URI certVerifyUrl = new URI(iotServerConfiguration.getVerificationEndpoint() + "ios"); URI certVerifyUrl = new URI(iotServerConfiguration.getVerificationEndpoint() + "ios");
Map<String, String> certVerifyHeaders = this.setHeaders(this.restInvoker); Map<String, String> certVerifyHeaders = this.setHeaders();
Certificate certificate = new Certificate(); Certificate certificate = new Certificate();
certificate.setPem(mdmSignature); certificate.setPem(mdmSignature);
@ -127,7 +127,7 @@ public class AuthenticationHandler extends AbstractHandler {
String deviceType = this.getDeviceType(messageContext.getTo().getAddress().trim()); String deviceType = this.getDeviceType(messageContext.getTo().getAddress().trim());
URI certVerifyUrl = new URI(iotServerConfiguration.getVerificationEndpoint() + deviceType); URI certVerifyUrl = new URI(iotServerConfiguration.getVerificationEndpoint() + deviceType);
Map<String, String> certVerifyHeaders = this.setHeaders(this.restInvoker); Map<String, String> certVerifyHeaders = this.setHeaders();
Certificate certificate = new Certificate(); Certificate certificate = new Certificate();
certificate.setPem(subjectDN); certificate.setPem(subjectDN);
certificate.setTenantId(tenantId); certificate.setTenantId(tenantId);
@ -157,7 +157,7 @@ public class AuthenticationHandler extends AbstractHandler {
} }
String deviceType = this.getDeviceType(messageContext.getTo().getAddress().trim()); String deviceType = this.getDeviceType(messageContext.getTo().getAddress().trim());
URI certVerifyUrl = new URI(iotServerConfiguration.getVerificationEndpoint() + deviceType); URI certVerifyUrl = new URI(iotServerConfiguration.getVerificationEndpoint() + deviceType);
Map<String, String> certVerifyHeaders = this.setHeaders(this.restInvoker); Map<String, String> certVerifyHeaders = this.setHeaders();
Certificate certificate = new Certificate(); Certificate certificate = new Certificate();
certificate.setPem(encodedPem); certificate.setPem(encodedPem);
@ -184,9 +184,6 @@ public class AuthenticationHandler extends AbstractHandler {
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
log.error("Error while processing certificate.", e); log.error("Error while processing certificate.", e);
return false; return false;
} catch (APIMCertificateMGTException e) {
log.error("Error while processing certificate.", e);
return false;
} catch (CertificateException e) { } catch (CertificateException e) {
log.error("Certificate issue occurred when generating converting PEM to x509Certificate", e); log.error("Certificate issue occurred when generating converting PEM to x509Certificate", e);
return false; return false;
@ -212,9 +209,9 @@ public class AuthenticationHandler extends AbstractHandler {
return null; return null;
} }
private Map<String, String> setHeaders(RESTInvoker restInvoker) throws APIMCertificateMGTException { private Map<String, String> setHeaders() {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
String accessToken = Utils.getAccessToken(iotServerConfiguration, restInvoker); String accessToken = Utils.getBase64EncodedToken(iotServerConfiguration);
map.put(AUTHORIZATION, BEARER + accessToken); map.put(AUTHORIZATION, BEARER + accessToken);
map.put(CONTENT_TYPE, "application/json"); map.put(CONTENT_TYPE, "application/json");
return map; return map;

@ -135,38 +135,14 @@ public class Utils {
} }
/** /**
* This class get the access token from the key manager. * This method is used to get the base64 encoded token.
* *
* @param iotServerConfiguration Instance of the IoTsererConfiguration. * @param iotServerConfiguration Instance of the IoTsererConfiguration.
* @return Access token will be returned. * @return Access token will be returned.
* @throws APIMCertificateMGTException
*/ */
public static String getAccessToken(IOTServerConfiguration iotServerConfiguration, RESTInvoker restInvoker) public static String getBase64EncodedToken(IOTServerConfiguration iotServerConfiguration) {
throws APIMCertificateMGTException { return Base64.encode((iotServerConfiguration.getUsername() + ":" + iotServerConfiguration.getPassword()).
try { getBytes());
if (clientId == null || clientSecret == null) {
getClientSecretes(iotServerConfiguration, restInvoker);
}
URI tokenUrl = new URI(iotServerConfiguration.getOauthTokenEndpoint());
String tokenContent = "grant_type=password&username=" + iotServerConfiguration.getUsername() + "&password=" +
iotServerConfiguration.getPassword() + "&scope=activity-view";
String tokenBasicAuth = "Basic " + Base64.encode((clientId + ":" + clientSecret).getBytes());
Map<String, String> tokenHeaders = new HashMap<>();
tokenHeaders.put("Authorization", tokenBasicAuth);
tokenHeaders.put("Content-Type", "application/x-www-form-urlencoded");
RESTResponse response = restInvoker.invokePOST(tokenUrl, tokenHeaders, tokenContent);
if (log.isDebugEnabled()) {
log.debug("Token response:" + response.getContent());
}
JSONObject jsonResponse = new JSONObject(response.getContent());
return jsonResponse.getString("access_token");
} catch (URISyntaxException | IOException e) {
throw new APIMCertificateMGTException("Error occurred while trying to call oauth token endpoint", e);
} catch (JSONException e) {
throw new APIMCertificateMGTException("Error occurred while converting the json to object", e);
}
} }
/** /**

Loading…
Cancel
Save