Fix sso loging app creation payload

pull/305/head
Pasindu Rupasinghe 1 year ago
parent 6b2d77f33e
commit 144170c4c9

@ -86,7 +86,8 @@ public class LoginHandler extends HttpServlet {
apiRegEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC + Base64.getEncoder() apiRegEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC + Base64.getEncoder()
.encodeToString((username + HandlerConstants.COLON + password).getBytes())); .encodeToString((username + HandlerConstants.COLON + password).getBytes()));
apiRegEndpoint.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); apiRegEndpoint.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
apiRegEndpoint.setEntity(HandlerUtil.constructAppRegPayload(tags, HandlerConstants.PUBLISHER_APPLICATION_NAME, username, password)); apiRegEndpoint.setEntity(HandlerUtil.constructAppRegPayload(tags, HandlerConstants.PUBLISHER_APPLICATION_NAME,
username, password, null, null));
ProxyResponse clientAppResponse = HandlerUtil.execute(apiRegEndpoint); ProxyResponse clientAppResponse = HandlerUtil.execute(apiRegEndpoint);

@ -38,7 +38,6 @@ import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HTTP;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
@ -157,6 +156,8 @@ public class SsoLoginHandler extends HttpServlet {
JsonArray tags = uiConfigJsonObject.get("appRegistration").getAsJsonObject().get("tags").getAsJsonArray(); JsonArray tags = uiConfigJsonObject.get("appRegistration").getAsJsonObject().get("tags").getAsJsonArray();
JsonArray scopes = uiConfigJsonObject.get("scopes").getAsJsonArray(); JsonArray scopes = uiConfigJsonObject.get("scopes").getAsJsonArray();
sessionTimeOut = Integer.parseInt(String.valueOf(uiConfigJsonObject.get("sessionTimeOut"))); sessionTimeOut = Integer.parseInt(String.valueOf(uiConfigJsonObject.get("sessionTimeOut")));
JsonArray supportedGrantTypes = constructAppGrantTypeUpdateArray();
String callbackUrl = iotsCoreUrl + baseContextPath + HandlerConstants.SSO_LOGIN_CALLBACK;
// Register the client application // Register the client application
HttpPost apiRegEndpoint = new HttpPost(gatewayUrl + HandlerConstants.APP_REG_ENDPOINT); HttpPost apiRegEndpoint = new HttpPost(gatewayUrl + HandlerConstants.APP_REG_ENDPOINT);
@ -165,7 +166,8 @@ public class SsoLoginHandler extends HttpServlet {
apiRegEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC + apiRegEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC +
encodedAdminCredentials); encodedAdminCredentials);
apiRegEndpoint.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); apiRegEndpoint.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
apiRegEndpoint.setEntity(HandlerUtil.constructAppRegPayload(tags, applicationName, adminUsername, adminPassword)); apiRegEndpoint.setEntity(HandlerUtil.constructAppRegPayload(tags, applicationName, adminUsername, adminPassword,
callbackUrl, supportedGrantTypes));
ProxyResponse clientAppResponse = HandlerUtil.execute(apiRegEndpoint); ProxyResponse clientAppResponse = HandlerUtil.execute(apiRegEndpoint);
@ -325,21 +327,17 @@ public class SsoLoginHandler extends HttpServlet {
/*** /***
* Generates payload for application grant_type update payload * Generates payload for application grant_type update payload
* *
* @return {@link StringEntity} of the payload to update application grant type * @return {@link JsonArray} of the payload to update application grant type
*/ */
private StringEntity constructAppGrantTypeUpdatePayload() { private JsonArray constructAppGrantTypeUpdateArray() {
JSONObject jsonObject = new JSONObject(); JsonArray jsonArray = new JsonArray();
jsonObject.put("supportedGrantTypes",
new JSONArray(new Object[]{ jsonArray.add( HandlerConstants.CODE_GRANT_TYPE);
HandlerConstants.CODE_GRANT_TYPE, jsonArray.add( HandlerConstants.REFRESH_TOKEN_GRANT_TYPE);
HandlerConstants.REFRESH_TOKEN_GRANT_TYPE, jsonArray.add( HandlerConstants.PASSWORD_GRANT_TYPE);
HandlerConstants.PASSWORD_GRANT_TYPE, jsonArray.add( HandlerConstants.JWT_BEARER_GRANT_TYPE);
HandlerConstants.JWT_BEARER_GRANT_TYPE
}) return jsonArray;
);
jsonObject.put(HandlerConstants.CALLBACK_URL_KEY, iotsCoreUrl + baseContextPath + HandlerConstants.SSO_LOGIN_CALLBACK);
String payload = jsonObject.toString();
return new StringEntity(payload, ContentType.APPLICATION_JSON);
} }
/*** /***
@ -390,107 +388,107 @@ public class SsoLoginHandler extends HttpServlet {
return jTokenResultAsJsonObject.get("access_token").getAsString(); return jTokenResultAsJsonObject.get("access_token").getAsString();
} }
/*** // /***
* Updates Application // * Updates Application
* // *
* @param appName - Application name // * @param appName - Application name
* @throws IOException IO exception throws if an error occurred when invoking token endpoint // * @throws IOException IO exception throws if an error occurred when invoking token endpoint
* @throws ParserConfigurationException,SAXException throws if an error occurred when parsing xml // * @throws ParserConfigurationException,SAXException throws if an error occurred when parsing xml
*/ // */
private void updateSaasApp(String appName) throws ParserConfigurationException, IOException, SAXException { // private void updateSaasApp(String appName) throws ParserConfigurationException, IOException, SAXException {
File getAppRequestXmlFile = new File(HandlerConstants.PAYLOADS_DIR + "/get-app-request.xml"); // File getAppRequestXmlFile = new File(HandlerConstants.PAYLOADS_DIR + "/get-app-request.xml");
String identityAppMgtUrl = apiMgtUrl + HandlerConstants.IDENTITY_APP_MGT_ENDPOINT; // String identityAppMgtUrl = apiMgtUrl + HandlerConstants.IDENTITY_APP_MGT_ENDPOINT;
//
HttpPost getApplicationEndpoint = new HttpPost(identityAppMgtUrl); // HttpPost getApplicationEndpoint = new HttpPost(identityAppMgtUrl);
getApplicationEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC + // getApplicationEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC +
encodedAdminCredentials); // encodedAdminCredentials);
getApplicationEndpoint.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_XML.toString()); // getApplicationEndpoint.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_XML.toString());
getApplicationEndpoint.setHeader(HandlerConstants.SOAP_ACTION_HEADER, "urn:getApplication"); // getApplicationEndpoint.setHeader(HandlerConstants.SOAP_ACTION_HEADER, "urn:getApplication");
//
String requestBodyString = HandlerUtil.xmlToString(getAppRequestXmlFile); // String requestBodyString = HandlerUtil.xmlToString(getAppRequestXmlFile);
//
Map<String, String> data = new HashMap<>(); // Map<String, String> data = new HashMap<>();
appName = adminUsername + HandlerConstants.UNDERSCORE + appName + HandlerConstants.UNDERSCORE + // appName = adminUsername + HandlerConstants.UNDERSCORE + appName + HandlerConstants.UNDERSCORE +
HandlerConstants.PRODUCTION_KEY; // HandlerConstants.PRODUCTION_KEY;
data.put("applicationName", appName); // data.put("applicationName", appName);
requestBodyString = StrSubstitutor.replace(requestBodyString, data); // requestBodyString = StrSubstitutor.replace(requestBodyString, data);
getApplicationEndpoint.setEntity(new StringEntity(requestBodyString, ContentType.TEXT_XML)); // getApplicationEndpoint.setEntity(new StringEntity(requestBodyString, ContentType.TEXT_XML));
//
ProxyResponse getApplicationEndpointResponse = HandlerUtil.execute(getApplicationEndpoint); // ProxyResponse getApplicationEndpointResponse = HandlerUtil.execute(getApplicationEndpoint);
//
Document doc = DocumentBuilderFactory.newInstance() // Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder() // .newDocumentBuilder()
.parse(new InputSource(new StringReader(getApplicationEndpointResponse.getData()))); // .parse(new InputSource(new StringReader(getApplicationEndpointResponse.getData())));
NodeList nodeList = doc.getElementsByTagName(HandlerConstants.AX_PREFIX + "inboundAuthenticationConfig"); // NodeList nodeList = doc.getElementsByTagName(HandlerConstants.AX_PREFIX + "inboundAuthenticationConfig");
NodeList childNodeList; // NodeList childNodeList;
String nodeName; // String nodeName;
//
data.clear(); // data.clear();
data.put("applicationId", doc.getElementsByTagName(HandlerConstants.AX_PREFIX + "applicationID").item(0).getTextContent()); // data.put("applicationId", doc.getElementsByTagName(HandlerConstants.AX_PREFIX + "applicationID").item(0).getTextContent());
data.put("applicationName", doc.getElementsByTagName(HandlerConstants.AX_PREFIX + "applicationName").item(0).getTextContent()); // data.put("applicationName", doc.getElementsByTagName(HandlerConstants.AX_PREFIX + "applicationName").item(0).getTextContent());
data.put("description", doc.getElementsByTagName(HandlerConstants.AX_PREFIX + "description").item(0).getTextContent()); // data.put("description", doc.getElementsByTagName(HandlerConstants.AX_PREFIX + "description").item(0).getTextContent());
data.put("saasApp", "true"); // data.put("saasApp", "true");
//
for (int i = 0; i < nodeList.getLength(); i++) { // for (int i = 0; i < nodeList.getLength(); i++) {
childNodeList = nodeList.item(i).getChildNodes(); // childNodeList = nodeList.item(i).getChildNodes();
for (int j = 0; j < childNodeList.getLength(); j++) { // for (int j = 0; j < childNodeList.getLength(); j++) {
if (childNodeList.item(j).getNodeName().equalsIgnoreCase(HandlerConstants.AX_PREFIX + "inboundAuthenticationRequestConfigs")) { // if (childNodeList.item(j).getNodeName().equalsIgnoreCase(HandlerConstants.AX_PREFIX + "inboundAuthenticationRequestConfigs")) {
NodeList inboundAuthRequestConfigs = childNodeList.item(j).getChildNodes(); // NodeList inboundAuthRequestConfigs = childNodeList.item(j).getChildNodes();
for (int k = 0; k < inboundAuthRequestConfigs.getLength(); k++) { // for (int k = 0; k < inboundAuthRequestConfigs.getLength(); k++) {
nodeName = inboundAuthRequestConfigs.item(k).getNodeName(); // nodeName = inboundAuthRequestConfigs.item(k).getNodeName();
if (nodeName.equalsIgnoreCase(HandlerConstants.AX_PREFIX + "inboundAuthKey")) { // if (nodeName.equalsIgnoreCase(HandlerConstants.AX_PREFIX + "inboundAuthKey")) {
data.put("inboundAuthKey", inboundAuthRequestConfigs.item(k).getTextContent()); // data.put("inboundAuthKey", inboundAuthRequestConfigs.item(k).getTextContent());
} // }
if (nodeName.equalsIgnoreCase(HandlerConstants.AX_PREFIX + "inboundAuthType")) { // if (nodeName.equalsIgnoreCase(HandlerConstants.AX_PREFIX + "inboundAuthType")) {
data.put("inboundAuthType", inboundAuthRequestConfigs.item(k).getTextContent()); // data.put("inboundAuthType", inboundAuthRequestConfigs.item(k).getTextContent());
} // }
if (nodeName.equalsIgnoreCase(HandlerConstants.AX_PREFIX + "inboundConfigType")) { // if (nodeName.equalsIgnoreCase(HandlerConstants.AX_PREFIX + "inboundConfigType")) {
data.put("inboundConfigType", inboundAuthRequestConfigs.item(k).getTextContent()); // data.put("inboundConfigType", inboundAuthRequestConfigs.item(k).getTextContent());
} // }
} // }
} // }
} // }
} // }
//
nodeList = doc.getElementsByTagName(HandlerConstants.AX_PREFIX + "owner"); // nodeList = doc.getElementsByTagName(HandlerConstants.AX_PREFIX + "owner");
for (int i = 0; i < nodeList.getLength(); i++) { // for (int i = 0; i < nodeList.getLength(); i++) {
childNodeList = nodeList.item(i).getChildNodes(); // childNodeList = nodeList.item(i).getChildNodes();
for (int j = 0; j < childNodeList.getLength(); j++) { // for (int j = 0; j < childNodeList.getLength(); j++) {
switch (childNodeList.item(j).getNodeName()) { // switch (childNodeList.item(j).getNodeName()) {
case HandlerConstants.AX_PREFIX + "tenantDomain": // case HandlerConstants.AX_PREFIX + "tenantDomain":
data.put("tenantDomain", childNodeList.item(j).getTextContent()); // data.put("tenantDomain", childNodeList.item(j).getTextContent());
break; // break;
case HandlerConstants.AX_PREFIX + "userName": // case HandlerConstants.AX_PREFIX + "userName":
data.put("userName", childNodeList.item(j).getTextContent()); // data.put("userName", childNodeList.item(j).getTextContent());
break; // break;
case HandlerConstants.AX_PREFIX + "userStoreDomain": // case HandlerConstants.AX_PREFIX + "userStoreDomain":
data.put("userStoreDomain", childNodeList.item(j).getTextContent()); // data.put("userStoreDomain", childNodeList.item(j).getTextContent());
break; // break;
} // }
} // }
} // }
//
nodeList = doc.getElementsByTagName(HandlerConstants.AX_PREFIX + "spProperties"); // nodeList = doc.getElementsByTagName(HandlerConstants.AX_PREFIX + "spProperties");
for (int k = 0; k < nodeList.getLength(); k++) { // for (int k = 0; k < nodeList.getLength(); k++) {
childNodeList = nodeList.item(k).getChildNodes(); // childNodeList = nodeList.item(k).getChildNodes();
for (int l = 0; l < childNodeList.getLength(); l++) { // for (int l = 0; l < childNodeList.getLength(); l++) {
if (childNodeList.item(l).getNodeName().equalsIgnoreCase(HandlerConstants.AX_PREFIX + "value")) { // if (childNodeList.item(l).getNodeName().equalsIgnoreCase(HandlerConstants.AX_PREFIX + "value")) {
data.put("displayName", childNodeList.item(l).getTextContent()); // data.put("displayName", childNodeList.item(l).getTextContent());
} // }
} // }
} // }
//
File appUpdateRequest = new File(HandlerConstants.PAYLOADS_DIR + "/update-app-request.xml"); // File appUpdateRequest = new File(HandlerConstants.PAYLOADS_DIR + "/update-app-request.xml");
String docStr = HandlerUtil.xmlToString(appUpdateRequest); // String docStr = HandlerUtil.xmlToString(appUpdateRequest);
requestBodyString = StrSubstitutor.replace(docStr, data); // requestBodyString = StrSubstitutor.replace(docStr, data);
//
HttpPost updateApplicationEndpoint = new HttpPost(identityAppMgtUrl); // HttpPost updateApplicationEndpoint = new HttpPost(identityAppMgtUrl);
updateApplicationEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC + // updateApplicationEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC +
encodedAdminCredentials); // encodedAdminCredentials);
updateApplicationEndpoint.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_XML.toString()); // updateApplicationEndpoint.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_XML.toString());
updateApplicationEndpoint.setHeader(HandlerConstants.SOAP_ACTION_HEADER, "urn:updateApplication"); // updateApplicationEndpoint.setHeader(HandlerConstants.SOAP_ACTION_HEADER, "urn:updateApplication");
updateApplicationEndpoint.setEntity(new StringEntity(requestBodyString, ContentType.TEXT_XML)); // updateApplicationEndpoint.setEntity(new StringEntity(requestBodyString, ContentType.TEXT_XML));
//
HandlerUtil.execute(updateApplicationEndpoint); // HandlerUtil.execute(updateApplicationEndpoint);
} // }
} }

