From d40dccc240ed908ba8d27ba07a94374272cbe045 Mon Sep 17 00:00:00 2001 From: Dharmakeerthi Lasantha Date: Wed, 14 Jun 2023 12:37:10 +0530 Subject: [PATCH] Add improvements to token generation for devices --- .../extension/service/KeyMgtServiceImpl.java | 1 + .../impl/DeviceManagementServiceImpl.java | 22 +++++++++++++++++++ .../mgt/api/jaxrs/util/DeviceMgtAPIUtils.java | 21 ++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java index 936011e90f..a9dd182ec2 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java @@ -312,6 +312,7 @@ public class KeyMgtServiceImpl implements KeyMgtService { * @throws KeyMgtException if any error occurs while creating response object */ private OAuthApplication createOauthApplication (String clientName, String owner, String[] tags, int validityPeriod) throws KeyMgtException { + //todo modify this to pass the password as well String oauthAppCreationPayloadStr = createOauthAppCreationPayload(clientName, owner, tags, validityPeriod); RequestBody oauthAppCreationPayload = RequestBody.Companion.create(oauthAppCreationPayloadStr, JSON); kmConfig = getKeyManagerConfig(); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java index 579f09c606..9bef3e44fc 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -798,6 +798,28 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { KeyMgtService keyMgtService = new KeyMgtServiceImpl(); try { //todo - lasantha - can't get password from here + + try { +// DCRResponse dcrResponse = keyMgtService.dynamicClientRegistration(applicationName, username, +// "client_credentials", null, new String[] {"device_management"}, false, validityTime); +// deviceConfig.setClientId(dcrResponse.getClientId()); +// deviceConfig.setClientSecret(dcrResponse.getClientSecret()); + + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + JWTClientManagerService jwtClientManagerService = (JWTClientManagerService) ctx. + getOSGiService(JWTClientManagerService.class, null); + JWTClient jwtClient = jwtClientManagerService.getJWTClient(); +// AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(), +// apiApplicationKey.getConsumerSecret(), +// username, Constants.ApplicationInstall.SUBSCRIPTION_SCOPE); + } catch (JWTClientException e) { + String msg = "Error while generating an OAuth token for user " + username; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + + //todo call REST APIs DCRResponse dcrResponse = keyMgtService.dynamicClientRegistration(applicationName, username, "client_credentials", null, new String[] {"device_management"}, false, validityTime); deviceConfig.setClientId(dcrResponse.getClientId()); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java index 1073737ef5..7bba263529 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.util; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.DeviceTypeVersionWrapper; @@ -154,6 +155,7 @@ public class DeviceMgtAPIUtils { private static volatile SubscriptionManager subscriptionManager; private static volatile ApplicationManager applicationManager; + private static volatile ConsumerRESTAPIServices consumerRESTAPIServices; static { String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password"); @@ -387,6 +389,25 @@ public class DeviceMgtAPIUtils { return otpManagementService; } + /** + * Initializing and accessing method for APIM Consumer REST API. + * + * @return ConsumerRESTAPIServices instance + * @throws IllegalStateException if ConsumerRESTAPIServices cannot be initialized + */ + public static synchronized ConsumerRESTAPIServices getConsumerRESTAPIServices() { + if (consumerRESTAPIServices == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + consumerRESTAPIServices = (ConsumerRESTAPIServices) ctx.getOSGiService(ConsumerRESTAPIServices.class, null); + if (consumerRESTAPIServices == null) { + String msg = "Consumer Rest API service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + return consumerRESTAPIServices; + } + public static RegistryService getRegistryService() { RegistryService registryService; PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();