Search in sources :

Example 1 with TenantManager

use of org.wso2.carbon.user.core.tenant.TenantManager in project carbon-business-process by wso2.

the class BPSUserIdentityManager method checkPassword.

@Override
public Boolean checkPassword(String userId, String password) {
    String tenantDomain = MultitenantUtils.getTenantDomain(userId);
    String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(userId);
    String userNameWithTenantDomain = tenantAwareUserName + "@" + tenantDomain;
    RealmService realmService = RegistryContext.getBaseInstance().getRealmService();
    TenantManager mgr = realmService.getTenantManager();
    int tenantId = 0;
    try {
        tenantId = mgr.getTenantId(tenantDomain);
    } catch (UserStoreException e) {
        throw new BPMNAuthenticationException("Identity exception thrown while getting tenant ID for user : " + userNameWithTenantDomain, e);
    }
    // tenantId == -1, means an invalid tenant.
    if (tenantId == -1) {
        if (log.isDebugEnabled()) {
            log.debug("Basic authentication request with an invalid tenant : " + userNameWithTenantDomain);
        }
        return false;
    }
    org.wso2.carbon.user.api.UserStoreManager userStoreManager = null;
    boolean authStatus = false;
    try {
        userStoreManager = realmService.getTenantUserRealm(tenantId).getUserStoreManager();
        authStatus = userStoreManager.authenticate(tenantAwareUserName, password);
    } catch (UserStoreException e) {
        throw new BPMNAuthenticationException("User store exception thrown while authenticating user : " + userNameWithTenantDomain, e);
    }
    /* IdentityService identityService = BPMNOSGIService.getIdentityService();
        authStatus = identityService.checkPassword(userName, password);*/
    if (log.isDebugEnabled()) {
        log.debug("Basic authentication request completed. " + "Username : " + userNameWithTenantDomain + ", Authentication State : " + authStatus);
    }
    return authStatus;
}
Also used : RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) BPMNAuthenticationException(org.wso2.carbon.bpmn.core.exception.BPMNAuthenticationException) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager)

Example 2 with TenantManager

use of org.wso2.carbon.user.core.tenant.TenantManager in project carbon-business-process by wso2.

the class BPMNServiceComponent method activate.

protected void activate(ComponentContext ctxt) {
    log.info("Initializing the BPMN core component...");
    try {
        BundleContext bundleContext = ctxt.getBundleContext();
        BPMNServerHolder holder = BPMNServerHolder.getInstance();
        ActivitiEngineBuilder activitiEngineBuilder = new ActivitiEngineBuilder();
        holder.setEngine(activitiEngineBuilder.buildEngine());
        holder.setTenantManager(new TenantManager());
        /*BPMNRestExtensionHolder restHolder = BPMNRestExtensionHolder.getInstance();

            restHolder.setRestInvoker(new RESTInvoker());*/
        BPMNEngineServiceImpl bpmnEngineService = new BPMNEngineServiceImpl();
        bpmnEngineService.setProcessEngine(ActivitiEngineBuilder.getProcessEngine());
        bundleContext.registerService(BPMNEngineService.class, bpmnEngineService, null);
        bundleContext.registerService(ServerStartupObserver.class.getName(), new BPMNEngineServerStartupObserver(), null);
        bundleContext.registerService(WaitBeforeShutdownObserver.class, new BPMNEngineWaitBeforeShutdownObserver(), null);
    // DataSourceHandler dataSourceHandler = new DataSourceHandler();
    // dataSourceHandler.initDataSource(activitiEngineBuilder.getDataSourceJndiName());
    // dataSourceHandler.closeDataSource();
    // } catch (BPMNMetaDataTableCreationException e) {
    // log.error("Could not create BPMN checksum table", e);
    // } catch (DatabaseConfigurationException e) {
    // log.error("Could not create BPMN checksum table", e);
    } catch (Throwable e) {
        log.error("Failed to initialize the BPMN core component.", e);
    }
}
Also used : BPMNEngineServerStartupObserver(org.wso2.carbon.bpmn.core.integration.BPMNEngineServerStartupObserver) ServerStartupObserver(org.wso2.carbon.core.ServerStartupObserver) ActivitiEngineBuilder(org.wso2.carbon.bpmn.core.ActivitiEngineBuilder) BPMNServerHolder(org.wso2.carbon.bpmn.core.BPMNServerHolder) BPMNEngineWaitBeforeShutdownObserver(org.wso2.carbon.bpmn.core.integration.BPMNEngineWaitBeforeShutdownObserver) TenantManager(org.wso2.carbon.bpmn.core.deployment.TenantManager) BPMNEngineServerStartupObserver(org.wso2.carbon.bpmn.core.integration.BPMNEngineServerStartupObserver) BundleContext(org.osgi.framework.BundleContext)

Example 3 with TenantManager

use of org.wso2.carbon.user.core.tenant.TenantManager in project carbon-business-process by wso2.

the class AuthenticationHandler method authenticate.

/**
 * Checks whether a given userName:password combination authenticates correctly against carbon userStore
 * Upon successful authentication returns true, false otherwise
 *
 * @param userName
 * @param password
 * @return
 * @throws RestApiBasicAuthenticationException wraps and throws exceptions occur when trying to authenticate
 *                                             the user
 */
