Improve APIM application key generate method

pull/142/head
Dharmakeerthi Lasantha 1 year ago
parent a09d72c33c
commit af17e4fa8e

@ -84,6 +84,12 @@
<artifactId>io.entgra.device.mgt.core.apimgt.extension.rest.api</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
<dependency>
<groupId>io.entgra.device.mgt.core</groupId>
<artifactId>io.entgra.device.mgt.core.device.mgt.common</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
</dependencies>
<build>
@ -124,6 +130,7 @@
io.entgra.device.mgt.core.identity.jwt.client.extension.dto;version="[5.0,6)",
io.entgra.device.mgt.core.identity.jwt.client.extension.exception;version="[5.0,6)",
io.entgra.device.mgt.core.identity.jwt.client.extension.service;version="[5.0,6)",
io.entgra.device.mgt.core.device.mgt.common;version="[5.0,6)",
org.apache.commons.lang;version="[2.6,3)",
org.apache.commons.logging;version="[1.2,2)",
org.json.simple;version="[1.1,2)",

@ -24,6 +24,10 @@ import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplication
import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException;
import io.entgra.device.mgt.core.apimgt.application.extension.internal.APIApplicationManagerExtensionDataHolder;
import io.entgra.device.mgt.core.apimgt.application.extension.util.APIManagerUtil;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataKeyAlreadyExistsException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService;
import io.entgra.device.mgt.core.identity.jwt.client.extension.JWTClient;
import io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo;
import io.entgra.device.mgt.core.identity.jwt.client.extension.exception.JWTClientException;
@ -41,6 +45,7 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.Unexpected
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.http.MetaData;
import org.wso2.carbon.apimgt.api.APIAdmin;
import org.wso2.carbon.apimgt.api.APIConsumer;
import org.wso2.carbon.apimgt.api.APIManagementException;
@ -159,13 +164,47 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
}
}
MetadataManagementService metadataManagementService = APIApplicationManagerExtensionDataHolder.getInstance().getMetadataManagementService();
if (isNewApplication) {
ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(applicationInfo, application);
ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey());
apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret());
return apiApplicationKey;
Metadata metaData = new Metadata();
metaData.setMetaKey(applicationName);
String metaValue = application.getApplicationId() + ":" + applicationKey.getKeyMappingId();
metaData.setMetaValue(metaValue);
try {
metadataManagementService.createMetadata(metaData);
return apiApplicationKey;
} catch (MetadataManagementException e) {
String msg = "Error occurred while creating the meta data entry for mata key: " + applicationName;
log.error(msg, e);
throw new APIManagerException(msg, e);
} catch (MetadataKeyAlreadyExistsException e) {
String msg = "Found duplicate meta value entry for meta key: " + applicationName;
log.error(msg, e);
throw new APIManagerException(msg, e);
}
} else {
try {
Metadata metaData = metadataManagementService.retrieveMetadata(applicationName);
if (metaData == null) {
String msg =
"Couldn't find application key data from meta data mgt service. Meta key: " + applicationName;
log.error(msg);
throw new APIManagerException(msg);
}
String[] metaValues = metaData.getMetaValue().split(":");
String applicationId = metaValues[0];
String keyMappingId = metaValues[1];
//todo call the API key retrieving call, return apiApplicationKey;
} catch (MetadataManagementException e) {
String msg = "Error occurred while getting meta data for meta key: " + applicationName;
log.error(msg, e);
throw new APIManagerException(msg, e);
}
return null;
}
} catch (APIServicesException e) {

@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.apimgt.application.extension.internal;
import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderService;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.identity.jwt.client.extension.service.JWTClientManagerService;
import org.wso2.carbon.registry.core.service.TenantRegistryLoader;
@ -39,6 +40,7 @@ public class APIApplicationManagerExtensionDataHolder {
private JWTClientManagerService jwtClientManagerService;
private ConsumerRESTAPIServices consumerRESTAPIServices;
private APIApplicationServices apiApplicationServices;
private MetadataManagementService metadataManagementService;
private APIApplicationManagerExtensionDataHolder() {
}
@ -123,4 +125,12 @@ public class APIApplicationManagerExtensionDataHolder {
public void setApiApplicationServices(APIApplicationServices apiApplicationServices) {
this.apiApplicationServices = apiApplicationServices;
}
public MetadataManagementService getMetadataManagementService() {
return metadataManagementService;
}
public void setMetadataManagementService(MetadataManagementService metadataManagementService) {
this.metadataManagementService = metadataManagementService;
}
}

@ -21,6 +21,7 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationService
import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices;
import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderService;
import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderServiceImpl;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;
@ -178,4 +179,34 @@ public class APIApplicationManagerExtensionServiceComponent {
}
APIApplicationManagerExtensionDataHolder.getInstance().setApiApplicationServices(null);
}
/**
* Sets Meta Data Mgt service.
*
* @param metadataManagementService An instance of MetadataManagementService
*/
@Reference(
name = "meta.data.mgt.service",
service = io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetAMetaMgtServices")
protected void setMetaMgtServices(MetadataManagementService metadataManagementService) {
if (log.isDebugEnabled()) {
log.debug("Setting Meta data mgt Service");
}
APIApplicationManagerExtensionDataHolder.getInstance().setMetadataManagementService(metadataManagementService);
}
/**
* Unset Meta Data Mgt service
*
* @param metadataManagementService An instance of MetadataManagementService
*/
protected void unsetAMetaMgtServices(MetadataManagementService metadataManagementService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting Meta Data mgt Service");
}
APIApplicationManagerExtensionDataHolder.getInstance().setMetadataManagementService(null);
}
}

