Search in sources :

Example 46 with Subscriber

use of org.wso2.carbon.apimgt.api.model.Subscriber in project carbon-apimgt by wso2.

the class NewAPIVersionEmailNotifierTest method setup.

@Before
public void setup() {
    notifier = new NewAPIVersionEmailNotifier();
    registry = Mockito.mock(Registry.class);
    claimsRetriever = Mockito.mock(ClaimsRetriever.class);
    Subscriber subscriber = new Subscriber(ADMIN);
    Set<Subscriber> subscribersOfAPI = new HashSet<Subscriber>();
    subscribersOfAPI.add(subscriber);
    Properties properties = new Properties();
    properties.put(NotifierConstants.API_KEY, new APIIdentifier(ADMIN, API_NAME, "1.0.0"));
    properties.put(NotifierConstants.NEW_API_KEY, new APIIdentifier(ADMIN, API_NAME, "2.0.0"));
    properties.put(NotifierConstants.TITLE_KEY, "New Version");
    properties.put(NotifierConstants.SUBSCRIBERS_PER_API, subscribersOfAPI);
    properties.put(NotifierConstants.CLAIMS_RETRIEVER_IMPL_CLASS, "org.wso2.carbon.apimgt.impl.token.DefaultClaimsRetriever");
    properties.put(NotifierConstants.TEMPLATE_KEY, "<html>$1</html>");
    notificationDTO = new NotificationDTO(properties, NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
    notificationDTO.setTenantID(TENANT_ID);
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Registry(org.wso2.carbon.registry.core.Registry) ClaimsRetriever(org.wso2.carbon.apimgt.impl.token.ClaimsRetriever) Before(org.junit.Before)

Example 47 with Subscriber

use of org.wso2.carbon.apimgt.api.model.Subscriber in project carbon-apimgt by wso2.

the class APIUtilRolesTest method testCreateDefaultRoles.

@Test
public void testCreateDefaultRoles() throws Exception {
    System.setProperty("carbon.home", APIUtilRolesTest.class.getResource("/").getFile());
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        final int tenantId = MultitenantConstants.SUPER_TENANT_ID;
        final String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
        File siteConfFile = new File(Thread.currentThread().getContextClassLoader().getResource("tenant-conf.json").getFile());
        String tenantConfValue = FileUtils.readFileToString(siteConfFile);
        InputStream signUpConfStream = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("default-sign-up-config.xml").getFile());
        ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
        RealmService realmService = Mockito.mock(RealmService.class);
        RegistryService registryService = Mockito.mock(RegistryService.class);
        TenantManager tenantManager = Mockito.mock(TenantManager.class);
        TenantIndexingLoader indexingLoader = Mockito.mock(TenantIndexingLoader.class);
        UserRealm userRealm = Mockito.mock(UserRealm.class);
        UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
        RealmConfiguration realmConfiguration = Mockito.mock(RealmConfiguration.class);
        APIMConfigService apimConfigService = Mockito.mock(APIMConfigService.class);
        PowerMockito.mockStatic(PrivilegedCarbonContext.class);
        PowerMockito.mockStatic(ServiceReferenceHolder.class);
        PowerMockito.mockStatic(APIManagerComponent.class);
        Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
        Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
        Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
        Mockito.when(serviceReferenceHolder.getIndexLoaderService()).thenReturn(indexingLoader);
        Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
        Mockito.when(realmService.getBootstrapRealm()).thenReturn(userRealm);
        Mockito.when(realmService.getTenantUserRealm(tenantId)).thenReturn(userRealm);
        Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
        Mockito.when(userRealm.getRealmConfiguration()).thenReturn(realmConfiguration);
        Mockito.when(realmConfiguration.getAdminUserName()).thenReturn("admin");
        Mockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantId);
        Mockito.when(tenantManager.getDomain(tenantId)).thenReturn(tenantDomain);
        Mockito.when(serviceReferenceHolder.getApimConfigService()).thenReturn(apimConfigService);
        Mockito.when(apimConfigService.getTenantConfig(tenantDomain)).thenReturn(tenantConfValue);
        Mockito.when(apimConfigService.getSelfSighupConfig(tenantDomain)).thenReturn(IOUtils.toString(signUpConfStream));
        APIUtil.createDefaultRoles(tenantId);
        String[] adminName = { "admin" };
        Mockito.verify(userStoreManager, Mockito.atLeastOnce()).addRole(eq("Internal/publisher"), eq(adminName), new Permission[] { Mockito.any(Permission.class) });
        Mockito.verify(userStoreManager, Mockito.atLeastOnce()).addRole(eq("Internal/subscriber"), eq(adminName), new Permission[] { Mockito.any(Permission.class) });
        Mockito.verify(userStoreManager, Mockito.atLeastOnce()).addRole(eq("Internal/creator"), eq(adminName), new Permission[] { Mockito.any(Permission.class) });
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) FileInputStream(java.io.FileInputStream) RealmConfiguration(org.wso2.carbon.user.core.config.RealmConfiguration) TenantIndexingLoader(org.wso2.carbon.registry.indexing.service.TenantIndexingLoader) UserRealm(org.wso2.carbon.user.core.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) Permission(org.wso2.carbon.user.api.Permission) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) File(java.io.File) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) APIMConfigService(org.wso2.carbon.apimgt.impl.config.APIMConfigService) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 48 with Subscriber

