Improve API publishing functionality

apim420
Dharmakeerthi Lasantha 1 year ago
parent 840ba3755a
commit 730f3aaa07

@ -59,15 +59,13 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
} }
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
.getRealmConfiguration().getAdminUserName(); .getRealmConfiguration().getAdminUserName();
//todo
PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
.getRealmConfiguration().getAdminPassword();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username); PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
applicationName, APIUtil.getDefaultTags(), applicationName, APIUtil.getDefaultTags(),
ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, false, ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, false,
ApiApplicationConstants.DEFAULT_VALIDITY_PERIOD); ApiApplicationConstants.DEFAULT_VALIDITY_PERIOD, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
.getRealmConfiguration().getAdminPassword());
return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build(); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build();
} catch (APIManagerException e) { } catch (APIManagerException e) {
String msg = "Error occurred while registering an application '" + applicationName + "'"; String msg = "Error occurred while registering an application '" + applicationName + "'";
@ -110,12 +108,10 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
if (username.equals(registrationProfile.getUsername())) { if (username.equals(registrationProfile.getUsername())) {
synchronized (ApiApplicationRegistrationServiceImpl.class) { synchronized (ApiApplicationRegistrationServiceImpl.class) {
//todo
registrationProfile.getPassword();
ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
applicationName, registrationProfile.getTags(), applicationName, registrationProfile.getTags(),
ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username,
registrationProfile.isAllowedToAllDomains(), validityPeriod); registrationProfile.isAllowedToAllDomains(), validityPeriod, registrationProfile.getPassword());
return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build(); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build();
} }
} }
@ -123,13 +119,11 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(PrivilegedCarbonContext. PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(PrivilegedCarbonContext.
getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration().getAdminUserName()); getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration().getAdminUserName());
//todo
registrationProfile.getPassword();
synchronized (ApiApplicationRegistrationServiceImpl.class) { synchronized (ApiApplicationRegistrationServiceImpl.class) {
ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
applicationName, registrationProfile.getTags(), applicationName, registrationProfile.getTags(),
ApiApplicationConstants.DEFAULT_TOKEN_TYPE, registrationProfile.getUsername(), ApiApplicationConstants.DEFAULT_TOKEN_TYPE, registrationProfile.getUsername(),
registrationProfile.isAllowedToAllDomains(), validityPeriod); registrationProfile.isAllowedToAllDomains(), validityPeriod, registrationProfile.getPassword());
return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build(); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build();
} }
} catch (APIManagerException e) { } catch (APIManagerException e) {

@ -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)