@ -43,6 +43,7 @@ public class HandlerConstants {
public static final String SESSION_DEFAULT_AUTH_DATA_KEY = "defaultAuthInfo"; public static final String SESSION_DEFAULT_AUTH_DATA_KEY = "defaultAuthInfo";
public static final String UI_CONFIG_KEY = "ui-config"; public static final String UI_CONFIG_KEY = "ui-config";
public static final String CALLBACK_URL_KEY = "callbackUrl"; public static final String CALLBACK_URL_KEY = "callbackUrl";
public static final String GRANT_TYPE_KEY = "supportedGrantTypes";
public static final String IS_ALLOWED_TO_ALL_DOMAINS_KEY = "isAllowedToAllDomains"; public static final String IS_ALLOWED_TO_ALL_DOMAINS_KEY = "isAllowedToAllDomains";
public static final String JSESSIONID_KEY = "JSESSIONID"; public static final String JSESSIONID_KEY = "JSESSIONID";
public static final String COMMON_AUTH_ID_KEY = "commonAuthId"; public static final String COMMON_AUTH_ID_KEY = "commonAuthId";

@ -461,15 +461,25 @@ public class HandlerUtil {
* @param tags - tags which are retrieved by reading app manager configuration * @param tags - tags which are retrieved by reading app manager configuration
* @param username - username provided from login form or admin username * @param username - username provided from login form or admin username
* @param password - password provided from login form or admin password * @param password - password provided from login form or admin password
* @param callbackUrl - callback url
* @param supportedGrantTypes - supported grant types
* @return {@link StringEntity} of the payload to create the client application * @return {@link StringEntity} of the payload to create the client application
*/ */
public static StringEntity constructAppRegPayload(JsonArray tags, String appName, String username, String password) { public static StringEntity constructAppRegPayload(JsonArray tags, String appName, String username, String password,
String callbackUrl, JsonArray supportedGrantTypes) {
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(HandlerConstants.APP_NAME_KEY, appName); jsonObject.addProperty(HandlerConstants.APP_NAME_KEY, appName);
jsonObject.addProperty(HandlerConstants.USERNAME, username); jsonObject.addProperty(HandlerConstants.USERNAME, username);
jsonObject.addProperty(HandlerConstants.PASSWORD, password); jsonObject.addProperty(HandlerConstants.PASSWORD, password);
jsonObject.addProperty(HandlerConstants.IS_ALLOWED_TO_ALL_DOMAINS_KEY, "false"); jsonObject.addProperty(HandlerConstants.IS_ALLOWED_TO_ALL_DOMAINS_KEY, "false");
jsonObject.add(HandlerConstants.TAGS_KEY, tags); jsonObject.add(HandlerConstants.TAGS_KEY, tags);
if (callbackUrl != null) {
jsonObject.addProperty(HandlerConstants.CALLBACK_URL_KEY, callbackUrl);
}
if (supportedGrantTypes != null) {
jsonObject.add(HandlerConstants.GRANT_TYPE_KEY, supportedGrantTypes);
}
String payload = jsonObject.toString(); String payload = jsonObject.toString();
return new StringEntity(payload, ContentType.APPLICATION_JSON); return new StringEntity(payload, ContentType.APPLICATION_JSON);
} }

Loading…
Cancel
Save