From e85d3af9c0b79252ad5b0f1e4b7952e1f08faf46 Mon Sep 17 00:00:00 2001 From: anjula-sack Date: Fri, 19 Jun 2020 18:59:10 +0530 Subject: [PATCH 1/7] Upgrade the npm version of mvn build --- .../pom.xml | 1 - .../org.wso2.carbon.device.application.mgt.store.ui/pom.xml | 1 - pom.xml | 3 +-- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/pom.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/pom.xml index 261da4429e6..5d7920e6fcf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/pom.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/pom.xml @@ -66,7 +66,6 @@ generate-resources ${node.version} - ${npm.version} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/pom.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/pom.xml index f406adc114a..09c75ff7eee 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/pom.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.ui/pom.xml @@ -69,7 +69,6 @@ generate-resources ${node.version} - ${npm.version} diff --git a/pom.xml b/pom.xml index 861a91c8e69..2fccdc25b32 100644 --- a/pom.xml +++ b/pom.xml @@ -2249,8 +2249,7 @@ 1.21 2.6.5 1.7.6 - v10.15.3 - 6.9.0 + v12.18.1 3.1.0 From e0eb4316f4154348522ea9443bee93f4f13621af Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Fri, 17 Jul 2020 06:24:15 +0530 Subject: [PATCH 2/7] Add Default Token Handling Servlet --- .../interceptor/DefaultTokenHandler.java | 139 ++++++++++++++++++ .../ui/request/interceptor/LoginHandler.java | 2 +- .../ui/request/interceptor/UserHandler.java | 20 +-- .../interceptor/util/HandlerConstants.java | 1 + .../request/interceptor/util/HandlerUtil.java | 16 +- 5 files changed, 157 insertions(+), 21 deletions(-) create mode 100644 components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java new file mode 100644 index 00000000000..c8db2cbc19f --- /dev/null +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java @@ -0,0 +1,139 @@ +/* Copyright (c) 2020, 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 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.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.HttpHeaders; +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.entity.ContentType; +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( + name = "DefaultTokenHandlerServlet", + description = "This servlet intercepts the api requests initiated from the user interface to get the default " + + "token", + urlPatterns = { "/default-credentials/*" } + ) +public class DefaultTokenHandler extends HttpServlet { + private static final Log log = LogFactory.getLog(DefaultTokenHandler.class); + + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) { + try { + HttpSession httpSession = req.getSession(false); + + if (httpSession != null) { + AuthData authData = (AuthData) httpSession.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY); + if (authData == null) { + HandlerUtil.sendUnAuthorizeResponse(resp); + return; + } + + AuthData defaultAuthData = (AuthData) httpSession + .getAttribute(HandlerConstants.SESSION_DEFAULT_AUTH_DATA_KEY); + if (defaultAuthData != null) { + HandlerUtil.handleSuccess(resp, constructSuccessProxyResponse(defaultAuthData.getAccessToken())); + return; + } + + String clientId = authData.getClientId(); + String clientSecret = authData.getClientSecret(); + + String iotsCorePort = System.getProperty("iot.core.https.port"); + if (HandlerConstants.HTTP_PROTOCOL.equals(req.getScheme())) { + iotsCorePort = System.getProperty("iot.core.http.port"); + } + String tokenUrl = + req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.core.host") + + HandlerConstants.COLON + iotsCorePort + "/api/device-mgt/v1.0/devices" + clientId + + HandlerConstants.SCHEME_SEPARATOR + clientSecret + HandlerConstants.SCHEME_SEPARATOR + + "default-token"; + + HttpGet defaultTokenRequest = new HttpGet(tokenUrl); + defaultTokenRequest + .setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BEARER + authData.getAccessToken()); + defaultTokenRequest + .setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString()); + ProxyResponse tokenResultResponse = HandlerUtil.execute(defaultTokenRequest); + + if (tokenResultResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { + log.error("Error occurred while invoking the API to get default token data."); + HandlerUtil.handleError(resp, tokenResultResponse); + return; + } + String tokenResult = tokenResultResponse.getData(); + if (tokenResult == null) { + log.error("Invalid default token response is received."); + HandlerUtil.handleError(resp, tokenResultResponse); + return; + } + + JsonParser jsonParser = new JsonParser(); + JsonElement jTokenResult = jsonParser.parse(tokenResult); + if (jTokenResult.isJsonObject()) { + JsonObject jTokenResultAsJsonObject = jTokenResult.getAsJsonObject(); + AuthData newDefaultAuthData = new AuthData(); + newDefaultAuthData.setClientId(clientId); + newDefaultAuthData.setClientSecret(clientSecret); + + String defaultToken = jTokenResultAsJsonObject.get("access_token").getAsString(); + newDefaultAuthData.setAccessToken(defaultToken); + newDefaultAuthData.setRefreshToken(jTokenResultAsJsonObject.get("refresh_token").getAsString()); + newDefaultAuthData.setScope(jTokenResultAsJsonObject.get("scope").getAsString()); + httpSession.setAttribute(HandlerConstants.SESSION_DEFAULT_AUTH_DATA_KEY, newDefaultAuthData); + + HandlerUtil.handleSuccess(resp, constructSuccessProxyResponse(defaultToken)); + } + } else { + HandlerUtil.sendUnAuthorizeResponse(resp); + } + } catch (IOException e) { + log.error("Error occurred when processing GET request to get default token.", e); + } + } + + /** + * Get Success Proxy Response + * @param responseString Response String + * @return {@link ProxyResponse} + */ + private ProxyResponse constructSuccessProxyResponse (String responseString) { + ProxyResponse proxyResponse = new ProxyResponse(); + proxyResponse.setCode(HttpStatus.SC_OK); + proxyResponse.setData(responseString); + return proxyResponse; + } +} 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 b9f5403accc..53faa63eda2 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 @@ -72,7 +72,6 @@ public class LoginHandler extends HttpServlet { httpSession.setMaxInactiveInterval(Math.toIntExact(HandlerConstants.TIMEOUT)); HttpGet uiConfigEndpoint = new HttpGet(uiConfigUrl); - JsonParser jsonParser = new JsonParser(); ProxyResponse uiConfigResponse = HandlerUtil.execute(uiConfigEndpoint); String executorResponse = uiConfigResponse.getExecutorResponse(); if (!StringUtils.isEmpty(executorResponse) && executorResponse @@ -88,6 +87,7 @@ public class LoginHandler extends HttpServlet { HandlerUtil.handleError(resp, null); return; } + JsonParser jsonParser = new JsonParser(); JsonElement uiConfigJsonElement = jsonParser.parse(uiConfigResponse.getData()); JsonObject uiConfigJsonObject = null; if (uiConfigJsonElement.isJsonObject()) { 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 index 0464488e56e..d35e4958352 100644 --- 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 @@ -56,13 +56,13 @@ public class UserHandler extends HttpServlet { + HandlerConstants.COLON + HandlerUtil.getGatewayPort(req.getScheme()); HttpSession httpSession = req.getSession(false); if (httpSession == null) { - sendUnAuthorizeResponse(resp); + HandlerUtil.sendUnAuthorizeResponse(resp); return; } AuthData authData = (AuthData) httpSession.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY); if (authData == null) { - sendUnAuthorizeResponse(resp); + HandlerUtil.sendUnAuthorizeResponse(resp); return; } @@ -91,7 +91,7 @@ public class UserHandler extends HttpServlet { if (jTokenResult.isJsonObject()) { JsonObject jTokenResultAsJsonObject = jTokenResult.getAsJsonObject(); if (!jTokenResultAsJsonObject.get("active").getAsBoolean()) { - sendUnAuthorizeResponse(resp); + HandlerUtil.sendUnAuthorizeResponse(resp); return; } ProxyResponse proxyResponse = new ProxyResponse(); @@ -106,18 +106,4 @@ public class UserHandler extends HttpServlet { log.error("Error occurred while parsing the response. ", e); } } - - /** - * Send UnAuthorized Response to the user - * - * @param resp HttpServletResponse object - */ - private void sendUnAuthorizeResponse(HttpServletResponse resp) - throws IOException { - ProxyResponse proxyResponse = new ProxyResponse(); - proxyResponse.setCode(HttpStatus.SC_UNAUTHORIZED); - proxyResponse.setExecutorResponse( - HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(HttpStatus.SC_UNAUTHORIZED)); - HandlerUtil.handleError(resp, proxyResponse); - } } 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 261a28bb4bd..a335f224c72 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 @@ -30,6 +30,7 @@ public class HandlerConstants { public static final String TAGS_KEY = "tags"; public static final String APP_NAME_KEY = "applicationName"; public static final String SESSION_AUTH_DATA_KEY = "authInfo"; + public static final String SESSION_DEFAULT_AUTH_DATA_KEY = "defaultAuthInfo"; public static final String UI_CONFIG_KEY = "ui-config"; public static final String PLATFORM = "platform"; public static final String USERNAME = "username"; 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 f35c0700eef..82685daa435 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 @@ -19,7 +19,6 @@ package io.entgra.ui.request.interceptor.util; import com.google.gson.Gson; -import com.google.gson.JsonObject; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -35,9 +34,7 @@ import org.json.JSONException; import org.json.JSONObject; import org.wso2.carbon.device.application.mgt.common.ProxyResponse; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -232,4 +229,17 @@ public class HandlerUtil { } } + /** + * Send UnAuthorized Response to the user + * + * @param resp HttpServletResponse object + */ + public static void sendUnAuthorizeResponse(HttpServletResponse resp) + throws IOException { + ProxyResponse proxyResponse = new ProxyResponse(); + proxyResponse.setCode(HttpStatus.SC_UNAUTHORIZED); + proxyResponse.setExecutorResponse( + HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(HttpStatus.SC_UNAUTHORIZED)); + handleError(resp, proxyResponse); + } } From 43a6c710add55462a23efcef4ed30b2a85c721b5 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Fri, 17 Jul 2020 08:11:44 +0530 Subject: [PATCH 3/7] Handle change of device name --- .../mgt/core/DeviceManagementConstants.java | 9 +++++++++ .../impl/DeviceInformationManagerImpl.java | 20 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index 7f7610126b4..1b402560b78 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -169,4 +169,13 @@ public final class DeviceManagementConstants { public static final String DEVICE_INFO_PARAM = "device-info"; public static final String APP_USAGE_ENDPOINT = REPORTING_CONTEXT + "/app-usage"; } + + public static final class Payload { + private Payload() { + throw new AssertionError(); + } + public static final String DEVICE_INFO_DEVICE_NAME = "DEVICE_NAME"; + public static final String DEVICE_INFO_IMEI = "IMEI"; + public static final String DEVICE_INFO_IMSI = "IMSI";; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 2b4756d0f5b..c111f02f3be 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -134,6 +134,20 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { deviceDetailsDAO.addDeviceProperties(injectableProps, device.getId(), device.getEnrolmentInfo().getId()); } + + if (deviceInfo.getDeviceDetailsMap().containsKey(DeviceManagementConstants + .Payload.DEVICE_INFO_DEVICE_NAME) && + StringUtils.isNotEmpty(deviceInfo.getDeviceDetailsMap() + .get(DeviceManagementConstants.Payload.DEVICE_INFO_DEVICE_NAME)) + && !device.getName().equals(deviceInfo.getDeviceDetailsMap() + .get(DeviceManagementConstants.Payload.DEVICE_INFO_DEVICE_NAME))) { + String name = deviceInfo.getDeviceDetailsMap() + .get(DeviceManagementConstants.Payload.DEVICE_INFO_DEVICE_NAME); + log.info("Device identifier " + device.getDeviceIdentifier() + ", Device name " + + "changed by user from " + device.getName() + " to " + name); + device.setName(name); + } + deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); DeviceManagementDAOFactory.commitTransaction(); @@ -142,8 +156,10 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { Object[] metaData = {device.getDeviceIdentifier(), device.getType()}; Object[] payload = new Object[]{ Calendar.getInstance().getTimeInMillis(), - deviceInfo.getDeviceDetailsMap().get("IMEI"), - deviceInfo.getDeviceDetailsMap().get("IMSI"), + deviceInfo.getDeviceDetailsMap().get(DeviceManagementConstants.Payload + .DEVICE_INFO_IMEI), + deviceInfo.getDeviceDetailsMap().get(DeviceManagementConstants.Payload + .DEVICE_INFO_IMSI), deviceInfo.getDeviceModel(), deviceInfo.getVendor(), deviceInfo.getOsVersion(), From 1f94415f7f793233bacc81351ccac33ef92b2f61 Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Thu, 16 Jul 2020 20:43:50 +0530 Subject: [PATCH 4/7] Add API to get default token by using client ID and secret --- .../pom.xml | 40 -------------- .../service/api/DeviceManagementService.java | 54 +++++++++++++++++++ .../impl/DeviceManagementServiceImpl.java | 23 ++++++++ 3 files changed, 77 insertions(+), 40 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/pom.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/pom.xml index a8f9f24ae43..d5af4494774 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/pom.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/pom.xml @@ -100,46 +100,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 02ded254dfe..60799bf5a27 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -2273,4 +2273,58 @@ public interface DeviceManagementService { response = ErrorResponse.class) }) Response getDeviceFilters(); + + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("/{clientId}/{clientSecret}/default-token") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Getting the default token", + notes = "Getting the default access token by using given client ID and the client secret value.", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:device:enroll") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully returned the default token details.", + response = Policy.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests.")}), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while retrieving the default token.", + response = ErrorResponse.class) + } + ) + Response getDefaultToken( + @ApiParam( + name = "client ID", + value = "Client Id.", + required = true) + @PathParam("clientId") + String clientId, + @ApiParam( + name = "client secret", + value = "Client Secret", + required = true) + @PathParam("clientSecret") + String clientSecret + ); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 3600ffb3ab1..4df23a511b5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -38,11 +38,13 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl; import java.util.LinkedList; import java.util.Queue; + import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceFilters; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; @@ -102,6 +104,10 @@ import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; +import org.wso2.carbon.identity.jwt.client.extension.JWTClient; +import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; +import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; +import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.user.api.UserStoreException; @@ -1328,4 +1334,21 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } + + @GET + @Path("/{clientId}/{clientSecret}/default-token") + @Override + public Response getDefaultToken(String clientId, String clientSecret) { + JWTClientManagerService jwtClientManagerService = DeviceMgtAPIUtils.getJWTClientManagerService(); + try { + JWTClient jwtClient = jwtClientManagerService.getJWTClient(); + AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(clientId, clientSecret, + PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(), "default"); + return Response.status(Response.Status.OK).entity(accessTokenInfo).build(); + } catch (JWTClientException e) { + String msg = "Error occurred while getting default access token by using given client Id and client secret."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } } From 44ba40fe7d4c2e4425490f765b6c5be9dfcc6664 Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Fri, 17 Jul 2020 17:13:12 +0530 Subject: [PATCH 5/7] Improve proxy handler --- .../entgra/ui/request/interceptor/DefaultTokenHandler.java | 7 +------ .../io/entgra/ui/request/interceptor/LogoutHandler.java | 1 - 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java index c8db2cbc19f..7d89550ef1d 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java @@ -40,12 +40,7 @@ import javax.servlet.http.HttpSession; import java.io.IOException; @MultipartConfig -@WebServlet( - name = "DefaultTokenHandlerServlet", - description = "This servlet intercepts the api requests initiated from the user interface to get the default " - + "token", - urlPatterns = { "/default-credentials/*" } - ) +@WebServlet("/default-credentials") public class DefaultTokenHandler extends HttpServlet { private static final Log log = LogFactory.getLog(DefaultTokenHandler.class); diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LogoutHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LogoutHandler.java index 771341cb41d..8ebacf1cb7b 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LogoutHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LogoutHandler.java @@ -18,7 +18,6 @@ package io.entgra.ui.request.interceptor; -import io.entgra.ui.request.interceptor.util.HandlerConstants; import io.entgra.ui.request.interceptor.util.HandlerUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; From 45e753f68fdfd51ad5cb058605e7e0c34cf4a13e Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Fri, 17 Jul 2020 19:31:38 +0530 Subject: [PATCH 6/7] Construct API endpoint to get default token --- .../entgra/ui/request/interceptor/DefaultTokenHandler.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java index 7d89550ef1d..a500c0027be 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java @@ -71,11 +71,11 @@ public class DefaultTokenHandler extends HttpServlet { if (HandlerConstants.HTTP_PROTOCOL.equals(req.getScheme())) { iotsCorePort = System.getProperty("iot.core.http.port"); } + String tokenUrl = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.core.host") - + HandlerConstants.COLON + iotsCorePort + "/api/device-mgt/v1.0/devices" + clientId - + HandlerConstants.SCHEME_SEPARATOR + clientSecret + HandlerConstants.SCHEME_SEPARATOR - + "default-token"; + + HandlerConstants.COLON + iotsCorePort + "/api/device-mgt/v1.0/devices/" + clientId + + "/" + clientSecret + "/default-token"; HttpGet defaultTokenRequest = new HttpGet(tokenUrl); defaultTokenRequest From 2ec29701a1422145e55c075c47bde243a1241546 Mon Sep 17 00:00:00 2001 From: shamalka Date: Fri, 17 Jul 2020 23:52:25 +0530 Subject: [PATCH 7/7] Fix get default token issues --- .../mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java | 2 +- .../entgra/ui/request/interceptor/DefaultTokenHandler.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 4df23a511b5..a4ca3f026d9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -1338,7 +1338,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @GET @Path("/{clientId}/{clientSecret}/default-token") @Override - public Response getDefaultToken(String clientId, String clientSecret) { + public Response getDefaultToken(@PathParam("clientId") String clientId, @PathParam("clientSecret") String clientSecret) { JWTClientManagerService jwtClientManagerService = DeviceMgtAPIUtils.getJWTClientManagerService(); try { JWTClient jwtClient = jwtClientManagerService.getJWTClient(); diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java index a500c0027be..1cff1d9d114 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java @@ -104,10 +104,10 @@ public class DefaultTokenHandler extends HttpServlet { newDefaultAuthData.setClientId(clientId); newDefaultAuthData.setClientSecret(clientSecret); - String defaultToken = jTokenResultAsJsonObject.get("access_token").getAsString(); + String defaultToken = jTokenResultAsJsonObject.get("accessToken").getAsString(); newDefaultAuthData.setAccessToken(defaultToken); - newDefaultAuthData.setRefreshToken(jTokenResultAsJsonObject.get("refresh_token").getAsString()); - newDefaultAuthData.setScope(jTokenResultAsJsonObject.get("scope").getAsString()); + newDefaultAuthData.setRefreshToken(jTokenResultAsJsonObject.get("refreshToken").getAsString()); + newDefaultAuthData.setScope(jTokenResultAsJsonObject.get("scopes").getAsString()); httpSession.setAttribute(HandlerConstants.SESSION_DEFAULT_AUTH_DATA_KEY, newDefaultAuthData); HandlerUtil.handleSuccess(resp, constructSuccessProxyResponse(defaultToken));