Skip to content

Commit

Permalink
Merge pull request #5968 from sadilchamishka/validation-code-update-m…
Browse files Browse the repository at this point in the history
…aster

Fix previous validation error persist in static variable
  • Loading branch information
sadilchamishka authored Oct 2, 2024
2 parents 050c479 + 6ce866c commit 107d65e
Show file tree
Hide file tree
Showing 6 changed files with 475 additions and 12 deletions.
10 changes: 10 additions & 0 deletions components/identity-mgt/org.wso2.carbon.identity.mgt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@
<groupId>org.wso2.orbit.javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ private VerificationBean sendNotification(String username, String key, String no
return bean;
}
} catch (IdentityException e1) {
bean = UserIdentityManagementUtil.getCustomErrorMessagesToVerifyCode(e1, username);
UserIdentityManagementUtil userIdentityManagementUtil = new UserIdentityManagementUtil();
bean = userIdentityManagementUtil.getCustomErrorMessagesForCodeVerification(e1, username);
if (bean.getError() == null) {
bean = handleError(VerificationBean.ERROR_CODE_INVALID_CODE + " Invalid confirmation code for user : "
+ username, e1);
Expand Down Expand Up @@ -244,7 +245,8 @@ private VerificationBean sendNotification(String username, String key, String no


} catch (IdentityException e) {
bean = UserIdentityManagementUtil.getCustomErrorMessagesToVerifyCode(e, username);
UserIdentityManagementUtil userIdentityManagementUtil = new UserIdentityManagementUtil();
bean = userIdentityManagementUtil.getCustomErrorMessagesForCodeVerification(e, username);
if (bean.getError() == null) {
bean = handleError(VerificationBean.ERROR_CODE_RECOVERY_NOTIFICATION_FAILURE + ": " + VerificationBean.
ERROR_CODE_UNEXPECTED + " Error when sending recovery message for " +
Expand Down Expand Up @@ -319,7 +321,8 @@ public VerificationBean verifyConfirmationCode(String username, String code,
log.error(bean.getError());
}
} catch (IdentityException e) {
bean = UserIdentityManagementUtil.getCustomErrorMessagesToVerifyCode(e, username);
UserIdentityManagementUtil userIdentityManagementUtil = new UserIdentityManagementUtil();
bean = userIdentityManagementUtil.getCustomErrorMessagesForCodeVerification(e, username);
if (bean.getError() == null) {
bean = handleError(VerificationBean.ERROR_CODE_INVALID_CODE + " Error verifying confirmation code for " +
"user : " + username, e);
Expand Down Expand Up @@ -397,7 +400,8 @@ public VerificationBean updatePassword(String username, String confirmationCode,
}

} catch (IdentityException e) {
bean = UserIdentityManagementUtil.getCustomErrorMessagesToVerifyCode(e, username);
UserIdentityManagementUtil userIdentityManagementUtil = new UserIdentityManagementUtil();
bean = userIdentityManagementUtil.getCustomErrorMessagesForCodeVerification(e, username);
if (bean.getError() == null) {
bean = handleError(VerificationBean.ERROR_CODE_UNEXPECTED + " Error while updating credential " +
"for user: " + username, e);
Expand Down Expand Up @@ -696,7 +700,8 @@ public VerificationBean verifyUserChallengeAnswer(String userName, String confir
bean.setVerified(false);
}
} catch (IdentityException e) {
bean = UserIdentityManagementUtil.getCustomErrorMessagesToVerifyCode(e, userName);
UserIdentityManagementUtil userIdentityManagementUtil = new UserIdentityManagementUtil();
bean = userIdentityManagementUtil.getCustomErrorMessagesForCodeVerification(e, userName);
if (bean == null) {
bean = handleError(VerificationBean.ERROR_CODE_INVALID_CODE + " " +
" Error verifying confirmation code for user : " + userName, e);
Expand Down Expand Up @@ -788,7 +793,8 @@ public VerificationBean verifyUserChallengeAnswers(String userName, String confi
}
} catch (IdentityException e) {
log.error("Error while verifying confirmation code.", e);
bean = UserIdentityManagementUtil.getCustomErrorMessagesToVerifyCode(e, userName);
UserIdentityManagementUtil userIdentityManagementUtil = new UserIdentityManagementUtil();
bean = userIdentityManagementUtil.getCustomErrorMessagesForCodeVerification(e, userName);
if (bean == null) {
bean = handleError(VerificationBean.ERROR_CODE_INVALID_CODE + " " +
" Error verifying confirmation code for user : " + userName, e);
Expand Down Expand Up @@ -1089,14 +1095,15 @@ public VerificationBean registerUser(String userName, String password,
vBean.setVerified(true);
}
} catch (UserStoreException | IdentityException e) {
vBean = UserIdentityManagementUtil.getCustomErrorMessagesWhenRegistering(e, userName);
UserIdentityManagementUtil userIdentityManagementUtil = new UserIdentityManagementUtil();
vBean = userIdentityManagementUtil.retrieveCustomErrorMessagesForRegistration(e, userName);
//Rollback if user exists
try {
if (!e.getMessage().contains(IdentityCoreConstants.EXISTING_USER) && userStoreManager.isExistingUser(userName)) {
userStoreManager.deleteUser(userName);
}
} catch (UserStoreException e1) {
vBean = UserIdentityManagementUtil.getCustomErrorMessagesWhenRegistering(e1, userName);
vBean = userIdentityManagementUtil.retrieveCustomErrorMessagesForRegistration(e1, userName);
}

return vBean;
Expand Down Expand Up @@ -1213,7 +1220,8 @@ public VerificationBean resendSignUpConfiramtionCode(String userName, String cod
vBean.setVerified(true);
}
} catch (IdentityException e) {
vBean = UserIdentityManagementUtil.getCustomErrorMessagesWhenRegistering(e, userName);
UserIdentityManagementUtil userIdentityManagementUtil = new UserIdentityManagementUtil();
vBean = userIdentityManagementUtil.retrieveCustomErrorMessagesForRegistration(e, userName);
return vBean;
}
} finally {
Expand Down Expand Up @@ -1321,7 +1329,8 @@ public VerificationBean confirmUserSelfRegistration(String username, String code
log.error("User verification failed against the given confirmation code");
}
} catch (IdentityException e) {
bean = UserIdentityManagementUtil.getCustomErrorMessagesToVerifyCode(e, username);
UserIdentityManagementUtil userIdentityManagementUtil = new UserIdentityManagementUtil();
bean = userIdentityManagementUtil.getCustomErrorMessagesForCodeVerification(e, username);
if (bean.getError() == null) {
bean = handleError("Error while validating confirmation code for user : " + username, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public class UserIdentityManagementUtil {
private static UserChallengesCollectionDTO userChallengesCollectionDTO = new UserChallengesCollectionDTO();
private static Log log = LogFactory.getLog(UserIdentityManagementUtil.class);

private UserIdentityManagementUtil() {
}
private VerificationBean vBeanInstance = new VerificationBean();

/**
* Returns the registration information such as the temporary password or
Expand Down Expand Up @@ -631,6 +630,10 @@ private static String[] getUserList(int tenantId, String claim, String value, St
}
}

/**
* @deprecated Use {@link #retrieveCustomErrorMessagesForRegistration} instead.
*/
@Deprecated
public static VerificationBean getCustomErrorMessagesWhenRegistering(Exception e, String userName) {
if (e.getMessage() != null) {
if (e.getMessage().contains(PASSWORD_INVALID)) {
Expand Down Expand Up @@ -688,6 +691,68 @@ public static VerificationBean getCustomErrorMessagesWhenRegistering(Exception e
}
}

public VerificationBean retrieveCustomErrorMessagesForRegistration(Exception e, String userName) {

if (e.getMessage() != null) {
if (e.getMessage().contains(PASSWORD_INVALID)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_INVALID_CREDENTIALS +
" Credential not valid. Credential must be a non null for the user : " + userName, e);
} else if (e.getMessage().contains(EXISTING_USER)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_INVALID_USER +
" Username '" + userName + "' already exists in the system. Please enter another username.", e);
} else if (e.getMessage().contains(INVALID_CLAIM_URL)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED + " Invalid claim uri has been provided.", e);
} else if (e.getMessage().contains(INVALID_USER_NAME)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_INVALID_USER +
" Username " + userName + " is not valid. User name must be a non null", e);
} else if (e.getMessage().contains(READ_ONLY_STORE)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED +
" Read-only UserStoreManager. Roles cannot be added or modified.", e);
} else if (e.getMessage().contains(READ_ONLY_PRIMARY_STORE)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED +
" Cannot add role to Read Only user store unless it is primary.", e);
} else if (e.getMessage().contains(INVALID_ROLE)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED +
" Invalid role name. Role name must be a non null string.", e);
} else if (e.getMessage().contains(NO_READ_WRITE_PERMISSIONS)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED +
" Role cannot be added. User store is read only or cannot write groups.", e);
} else if (e.getMessage().contains(EXISTING_ROLE)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED +
" Role already exists in the system. Please enter another role name.", e);
} else if (e.getMessage().contains(SHARED_USER_ROLES)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED +
" User store doesn't support shared user roles functionality.", e);
} else if (e.getMessage().contains(REMOVE_ADMIN_USER)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED + " Cannot remove Admin user from Admin role.", e);
} else if (e.getMessage().contains(LOGGED_IN_USER)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED + " Cannot remove Admin user from Admin role.", e);
} else if (e.getMessage().contains(ADMIN_USER)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED + " Cannot remove Admin user from Admin role.", e);
} else if (e.getMessage().contains(ANONYMOUS_USER)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED +
" Cannot delete anonymous user.", e);
} else if (e.getMessage().contains(INVALID_OPERATION)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED + " Invalid operation. User store is read only.", e);
} else if (e.getMessage().contains(PASSWORD_POLICY_VIOLATION)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_UNEXPECTED + " " + e.getMessage(), e);
} else {
vBeanInstance = handleError(
VerificationBean.ERROR_CODE_UNEXPECTED + " Error occurred while adding user : " + userName, e);
return vBeanInstance;
}
return vBeanInstance;
} else {
vBeanInstance = handleError(
VerificationBean.ERROR_CODE_UNEXPECTED + " Error occurred while adding user : " + userName, e);
return vBeanInstance;
}
}

