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;
}
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);
}
}
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;
}
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);
}
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);
}
Aggregations