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.
4.x.x
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 {
// 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";
}
}

@ -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";
}

@ -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<String, String> drcHeaders = new HashMap<String, String>();
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<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());
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);
}

Loading…
Cancel
Save