From 354dcd3fd4ba9cf7f9da5bce006c5f6320b82c3e Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 10 Oct 2019 15:55:03 +0530 Subject: [PATCH 1/7] Add null and empty check for device location related values --- .../mgt/core/service/DeviceManagementProviderServiceImpl.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 0e52285893..3be6900f90 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -3271,7 +3271,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv distance = p.getValue(); } } - if (latitude != null && longitude != null && !latitude.isEmpty() && !longitude.isEmpty()) { + if (StringUtils.isNotBlank(latitude) && StringUtils.isNotBlank(longitude) && + StringUtils.isNotBlank(altitude) && StringUtils.isNotBlank(speed) && + StringUtils.isNotBlank(bearing) && StringUtils.isNotBlank(distance)) { DeviceLocation deviceLocation = new DeviceLocation(); deviceLocation.setDeviceId(device.getId()); deviceLocation.setDeviceIdentifier(new DeviceIdentifier(device.getDeviceIdentifier(), From c762c9d2a7a85a459ce9b1f5650ad586963306b1 Mon Sep 17 00:00:00 2001 From: shamalka Date: Tue, 8 Oct 2019 16:08:55 +0530 Subject: [PATCH 2/7] Change bulk action UI --- .../react-app/src/pages/Dashboard/Devices/Devices.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/pages/Dashboard/Devices/Devices.js b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/pages/Dashboard/Devices/Devices.js index f78f1c89f9..cb244edfec 100644 --- a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/pages/Dashboard/Devices/Devices.js +++ b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/pages/Dashboard/Devices/Devices.js @@ -48,7 +48,7 @@ class Devices extends React.Component { Devices
-

Devices

+

Devicesssdsds

Lorem ipsum dolor sit amet, est similique constituto at, quot inermis id mel, an illud incorrupte nam.
From b92046fa3d45912cd77b6ca6f9a8c9f2014a1e5b Mon Sep 17 00:00:00 2001 From: shamalka Date: Tue, 8 Oct 2019 17:27:05 +0530 Subject: [PATCH 3/7] Change UI structure --- .../react-app/src/pages/Dashboard/Devices/Devices.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/pages/Dashboard/Devices/Devices.js b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/pages/Dashboard/Devices/Devices.js index cb244edfec..f78f1c89f9 100644 --- a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/pages/Dashboard/Devices/Devices.js +++ b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/pages/Dashboard/Devices/Devices.js @@ -48,7 +48,7 @@ class Devices extends React.Component { Devices
-

Devicesssdsds

+

Devices

Lorem ipsum dolor sit amet, est similique constituto at, quot inermis id mel, an illud incorrupte nam.
From 5ba56207c995a7d78d728397ee822946b0b62e14 Mon Sep 17 00:00:00 2001 From: shamalka Date: Wed, 9 Oct 2019 17:22:51 +0530 Subject: [PATCH 4/7] Improve delete device UI --- .../src/components/Devices/BulkActionBar.js | 116 +++++++++++------- .../src/components/Devices/DevicesTable.js | 77 ++++++------ 2 files changed, 106 insertions(+), 87 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/BulkActionBar.js b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/BulkActionBar.js index 4964d4d9cb..b8076683da 100644 --- a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/BulkActionBar.js +++ b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/BulkActionBar.js @@ -17,67 +17,91 @@ */ import React from "react"; -import {Button, Icon, notification} from "antd"; +import {Button, Tooltip, Popconfirm, Divider} from "antd"; class BulkActionBar extends React.Component { - constructor(props){ + constructor(props) { super(props); this.state = { - selectedMultiple:false, - selectedSingle:false + selectedMultiple: false, + selectedSingle: false, + deleteable: true, } } //This method is used to trigger delete request on selected devices - deleteDevice = () => { - const deviceStatusArray = this.props.selectedRows.map(obj => obj.enrolmentInfo.status); - if(deviceStatusArray.includes("ACTIVE") || deviceStatusArray.includes("INACTIVE")){ - notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Cannot delete ACTIVE/INACTIVE devices.", - }); - }else{ - this.props.deleteDevice(); + onDeleteDeviceCall = () => { + let i; + for(i=0; i < this.props.selectedRows.length; i++){ + if(this.props.selectedRows[i].enrolmentInfo.status != "REMOVED"){ + this.setState({deletable:false}); + break; + } + this.setState({deletable:true}); } - } + }; - componentDidUpdate(prevProps, prevState, snapshot) { - if(prevProps.selectedRows !== this.props.selectedRows){ - if(this.props.selectedRows.length > 1){ - this.setState({selectedMultiple:true,selectedSingle:false}) - }else if(this.props.selectedRows.length == 1){ - this.setState({selectedSingle:true,selectedMultiple:true}) - }else{ - this.setState({selectedSingle:false,selectedMultiple:false}) - } + onConfirmDelete = () => { + if (this.state.deletable) { + this.props.deleteDevice(); } - } + }; + + onConfirmDisenroll = () => { + //TODO: Implement disenrollment function + }; render() { - return( -
- + const isSelected = this.props.selectedRows.length > 0; + const isSelectedSingle = this.props.selectedRows.length == 1; + + return ( +
+ + + - -
+
) } } diff --git a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/DevicesTable.js b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/DevicesTable.js index 7a096a2147..ae14f450f2 100644 --- a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/DevicesTable.js +++ b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/DevicesTable.js @@ -118,9 +118,9 @@ const columns = [ key: 'action', render: () => ( - - - + + + ), }, @@ -135,7 +135,7 @@ const getTimeAgo = (time) => { class DeviceTable extends React.Component { constructor(props) { super(props); - config = this.props.context; + config = this.props.context; TimeAgo.addLocale(en); this.state = { data: [], @@ -173,7 +173,7 @@ class DeviceTable extends React.Component { }; const encodedExtraParams = Object.keys(extraParams) - .map(key => key + '=' + extraParams[key]).join('&'); + .map(key => key + '=' + extraParams[key]).join('&'); //send request to the invoker axios.get( @@ -216,21 +216,14 @@ class DeviceTable extends React.Component { //send request to the invoker axios.put( - window.location.origin + config.serverConfig.invoker.uri + - config.serverConfig.invoker.deviceMgt + - "/admin/devices/permanent-delete", - deviceData, - { headers : {'Content-Type': 'application/json'}} - + window.location.origin + config.serverConfig.invoker.uri + + config.serverConfig.invoker.deviceMgt + + "/admin/devices/permanent-delete", + deviceData, + {headers: {'Content-Type': 'application/json'}} ).then(res => { if (res.status === 200) { this.fetch(); - const pagination = {...this.state.pagination}; - this.setState({ - loading: false, - data: res.data.data.devices, - pagination, - }); } }).catch((error) => { if (error.hasOwnProperty("response") && error.response.status === 401) { @@ -239,16 +232,16 @@ class DeviceTable extends React.Component { window.location.href = window.location.origin + '/entgra/login'; } else { notification["error"]({ - message: "There was a problem", - duration: 0, - description: - "Error occurred while trying to delete devices.", - }); + message: "There was a problem", + duration: 0, + description: + "Error occurred while trying to delete devices.", + }); } this.setState({loading: false}); }); - } + }; handleTableChange = (pagination, filters, sorter) => { const pager = {...this.state.pagination}; @@ -270,24 +263,26 @@ class DeviceTable extends React.Component { return (
- (record.deviceIdentifier + record.enrolmentInfo.owner + record.enrolmentInfo.ownership)} - dataSource={data} - pagination={{ - ...pagination, - size: "small", - // position: "top", - showTotal: (total, range) => `showing ${range[0]}-${range[1]} of ${total} devices` - // showQuickJumper: true - }} - loading={loading} - onChange={this.handleTableChange} - rowSelection={this.rowSelection} - scroll={{x: 1000}} - /> + deleteDevice={this.deleteDevice} + selectedRows={this.state.selectedRows}/> +
+
(record.deviceIdentifier + record.enrolmentInfo.owner + record.enrolmentInfo.ownership)} + dataSource={data} + pagination={{ + ...pagination, + size: "small", + // position: "top", + showTotal: (total, range) => `showing ${range[0]}-${range[1]} of ${total} devices` + // showQuickJumper: true + }} + loading={loading} + onChange={this.handleTableChange} + rowSelection={this.rowSelection} + scroll={{x: 1000}} + /> + ); } From c69f9c9f1d8eb10cbdc53e10cbc4a06372e78c0c Mon Sep 17 00:00:00 2001 From: shamalka Date: Wed, 9 Oct 2019 17:57:05 +0530 Subject: [PATCH 5/7] Fix spelling mistakes --- .../src/components/Devices/BulkActionBar.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/BulkActionBar.js b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/BulkActionBar.js index b8076683da..55fac8e17c 100644 --- a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/BulkActionBar.js +++ b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/BulkActionBar.js @@ -26,24 +26,24 @@ class BulkActionBar extends React.Component { this.state = { selectedMultiple: false, selectedSingle: false, - deleteable: true, + canDelete: true, } } - //This method is used to trigger delete request on selected devices + //This method checks whether NON-REMOVED devices are selected onDeleteDeviceCall = () => { let i; for(i=0; i < this.props.selectedRows.length; i++){ if(this.props.selectedRows[i].enrolmentInfo.status != "REMOVED"){ - this.setState({deletable:false}); + this.setState({canDelete:false}); break; } - this.setState({deletable:true}); + this.setState({canDelete:true}); } }; onConfirmDelete = () => { - if (this.state.deletable) { + if (this.state.canDelete) { this.props.deleteDevice(); } }; @@ -67,7 +67,7 @@ class BulkActionBar extends React.Component { Date: Thu, 10 Oct 2019 18:04:05 +0530 Subject: [PATCH 6/7] Add devices to groups --- .../src/components/Devices/BulkActionBar.js | 31 +++- .../src/components/Devices/DevicesTable.js | 171 ++++++++++++++++-- 2 files changed, 178 insertions(+), 24 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/BulkActionBar.js b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/BulkActionBar.js index 55fac8e17c..5b4f49e6ed 100644 --- a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/BulkActionBar.js +++ b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/BulkActionBar.js @@ -32,14 +32,15 @@ class BulkActionBar extends React.Component { //This method checks whether NON-REMOVED devices are selected onDeleteDeviceCall = () => { - let i; - for(i=0; i < this.props.selectedRows.length; i++){ + let tempDeleteState; + for(let i=0; i < this.props.selectedRows.length; i++){ if(this.props.selectedRows[i].enrolmentInfo.status != "REMOVED"){ - this.setState({canDelete:false}); + tempDeleteState = false; break; } - this.setState({canDelete:true}); + tempDeleteState = true; } + this.setState({canDelete:tempDeleteState}) }; onConfirmDelete = () => { @@ -52,13 +53,16 @@ class BulkActionBar extends React.Component { //TODO: Implement disenrollment function }; + onDeviceGroupCall = () => { + this.props.getGroups(); + } + render() { const isSelected = this.props.selectedRows.length > 0; const isSelectedSingle = this.props.selectedRows.length == 1; return ( -
+
+ style={{display:isSelectedSingle ? "inline" : "none", margin: "2px"}}/> + + + +
) } diff --git a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/DevicesTable.js b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/DevicesTable.js index ae14f450f2..fd87a6a7e1 100644 --- a/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/DevicesTable.js +++ b/components/device-mgt/io.entgra.device.mgt.ui/react-app/src/components/Devices/DevicesTable.js @@ -18,7 +18,7 @@ import React from "react"; import axios from "axios"; -import {Tag, message, notification, Table, Typography, Tooltip, Icon, Divider, Button} from "antd"; +import {Tag, message, notification, Table, Typography, Tooltip, Icon, Divider, Button, Modal, Select} from "antd"; import TimeAgo from 'javascript-time-ago' // Load locale-specific relative date/time formatting rules. @@ -112,18 +112,7 @@ const columns = [ return {timeAgoString}; } // todo add filtering options - }, - { - title: 'Action', - key: 'action', - render: () => ( - - - - - - ), - }, + } ]; const getTimeAgo = (time) => { @@ -142,7 +131,9 @@ class DeviceTable extends React.Component { pagination: {}, loading: false, selectedRows: [], - deviceIds: [] + deviceGroups: [], + groupModalVisible: false, + selectedGroupId: [] }; } @@ -151,7 +142,6 @@ class DeviceTable extends React.Component { this.setState({ selectedRows: selectedRows }); - this.state.deviceIds = selectedRows.map(obj => obj.deviceIdentifier); } }; @@ -212,7 +202,7 @@ class DeviceTable extends React.Component { const config = this.props.context; this.setState({loading: true}); - const deviceData = this.state.deviceIds; + const deviceData = this.state.selectedRows.map(obj => obj.deviceIdentifier); //send request to the invoker axios.put( @@ -243,6 +233,126 @@ class DeviceTable extends React.Component { }); }; + addDevicesToGroup = (groupId) => { + const config = this.props.context; + this.setState({loading: true}); + + let apiUrl; + let deviceData; + if (this.state.selectedRows.length === 1) { + apiUrl = window.location.origin + config.serverConfig.invoker.uri + + config.serverConfig.invoker.deviceMgt + + "/groups/device/assign"; + deviceData = { + deviceIdentifier: { + id: this.state.selectedRows[0].deviceIdentifier, + type: this.state.selectedRows[0].type + }, + deviceGroupIds: groupId + } + } else if (!groupId[0]){ + apiUrl = window.location.origin + config.serverConfig.invoker.uri + + config.serverConfig.invoker.deviceMgt + + "/groups/id/" + groupId + "/devices/add"; + deviceData = this.state.selectedRows.map(obj => ({id: obj.deviceIdentifier, type: obj.type})); + } else{ + apiUrl = window.location.origin + config.serverConfig.invoker.uri + + config.serverConfig.invoker.deviceMgt + + "/groups/id/" + groupId[0] + "/devices/add"; + deviceData = this.state.selectedRows.map(obj => ({id: obj.deviceIdentifier, type: obj.type})); + } + + //send request to the invoker + axios.post( + apiUrl, + deviceData, + {headers: {'Content-Type': 'application/json'}} + ).then(res => { + if (res.status === 200) { + this.setState({ + loading: false + }); + notification["success"]({ + message: "Done", + duration: 4, + description: + "Successfully added to the device group.", + }); + } + }).catch((error) => { + if (error.hasOwnProperty("response") && error.response.status === 401) { + //todo display a popop with error + message.error('You are not logged in'); + window.location.href = window.location.origin + '/entgra/login'; + } else { + notification["error"]({ + message: "There was a problem", + duration: 0, + description: + "Error occurred while adding to the device group.", + }); + } + + this.setState({loading: false}); + }); + }; + + getGroups = () => { + this.setState({ + groupModalVisible: true + }); + //send request to the invoker + axios.get( + window.location.origin + config.serverConfig.invoker.uri + + config.serverConfig.invoker.deviceMgt + + "/groups" + ).then(res => { + this.setState({deviceGroups: res.data.data.deviceGroups}) + }).catch((error) => { + if (error.hasOwnProperty("response") && error.response.status === 401) { + //todo display a popop with error + message.error('You are not logged in'); + window.location.href = window.location.origin + '/entgra/login'; + } else { + notification["error"]({ + message: "There was a problem", + duration: 0, + description: + "Error occurred while retrieving device groups.", + }); + } + + this.setState({loading: false}); + }); + }; + + handleOk = e => { + if(this.state.selectedGroupId){ + this.addDevicesToGroup(this.state.selectedGroupId); + this.setState({ + groupModalVisible: false + }); + }else{ + notification["error"]({ + message: "There was a problem", + duration: 0, + description: + "Please select a group.", + }); + } + + }; + + handleCancel = e => { + this.setState({ + groupModalVisible: false, + }); + }; + + onGroupSelectChange = (value) => { + this.setState({selectedGroupId: value}); + }; + handleTableChange = (pagination, filters, sorter) => { const pager = {...this.state.pagination}; pager.current = pagination.current; @@ -260,10 +370,20 @@ class DeviceTable extends React.Component { render() { const {data, pagination, loading, selectedRows} = this.state; + + const isSelectedSingle = this.state.selectedRows.length == 1; + + let item = this.state.deviceGroups.map((data) => + + {data.name} + ); return (
+ + + + ); } From fefdb7aba3bc00282dd9331abcaf285100b01774 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 10 Oct 2019 22:40:41 +0530 Subject: [PATCH 7/7] Add new servlet to get logged-in user's data --- .../ui/request/interceptor/LoginHandler.java | 1 - .../ui/request/interceptor/UserHandler.java | 113 ++++++++++++++++++ .../interceptor/util/HandlerConstants.java | 1 + .../request/interceptor/util/HandlerUtil.java | 1 - 4 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java index 314a863c56..1d046a4aed 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java @@ -191,7 +191,6 @@ public class LoginHandler extends HttpServlet { authData.setScope(jTokenResultAsJsonObject.get("scope").getAsString()); session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, authData); return true; - } } return false; diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java new file mode 100644 index 0000000000..e742e543fd --- /dev/null +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.ui.request.interceptor; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonSyntaxException; +import io.entgra.ui.request.interceptor.beans.AuthData; +import io.entgra.ui.request.interceptor.util.HandlerConstants; +import io.entgra.ui.request.interceptor.util.HandlerUtil; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.HttpHeaders; +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.wso2.carbon.device.application.mgt.common.ProxyResponse; + +import javax.servlet.annotation.MultipartConfig; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +@MultipartConfig @WebServlet("/user") public class UserHandler extends HttpServlet { + private static final Log log = LogFactory.getLog(UserHandler.class); + private static final long serialVersionUID = 9050048549140517002L; + + @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) { + try { + String platform = req.getParameter(HandlerConstants.PLATFORM); + String serverUrl = + req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + req.getServerName() + HandlerConstants.COLON + + System.getProperty("iot.gateway.https.port"); + if (StringUtils.isBlank(platform)) { + ProxyResponse proxyResponse = new ProxyResponse(); + proxyResponse.setCode(HttpStatus.SC_BAD_REQUEST); + HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse); + return; + } + + HttpSession httpSession = req.getSession(false); + if (httpSession == null) { + ProxyResponse proxyResponse = new ProxyResponse(); + proxyResponse.setCode(HttpStatus.SC_UNAUTHORIZED); + HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse); + return; + } + + AuthData authData = (AuthData) httpSession.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY); + String accessToken = authData.getAccessToken(); + + HttpPost tokenEndpoint = new HttpPost(serverUrl + HandlerConstants.INTROSPECT_ENDPOINT); + tokenEndpoint.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString()); + StringEntity tokenEPPayload = new StringEntity("token=" + accessToken, + ContentType.APPLICATION_FORM_URLENCODED); + tokenEndpoint.setEntity(tokenEPPayload); + ProxyResponse tokenStatus = HandlerUtil.execute(tokenEndpoint); + + if (tokenStatus.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { + log.error("Error occurred while invoking the API to get token status."); + HandlerUtil.handleError(req, resp, serverUrl, platform, tokenStatus); + return; + } + String tokenData = tokenStatus.getData(); + if (tokenData == null) { + log.error("Invalid token data is received."); + HandlerUtil.handleError(req, resp, serverUrl, platform, tokenStatus); + return; + } + JsonParser jsonParser = new JsonParser(); + JsonElement jTokenResult = jsonParser.parse(tokenData); + if (jTokenResult.isJsonObject()) { + JsonObject jTokenResultAsJsonObject = jTokenResult.getAsJsonObject(); + if (!jTokenResultAsJsonObject.get("active").getAsBoolean()) { + ProxyResponse proxyResponse = new ProxyResponse(); + proxyResponse.setCode(HttpStatus.SC_UNAUTHORIZED); + HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse); + return; + } + ProxyResponse proxyResponse = new ProxyResponse(); + proxyResponse.setCode(HttpStatus.SC_OK); + proxyResponse.setData(jTokenResultAsJsonObject.get("username").getAsString()); + HandlerUtil.handleSuccess(req, resp, serverUrl, platform, proxyResponse); + } + } catch (IOException e) { + log.error("Error occurred while sending the response into the socket. ", e); + } catch (JsonSyntaxException e) { + log.error("Error occurred while parsing the response. ", e); + } + } +} diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java index 562f497678..d74308104f 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java @@ -23,6 +23,7 @@ public class HandlerConstants { public static final String APP_REG_ENDPOINT = "/api-application-registration/register"; public static final String UI_CONFIG_ENDPOINT = "/api/application-mgt/v1.0/config/ui-config"; public static final String TOKEN_ENDPOINT = "/token"; + public static final String INTROSPECT_ENDPOINT = "/oauth2/introspect"; public static final String LOGIN_PAGE = "/login"; public static final String BASIC = "Basic "; public static final String BEARER = "Bearer "; diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java index 4093902327..ae0fe3c639 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java @@ -20,7 +20,6 @@ package io.entgra.ui.request.interceptor.util; import com.google.gson.Gson; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;