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.
revert-70aa11f8
Madawa Soysa 8 years ago committed by amalhub
parent a9888b5cf7
commit 0eeef0a568

@ -24,14 +24,12 @@ package org.wso2.carbon.apimgt.handlers.beans;
*/ */
public class DCR { public class DCR {
// Owner of the application private String callbackUrl;
private String owner; private String owner;
// Client name
private String clientName; private String clientName;
// Oauth Grant type
private String grantType; private String grantType;
// Scope of the token
private String tokenScope; private String tokenScope;
private boolean isSaasApp;
public String getOwner() { public String getOwner() {
return owner; return owner;
@ -64,5 +62,27 @@ public class DCR {
public void setTokenScope(String tokenScope) { public void setTokenScope(String tokenScope) {
this.tokenScope = 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";
}
} }

@ -36,4 +36,14 @@ public class AuthConstants {
public static final String MDM_SIGNATURE = "mdm-signature"; public static final String MDM_SIGNATURE = "mdm-signature";
public static final String PROXY_MUTUAL_AUTH_HEADER = "proxy-mutual-auth-header"; public static final String PROXY_MUTUAL_AUTH_HEADER = "proxy-mutual-auth-header";
public static final String ENCODED_PEM = "encoded-pem"; 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";
} }

@ -19,7 +19,6 @@
package org.wso2.carbon.apimgt.handlers.utils; package org.wso2.carbon.apimgt.handlers.utils;
import com.google.gson.Gson;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.ws.security.util.Base64; import org.apache.ws.security.util.Base64;
@ -118,8 +117,7 @@ public class Utils {
tokenHeaders.put("Content-Type", "application/x-www-form-urlencoded"); tokenHeaders.put("Content-Type", "application/x-www-form-urlencoded");
RESTInvoker restInvoker = new RESTInvoker(); RESTInvoker restInvoker = new RESTInvoker();
RESTResponse response = restInvoker.invokePOST(tokenUrl, tokenHeaders, null, RESTResponse response = restInvoker.invokePOST(tokenUrl, tokenHeaders, null, null, tokenContent);
null, tokenContent);
if(log.isDebugEnabled()) { if(log.isDebugEnabled()) {
log.debug("Token response:" + response.getContent()); log.debug("Token response:" + response.getContent());
} }
@ -144,31 +142,32 @@ public class Utils {
private static void getClientSecretes(IOTServerConfiguration iotServerConfiguration) private static void getClientSecretes(IOTServerConfiguration iotServerConfiguration)
throws APIMCertificateMGTException { throws APIMCertificateMGTException {
try { try {
String username = iotServerConfiguration.getUsername();
String password = iotServerConfiguration.getPassword();
DCR dcr = new DCR(); DCR dcr = new DCR();
dcr.setOwner(iotServerConfiguration.getUsername()); dcr.setOwner(iotServerConfiguration.getUsername());
dcr.setClientName("IOT-API-MANAGER"); dcr.setClientName(AuthConstants.CLIENT_NAME);
dcr.setGrantType("refresh_token password client_credentials"); dcr.setGrantType(AuthConstants.GRANT_TYPE);
dcr.setTokenScope("default"); dcr.setTokenScope(AuthConstants.TOKEN_SCOPE);
Gson gson = new Gson(); dcr.setCallbackUrl(AuthConstants.CALLBACK_URL);
String dcrContent = gson.toJson(dcr); dcr.setIsSaasApp(true);
Map<String, String> drcHeaders = new HashMap<String, String>(); String dcrContent = dcr.toJSON();
drcHeaders.put("Content-Type", "application/json"); Map<String, String> dcrHeaders = new HashMap<String, String>();
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()); URI dcrUrl = new URI(iotServerConfiguration.getDynamicClientRegistrationEndpoint());
RESTInvoker restInvoker = new RESTInvoker(); RESTInvoker restInvoker = new RESTInvoker();
RESTResponse response = restInvoker.invokePOST(dcrUrl, drcHeaders, null, RESTResponse response = restInvoker.invokePOST(dcrUrl, dcrHeaders, null, null, dcrContent);
null, dcrContent);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("DCR response :" + response.getContent()); log.debug("DCR response :" + response.getContent());
} }
JSONObject jsonResponse = new JSONObject(response.getContent()); JSONObject jsonResponse = new JSONObject(response.getContent());
clientId = jsonResponse.getString("client_id"); clientId = jsonResponse.getString(AuthConstants.CLIENT_ID);
clientSecret = jsonResponse.getString("client_secret"); clientSecret = jsonResponse.getString(AuthConstants.CLIENT_SECRET);
} catch (JSONException e) { } catch (JSONException e) {
throw new APIMCertificateMGTException("Error occurred while converting the json to object", e); throw new APIMCertificateMGTException("Error occurred while converting the json to object", e);
} catch (IOException e) { } catch (IOException | URISyntaxException e) {
throw new APIMCertificateMGTException("Error occurred while trying to call DCR endpoint", e);
} catch (URISyntaxException e) {
throw new APIMCertificateMGTException("Error occurred while trying to call DCR endpoint", e); throw new APIMCertificateMGTException("Error occurred while trying to call DCR endpoint", e);
} }

Loading…
Cancel
Save