/**
* @deprecated Use {@link #getCustomErrorMessagesForCodeVerification} instead.
*/
@Deprecated
public static VerificationBean getCustomErrorMessagesToVerifyCode(IdentityException e, String userName) {
if (e.getMessage() != null) {
if (e.getMessage().contains(VerificationBean.ERROR_CODE_EXPIRED_CODE)) {
Expand Down Expand Up @@ -726,6 +791,45 @@ public static VerificationBean getCustomErrorMessagesToVerifyCode(IdentityExcept
}
}

public VerificationBean getCustomErrorMessagesForCodeVerification(IdentityException e, String userName) {

if (e.getMessage() != null) {
if (e.getMessage().contains(VerificationBean.ERROR_CODE_EXPIRED_CODE)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_EXPIRED_CODE + " The code is " + "expired", e);
} else if (e.getMessage().contains(IdentityMgtConstants.ErrorHandling.INVALID_CONFIRMATION_CODE)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_INVALID_CODE + " " +
IdentityMgtConstants.ErrorHandling.INVALID_CONFIRMATION_CODE, e);
} else if (e.getMessage().contains(VerificationBean.ERROR_CODE_LOADING_DATA_FAILURE)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_LOADING_DATA_FAILURE + " Error" +
" loading data for user : " + userName, e);
} else if (e.getMessage().contains(IdentityMgtConstants.ErrorHandling.EXTERNAL_CODE)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_INVALID_CODE + " " +
IdentityMgtConstants.ErrorHandling.EXTERNAL_CODE + ": " + userName, e);
} else if (e.getMessage().contains(IdentityMgtConstants.ErrorHandling.NOTIFICATION_FAILURE)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_RECOVERY_NOTIFICATION_FAILURE + " " + IdentityMgtConstants.
ErrorHandling.NOTIFICATION_FAILURE + ": " + userName, e);
} else if (e.getMessage().contains(IdentityMgtConstants.ErrorHandling.ERROR_LOADING_EMAIL_TEMP)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_RECOVERY_NOTIFICATION_FAILURE + ": " + IdentityMgtConstants.
ErrorHandling.ERROR_LOADING_EMAIL_TEMP + " " + userName, e);
} else if (e.getMessage().contains(IdentityMgtConstants.ErrorHandling.EXTERNAL_CODE)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_INVALID_CODE + ": " + IdentityMgtConstants.
ErrorHandling.EXTERNAL_CODE + " " + userName, e);
} else if (e.getMessage().contains(IdentityMgtConstants.ErrorHandling.CREATING_NOTIFICATION_ERROR)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_RECOVERY_NOTIFICATION_FAILURE + ": " + IdentityMgtConstants.
ErrorHandling.CREATING_NOTIFICATION_ERROR + " " + userName, e);
} else if (e.getMessage().contains(VerificationBean.ERROR_CODE_LOADING_DATA_FAILURE)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_LOADING_DATA_FAILURE + " Error" +
" loading data for user : " + userName, e);
} else if (e.getMessage().contains(IdentityMgtConstants.ErrorHandling.USER_ACCOUNT)) {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_INVALID_CODE + " No user account found for user", e);
}
return vBeanInstance;
} else {
vBeanInstance = handleError(VerificationBean.ERROR_CODE_INVALID_CODE + " No user account found for user", e);
return vBeanInstance;
}
}

public static ChallengeQuestionIdsDTO getCustomErrorMessagesForChallengeQuestionIds(Exception e, String userName) {
if (e.getMessage() != null) {
if (e.getMessage().contains(VerificationBean.ERROR_CODE_EXPIRED_CODE)) {
Expand Down
Loading

0 comments on commit 107d65e

Please sign in to comment.