private boolean authenticate(String userName, String password) throws RestApiBasicAuthenticationException {
    boolean authStatus;
    try {
        IdentityService identityService = BPMNOSGIService.getIdentityService();
        authStatus = identityService.checkPassword(userName, password);
        if (!authStatus) {
            return false;
        }
    } catch (BPMNAuthenticationException e) {
        throw new RestApiBasicAuthenticationException(e.getMessage(), e);
    }
    String tenantDomain = MultitenantUtils.getTenantDomain(userName);
    String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(userName);
    String userNameWithTenantDomain = tenantAwareUserName + "@" + tenantDomain;
    RealmService realmService = RegistryContext.getBaseInstance().getRealmService();
    TenantManager mgr = realmService.getTenantManager();
    int tenantId = 0;
    try {
        tenantId = mgr.getTenantId(tenantDomain);
        // tenantId == -1, means an invalid tenant.
        if (tenantId == -1) {
            if (log.isDebugEnabled()) {
                log.debug("Basic authentication request with an invalid tenant : " + userNameWithTenantDomain);
            }
            return false;
        }
    } catch (UserStoreException e) {
        throw new RestApiBasicAuthenticationException("Identity exception thrown while getting tenant ID for user : " + userNameWithTenantDomain, e);
    }
    /* Upon successful authentication existing thread local carbon context
             * is updated to mimic the authenticated user */
    PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    carbonContext.setUsername(tenantAwareUserName);
    carbonContext.setTenantId(tenantId);
    carbonContext.setTenantDomain(tenantDomain);
    return true;
}
Also used : IdentityService(org.activiti.engine.IdentityService) RestApiBasicAuthenticationException(org.wso2.carbon.bpmn.rest.common.exception.RestApiBasicAuthenticationException) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) BPMNAuthenticationException(org.wso2.carbon.bpmn.core.exception.BPMNAuthenticationException) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager)

Example 4 with TenantManager

use of org.wso2.carbon.user.core.tenant.TenantManager in project carbon-apimgt by wso2.

the class DefaultGroupIDExtractorImplTest method getGroupingIdentifierListTestCase.

@Test
public void getGroupingIdentifierListTestCase() throws UserStoreException {
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    RealmService realmService = Mockito.mock(RealmService.class);
    UserRealm userRealm = Mockito.mock(UserRealm.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    Mockito.when(tenantManager.getTenantId("carbon.super")).thenReturn(-1234);
    Mockito.when(realmService.getTenantUserRealm(-1234)).thenReturn(userRealm);
    Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
    Mockito.when(userStoreManager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername("user"), "http://wso2.org/claims/organization", null)).thenReturn("org1,org2,org3");
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_STORE_GROUP_EXTRACTOR_CLAIM_URI)).thenReturn("http://wso2.org/claims/organization");
    DefaultGroupIDExtractorImpl defaultGroupIDExtractor = new DefaultGroupIDExtractorImpl();
    String[] expectedArr = new String[] { "org1", "org2", "org3" };
    Assert.assertEquals(expectedArr[0], defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":true}")[0]);
    Assert.assertEquals(expectedArr[1], defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":true}")[1]);
    Assert.assertEquals(expectedArr[2], defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":true}")[2]);
    Assert.assertEquals(expectedArr[0], defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":false}")[0]);
    Mockito.when(userStoreManager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername("user"), "http://wso2.org/claims/organization", null)).thenReturn("org1|org2|org3");
    Assert.assertEquals("org1|org2|org3", defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", \"isSuperTenant\":false}")[0]);
    Mockito.when(userStoreManager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername("user"), "http://wso2.org/claims/organization", null)).thenReturn(null);
    Assert.assertEquals(0, defaultGroupIDExtractor.getGroupingIdentifierList("{\"user\":\"user\", " + "\"isSuperTenant\":false}").length);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) UserRealm(org.wso2.carbon.user.core.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with TenantManager

use of org.wso2.carbon.user.core.tenant.TenantManager in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetSwaggerDefinitionTimeStamps.

@Test
public void testGetSwaggerDefinitionTimeStamps() throws Exception {
    APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(-1234);
    PowerMockito.mockStatic(OASParserUtil.class);
    Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenThrow(RegistryException.class).thenReturn(registry);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(null, registryService, registry, tenantManager);
    Assert.assertNull(abstractAPIManager.getSwaggerDefinitionTimeStamps(identifier));
    Assert.assertNull(abstractAPIManager.getSwaggerDefinitionTimeStamps(identifier));
    abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
    Map<String, String> result = new HashMap<String, String>();
    result.put("swagger1", "scopes:apim_create,resources:{get:/*}");
    result.put("swagger2", "scopes:apim_view,resources:{get:/menu}");
// Mockito.when(apiDefinitionFromOpenAPISpec.getAPIOpenAPIDefinitionTimeStamps((APIIdentifier) Mockito.any(),
// (org.wso2.carbon.registry.api.Registry) Mockito.any())).thenReturn(result);
// Assert.assertEquals(abstractAPIManager.getSwaggerDefinitionTimeStamps(identifier).size(),2);
// abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN;
// result.put("swagger3","");
// Assert.assertEquals(abstractAPIManager.getSwaggerDefinitionTimeStamps(identifier).size(),3);
}
Also used : HashMap(java.util.HashMap) UserStoreException(org.wso2.carbon.user.core.UserStoreException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

RealmService (org.wso2.carbon.user.core.service.RealmService)50 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)49 Test (org.junit.Test)46 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)46 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)42 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)32 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)26 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)23 Resource (org.wso2.carbon.registry.core.Resource)21 API (org.wso2.carbon.apimgt.api.model.API)20 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)17 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)15 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)15 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)15 QName (javax.xml.namespace.QName)14 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)14 Registry (org.wso2.carbon.registry.core.Registry)13 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)12 Before (org.junit.Before)10 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10