@ -153,17 +153,8 @@ public class APIPublisherServiceImpl implements APIPublisherService {
} }
if (!apiFound) { if (!apiFound) {
// add new scopes as shared scopes // add new scopes as shared scopes
for (ApiScope apiScope : apiConfig.getScopes()) { addNewSharedScope(apiConfig.getScopes(), publisherRESTAPIServices, apiApplicationKey,
if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, accessTokenInfo);
apiScope.getKey())) {
Scope scope = new Scope();
scope.setName(apiScope.getName());
scope.setDescription(apiScope.getDescription());
scope.setKey(apiScope.getKey());
scope.setRoles(apiScope.getRoles());
publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope);
}
}
APIInfo api = getAPI(apiConfig, true); APIInfo api = getAPI(apiConfig, true);
JSONObject createdAPI = publisherRESTAPIServices.addAPI(apiApplicationKey, accessTokenInfo, api); JSONObject createdAPI = publisherRESTAPIServices.addAPI(apiApplicationKey, accessTokenInfo, api);
if (apiConfig.getEndpointType() != null && "WS".equals(apiConfig.getEndpointType())) { if (apiConfig.getEndpointType() != null && "WS".equals(apiConfig.getEndpointType())) {
@ -202,7 +193,6 @@ public class APIPublisherServiceImpl implements APIPublisherService {
} }
} else { } else {
if (WebappPublisherConfig.getInstance().isEnabledUpdateApi()) { if (WebappPublisherConfig.getInstance().isEnabledUpdateApi()) {
// With 4.x to 5.x upgrade // With 4.x to 5.x upgrade
// - there cannot be same local scope assigned in 2 different APIs // - there cannot be same local scope assigned in 2 different APIs
// - local scopes will be deprecated in the future, so need to move all scopes as shared scopes // - local scopes will be deprecated in the future, so need to move all scopes as shared scopes
@ -217,50 +207,17 @@ public class APIPublisherServiceImpl implements APIPublisherService {
// 1. add new scopes as shared scopes // 1. add new scopes as shared scopes
// 2. update the API adding scopes for the URI Templates // 2. update the API adding scopes for the URI Templates
Set<ApiScope> scopesToMoveAsSharedScopes = new HashSet<>(); // It is guaranteed that there is no local scope if we update from 5.0.0 to the most
for (ApiScope apiScope : apiConfig.getScopes()) { // recent version. Therefore, if the scope is not already available as a shared scope,
// if the scope is not available as shared scope, and it is assigned to an API as a local scope // new scopes must be added as shared scopes. Additionally, it is necessary to
// need remove the local scope and add as a shared scope // upgrade to 5.0.0 first before updating from 5.0.0 to the most recent version if we
if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, // are updating from a version that is older than 5.0.0.
apiScope.getKey())) {
//resolve- todo:apim- resolve
// if (apiProvider.isScopeKeyAssignedLocally(apiIdentifier, apiScope.getKey(), tenantId)) {
if (true) {
// collect scope to move as shared scopes
scopesToMoveAsSharedScopes.add(apiScope);
} else {
// if new scope add as shared scope
Scope scope = new Scope();
scope.setName(apiScope.getName());
scope.setDescription(apiScope.getDescription());
scope.setKey(apiScope.getKey());
scope.setRoles(apiScope.getRoles());
publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope);
} addNewSharedScope(apiConfig.getScopes(), publisherRESTAPIServices, apiApplicationKey,
} accessTokenInfo);
}
// Get existing API JSONObject existingAPI = publisherRESTAPIServices.getApi(apiApplicationKey,
JSONObject existingAPI = publisherRESTAPIServices.getApi(apiApplicationKey, accessTokenInfo, accessTokenInfo, apiIdentifier);
apiIdentifier);
if (scopesToMoveAsSharedScopes.size() > 0) {
// update API to remove local scopes
APIInfo api = getAPI(apiConfig, false);
api.setLifeCycleStatus(existingAPI.getString("lifeCycleStatus"));
publisherRESTAPIServices.updateApi(apiApplicationKey, accessTokenInfo, api);
for (ApiScope apiScope : scopesToMoveAsSharedScopes) {
Scope scope = new Scope();
scope.setName(apiScope.getName());
scope.setDescription(apiScope.getDescription());
scope.setKey(apiScope.getKey());
scope.setRoles(apiScope.getRoles());
publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope);
}
}
existingAPI = publisherRESTAPIServices.getApi(apiApplicationKey, accessTokenInfo, apiIdentifier);
APIInfo api = getAPI(apiConfig, true); APIInfo api = getAPI(apiConfig, true);
api.setLastUpdatedTime(existingAPI.getString("lifeCycleStatus")); api.setLastUpdatedTime(existingAPI.getString("lifeCycleStatus"));
api.setId(existingAPI.getString("id")); api.setId(existingAPI.getString("id"));
@ -408,6 +365,32 @@ public class APIPublisherServiceImpl implements APIPublisherService {
} }
} }
/**
* Add new Shared Scopes
*
* @param apiScopes set of API scopes
* @param publisherRESTAPIServices {@link PublisherRESTAPIServices}
* @param apiApplicationKey API application Key
* @param accessTokenInfo Details of access token
* @throws BadRequestException if invalid payload receives to add new shared scopes.
* @throws UnexpectedResponseException if the response is not either 200 or 400.
* @throws APIServicesException if error occurred while processing the response.
*/
private void addNewSharedScope(Set<ApiScope> apiScopes, PublisherRESTAPIServices publisherRESTAPIServices,
APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo) throws BadRequestException, UnexpectedResponseException, APIServicesException {
for (ApiScope apiScope : apiScopes) {
if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo,
apiScope.getKey())) {
Scope scope = new Scope();
scope.setName(apiScope.getName());
scope.setDescription(apiScope.getDescription());
scope.setKey(apiScope.getKey());
scope.setRoles(apiScope.getRoles());
publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope);
}
}
}
@Override @Override
public void updateScopeRoleMapping() public void updateScopeRoleMapping()
throws APIManagerPublisherException { throws APIManagerPublisherException {

@ -55,16 +55,18 @@ public class OAuthUtils {
try { try {
PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(PrivilegedCarbonContext. String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration().getAdminUserName()); .getRealmConfiguration().getAdminUserName();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
APIManagementProviderService apiManagementProviderService = (APIManagementProviderService) ctx. APIManagementProviderService apiManagementProviderService = (APIManagementProviderService) ctx.
getOSGiService(APIManagementProviderService.class, null); getOSGiService(APIManagementProviderService.class, null);
apiApplicationKeyInfo = apiManagementProviderService. apiApplicationKeyInfo = apiManagementProviderService.
generateAndRetrieveApplicationKeys(registrationProfile.getApplicationName(), generateAndRetrieveApplicationKeys(registrationProfile.getApplicationName(),
registrationProfile.getTags(), Constants.ApplicationInstall.DEFAULT_TOKEN_TYPE, registrationProfile.getTags(), Constants.ApplicationInstall.DEFAULT_TOKEN_TYPE,
null, registrationProfile.isAllowedToAllDomains(), username, registrationProfile.isAllowedToAllDomains(),
Constants.ApplicationInstall.DEFAULT_VALIDITY_PERIOD); Constants.ApplicationInstall.DEFAULT_VALIDITY_PERIOD, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
.getRealmConfiguration().getAdminPassword());
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }

Loading…
Cancel
Save