Add improvements to token generation for devices

apim420
Dharmakeerthi Lasantha 1 year ago
parent 253157ba1a
commit d40dccc240

@ -312,6 +312,7 @@ public class KeyMgtServiceImpl implements KeyMgtService {
* @throws KeyMgtException if any error occurs while creating response object * @throws KeyMgtException if any error occurs while creating response object
*/ */
private OAuthApplication createOauthApplication (String clientName, String owner, String[] tags, int validityPeriod) throws KeyMgtException { 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); String oauthAppCreationPayloadStr = createOauthAppCreationPayload(clientName, owner, tags, validityPeriod);
RequestBody oauthAppCreationPayload = RequestBody.Companion.create(oauthAppCreationPayloadStr, JSON); RequestBody oauthAppCreationPayload = RequestBody.Companion.create(oauthAppCreationPayloadStr, JSON);
kmConfig = getKeyManagerConfig(); kmConfig = getKeyManagerConfig();

@ -798,6 +798,28 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
KeyMgtService keyMgtService = new KeyMgtServiceImpl(); KeyMgtService keyMgtService = new KeyMgtServiceImpl();
try { try {
//todo - lasantha - can't get password from here //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, DCRResponse dcrResponse = keyMgtService.dynamicClientRegistration(applicationName, username,
"client_credentials", null, new String[] {"device_management"}, false, validityTime); "client_credentials", null, new String[] {"device_management"}, false, validityTime);
deviceConfig.setClientId(dcrResponse.getClientId()); deviceConfig.setClientId(dcrResponse.getClientId());

@ -18,6 +18,7 @@
package io.entgra.device.mgt.core.device.mgt.api.jaxrs.util; 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.ApplicationManager;
import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager; import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.DeviceTypeVersionWrapper; 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 SubscriptionManager subscriptionManager;
private static volatile ApplicationManager applicationManager; private static volatile ApplicationManager applicationManager;
private static volatile ConsumerRESTAPIServices consumerRESTAPIServices;
static { static {
String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password"); String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password");
@ -387,6 +389,25 @@ public class DeviceMgtAPIUtils {
return otpManagementService; 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() { public static RegistryService getRegistryService() {
RegistryService registryService; RegistryService registryService;
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();

Loading…
Cancel
Save