use of org.wso2.carbon.apimgt.api.model.Subscriber in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method preProcessAndAddApplication.

/**
 * Preprocess and add the application
 *
 * @param username       Username
 * @param applicationDto Application DTO
 * @param organization   Identifier of an organization
 * @return Created application
 */
private Application preProcessAndAddApplication(String username, ApplicationDTO applicationDto, String organization) throws APIManagementException {
    APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
    // validate the tier specified for the application
    String tierName = applicationDto.getThrottlingPolicy();
    if (tierName == null) {
        RestApiUtil.handleBadRequest("Throttling tier cannot be null", log);
    }
    Object applicationAttributesFromUser = applicationDto.getAttributes();
    Map<String, String> applicationAttributes = new ObjectMapper().convertValue(applicationAttributesFromUser, Map.class);
    if (applicationAttributes != null) {
        applicationDto.setAttributes(applicationAttributes);
    }
    // we do not honor tokenType sent in the body and
    // all the applications created will of 'JWT' token type
    applicationDto.setTokenType(ApplicationDTO.TokenTypeEnum.JWT);
    // subscriber field of the body is not honored. It is taken from the context
    Application application = ApplicationMappingUtil.fromDTOtoApplication(applicationDto, username);
    int applicationId = apiConsumer.addApplication(application, username, organization);
    // retrieves the created application and send as the response
    return apiConsumer.getApplicationById(applicationId);
}
Also used : JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) ExportedApplication(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication) Application(org.wso2.carbon.apimgt.api.model.Application) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 49 with Subscriber

use of org.wso2.carbon.apimgt.api.model.Subscriber in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method preProcessAndUpdateApplication.

/**
 * Preprocess and update the application
 *
 * @param username       Username
 * @param applicationDto Application DTO
 * @param oldApplication Old application
 * @param applicationId  Application UUID
 * @return Updated application
 */
private Application preProcessAndUpdateApplication(String username, ApplicationDTO applicationDto, Application oldApplication, String applicationId) throws APIManagementException {
    APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
    Object applicationAttributesFromUser = applicationDto.getAttributes();
    Map<String, String> applicationAttributes = new ObjectMapper().convertValue(applicationAttributesFromUser, Map.class);
    if (applicationAttributes != null) {
        applicationDto.setAttributes(applicationAttributes);
    }
    // we do not honor the subscriber coming from the request body as we can't change the subscriber of the application
    Application application = ApplicationMappingUtil.fromDTOtoApplication(applicationDto, username);
    // we do not honor the application id which is sent via the request body
    application.setUUID(oldApplication != null ? oldApplication.getUUID() : null);
    apiConsumer.updateApplication(application);
    // retrieves the updated application and send as the response
    return apiConsumer.getApplicationByUUID(applicationId);
}
Also used : JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) ExportedApplication(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication) Application(org.wso2.carbon.apimgt.api.model.Application) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 50 with Subscriber

use of org.wso2.carbon.apimgt.api.model.Subscriber in project carbon-apimgt by wso2.

