forked from community/device-mgt-core
parent
aba133503d
commit
1d0a087734
@ -1,277 +1,298 @@
|
|||||||
///*
|
/*
|
||||||
// * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
* Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||||
// *
|
*
|
||||||
// * Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||||
// * Version 2.0 (the "License"); you may not use this file except
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
// * in compliance with the License.
|
* in compliance with the License.
|
||||||
// * You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
// *
|
*
|
||||||
// * http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
// *
|
*
|
||||||
// * Unless required by applicable law or agreed to in writing,
|
* Unless required by applicable law or agreed to in writing,
|
||||||
// * software distributed under the License is distributed on an
|
* software distributed under the License is distributed on an
|
||||||
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
// * KIND, either express or implied. See the License for the
|
* KIND, either express or implied. See the License for the
|
||||||
// * specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
// * under the License.
|
* under the License.
|
||||||
// */
|
*/
|
||||||
//
|
|
||||||
//package io.entgra.device.mgt.core.ui.request.interceptor;
|
package io.entgra.device.mgt.core.ui.request.interceptor;
|
||||||
//
|
|
||||||
//import com.google.gson.JsonArray;
|
import com.fasterxml.jackson.core.JsonFactory;
|
||||||
//import com.google.gson.JsonElement;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
//import com.google.gson.JsonObject;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
//import com.google.gson.JsonParser;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
//import io.entgra.device.mgt.core.ui.request.interceptor.beans.AuthData;
|
import com.google.gson.JsonArray;
|
||||||
//import io.entgra.device.mgt.core.ui.request.interceptor.beans.JITData;
|
import com.google.gson.JsonElement;
|
||||||
//import io.entgra.device.mgt.core.ui.request.interceptor.beans.JITEnrollmentData;
|
import com.google.gson.JsonObject;
|
||||||
//import io.entgra.device.mgt.core.ui.request.interceptor.beans.ProxyResponse;
|
import com.google.gson.JsonParser;
|
||||||
//import io.entgra.device.mgt.core.ui.request.interceptor.exceptions.JITEnrollmentException;
|
import io.entgra.device.mgt.core.ui.request.interceptor.beans.AuthData;
|
||||||
//import io.entgra.device.mgt.core.ui.request.interceptor.util.HandlerConstants;
|
import io.entgra.device.mgt.core.ui.request.interceptor.beans.JITData;
|
||||||
//import io.entgra.device.mgt.core.ui.request.interceptor.util.HandlerUtil;
|
import io.entgra.device.mgt.core.ui.request.interceptor.beans.JITEnrollmentData;
|
||||||
//import org.apache.commons.httpclient.HttpStatus;
|
import io.entgra.device.mgt.core.ui.request.interceptor.beans.ProxyResponse;
|
||||||
//import org.apache.commons.logging.Log;
|
import io.entgra.device.mgt.core.ui.request.interceptor.exceptions.JITEnrollmentException;
|
||||||
//import org.apache.commons.logging.LogFactory;
|
import io.entgra.device.mgt.core.ui.request.interceptor.util.HandlerConstants;
|
||||||
//import org.apache.http.HttpHeaders;
|
import io.entgra.device.mgt.core.ui.request.interceptor.util.HandlerUtil;
|
||||||
//import org.apache.http.client.methods.HttpPost;
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
//import org.apache.http.entity.ContentType;
|
import org.apache.commons.logging.Log;
|
||||||
//import org.apache.http.entity.StringEntity;
|
import org.apache.commons.logging.LogFactory;
|
||||||
//import org.w3c.dom.Document;
|
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
|
||||||
//import org.w3c.dom.Element;
|
import org.apache.hc.core5.http.ClassicHttpRequest;
|
||||||
//import org.w3c.dom.Node;
|
import org.apache.hc.core5.http.NameValuePair;
|
||||||
//import org.w3c.dom.NodeList;
|
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
|
||||||
//import org.wso2.carbon.utils.CarbonUtils;
|
import org.apache.hc.core5.http.message.BasicNameValuePair;
|
||||||
//import org.xml.sax.SAXException;
|
import org.apache.http.HttpHeaders;
|
||||||
//
|
import org.apache.http.client.methods.HttpPost;
|
||||||
//import javax.servlet.annotation.WebServlet;
|
import org.apache.http.entity.ContentType;
|
||||||
//import javax.servlet.http.HttpServlet;
|
import org.apache.http.entity.StringEntity;
|
||||||
//import javax.servlet.http.HttpServletRequest;
|
import org.w3c.dom.Document;
|
||||||
//import javax.servlet.http.HttpServletResponse;
|
import org.w3c.dom.Element;
|
||||||
//import javax.servlet.http.HttpSession;
|
import org.w3c.dom.Node;
|
||||||
//import javax.xml.parsers.DocumentBuilder;
|
import org.w3c.dom.NodeList;
|
||||||
//import javax.xml.parsers.DocumentBuilderFactory;
|
import org.wso2.carbon.utils.CarbonUtils;
|
||||||
//import javax.xml.parsers.ParserConfigurationException;
|
import org.xml.sax.SAXException;
|
||||||
//import java.io.File;
|
|
||||||
//import java.io.IOException;
|
import javax.servlet.annotation.WebServlet;
|
||||||
//import java.util.Base64;
|
import javax.servlet.http.HttpServlet;
|
||||||
//import java.util.Objects;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
//
|
import javax.servlet.http.HttpServletResponse;
|
||||||
//@WebServlet(
|
import javax.servlet.http.HttpSession;
|
||||||
// name = "JIT Enrollment callback handler",
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
// description = "Call token endpoint and retrieve token",
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
// urlPatterns = {
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
// "/jit-enrollment-callback"
|
import java.io.File;
|
||||||
// }
|
import java.io.IOException;
|
||||||
//)
|
import java.util.*;
|
||||||
//public class JITEnrollmentCallbackHandler extends HttpServlet {
|
|
||||||
// private static final Log log = LogFactory.getLog(JITEnrollmentCallbackHandler.class);
|
@WebServlet(
|
||||||
// private String gatewayUrl;
|
name = "JIT Enrollment callback handler",
|
||||||
// private String keyManagerUrl;
|
description = "Call token endpoint and retrieve token",
|
||||||
// private JITData JITInfo;
|
urlPatterns = {
|
||||||
// private String encodedClientCredentials;
|
"/jit-enrollment-callback"
|
||||||
// private String applicationName;
|
}
|
||||||
// private String clientId;
|
)
|
||||||
// private String clientSecret;
|
public class JITEnrollmentCallbackHandler extends HttpServlet {
|
||||||
// private String scope;
|
private static final Log log = LogFactory.getLog(JITEnrollmentCallbackHandler.class);
|
||||||
// private String JITConfigurationPath;
|
private String gatewayUrl;
|
||||||
// private JITEnrollmentData JITEnrollmentInfo;
|
private String keyManagerUrl;
|
||||||
// @Override
|
private JITData JITInfo;
|
||||||
// protected void doGet(HttpServletRequest request, HttpServletResponse response) {
|
private String encodedClientCredentials;
|
||||||
// gatewayUrl = request.getScheme() + HandlerConstants.SCHEME_SEPARATOR
|
private String applicationName;
|
||||||
// + System.getProperty(HandlerConstants.IOT_GW_HOST_ENV_VAR)
|
private String clientId;
|
||||||
// + HandlerConstants.COLON + HandlerUtil.getGatewayPort(request.getScheme());
|
private String clientSecret;
|
||||||
// keyManagerUrl = request.getScheme() + HandlerConstants.SCHEME_SEPARATOR
|
private String scope;
|
||||||
// + System.getProperty(HandlerConstants.IOT_KM_HOST_ENV_VAR)
|
private String JITConfigurationPath;
|
||||||
// + HandlerConstants.COLON + HandlerUtil.getKeyManagerPort(request.getScheme());
|
private JITEnrollmentData JITEnrollmentInfo;
|
||||||
// JITConfigurationPath = CarbonUtils.getCarbonConfigDirPath() + File.separator + "jit-config.xml";
|
|
||||||
// HttpSession session = request.getSession(false);
|
@Override
|
||||||
// try {
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
|
||||||
// if (session == null) {
|
gatewayUrl = request.getScheme() + HandlerConstants.SCHEME_SEPARATOR
|
||||||
// response.sendError(HttpStatus.SC_UNAUTHORIZED);
|
+ System.getProperty(HandlerConstants.IOT_GW_HOST_ENV_VAR)
|
||||||
// return;
|
+ HandlerConstants.COLON + HandlerUtil.getGatewayPort(request.getScheme());
|
||||||
// }
|
keyManagerUrl = request.getScheme() + HandlerConstants.SCHEME_SEPARATOR
|
||||||
//
|
+ System.getProperty(HandlerConstants.IOT_KM_HOST_ENV_VAR)
|
||||||
// JITInfo = (JITData) session.getAttribute(HandlerConstants.SESSION_JIT_DATA_KEY);
|
+ HandlerConstants.COLON + HandlerUtil.getKeyManagerPort(request.getScheme());
|
||||||
// if (JITInfo == null) {
|
JITConfigurationPath = CarbonUtils.getCarbonConfigDirPath() + File.separator + "jit-config.xml";
|
||||||
// response.sendError(HttpStatus.SC_UNAUTHORIZED);
|
HttpSession session = request.getSession(false);
|
||||||
// return;
|
try {
|
||||||
// }
|
if (session == null) {
|
||||||
//
|
response.sendError(HttpStatus.SC_UNAUTHORIZED);
|
||||||
// JITEnrollmentInfo = (JITEnrollmentData)
|
return;
|
||||||
// session.getAttribute(HandlerConstants.SESSION_JIT_ENROLLMENT_DATA_KEY);
|
}
|
||||||
// if (JITEnrollmentInfo == null) {
|
|
||||||
// response.sendError(HttpStatus.SC_UNAUTHORIZED);
|
JITInfo = (JITData) session.getAttribute(HandlerConstants.SESSION_JIT_DATA_KEY);
|
||||||
// return;
|
if (JITInfo == null) {
|
||||||
// }
|
response.sendError(HttpStatus.SC_UNAUTHORIZED);
|
||||||
// applicationName = request.getContextPath().substring(1,
|
return;
|
||||||
// request.getContextPath().indexOf("-ui-request-handler"));
|
}
|
||||||
// initializeJITEnrollmentConfigurations();
|
|
||||||
// populateApplicationData(registerApplication());
|
JITEnrollmentInfo = (JITEnrollmentData)
|
||||||
// persistAuthData(session, getToken());
|
session.getAttribute(HandlerConstants.SESSION_JIT_ENROLLMENT_DATA_KEY);
|
||||||
// response.sendRedirect(JITEnrollmentInfo.getRedirectUrl() + "?ownershipType=" +
|
if (JITEnrollmentInfo == null) {
|
||||||
// JITEnrollmentInfo.getOwnershipType() + "&os=" + JITEnrollmentInfo.getOs() + "&username=" +
|
response.sendError(HttpStatus.SC_UNAUTHORIZED);
|
||||||
// JITEnrollmentInfo.getUsername() + "&tenantDomain=" + JITEnrollmentInfo.getTenantDomain());
|
return;
|
||||||
// } catch (JITEnrollmentException | IOException ex) {
|
}
|
||||||
// log.error("Error occurred while processing JIT provisioning callback request", ex);
|
applicationName = request.getContextPath().substring(1,
|
||||||
// }
|
request.getContextPath().indexOf("-ui-request-handler"));
|
||||||
// }
|
initializeJITEnrollmentConfigurations();
|
||||||
//
|
populateApplicationData(registerApplication());
|
||||||
// private void initializeJITEnrollmentConfigurations() throws JITEnrollmentException {
|
persistAuthData(session, getToken());
|
||||||
// try {
|
response.sendRedirect(JITEnrollmentInfo.getRedirectUrl() + "?ownershipType=" +
|
||||||
// File JITConfigurationFile = new File(JITConfigurationPath);
|
JITEnrollmentInfo.getOwnershipType() + "&os=" + JITEnrollmentInfo.getOs() + "&username=" +
|
||||||
// DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
JITEnrollmentInfo.getUsername() + "&tenantDomain=" + JITEnrollmentInfo.getTenantDomain());
|
||||||
// DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
} catch (JITEnrollmentException | IOException ex) {
|
||||||
// Document JITConfigurationDoc = documentBuilder.parse(JITConfigurationFile);
|
log.error("Error occurred while processing JIT provisioning callback request", ex);
|
||||||
// JITConfigurationDoc.getDocumentElement().normalize();
|
}
|
||||||
// Element enrollmentScopes;
|
}
|
||||||
// if (Objects.equals(JITEnrollmentInfo.getOs(), HandlerConstants.OS_ANDROID)) {
|
|
||||||
// enrollmentScopes = (Element) JITConfigurationDoc.
|
private void initializeJITEnrollmentConfigurations() throws JITEnrollmentException {
|
||||||
// getElementsByTagName(HandlerConstants.TAG_ANDROID_ENROLLMENT_SCOPES).item(0);
|
try {
|
||||||
// } else if (Objects.equals(JITEnrollmentInfo.getOs(), HandlerConstants.OS_IOS)) {
|
File JITConfigurationFile = new File(JITConfigurationPath);
|
||||||
// enrollmentScopes = (Element) JITConfigurationDoc.
|
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||||
// getElementsByTagName(HandlerConstants.TAG_IOS_ENROLLMENT_SCOPES).item(0);
|
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||||
// } else if (Objects.equals(JITEnrollmentInfo.getOs(), HandlerConstants.OS_WINDOWS)) {
|
Document JITConfigurationDoc = documentBuilder.parse(JITConfigurationFile);
|
||||||
// enrollmentScopes = (Element) JITConfigurationDoc.
|
JITConfigurationDoc.getDocumentElement().normalize();
|
||||||
// getElementsByTagName(HandlerConstants.TAG_WINDOWS_ENROLLMENT_SCOPES).item(0);
|
Element enrollmentScopes;
|
||||||
// } else {
|
if (Objects.equals(JITEnrollmentInfo.getOs(), HandlerConstants.OS_ANDROID)) {
|
||||||
// String msg = "OS type not supported";
|
enrollmentScopes = (Element) JITConfigurationDoc.
|
||||||
// if (log.isDebugEnabled()) {
|
getElementsByTagName(HandlerConstants.TAG_ANDROID_ENROLLMENT_SCOPES).item(0);
|
||||||
// log.error(msg);
|
} else if (Objects.equals(JITEnrollmentInfo.getOs(), HandlerConstants.OS_IOS)) {
|
||||||
// }
|
enrollmentScopes = (Element) JITConfigurationDoc.
|
||||||
// throw new JITEnrollmentException(msg);
|
getElementsByTagName(HandlerConstants.TAG_IOS_ENROLLMENT_SCOPES).item(0);
|
||||||
// }
|
} else if (Objects.equals(JITEnrollmentInfo.getOs(), HandlerConstants.OS_WINDOWS)) {
|
||||||
// NodeList scopeList = enrollmentScopes.getElementsByTagName("Scope");
|
enrollmentScopes = (Element) JITConfigurationDoc.
|
||||||
// StringBuilder scopeStr = new StringBuilder();
|
getElementsByTagName(HandlerConstants.TAG_WINDOWS_ENROLLMENT_SCOPES).item(0);
|
||||||
// for (int idx = 0; idx < scopeList.getLength(); idx++) {
|
} else {
|
||||||
// Node scopeNode = scopeList.item(idx);
|
String msg = "OS type not supported";
|
||||||
// if (scopeNode.getNodeType() == Node.ELEMENT_NODE) {
|
if (log.isDebugEnabled()) {
|
||||||
// Element scopeElement = (Element) scopeNode;
|
log.error(msg);
|
||||||
// scopeStr.append(" ").append(scopeElement.getTextContent());
|
}
|
||||||
// }
|
throw new JITEnrollmentException(msg);
|
||||||
// }
|
}
|
||||||
// scope = scopeStr.toString();
|
NodeList scopeList = enrollmentScopes.getElementsByTagName("Scope");
|
||||||
// } catch (ParserConfigurationException ex) {
|
StringBuilder scopeStr = new StringBuilder();
|
||||||
// String msg = "Error occurred when document builder creating the file configuration";
|
for (int idx = 0; idx < scopeList.getLength(); idx++) {
|
||||||
// throw new JITEnrollmentException(msg, ex);
|
Node scopeNode = scopeList.item(idx);
|
||||||
// } catch (IOException ex) {
|
if (scopeNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
// String msg = "IO error occurred while parsing the JIT config file";
|
Element scopeElement = (Element) scopeNode;
|
||||||
// throw new JITEnrollmentException(msg, ex);
|
scopeStr.append(" ").append(scopeElement.getTextContent());
|
||||||
// } catch (SAXException ex) {
|
}
|
||||||
// String msg = "Parse error occurred while parsing the JIT config document";
|
}
|
||||||
// throw new JITEnrollmentException(msg, ex);
|
scope = scopeStr.toString();
|
||||||
// }
|
} catch (ParserConfigurationException ex) {
|
||||||
// }
|
String msg = "Error occurred when document builder creating the file configuration";
|
||||||
//
|
throw new JITEnrollmentException(msg, ex);
|
||||||
// /***
|
} catch (IOException ex) {
|
||||||
// * Parse string data and build json object
|
String msg = "IO error occurred while parsing the JIT config file";
|
||||||
// * @param data - Json string
|
throw new JITEnrollmentException(msg, ex);
|
||||||
// * @return {@link JsonObject} Json object corresponding to provided json string
|
} catch (SAXException ex) {
|
||||||
// * @throws JITEnrollmentException throws when error occurred while parsing
|
String msg = "Parse error occurred while parsing the JIT config document";
|
||||||
// */
|
throw new JITEnrollmentException(msg, ex);
|
||||||
// private JsonObject parseResponseData(String data) throws JITEnrollmentException {
|
}
|
||||||
// JsonParser parser = new JsonParser();
|
}
|
||||||
// JsonElement responseData = parser.parse(data);
|
|
||||||
// if (responseData.isJsonObject()) {
|
/***
|
||||||
// return responseData.getAsJsonObject();
|
* Parse string data and build json object
|
||||||
// }
|
* @param data - Json string
|
||||||
// throw new JITEnrollmentException("Unexpected response body return");
|
* @return {@link JsonObject} Json object corresponding to provided json string
|
||||||
// }
|
* @throws JITEnrollmentException throws when error occurred while parsing
|
||||||
//
|
*/
|
||||||
// /***
|
private JsonNode parseResponseData(String data) throws JITEnrollmentException {
|
||||||
// * Build application registration request
|
|
||||||
// * @return {@link HttpPost} Application registration request
|
JsonFactory jsonFactory = new JsonFactory();
|
||||||
// */
|
ObjectMapper objectMapper = new ObjectMapper(jsonFactory);
|
||||||
// private HttpPost buildApplicationRegistrationRequest() {
|
try {
|
||||||
// HttpPost applicationRegistrationRequest = new HttpPost(gatewayUrl + HandlerConstants.APP_REG_ENDPOINT);
|
return objectMapper.readTree(data);
|
||||||
// applicationRegistrationRequest.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC
|
} catch (JsonProcessingException e) {
|
||||||
// + JITInfo.getEncodedClientCredentials());
|
throw new JITEnrollmentException("Unexpected response body return");
|
||||||
// applicationRegistrationRequest.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
|
}
|
||||||
// JsonArray tags = new JsonArray();
|
}
|
||||||
// tags.add("device_management");
|
|
||||||
// JsonObject payload = new JsonObject();
|
/***
|
||||||
// payload.addProperty("applicationName", applicationName);
|
* Build application registration request
|
||||||
// payload.add("tags", tags);
|
* @return {@link HttpPost} Application registration request
|
||||||
// payload.addProperty("allowedToAllDomains", false);
|
*/
|
||||||
// payload.addProperty("mappingAnExistingOAuthApp", false);
|
private ClassicHttpRequest buildApplicationRegistrationRequest() {
|
||||||
// applicationRegistrationRequest.setEntity(new StringEntity(payload.toString(), ContentType.APPLICATION_JSON));
|
|
||||||
// return applicationRegistrationRequest;
|
JsonArray tags = new JsonArray();
|
||||||
// }
|
tags.add("device_management");
|
||||||
//
|
|
||||||
// /***
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
// * Populate dynamic client's data
|
Map<String, Object> payload = new HashMap<>();
|
||||||
// * @param application - application data receiving from dcr request
|
|
||||||
// */
|
payload.put("applicationName", applicationName);
|
||||||
// private void populateApplicationData(JsonObject application) {
|
payload.put("tags", tags);
|
||||||
// clientId = application.get("client_id").getAsString();
|
payload.put("allowedToAllDomains", false);
|
||||||
// clientSecret = application.get("client_secret").getAsString();
|
payload.put("mappingAnExistingOAuthApp", false);
|
||||||
// String headerValue = clientId+ ':' + clientSecret;
|
|
||||||
// encodedClientCredentials = Base64.getEncoder().encodeToString(headerValue.getBytes());
|
ClassicHttpRequest applicationRegistrationRequest = ClassicRequestBuilder.post(gatewayUrl + HandlerConstants.APP_REG_ENDPOINT)
|
||||||
// }
|
.setEntity(new org.apache.hc.core5.http.io.entity.StringEntity(objectMapper.valueToTree(payload).toString(),
|
||||||
//
|
org.apache.hc.core5.http.ContentType.APPLICATION_JSON))
|
||||||
// /***
|
.setHeader(org.apache.hc.core5.http.HttpHeaders.CONTENT_TYPE,
|
||||||
// * Register client application
|
org.apache.hc.core5.http.ContentType.APPLICATION_JSON.toString())
|
||||||
// * @return {@link JsonObject} Json object contain registered application data
|
.setHeader(org.apache.hc.core5.http.HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC +
|
||||||
// * @throws JITEnrollmentException throws when error occurred while application registration
|
JITInfo.getEncodedClientCredentials())
|
||||||
// */
|
.build();
|
||||||
// private JsonObject registerApplication() throws JITEnrollmentException {
|
return applicationRegistrationRequest;
|
||||||
// try {
|
}
|
||||||
// ProxyResponse proxyResponse = HandlerUtil.execute(buildApplicationRegistrationRequest());
|
|
||||||
// if (proxyResponse.getCode() == HttpStatus.SC_CREATED ||
|
/***
|
||||||
// proxyResponse.getCode() == HttpStatus.SC_OK) {
|
* Populate dynamic client's data
|
||||||
// return parseResponseData(proxyResponse.getData());
|
* @param application - application data receiving from dcr request
|
||||||
// }
|
*/
|
||||||
// throw new JITEnrollmentException("Unexpected response status return for application registration request");
|
private void populateApplicationData(JsonNode application) {
|
||||||
// } catch (IOException ex) {
|
clientId = application.get("client_id").asText();
|
||||||
// throw new JITEnrollmentException("Error occurred while executing application registration request", ex);
|
clientSecret = application.get("client_secret").asText();
|
||||||
// }
|
String headerValue = clientId + ':' + clientSecret;
|
||||||
// }
|
encodedClientCredentials = Base64.getEncoder().encodeToString(headerValue.getBytes());
|
||||||
//
|
}
|
||||||
// /***
|
|
||||||
// * Acquire token
|
/***
|
||||||
// * @return {@link JsonObject} Json object containing token data
|
* Register client application
|
||||||
// * @throws JITEnrollmentException throws when error occurred while acquiring token
|
* @return {@link JsonObject} Json object contain registered application data
|
||||||
// */
|
* @throws JITEnrollmentException throws when error occurred while application registration
|
||||||
// private JsonObject getToken() throws JITEnrollmentException {
|
*/
|
||||||
// try {
|
private JsonNode registerApplication() throws JITEnrollmentException {
|
||||||
// ProxyResponse proxyResponse = HandlerUtil.execute(buildTokenAcquireRequest());
|
try {
|
||||||
// if (proxyResponse.getCode() == org.apache.http.HttpStatus.SC_CREATED ||
|
ProxyResponse proxyResponse = HandlerUtil.execute(buildApplicationRegistrationRequest());
|
||||||
// proxyResponse.getCode() == org.apache.http.HttpStatus.SC_OK) {
|
if (proxyResponse.getCode() == HttpStatus.SC_CREATED ||
|
||||||
// return parseResponseData(proxyResponse.getData().toString());
|
proxyResponse.getCode() == HttpStatus.SC_OK) {
|
||||||
// }
|
return parseResponseData(proxyResponse.getData().toString());
|
||||||
// throw new JITEnrollmentException("Unexpected response status return for token acquiring request");
|
}
|
||||||
// } catch (IOException ex) {
|
throw new JITEnrollmentException("Unexpected response status return for application registration request");
|
||||||
// throw new JITEnrollmentException("Error occurred while executing token acquiring request", ex);
|
} catch (IOException ex) {
|
||||||
// }
|
throw new JITEnrollmentException("Error occurred while executing application registration request", ex);
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// /***
|
|
||||||
// * Build token acquire request
|
/***
|
||||||
// * @return {@link HttpPost} Token acquire request
|
* Acquire token
|
||||||
// */
|
* @return {@link JsonObject} Json object containing token data
|
||||||
// private HttpPost buildTokenAcquireRequest() {
|
* @throws JITEnrollmentException throws when error occurred while acquiring token
|
||||||
// HttpPost tokenAcquiringRequest = new HttpPost(keyManagerUrl + HandlerConstants.OAUTH2_TOKEN_ENDPOINT);
|
*/
|
||||||
// tokenAcquiringRequest.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString());
|
private JsonNode getToken() throws JITEnrollmentException {
|
||||||
// tokenAcquiringRequest.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC
|
try {
|
||||||
// + encodedClientCredentials);
|
ProxyResponse proxyResponse = HandlerUtil.execute(buildTokenAcquireRequest());
|
||||||
// StringEntity payload = new StringEntity(
|
if (proxyResponse.getCode() == org.apache.http.HttpStatus.SC_CREATED ||
|
||||||
// "grant_type=" + HandlerConstants.CLIENT_CREDENTIAL_GRANT_TYPE + "&scope=" + scope,
|
proxyResponse.getCode() == org.apache.http.HttpStatus.SC_OK) {
|
||||||
// ContentType.APPLICATION_FORM_URLENCODED);
|
return parseResponseData(proxyResponse.getData().toString());
|
||||||
// tokenAcquiringRequest.setEntity(payload);
|
}
|
||||||
// return tokenAcquiringRequest;
|
throw new JITEnrollmentException("Unexpected response status return for token acquiring request");
|
||||||
// }
|
} catch (IOException ex) {
|
||||||
//
|
throw new JITEnrollmentException("Error occurred while executing token acquiring request", ex);
|
||||||
// /***
|
}
|
||||||
// * Persists auth data in session
|
}
|
||||||
// * @param session - {@link HttpSession}
|
|
||||||
// * @param token - Json object containing token data
|
/***
|
||||||
// */
|
* Build token acquire request
|
||||||
// private void persistAuthData(HttpSession session, JsonObject token) {
|
* @return {@link HttpPost} Token acquire request
|
||||||
// AuthData authData = new AuthData();
|
*/
|
||||||
// authData.setAccessToken(token.get("access_token").getAsString());
|
private ClassicHttpRequest buildTokenAcquireRequest() {
|
||||||
// authData.setClientId(clientId);
|
|
||||||
// authData.setClientSecret(clientSecret);
|
StringEntity payload = new StringEntity(
|
||||||
// authData.setEncodedClientApp(encodedClientCredentials);
|
"grant_type=" + HandlerConstants.CLIENT_CREDENTIAL_GRANT_TYPE + "&scope=" + scope,
|
||||||
// authData.setScope(token.get("scope").getAsString());
|
ContentType.APPLICATION_FORM_URLENCODED);
|
||||||
// session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, authData);
|
|
||||||
// }
|
ClassicHttpRequest tokenAcquiringRequest = ClassicRequestBuilder.post(keyManagerUrl + HandlerConstants.OAUTH2_TOKEN_ENDPOINT)
|
||||||
//}
|
.setHeader(org.apache.hc.core5.http.HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString())
|
||||||
|
.setHeader(org.apache.hc.core5.http.HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC + encodedClientCredentials)
|
||||||
|
.setEntity(new org.apache.hc.core5.http.io.entity.StringEntity(payload.toString(), org.apache.hc.core5.http.ContentType.APPLICATION_JSON))
|
||||||
|
.build();
|
||||||
|
return tokenAcquiringRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* Persists auth data in session
|
||||||
|
* @param session - {@link HttpSession}
|
||||||
|
* @param token - Json object containing token data
|
||||||
|
*/
|
||||||
|
private void persistAuthData(HttpSession session, JsonNode token) {
|
||||||
|
AuthData authData = new AuthData();
|
||||||
|
authData.setAccessToken(token.get("access_token").asText());
|
||||||
|
authData.setClientId(clientId);
|
||||||
|
authData.setClientSecret(clientSecret);
|
||||||
|
authData.setEncodedClientApp(encodedClientCredentials);
|
||||||
|
authData.setScope(token.get("scope"));
|
||||||
|
session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, authData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in new issue