|
|
@ -45,7 +45,6 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.Unexpected
|
|
|
|
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.eclipse.jetty.http.MetaData;
|
|
|
|
|
|
|
|
import org.wso2.carbon.apimgt.api.APIAdmin;
|
|
|
|
import org.wso2.carbon.apimgt.api.APIAdmin;
|
|
|
|
import org.wso2.carbon.apimgt.api.APIConsumer;
|
|
|
|
import org.wso2.carbon.apimgt.api.APIConsumer;
|
|
|
|
import org.wso2.carbon.apimgt.api.APIManagementException;
|
|
|
|
import org.wso2.carbon.apimgt.api.APIManagementException;
|
|
|
@ -64,7 +63,14 @@ import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
|
|
|
|
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* This class represents an implementation of APIManagementProviderService.
|
|
|
|
* This class represents an implementation of APIManagementProviderService.
|
|
|
@ -197,6 +203,11 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
|
|
|
|
throw new APIManagerException(msg);
|
|
|
|
throw new APIManagerException(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String[] metaValues = metaData.getMetaValue().split(":");
|
|
|
|
String[] metaValues = metaData.getMetaValue().split(":");
|
|
|
|
|
|
|
|
if (metaValues.length != 2) {
|
|
|
|
|
|
|
|
String msg = "Found invalid Meta value for meta key: " + applicationName;
|
|
|
|
|
|
|
|
log.error(msg);
|
|
|
|
|
|
|
|
throw new APIManagerException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
String applicationId = metaValues[0];
|
|
|
|
String applicationId = metaValues[0];
|
|
|
|
String keyMappingId = metaValues[1];
|
|
|
|
String keyMappingId = metaValues[1];
|
|
|
|
//todo call the API key retrieving call, return apiApplicationKey;
|
|
|
|
//todo call the API key retrieving call, return apiApplicationKey;
|
|
|
@ -296,41 +307,29 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
APIConsumer apiConsumer = API_MANAGER_FACTORY.getAPIConsumer(username);
|
|
|
|
APIConsumer apiConsumer = API_MANAGER_FACTORY.getAPIConsumer(username);
|
|
|
|
Application application = null; // todo:apim - apiConsumer.getApplicationsByName(username, applicationName, "");
|
|
|
|
Application application = null; // todo:resolve:apim - apiConsumer.getApplicationsByName(username, applicationName, "");
|
|
|
|
// cnt rm
|
|
|
|
|
|
|
|
// // curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://localhost:9443/api/am/devportal/v3/applications?query=CalculatorApp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int applicationId = 0;
|
|
|
|
int applicationId = 0;
|
|
|
|
Subscriber subscriber = null;
|
|
|
|
Subscriber subscriber = null;
|
|
|
|
if (application == null) {
|
|
|
|
if (application == null) {
|
|
|
|
subscriber = null; // todo:apim - apiConsumer.getSubscriber(username);
|
|
|
|
subscriber = null; // todo:resolve:apim - apiConsumer.getSubscriber(username);
|
|
|
|
// cnt rm
|
|
|
|
|
|
|
|
if (subscriber == null) {
|
|
|
|
if (subscriber == null) {
|
|
|
|
// create subscriber
|
|
|
|
// create subscriber
|
|
|
|
// todo:apim - apiConsumer.addSubscriber(username, "");
|
|
|
|
// todo:resolve:apim - apiConsumer.addSubscriber(username, "");
|
|
|
|
// cnt rm
|
|
|
|
subscriber = null; // todo:resolve:apim - apiConsumer.getSubscriber(username);
|
|
|
|
subscriber = null; // todo:apim - apiConsumer.getSubscriber(username);
|
|
|
|
|
|
|
|
// cnt rm
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//create application
|
|
|
|
//create application
|
|
|
|
application = new Application(applicationName, subscriber);
|
|
|
|
application = new Application(applicationName, subscriber);
|
|
|
|
application.setTier(ApiApplicationConstants.DEFAULT_TIER);
|
|
|
|
application.setTier(ApiApplicationConstants.DEFAULT_TIER);
|
|
|
|
application.setGroupId("");
|
|
|
|
application.setGroupId("");
|
|
|
|
application.setTokenType("OAUTH");
|
|
|
|
application.setTokenType("OAUTH");
|
|
|
|
// todo:apim - apiConsumer.addApplication(application, username);
|
|
|
|
// todo:resolve:apim - apiConsumer.addApplication(application, username);
|
|
|
|
// cnt rm
|
|
|
|
application = null; // todo:resolve:apim - apiConsumer.getApplicationsByName(username, applicationName, "");
|
|
|
|
application = null; // todo:apim - apiConsumer.getApplicationsByName(username, applicationName, "");
|
|
|
|
|
|
|
|
// cnt rm
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
subscriber = null; // todo:apim - apiConsumer.getSubscriber(username);
|
|
|
|
subscriber = null; // todo:resolve:apim - apiConsumer.getSubscriber(username);
|
|
|
|
// cnt rm
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Set<SubscribedAPI> subscribedAPIs =
|
|
|
|
Set<SubscribedAPI> subscribedAPIs =
|
|
|
|
null; // todo:apim - apiConsumer.getSubscribedAPIs(subscriber, applicationName, "");
|
|
|
|
null; // todo:resolve:apim - apiConsumer.getSubscribedAPIs(subscriber, applicationName, "");
|
|
|
|
|
|
|
|
|
|
|
|
//curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://localhost:9443/api/am/devportal/v3/subscriptions?apiId=02e658e7-71c7-4b1d-a623-be145b789340"
|
|
|
|
|
|
|
|
// cnt rm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.info("Already subscribed API count: " + subscribedAPIs.size());
|
|
|
|
log.info("Already subscribed API count: " + subscribedAPIs.size());
|
|
|
|
|
|
|
|
|
|
|
@ -339,9 +338,7 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
if (tags != null && tags.length > 0) {
|
|
|
|
if (tags != null && tags.length > 0) {
|
|
|
|
for (String tag : tags) {
|
|
|
|
for (String tag : tags) {
|
|
|
|
boolean startedTenantFlow = false;
|
|
|
|
boolean startedTenantFlow = false;
|
|
|
|
Set<API> apisWithTag = null; // todo:apim - apiConsumer.getAPIsWithTag(tag, tenantDomain);
|
|
|
|
Set<API> apisWithTag = null; // todo:resolve:apim - apiConsumer.getAPIsWithTag(tag, tenantDomain);
|
|
|
|
// curl -k "https://localhost:9443/api/am/devportal/v3/apis"
|
|
|
|
|
|
|
|
// cnt rm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* From APIM 4.0.0, APIs published in the super tenant can only be listed by
|
|
|
|
* From APIM 4.0.0, APIs published in the super tenant can only be listed by
|
|
|
@ -364,8 +361,7 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
". Caused by to inability to get super tenant username", e);
|
|
|
|
". Caused by to inability to get super tenant username", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
apisWithTag = null; // todo:apim - apiConsumerAPIPublishedTenant.getAPIsWithTag(tag, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
|
|
|
|
apisWithTag = null; // todo:resolve:apim - apiConsumerAPIPublishedTenant.getAPIsWithTag(tag, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
|
|
|
|
// cnt rm
|
|
|
|
|
|
|
|
startedTenantFlow = true;
|
|
|
|
startedTenantFlow = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -378,8 +374,7 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
boolean subscriptionExist = false;
|
|
|
|
boolean subscriptionExist = false;
|
|
|
|
if (subscribedAPIs.size() > 0) {
|
|
|
|
if (subscribedAPIs.size() > 0) {
|
|
|
|
for (SubscribedAPI subscribedAPI : subscribedAPIs) {
|
|
|
|
for (SubscribedAPI subscribedAPI : subscribedAPIs) {
|
|
|
|
// todo:apim
|
|
|
|
// todo:resolve:apim
|
|
|
|
// cnt rm
|
|
|
|
|
|
|
|
// if (String.valueOf(subscribedAPI.getApiId().toString()).equals(id)) {
|
|
|
|
// if (String.valueOf(subscribedAPI.getApiId().toString()).equals(id)) {
|
|
|
|
// subscriptionExist = true;
|
|
|
|
// subscriptionExist = true;
|
|
|
|
// break;
|
|
|
|
// break;
|
|
|
@ -422,8 +417,7 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
* Oauth app for a child tenant.
|
|
|
|
* Oauth app for a child tenant.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
for (ApiTypeWrapper apiTypeWrapper : apiTypeWrapperList) {
|
|
|
|
for (ApiTypeWrapper apiTypeWrapper : apiTypeWrapperList) {
|
|
|
|
// todo:apim - apiConsumer.addSubscription(apiTypeWrapper, username, application);
|
|
|
|
// todo:resolve:apim - apiConsumer.addSubscription(apiTypeWrapper, username, application);
|
|
|
|
// cnt rm
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -454,7 +448,7 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
APIAdmin apiAdmin = new APIAdminImpl();
|
|
|
|
APIAdmin apiAdmin = new APIAdminImpl();
|
|
|
|
String keyManagerId = null;
|
|
|
|
String keyManagerId = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
List<KeyManagerConfigurationDTO> keyManagerConfigurations = null; // todo:apim -
|
|
|
|
List<KeyManagerConfigurationDTO> keyManagerConfigurations = null; // todo:resolve:apim -
|
|
|
|
// apiAdmin.getKeyManagerConfigurationsByTenant(tenantDomain);
|
|
|
|
// apiAdmin.getKeyManagerConfigurationsByTenant(tenantDomain);
|
|
|
|
if (keyManagerConfigurations != null) {
|
|
|
|
if (keyManagerConfigurations != null) {
|
|
|
|
for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurations) {
|
|
|
|
for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurations) {
|
|
|
@ -474,7 +468,7 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
"\\\"id_token_expiry_time\\\":\\\"N\\/A\\\"}\"," +
|
|
|
|
"\\\"id_token_expiry_time\\\":\\\"N\\/A\\\"}\"," +
|
|
|
|
"\"username\":\"" + username + "\"}";
|
|
|
|
"\"username\":\"" + username + "\"}";
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> keyDetails = null; // todo:apim - apiConsumer
|
|
|
|
Map<String, Object> keyDetails = null; // todo:resolve:apim - apiConsumer
|
|
|
|
// .requestApprovalForApplicationRegistration(username, applicationName, keyType, "",
|
|
|
|
// .requestApprovalForApplicationRegistration(username, applicationName, keyType, "",
|
|
|
|
// allowedDomains.toArray(new String[allowedDomains.size()]), validityTime, "default", "",
|
|
|
|
// allowedDomains.toArray(new String[allowedDomains.size()]), validityTime, "default", "",
|
|
|
|
// jsonString, keyManagerId, tenantDomain);
|
|
|
|
// jsonString, keyManagerId, tenantDomain);
|
|
|
@ -486,7 +480,7 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
return apiApplicationKey;
|
|
|
|
return apiApplicationKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new APIManagerException("Failed to generate keys for tenant: " + tenantDomain);
|
|
|
|
throw new APIManagerException("Failed to generate keys for tenant: " + tenantDomain);
|
|
|
|
// todo:apim - commected as it says never throw since we commented apim calls above
|
|
|
|
// todo:resolve:apim - commected as it says never throw since we commented apim calls above
|
|
|
|
// cnt rm
|
|
|
|
// cnt rm
|
|
|
|
// } catch (APIManagementException e) {
|
|
|
|
// } catch (APIManagementException e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
@ -525,11 +519,10 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
JWTClientManagerService jwtClientManagerService = APIApplicationManagerExtensionDataHolder.getInstance()
|
|
|
|
JWTClientManagerService jwtClientManagerService = APIApplicationManagerExtensionDataHolder.getInstance()
|
|
|
|
.getJwtClientManagerService();
|
|
|
|
.getJwtClientManagerService();
|
|
|
|
JWTClient jwtClient = jwtClientManagerService.getJWTClient();
|
|
|
|
JWTClient jwtClient = jwtClientManagerService.getJWTClient();
|
|
|
|
AccessTokenInfo accessTokenForAdmin = jwtClient
|
|
|
|
|
|
|
|
|
|
|
|
return jwtClient
|
|
|
|
.getAccessToken(clientCredentials.getConsumerKey(), clientCredentials.getConsumerSecret(), username,
|
|
|
|
.getAccessToken(clientCredentials.getConsumerKey(), clientCredentials.getConsumerSecret(), username,
|
|
|
|
scopes);
|
|
|
|
scopes);
|
|
|
|
|
|
|
|
|
|
|
|
return accessTokenForAdmin;
|
|
|
|
|
|
|
|
} catch (JWTClientException e) {
|
|
|
|
} catch (JWTClientException e) {
|
|
|
|
String msg = "JWT Error occurred while registering Application to get access token.";
|
|
|
|
String msg = "JWT Error occurred while registering Application to get access token.";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
@ -546,7 +539,8 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get Client credentials
|
|
|
|
* Get Client credentials of application belongs to tenant admin
|
|
|
|
|
|
|
|
*
|
|
|
|
* @param tenantDomain Tenant Domain
|
|
|
|
* @param tenantDomain Tenant Domain
|
|
|
|
* @param tags Tags
|
|
|
|
* @param tags Tags
|
|
|
|
* @param applicationName Application Name
|
|
|
|
* @param applicationName Application Name
|
|
|
@ -554,7 +548,7 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
* @param validityPeriod Validity Period
|
|
|
|
* @param validityPeriod Validity Period
|
|
|
|
* @return {@link ApiApplicationKey}
|
|
|
|
* @return {@link ApiApplicationKey}
|
|
|
|
* @throws APIManagerException if error occurred while generating access token
|
|
|
|
* @throws APIManagerException if error occurred while generating access token
|
|
|
|
* @throws UserStoreException if error ocurred while getting admin username.
|
|
|
|
* @throws UserStoreException if error occurred while getting admin username.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private ApiApplicationKey getClientCredentials(String tenantDomain, String[] tags, String applicationName,
|
|
|
|
private ApiApplicationKey getClientCredentials(String tenantDomain, String[] tags, String applicationName,
|
|
|
|
String tokenType, String validityPeriod) throws APIManagerException, UserStoreException {
|
|
|
|
String tokenType, String validityPeriod) throws APIManagerException, UserStoreException {
|
|
|
@ -565,7 +559,6 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
registrationProfile.setTags(tags);
|
|
|
|
registrationProfile.setTags(tags);
|
|
|
|
registrationProfile.setApplicationName(applicationName);
|
|
|
|
registrationProfile.setApplicationName(applicationName);
|
|
|
|
|
|
|
|
|
|
|
|
ApiApplicationKey info = null;
|
|
|
|
|
|
|
|
if (tenantDomain == null || tenantDomain.isEmpty()) {
|
|
|
|
if (tenantDomain == null || tenantDomain.isEmpty()) {
|
|
|
|
tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
|
|
|
|
tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -576,15 +569,14 @@ Otherwise, Generate Application Keys and return them
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration()
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration()
|
|
|
|
.getAdminUserName());
|
|
|
|
.getAdminUserName());
|
|
|
|
|
|
|
|
|
|
|
|
if (registrationProfile.getUsername() == null || registrationProfile.getUsername().isEmpty()) {
|
|
|
|
return generateAndRetrieveApplicationKeys(registrationProfile.getApplicationName(),
|
|
|
|
info = generateAndRetrieveApplicationKeys(registrationProfile.getApplicationName(),
|
|
|
|
registrationProfile.getTags(), tokenType, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
|
|
|
registrationProfile.getTags(), tokenType, null,
|
|
|
|
.getRealmConfiguration().getAdminUserName(),
|
|
|
|
registrationProfile.isAllowedToAllDomains(), validityPeriod);
|
|
|
|
registrationProfile.isAllowedToAllDomains(), validityPeriod, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
|
|
|
}
|
|
|
|
.getRealmConfiguration().getAdminPassword());
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ApiApplicationInfo getApplicationInfo(String username, String password)
|
|
|
|
private ApiApplicationInfo getApplicationInfo(String username, String password)
|
|
|
|