use of org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException in project carbon-identity-framework by wso2.
the class UserFunctionalityManagerImplTest method testIsFunctionalityLockedForUser.
@Test(dataProvider = "IsFunctionalityLockedData")
public void testIsFunctionalityLockedForUser(String functionalityIdentifier, String userId, int tenantId, boolean expected) {
DataSource dataSource = mock(DataSource.class);
TestUtils.mockDataSource(dataSource);
try {
try (Connection connection = TestUtils.getConnection()) {
Connection spyConnection = TestUtils.spyConnection(connection);
when(dataSource.getConnection()).thenReturn(spyConnection);
try {
mockIsUserStoreManager(userId);
assertEquals(userFunctionalityManager.getLockStatus(userId, tenantId, functionalityIdentifier).getLockStatus(), expected);
} catch (UserFunctionalityManagementException | UserStoreException e) {
log.error(String.format("Error while selecting functionality: %s", functionalityIdentifier), e);
}
}
} catch (SQLException e) {
log.error("SQL Exception", e);
}
}
use of org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException in project carbon-identity-framework by wso2.
the class UserFunctionalityManagerImplTest method testSetFunctionalityLockProperties.
@Test(dataProvider = "TestSetFunctionalityLockPropertiesData")
public void testSetFunctionalityLockProperties(String userId, int tenantId, String functionalityIdentifier, Map<String, String> properties, Map<String, String> propertiesToUpdate, Map<String, String> expectedProperties) {
DataSource dataSource = mock(DataSource.class);
TestUtils.mockDataSource(dataSource);
try (Connection connection = TestUtils.getConnection()) {
Connection spyConnection = TestUtils.spyConnection(connection);
when(dataSource.getConnection()).thenReturn(spyConnection);
mockIsUserStoreManager(userId);
userFunctionalityPropertyDAO.addProperties(userId, tenantId, functionalityIdentifier, properties);
userFunctionalityManager.setProperties(userId, tenantId, functionalityIdentifier, propertiesToUpdate);
Map<String, String> functionalityLockProperties = userFunctionalityPropertyDAO.getAllProperties(userId, tenantId, functionalityIdentifier);
assertEquals(functionalityLockProperties, expectedProperties);
} catch (SQLException | UserFunctionalityManagementException | UserStoreException e) {
// Mock behaviour. Hence ignored.
}
}
use of org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException in project carbon-identity-framework by wso2.
the class UserFunctionalityManagerImplTest method testGetFunctionalityLockProperties.
@Test(dataProvider = "TestGetFunctionalityLockPropertiesData")
public void testGetFunctionalityLockProperties(String userId, int tenantId, String functionalityIdentifier, Map<String, String> properties) {
DataSource dataSource = mock(DataSource.class);
TestUtils.mockDataSource(dataSource);
try {
try (Connection connection = TestUtils.getConnection()) {
Connection spyConnection = TestUtils.spyConnection(connection);
when(dataSource.getConnection()).thenReturn(spyConnection);
try {
mockIsUserStoreManager(userId);
userFunctionalityPropertyDAO.addProperties(userId, tenantId, functionalityIdentifier, properties);
assertEquals(userFunctionalityManager.getProperties(userId, tenantId, functionalityIdentifier), properties);
} catch (UserFunctionalityManagementException e) {
log.error(String.format("Error while selecting functionality: %s", functionalityIdentifier), e);
} catch (UserStoreException e) {
log.error("Error while checking userid in userstore", e);
}
}
} catch (SQLException e) {
log.error("SQL Exception", e);
}
}
use of org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException in project carbon-identity-framework by wso2.
the class UserFunctionalityManagerImpl method lock.
/**
* {@inheritDoc}
*/
@Override
public void lock(String userId, int tenantId, String functionalityIdentifier, long timeToLock, String functionalityLockReasonCode, String functionalityLockReason) throws UserFunctionalityManagementException {
if (!isPerUserFunctionalityLockingEnabled()) {
throw new UnsupportedOperationException("Per-user functionality locking is not enabled.");
}
if (StringUtils.isEmpty(userId) || !isUserIdExists(userId, tenantId)) {
if (log.isDebugEnabled()) {
log.debug("Cannot retrieve user from userId: " + userId);
}
throw buildUserNotFoundError();
}
long unlockTime = Long.MAX_VALUE;
if (timeToLock >= 0) {
unlockTime = System.currentTimeMillis() + timeToLock;
}
FunctionalityLockStatus functionalityLockStatus = userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier);
if (functionalityLockStatus != null) {
boolean isFunctionalityLockedForUser = functionalityLockStatus.getLockStatus();
long oldUnlockTime = functionalityLockStatus.getUnlockTime();
if (!isFunctionalityLockedForUser) {
FunctionalityLockStatus newFunctionalityLockStatus = new FunctionalityLockStatus(true, unlockTime, functionalityLockReasonCode, functionalityLockReason);
userFunctionalityManagerDAO.updateLockStatusForUser(userId, tenantId, functionalityIdentifier, newFunctionalityLockStatus);
} else if (oldUnlockTime < unlockTime) {
functionalityLockStatus.setLockReasonCode(functionalityLockReasonCode);
functionalityLockStatus.setLockReason(functionalityLockReason);
functionalityLockStatus.setUnlockTime(unlockTime);
userFunctionalityManagerDAO.updateLockStatusForUser(userId, tenantId, functionalityIdentifier, functionalityLockStatus);
}
} else {
FunctionalityLockStatus newFunctionalityLockStatus = new FunctionalityLockStatus(true, unlockTime, functionalityLockReasonCode, functionalityLockReason);
userFunctionalityManagerDAO.addFunctionalityLock(userId, tenantId, functionalityIdentifier, newFunctionalityLockStatus);
}
}
use of org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException in project identity-governance by wso2-extensions.
the class SecurityQuestionPasswordRecoveryManager method handleAnswerVerificationFailInFunctionalityLockMode.
private void handleAnswerVerificationFailInFunctionalityLockMode(User user) throws IdentityRecoveryException {
if (Utils.isAccountLocked(user)) {
return;
}
int tenantId = IdentityTenantUtil.getTenantId(user.getTenantDomain());
String userId = Utils.getUserId(user.getUserName(), tenantId);
Map<String, String> configStoreProperties = ConfigStoreFunctionalityLockPropertyHandler.getInstance().getConfigStoreProperties(user.getTenantDomain(), IdentityRecoveryConstants.FunctionalityTypes.FUNCTIONALITY_SECURITY_QUESTION_PW_RECOVERY.getFunctionalityIdentifier());
validateUserFunctionalityProperties(configStoreProperties);
int maxAttempts = Integer.parseInt(configStoreProperties.get(IdentityRecoveryConstants.FUNCTION_MAX_ATTEMPTS_PROPERTY));
long unlockTimePropertyValue = Integer.parseInt(configStoreProperties.get(IdentityRecoveryConstants.FUNCTION_LOCKOUT_TIME_PROPERTY));
double unlockTimeRatio = Double.parseDouble(configStoreProperties.get(IdentityRecoveryConstants.FUNCTION_LOGIN_FAIL_TIMEOUT_RATIO_PROPERTY));
int currentAttempts = 0;
int failedLoginLockoutCountValue = 0;
UserFunctionalityManager userFunctionalityManager = IdentityRecoveryServiceDataHolder.getInstance().getUserFunctionalityManagerService();
Map<String, String> functionalityLockProperties;
try {
functionalityLockProperties = userFunctionalityManager.getProperties(userId, tenantId, IdentityRecoveryConstants.FunctionalityTypes.FUNCTIONALITY_SECURITY_QUESTION_PW_RECOVERY.getFunctionalityIdentifier());
} catch (UserFunctionalityManagementException e) {
throw Utils.handleFunctionalityLockMgtServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_FAILED_TO_GET_PROPERTIES_FOR_FUNCTIONALITY, userId, tenantId, IdentityRecoveryConstants.FunctionalityTypes.FUNCTIONALITY_SECURITY_QUESTION_PW_RECOVERY.getFunctionalityIdentifier(), isDetailedErrorMessagesEnabled);
}
if (functionalityLockProperties.isEmpty()) {
functionalityLockProperties.put(IdentityRecoveryConstants.FUNCTION_LOCKOUT_COUNT_PROPERTY, String.valueOf(failedLoginLockoutCountValue));
functionalityLockProperties.put(IdentityRecoveryConstants.FUNCTION_FAILED_ATTEMPTS_PROPERTY, String.valueOf(currentAttempts));
functionalityLockProperties.put(IdentityRecoveryConstants.FUNCTION_MAX_ATTEMPTS_PROPERTY, String.valueOf(maxAttempts));
try {
userFunctionalityManager.setProperties(userId, tenantId, IdentityRecoveryConstants.FunctionalityTypes.FUNCTIONALITY_SECURITY_QUESTION_PW_RECOVERY.getFunctionalityIdentifier(), functionalityLockProperties);
} catch (UserFunctionalityManagementException e) {
throw Utils.handleFunctionalityLockMgtServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_FAILED_TO_ADD_PROPERTIES_FOR_FUNCTIONALITY, userId, tenantId, IdentityRecoveryConstants.FunctionalityTypes.FUNCTIONALITY_SECURITY_QUESTION_PW_RECOVERY.getFunctionalityIdentifier(), isDetailedErrorMessagesEnabled);
}
} else {
if (NumberUtils.isNumber(functionalityLockProperties.get(IdentityRecoveryConstants.FUNCTION_LOCKOUT_COUNT_PROPERTY))) {
failedLoginLockoutCountValue = Integer.parseInt(functionalityLockProperties.get(IdentityRecoveryConstants.FUNCTION_LOCKOUT_COUNT_PROPERTY));
}
if (NumberUtils.isNumber(functionalityLockProperties.get(IdentityRecoveryConstants.FUNCTION_FAILED_ATTEMPTS_PROPERTY))) {
currentAttempts = Integer.parseInt(functionalityLockProperties.get(IdentityRecoveryConstants.FUNCTION_FAILED_ATTEMPTS_PROPERTY));
}
}
Map<String, String> updatedFunctionalityLockProperties = new HashMap<>();
if ((currentAttempts + 1) >= maxAttempts) {
// Calculate the incremental unlock-time-interval in milli seconds.
unlockTimePropertyValue = (long) (unlockTimePropertyValue * 1000 * 60 * Math.pow(unlockTimeRatio, failedLoginLockoutCountValue));
try {
updatedFunctionalityLockProperties.put(IdentityRecoveryConstants.FUNCTION_FAILED_ATTEMPTS_PROPERTY, "0");
updatedFunctionalityLockProperties.put(IdentityRecoveryConstants.FUNCTION_LOCKOUT_COUNT_PROPERTY, String.valueOf(failedLoginLockoutCountValue + 1));
userFunctionalityManager.lock(userId, tenantId, IdentityRecoveryConstants.FunctionalityTypes.FUNCTIONALITY_SECURITY_QUESTION_PW_RECOVERY.getFunctionalityIdentifier(), unlockTimePropertyValue, IdentityRecoveryConstants.RecoveryLockReasons.PWD_RECOVERY_MAX_ATTEMPTS_EXCEEDED.getFunctionalityLockCode(), IdentityRecoveryConstants.RecoveryLockReasons.PWD_RECOVERY_MAX_ATTEMPTS_EXCEEDED.getFunctionalityLockReason());
userFunctionalityManager.setProperties(userId, tenantId, IdentityRecoveryConstants.FunctionalityTypes.FUNCTIONALITY_SECURITY_QUESTION_PW_RECOVERY.getFunctionalityIdentifier(), updatedFunctionalityLockProperties);
} catch (UserFunctionalityManagementServerException e) {
throw Utils.handleFunctionalityLockMgtServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_FAILED_TO_LOCK_FUNCTIONALITY_FOR_USER, userId, tenantId, IdentityRecoveryConstants.FunctionalityTypes.FUNCTIONALITY_SECURITY_QUESTION_PW_RECOVERY.getFunctionalityIdentifier(), isDetailedErrorMessagesEnabled);
} catch (UserFunctionalityManagementException e) {
e.printStackTrace();
}
StringBuilder message = new StringBuilder(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_SECURITY_QUESTION_BASED_PWR_LOCKED.getMessage());
if (isDetailedErrorMessagesEnabled) {
message.append(": ").append(IdentityRecoveryConstants.RecoveryLockReasons.PWD_RECOVERY_MAX_ATTEMPTS_EXCEEDED.getFunctionalityLockReason());
}
throw IdentityException.error(IdentityRecoveryClientException.class, IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_SECURITY_QUESTION_BASED_PWR_LOCKED.getCode(), message.toString());
} else {
try {
Map<String, String> propertiesToUpdate = new HashMap<>();
propertiesToUpdate.put(IdentityRecoveryConstants.FUNCTION_FAILED_ATTEMPTS_PROPERTY, String.valueOf(currentAttempts + 1));
userFunctionalityManager.setProperties(userId, tenantId, IdentityRecoveryConstants.FunctionalityTypes.FUNCTIONALITY_SECURITY_QUESTION_PW_RECOVERY.getFunctionalityIdentifier(), propertiesToUpdate);
} catch (UserFunctionalityManagementException e) {
throw Utils.handleFunctionalityLockMgtServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_FAILED_TO_UPDATE_PROPERTIES_FOR_FUNCTIONALITY, userId, tenantId, IdentityRecoveryConstants.FunctionalityTypes.FUNCTIONALITY_SECURITY_QUESTION_PW_RECOVERY.getFunctionalityIdentifier(), isDetailedErrorMessagesEnabled);
}
}
}
Aggregations