the class APIMOPARequestGenerator method generateRequest.

@Override
public String generateRequest(String policyName, String rule, Map<String, String> additionalParameters, MessageContext messageContext) throws OPASecurityException {
    JSONObject inputObject = new JSONObject();
    JSONObject opaPayload = new JSONObject();
    org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
    TreeMap<String, String> transportHeadersMap = (TreeMap<String, String>) axis2MessageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    String requestOriginIP = OPAUtils.getRequestIp(axis2MessageContext);
    String requestMethod = (String) axis2MessageContext.getProperty(OPAConstants.HTTP_METHOD_STRING);
    String requestPath = (String) axis2MessageContext.getProperty(OPAConstants.API_BASEPATH_STRING);
    String electedResource = (String) axis2MessageContext.getProperty(ELECTED_RESOURCE_STRING);
    AuthenticationContext authContext = (AuthenticationContext) messageContext.getProperty("__API_AUTH_CONTEXT");
    if (authContext != null) {
        JSONObject apiContext = new JSONObject();
        apiContext.put("apiName", authContext.getApiName());
        apiContext.put("apiVersion", authContext.getApiVersion());
        apiContext.put("subscriberOrganization", authContext.getSubscriberTenantDomain());
        apiContext.put("isAuthenticated", authContext.isAuthenticated());
        apiContext.put("issuer", authContext.getIssuer());
        apiContext.put("apiPublisher", authContext.getApiPublisher());
        apiContext.put("keyType", authContext.getKeyType());
        apiContext.put("subscriber", authContext.getSubscriber());
        apiContext.put("consumerKey", authContext.getConsumerKey());
        apiContext.put("applicationUUID", authContext.getApplicationUUID());
        apiContext.put("applicationName", authContext.getApplicationName());
        apiContext.put("username", authContext.getUsername());
        if (additionalParameters.get("sendAccessToken") != null) {
            if (JavaUtils.isTrueExplicitly(additionalParameters.get("sendAccessToken"))) {
                apiContext.put("accessToken", authContext.getAccessToken());
            }
        }
        opaPayload.put("apiContext", apiContext);
    }
    opaPayload.put(OPAConstants.REQUEST_ORIGIN_KEY, requestOriginIP);
    opaPayload.put(OPAConstants.REQUEST_METHOD_KEY, requestMethod);
    opaPayload.put(OPAConstants.REQUEST_PATH_KEY, requestPath);
    opaPayload.put(OPAConstants.REQUEST_TRANSPORT_HEADERS_KEY, new JSONObject(transportHeadersMap));
    opaPayload.put("electedResource", electedResource);
    if (additionalParameters.get(OPAConstants.ADDITIONAL_MC_PROPERTY_PARAMETER) != null) {
        String additionalMCPropertiesString = additionalParameters.get(OPAConstants.ADDITIONAL_MC_PROPERTY_PARAMETER);
        String[] additionalMCProperties = additionalMCPropertiesString.split(OPAConstants.ADDITIONAL_MC_PROPERTY_DIVIDER);
        for (String mcProperty : additionalMCProperties) {
            if (messageContext.getProperty(mcProperty) != null) {
                opaPayload.put(mcProperty, messageContext.getProperty(mcProperty));
            }
        }
    }
    inputObject.put(OPAConstants.INPUT_KEY, opaPayload);
    return inputObject.toString();
}
Also used : AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) JSONObject(org.json.JSONObject) TreeMap(java.util.TreeMap) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)98 Test (org.junit.Test)64 Application (org.wso2.carbon.apimgt.api.model.Application)63 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)60 PreparedStatement (java.sql.PreparedStatement)39 SQLException (java.sql.SQLException)39 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)39 ResultSet (java.sql.ResultSet)37 Connection (java.sql.Connection)31 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)25 Tier (org.wso2.carbon.apimgt.api.model.Tier)20 ArrayList (java.util.ArrayList)19 HashSet (java.util.HashSet)19 Date (java.util.Date)14 HashMap (java.util.HashMap)11 LinkedHashSet (java.util.LinkedHashSet)10 JSONObject (org.json.simple.JSONObject)10 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)10 TreeMap (java.util.TreeMap)9