From 0eeef0a5686abed9c825cf26268dea5a05c1831e Mon Sep 17 00:00:00 2001 From: Madawa Soysa Date: Wed, 19 Apr 2017 22:05:25 +0530 Subject: [PATCH] Refactoring to use APIManager DCR This commit is done to refactor the Auth application creation process to use the APIManager Key manager client registration endpoint instead of IOT DCR endpoint. --- .../carbon/apimgt/handlers/beans/DCR.java | 28 ++++++++++++--- .../apimgt/handlers/utils/AuthConstants.java | 10 ++++++ .../carbon/apimgt/handlers/utils/Utils.java | 35 +++++++++---------- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/beans/DCR.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/beans/DCR.java index 4091afcb9e..ae5e42b59a 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/beans/DCR.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/beans/DCR.java @@ -24,14 +24,12 @@ package org.wso2.carbon.apimgt.handlers.beans; */ public class DCR { - // Owner of the application + private String callbackUrl; private String owner; - // Client name private String clientName; - // Oauth Grant type private String grantType; - // Scope of the token private String tokenScope; + private boolean isSaasApp; public String getOwner() { return owner; @@ -64,5 +62,27 @@ public class DCR { public void setTokenScope(String tokenScope) { this.tokenScope = tokenScope; } + + public boolean getIsSaasApp() { + return isSaasApp; + } + + public void setIsSaasApp(boolean isSaasApp) { + this.isSaasApp = isSaasApp; + } + + public String getCallbackUrl() { + return callbackUrl; + } + + public void setCallbackUrl(String callbackUrl) { + this.callbackUrl = callbackUrl; + } + + public String toJSON() { + return "{\"callbackUrl\": \"" + callbackUrl + "\",\"clientName\": \"" + clientName + "\", \"tokenScope\": " + + "\"" + tokenScope + "\", \"owner\": \"" + owner + "\"," + "\"grantType\": \"" + grantType + + "\", \"saasApp\" :" + isSaasApp + " }\n"; + } } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/utils/AuthConstants.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/utils/AuthConstants.java index 531c7c11d5..a1343c312b 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/utils/AuthConstants.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/utils/AuthConstants.java @@ -36,4 +36,14 @@ public class AuthConstants { public static final String MDM_SIGNATURE = "mdm-signature"; public static final String PROXY_MUTUAL_AUTH_HEADER = "proxy-mutual-auth-header"; public static final String ENCODED_PEM = "encoded-pem"; + public static final String CALLBACK_URL = ""; + public static final String CLIENT_NAME = "IOT-API-MANAGER"; + public static final String GRANT_TYPE = "refresh_token password client_credentials"; + public static final String TOKEN_SCOPE = "default"; + public static final String CONTENT_TYPE_HEADER = "Content-Type"; + public static final String CONTENT_TYPE = "application/json"; + public static final String AUTHORIZATION_HEADER = "Authorization"; + public static final String BASIC_AUTH_PREFIX = "Basic "; + public static final String CLIENT_ID = "clientId"; + public static final String CLIENT_SECRET = "clientSecret"; } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/utils/Utils.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/utils/Utils.java index 3e66ed7edd..d3c8ad3a9f 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/utils/Utils.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/utils/Utils.java @@ -19,7 +19,6 @@ package org.wso2.carbon.apimgt.handlers.utils; -import com.google.gson.Gson; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.ws.security.util.Base64; @@ -118,8 +117,7 @@ public class Utils { tokenHeaders.put("Content-Type", "application/x-www-form-urlencoded"); RESTInvoker restInvoker = new RESTInvoker(); - RESTResponse response = restInvoker.invokePOST(tokenUrl, tokenHeaders, null, - null, tokenContent); + RESTResponse response = restInvoker.invokePOST(tokenUrl, tokenHeaders, null, null, tokenContent); if(log.isDebugEnabled()) { log.debug("Token response:" + response.getContent()); } @@ -144,31 +142,32 @@ public class Utils { private static void getClientSecretes(IOTServerConfiguration iotServerConfiguration) throws APIMCertificateMGTException { try { + String username = iotServerConfiguration.getUsername(); + String password = iotServerConfiguration.getPassword(); DCR dcr = new DCR(); dcr.setOwner(iotServerConfiguration.getUsername()); - dcr.setClientName("IOT-API-MANAGER"); - dcr.setGrantType("refresh_token password client_credentials"); - dcr.setTokenScope("default"); - Gson gson = new Gson(); - String dcrContent = gson.toJson(dcr); - Map drcHeaders = new HashMap(); - drcHeaders.put("Content-Type", "application/json"); + dcr.setClientName(AuthConstants.CLIENT_NAME); + dcr.setGrantType(AuthConstants.GRANT_TYPE); + dcr.setTokenScope(AuthConstants.TOKEN_SCOPE); + dcr.setCallbackUrl(AuthConstants.CALLBACK_URL); + dcr.setIsSaasApp(true); + String dcrContent = dcr.toJSON(); + Map dcrHeaders = new HashMap(); + String basicAuth = Base64.encode((username + ":" + password).getBytes()); + dcrHeaders.put(AuthConstants.CONTENT_TYPE_HEADER, AuthConstants.CONTENT_TYPE); + dcrHeaders.put(AuthConstants.AUTHORIZATION_HEADER, AuthConstants.BASIC_AUTH_PREFIX + basicAuth); URI dcrUrl = new URI(iotServerConfiguration.getDynamicClientRegistrationEndpoint()); RESTInvoker restInvoker = new RESTInvoker(); - RESTResponse response = restInvoker.invokePOST(dcrUrl, drcHeaders, null, - null, dcrContent); - + RESTResponse response = restInvoker.invokePOST(dcrUrl, dcrHeaders, null, null, dcrContent); if (log.isDebugEnabled()) { log.debug("DCR response :" + response.getContent()); } JSONObject jsonResponse = new JSONObject(response.getContent()); - clientId = jsonResponse.getString("client_id"); - clientSecret = jsonResponse.getString("client_secret"); + clientId = jsonResponse.getString(AuthConstants.CLIENT_ID); + clientSecret = jsonResponse.getString(AuthConstants.CLIENT_SECRET); } catch (JSONException e) { throw new APIMCertificateMGTException("Error occurred while converting the json to object", e); - } catch (IOException e) { - throw new APIMCertificateMGTException("Error occurred while trying to call DCR endpoint", e); - } catch (URISyntaxException e) { + } catch (IOException | URISyntaxException e) { throw new APIMCertificateMGTException("Error occurred while trying to call DCR endpoint", e); }