use of org.wso2.carbon.user.api.UserStoreManager in project carbon-apimgt by wso2.
the class APIUtilTest method testIsRoleNameNotExist.
@Test
public void testIsRoleNameNotExist() throws Exception {
String userName = "John";
String roleName = "developer";
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
Mockito.when(realmService.getTenantUserRealm(Mockito.anyInt())).thenReturn(userRealm);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
Mockito.when(userStoreManager.isExistingRole(roleName)).thenReturn(false);
Assert.assertFalse(APIUtil.isRoleNameExist(userName, roleName));
}
use of org.wso2.carbon.user.api.UserStoreManager in project carbon-apimgt by wso2.
the class BasicAuthenticationInterceptor method authenticate.
/**
* This method authenticates the request using Basic authentication and validate the roles of user based on
* roles of scope.
*
* @param inMessage cxf Message
* @param username username in basic auth header
* @param password password in basic auth header
* @return true if user is successfully authenticated and authorized. false otherwise.
*/
private boolean authenticate(Message inMessage, String username, String password) {
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
UserRealm userRealm;
String tenantDomain = MultitenantUtils.getTenantDomain(username);
int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId);
userRealm = RealmUtil.getTenantUserRealm(tenantId);
if (userRealm == null) {
log.error("Authentication failed: invalid domain or unactivated tenant login");
return false;
}
UserStoreManager userStoreManager = userRealm.getUserStoreManager();
boolean isAuthenticated = userStoreManager.authenticate(MultitenantUtils.getTenantAwareUsername(username), password);
if (isAuthenticated) {
String domain = UserCoreUtil.getDomainFromThreadLocal();
String domainAwareUserName = UserCoreUtil.addDomainToName(username, domain);
RestApiCommonUtil.setThreadLocalRequestedTenant(MultitenantUtils.getTenantAwareUsername(username));
carbonContext.setTenantDomain(tenantDomain);
carbonContext.setTenantId(tenantId);
carbonContext.setUsername(domainAwareUserName);
inMessage.put(RestApiConstants.SUB_ORGANIZATION, tenantDomain);
if (!tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
APIUtil.loadTenantConfigBlockingMode(tenantDomain);
}
return validateRoles(inMessage, userRealm, tenantDomain, username);
}
} catch (UserStoreException e) {
log.error("Error occurred while authenticating user: " + username, e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
return false;
}
use of org.wso2.carbon.user.api.UserStoreManager in project carbon-apimgt by wso2.
the class APIUtilTest method testIsRoleNameExist.
@Test
public void testIsRoleNameExist() throws Exception {
String userName = "John";
String roleName = "developer";
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
Mockito.when(realmService.getTenantUserRealm(Mockito.anyInt())).thenReturn(userRealm);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
Mockito.when(userStoreManager.isExistingRole(roleName)).thenReturn(true);
Mockito.when(userStoreManager.isExistingRole("NonExistingDomain/role")).thenThrow(UserStoreException.class);
Mockito.when(userStoreManager.isExistingRole("NonExistingDomain/")).thenThrow(UserStoreException.class);
Assert.assertTrue(APIUtil.isRoleNameExist(userName, roleName));
Assert.assertFalse(APIUtil.isRoleNameExist(userName, "NonExistingDomain/role"));
Assert.assertFalse(APIUtil.isRoleNameExist(userName, "NonExistingDomain/"));
// allow adding empty role
Assert.assertTrue(APIUtil.isRoleNameExist(userName, ""));
}
use of org.wso2.carbon.user.api.UserStoreManager in project carbon-apimgt by wso2.
the class UserSignUpWSWorkflowExecutorTest method testCompletingUserSignUpWorkflowApprovedByAdmin.
@Test
public void testCompletingUserSignUpWorkflowApprovedByAdmin() throws Exception {
Map<String, Boolean> roleMap = new HashMap<String, Boolean>();
roleMap.put(signUpRole, false);
UserRegistrationConfigDTO userRegistrationConfigDTO = new UserRegistrationConfigDTO();
userRegistrationConfigDTO.setAdminUserName("admin");
userRegistrationConfigDTO.setAdminPassword("admin");
userRegistrationConfigDTO.setRoles(roleMap);
PowerMockito.when(SelfSignUpUtil.getSignupConfiguration(tenantDomain)).thenReturn(userRegistrationConfigDTO);
PowerMockito.when(SelfSignUpUtil.getRoleNames(userRegistrationConfigDTO)).thenCallRealMethod();
PowerMockito.doNothing().when(apiMgtDAO).updateWorkflowStatus(workflowDTO);
Mockito.when(userStoreManager.isExistingUser(testUsername)).thenReturn(true);
Mockito.when(userStoreManager.isExistingRole("Internal/" + signUpRole)).thenReturn(true);
PowerMockito.doNothing().when(userStoreManager).updateRoleListOfUser(testUsername, null, new String[] { "Internal/" + signUpRole });
// Set workflow status to be approved
workflowDTO.setStatus(WorkflowStatus.APPROVED);
try {
Assert.assertNotNull(userSignUpWSWorkflowExecutor.complete(workflowDTO));
} catch (WorkflowException e) {
Assert.fail("Unexpected WorkflowException occurred while completing 'APPROVED' user sign up workflow");
}
}
use of org.wso2.carbon.user.api.UserStoreManager in project carbon-apimgt by wso2.
the class StandaloneAuthorizationManagerClientTestCase method setup.
@Before
public void setup() throws Exception {
ServiceReferenceHolderMockCreator serviceReferenceHolderMockCreator = new ServiceReferenceHolderMockCreator(4444);
serviceReferenceHolder = serviceReferenceHolderMockCreator.getMock();
Mockito.when(serviceReferenceHolder.getUserRealm()).thenReturn(userRealm);
Mockito.when(userRealm.getAuthorizationManager()).thenReturn(authorizationManager);
Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
}
Aggregations