From 44c7affb49c31cd38f1b3243c0061b0ba890d34d Mon Sep 17 00:00:00 2001 From: Vigneshan Date: Fri, 4 Dec 2020 10:07:08 +0530 Subject: [PATCH] Improve publisher ui to support sso --- .../react-app/public/conf/config.json | 6 ++- .../react-app/src/App.js | 30 ++++++++--- .../scenes/Home/components/Logout/index.js | 50 ++++++++++++------- .../src/services/utils/uiConfigHandler.js | 40 +++++++++++++++ 4 files changed, 100 insertions(+), 26 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/src/services/utils/uiConfigHandler.js diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/public/conf/config.json b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/public/conf/config.json index 9264aac6d7..e953f2a4b1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/public/conf/config.json +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/public/conf/config.json @@ -1,4 +1,5 @@ { + "appName": "publisher", "theme": { "logo": "https://entgra.io/assets/images/svg/logo.svg", "primaryColor": "rgb(24, 144, 255)", @@ -14,7 +15,10 @@ }, "loginUri": "/publisher-ui-request-handler/login", "logoutUri": "/publisher-ui-request-handler/logout", - "platform": "publisher" + "ssoLoginUri": "/publisher-ui-request-handler/ssoLogin", + "ssoLogoutUri": "/publisher-ui-request-handler/ssoLogout", + "platform": "publisher", + "appUiConfigUri": "/api/device-mgt-config/v1.0/configurations/ui-config" }, "defaultPlatformIcons": { "default": { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/src/App.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/src/App.js index 78a387b656..33332fc89e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/src/App.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/src/App.js @@ -23,6 +23,7 @@ import { BrowserRouter as Router, Redirect, Switch } from 'react-router-dom'; import axios from 'axios'; import { Layout, Spin, Result } from 'antd'; import ConfigContext from './components/ConfigContext'; +import { getUiConfig } from './services/utils/uiConfigHandler'; const { Content } = Layout; const loadingView = ( @@ -161,12 +162,29 @@ class App extends React.Component { const redirectUrl = encodeURI(window.location.href); const pageURL = window.location.pathname; const lastURLSegment = pageURL.substr(pageURL.lastIndexOf('/') + 1); - if (lastURLSegment !== 'login') { - window.location.href = - window.location.origin + `/publisher/login?redirect=${redirectUrl}`; - } else { - this.getAndroidEnterpriseToken(config); - } + getUiConfig(config).then(uiConfig => { + if (uiConfig !== undefined) { + if (uiConfig.isSsoEnable) { + window.location = + window.location.origin + + config.serverConfig.ssoLoginUri + + '?redirect=' + + window.location.origin + + pageURL; + } else if (lastURLSegment !== 'login') { + window.location.href = + window.location.origin + + `/${config.appName}/login?redirect=${redirectUrl}`; + } else { + this.getAndroidEnterpriseToken(config); + } + } else { + this.setState({ + loading: false, + error: true, + }); + } + }); } else { this.setState({ loading: false, diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/src/scenes/Home/components/Logout/index.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/src/scenes/Home/components/Logout/index.js index f41e654bcc..0ecbb604fa 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/src/scenes/Home/components/Logout/index.js +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/src/scenes/Home/components/Logout/index.js @@ -21,6 +21,7 @@ import { LogoutOutlined } from '@ant-design/icons'; import { notification, Menu } from 'antd'; import axios from 'axios'; import { withConfigContext } from '../../../../components/ConfigContext'; +import { getUiConfig } from '../../../../services/utils/uiConfigHandler'; /* This class for call the logout api by sending request @@ -44,27 +45,38 @@ class Logout extends React.Component { inValid: false, }); - axios - .post(window.location.origin + config.serverConfig.logoutUri) - .then(res => { - // if the api call status is correct then user will logout and then it goes to login page - if (res.status === 200) { - window.location = window.location.origin + '/publisher/login'; - } - }) - .catch(function(error) { - if (error.hasOwnProperty('response') && error.response.status === 400) { - thisForm.setState({ - inValid: true, - }); + let logoutUri; + getUiConfig(config).then(uiConfig => { + if (uiConfig !== undefined) { + if (uiConfig.isSsoEnable) { + logoutUri = window.location.origin + config.serverConfig.ssoLogoutUri; } else { - notification.error({ - message: 'There was a problem', - duration: 0, - description: 'Error occurred while trying to logout.', - }); + logoutUri = window.location.origin + config.serverConfig.logoutUri; } - }); + axios + .post(logoutUri) + .then(res => { + // if the api call status is correct then user + // will logout and then it goes to login page + if (res.status === 200) { + window.location = + window.location.origin + `/${config.appName}/login`; + } + }) + .catch(function(error) { + notification.error({ + message: 'There was a problem', + duration: 0, + description: 'Error occurred while trying to logout.', + }); + }); + } else { + this.setState({ + loading: false, + error: true, + }); + } + }); }; render() { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/src/services/utils/uiConfigHandler.js b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/src/services/utils/uiConfigHandler.js new file mode 100644 index 0000000000..77fd691c37 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/react-app/src/services/utils/uiConfigHandler.js @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2020. Entgra (Pvt) Ltd, https://entgra.io + * All Rights Reserved. + * + * Unauthorized copying/redistribution of this file, via any medium + * is strictly prohibited. + * Proprietary and confidential. + * + * Licensed under the Entgra Commercial License, + * Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * 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. + * + * You may obtain a copy of the License at + * https://entgra.io/licenses/entgra-commercial/1.0 + */ + +import axios from 'axios'; +import { notification } from 'antd'; + +export const getUiConfig = config => { + return axios + .get(window.location.origin + config.serverConfig.appUiConfigUri) + .then(res => { + return res.data; + }) + .catch(error => { + notification.error({ + message: 'There was a problem', + duration: 0, + description: 'Error occurred while trying to load UI configurations.', + }); + }); +};