|
|
@ -18,7 +18,7 @@
|
|
|
|
package io.entgra.device.mgt.core.apimgt.webapp.publisher;
|
|
|
|
package io.entgra.device.mgt.core.apimgt.webapp.publisher;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.annotations.Scopes;
|
|
|
|
//import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServicesImpl;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServicesImpl;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices;
|
|
|
@ -43,7 +43,6 @@ import io.entgra.device.mgt.core.device.mgt.core.config.permission.ScopeMapping;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.wso2.carbon.CarbonConstants;
|
|
|
|
import org.wso2.carbon.CarbonConstants;
|
|
|
|
import org.wso2.carbon.apimgt.api.APIManagementException;
|
|
|
|
import org.wso2.carbon.apimgt.api.APIManagementException;
|
|
|
@ -70,16 +69,7 @@ import java.io.IOException;
|
|
|
|
import java.nio.file.DirectoryIteratorException;
|
|
|
|
import java.nio.file.DirectoryIteratorException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* This class represents the concrete implementation of the APIPublisherService that corresponds to providing all
|
|
|
|
* This class represents the concrete implementation of the APIPublisherService that corresponds to providing all
|
|
|
@ -111,11 +101,16 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
|
|
|
|
|
|
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
APIApplicationKey apiApplicationKey;
|
|
|
|
APIApplicationKey apiApplicationKey;
|
|
|
|
AccessTokenInfo accessTokenInfo;
|
|
|
|
|
|
|
|
|
|
|
|
AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials();
|
|
|
|
apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials();
|
|
|
|
accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication(
|
|
|
|
// accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication(
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
// apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (APIServicesException e) {
|
|
|
|
} catch (APIServicesException e) {
|
|
|
|
String errorMsg = "Error occurred while generating the API application";
|
|
|
|
String errorMsg = "Error occurred while generating the API application";
|
|
|
|
log.error(errorMsg, e);
|
|
|
|
log.error(errorMsg, e);
|
|
|
@ -127,8 +122,65 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
|
|
|
boolean tenantsLoaded = false;
|
|
|
|
boolean tenantsLoaded = false;
|
|
|
|
TenantSearchResult tenantSearchResult = null;
|
|
|
|
TenantSearchResult tenantSearchResult = null;
|
|
|
|
for (String tenantDomain : tenants) {
|
|
|
|
for (String tenantDomain : tenants) {
|
|
|
|
|
|
|
|
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(tenantDomain.equals("carbon.super")) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication(
|
|
|
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (APIServicesException e) {
|
|
|
|
|
|
|
|
String errorMsg = "Error occurred while generating the API application";
|
|
|
|
|
|
|
|
log.error(errorMsg, e);
|
|
|
|
|
|
|
|
throw new APIManagerPublisherException(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
String SCOPE = APIPublisherUtil.ApplicationInstall.ENROLLMENT_SCOPE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String username = "admin@shamalka.com";
|
|
|
|
|
|
|
|
List<String> tags = new ArrayList<>();
|
|
|
|
|
|
|
|
tags.add(APIPublisherUtil.ApplicationInstall.ANDROID_TAG);
|
|
|
|
|
|
|
|
tags.add(APIPublisherUtil.ApplicationInstall.DEVICE_MGT_TAG);
|
|
|
|
|
|
|
|
io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo jwtAccessTokenInfo = new io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
jwtAccessTokenInfo = APIPublisherDataHolder.getInstance().getApiManagementProviderService().getAccessToken(
|
|
|
|
|
|
|
|
SCOPE, tags.toArray(new String[0]),
|
|
|
|
|
|
|
|
APIPublisherUtil.ApplicationInstall.ANDROID_QR_ENROLLMENT_APP_NAME,
|
|
|
|
|
|
|
|
APIPublisherUtil.ApplicationInstall.DEFAULT_TOKEN_TYPE,
|
|
|
|
|
|
|
|
APIPublisherUtil.ApplicationInstall.DEFAULT_VALIDITY_PERIOD,
|
|
|
|
|
|
|
|
username
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred while getting access token for the device enrollment.";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new APIManagerPublisherException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (jwtAccessTokenInfo == null || StringUtils.isBlank(jwtAccessTokenInfo.getAccessToken())) {
|
|
|
|
|
|
|
|
String msg = "Couldn't get a access token for user " + PrivilegedCarbonContext.getThreadLocalCarbonContext()
|
|
|
|
|
|
|
|
.getUsername();
|
|
|
|
|
|
|
|
log.error(msg);
|
|
|
|
|
|
|
|
throw new APIManagerPublisherException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String[] dividedAccessToken = jwtAccessTokenInfo.getAccessToken().split("_");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
accessTokenInfo.setAccess_token(dividedAccessToken[1]);
|
|
|
|
|
|
|
|
accessTokenInfo.setRefresh_token(jwtAccessTokenInfo.getRefreshToken());
|
|
|
|
|
|
|
|
accessTokenInfo.setScope(jwtAccessTokenInfo.getScopes());
|
|
|
|
|
|
|
|
accessTokenInfo.setExpires_in(jwtAccessTokenInfo.getExpiresIn());
|
|
|
|
|
|
|
|
accessTokenInfo.setToken_type(jwtAccessTokenInfo.getTokenType());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!tenantsLoaded) {
|
|
|
|
if (!tenantsLoaded) {
|
|
|
|
tenantSearchResult = realmService.getTenantManager()
|
|
|
|
tenantSearchResult = realmService.getTenantManager()
|
|
|
|
.listTenants(Integer.MAX_VALUE, 0, "asc", "UM_ID", null);
|
|
|
|
.listTenants(Integer.MAX_VALUE, 0, "asc", "UM_ID", null);
|
|
|
|