@ -32,6 +32,8 @@ public class DCRRequest {
@XmlElement(required = true)
private String username;
@XmlElement(required = true)
private String password;
@XmlElement(required = true)
private String grantTypes;
@XmlElement
private String callBackUrl;
@ -98,4 +100,8 @@ public class DCRRequest {
public void setValidityPeriod(int validityPeriod) {
this.validityPeriod = validityPeriod;
}
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
}

@ -55,6 +55,11 @@
<groupId>io.entgra.device.mgt.core</groupId>
<artifactId>io.entgra.device.mgt.core.device.mgt.common</artifactId>
</dependency>
<dependency>
<groupId>io.entgra.device.mgt.core</groupId>
<artifactId>io.entgra.device.mgt.core.apimgt.extension.rest.api</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
</dependencies>
<build>
@ -108,6 +113,7 @@
org.wso2.carbon.apimgt.keymgt.model;version="[9.28,10)",
org.wso2.carbon.apimgt.keymgt.model.entity;version="[9.28,10)",
org.wso2.carbon.apimgt.keymgt.service;version="[9.28,10)",
io.entgra.device.mgt.core.apimgt.extension.rest.api,
org.wso2.carbon.context;version="[4.8,5)",
org.wso2.carbon.user.api;version="[1.0,2)",
org.wso2.carbon.user.core.service;version="[4.8,5)",

@ -18,12 +18,16 @@
package io.entgra.device.mgt.core.apimgt.keymgt.extension.internal;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices;
import io.entgra.device.mgt.core.apimgt.keymgt.extension.service.KeyMgtService;
public class KeyMgtDataHolder {
private static final KeyMgtDataHolder thisInstance = new KeyMgtDataHolder();
private KeyMgtService keyMgtService;
private ConsumerRESTAPIServices consumerRESTAPIServices;
private APIApplicationServices apiApplicationServices;
public static KeyMgtDataHolder getInstance() {
return thisInstance;
@ -37,4 +41,20 @@ public class KeyMgtDataHolder {
this.keyMgtService = keyMgtService;
}
public ConsumerRESTAPIServices getConsumerRESTAPIServices() {
return consumerRESTAPIServices;
}
public void setConsumerRESTAPIServices(ConsumerRESTAPIServices consumerRESTAPIServices) {
this.consumerRESTAPIServices = consumerRESTAPIServices;
}
public APIApplicationServices getApiApplicationServices() {
return apiApplicationServices;
}
public void setApiApplicationServices(APIApplicationServices apiApplicationServices) {
this.apiApplicationServices = apiApplicationServices;
}
}

@ -18,15 +18,16 @@
package io.entgra.device.mgt.core.apimgt.keymgt.extension.internal;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices;
import io.entgra.device.mgt.core.apimgt.keymgt.extension.service.KeyMgtService;
import io.entgra.device.mgt.core.apimgt.keymgt.extension.service.KeyMgtServiceImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.*;
@Component(
name = "io.entgra.device.mgt.core.apimgt.keymgt.extension.internal.KeyMgtServiceComponent",
immediate = true)
@ -63,4 +64,64 @@ public class KeyMgtServiceComponent {
log.debug("De-activating Key Management Service Component");
}
}
/**
* Sets APIM Consumer REST API service.
*
* @param consumerRESTAPIServices An instance of ConsumerRESTAPIServices
*/
@Reference(
name = "APIM.consumer.service",
service = io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetConsumerRESTAPIServices")
protected void setConsumerRESTAPIServices(ConsumerRESTAPIServices consumerRESTAPIServices) {
if (log.isDebugEnabled()) {
log.debug("Setting APIM Consumer REST API Service");
}
KeyMgtDataHolder.getInstance().setConsumerRESTAPIServices(consumerRESTAPIServices);
}
/**
* Unset APIM Consumer REST API service
*
* @param consumerRESTAPIServices An instance of ConsumerRESTAPIServices
*/
protected void unsetConsumerRESTAPIServices(ConsumerRESTAPIServices consumerRESTAPIServices) {
if (log.isDebugEnabled()) {
log.debug("Unsetting APIM Consumer REST API Service");
}
KeyMgtDataHolder.getInstance().setConsumerRESTAPIServices(null);
}
/**
* Sets DCR REST API service.
*
* @param apiApplicationServices An instance of APIApplicationServices
*/
@Reference(
name = "APIM.application.service",
service = io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetAPIApplicationServices")
protected void setAPIApplicationServices(APIApplicationServices apiApplicationServices) {
if (log.isDebugEnabled()) {
log.debug("Setting DCR REST API Service");
}
KeyMgtDataHolder.getInstance().setApiApplicationServices(apiApplicationServices);
}
/**
* Unset DCR REST API service
*
* @param apiApplicationServices An instance of APIApplicationServices
*/
protected void unsetAPIApplicationServices(APIApplicationServices apiApplicationServices) {
if (log.isDebugEnabled()) {
log.debug("Unsetting DCR REST API Service");
}
KeyMgtDataHolder.getInstance().setApiApplicationServices(null);
}
}

Loading…
Cancel
Save