Skip to content

Commit

Permalink
MOSIP-31314 OTP validation security fix (#1197)
Browse files Browse the repository at this point in the history
Signed-off-by: kameshsr <[email protected]>
  • Loading branch information
kameshsr authored Feb 12, 2024
1 parent 4d985b5 commit 3cca8ef
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

/**
* OTPManager handling with OTP-Generation and OTP-Validation.
*
*
* @author Rakesh Roshan
* @author Dinesh Karuppiah.T
* @author Manoj SP
Expand Down Expand Up @@ -76,11 +76,11 @@ public class OTPManager {
/** The notification service. */
@Autowired
private NotificationService notificationService;

/** The number of validation attempts allowed. */
@Value("${mosip.ida.otp.validation.attempt.count.threshold:5}")
private int numberOfValidationAttemptsAllowed;

/** The otp frozen time minutes. */
@Value("${mosip.ida.otp.frozen.duration.minutes:30}")
private int otpFrozenTimeMinutes;
Expand All @@ -103,21 +103,21 @@ public class OTPManager {
*/
public boolean sendOtp(OtpRequestDTO otpRequestDTO, String idvid, String idvidType, Map<String, String> valueMap, List<String> templateLanguages)
throws IdAuthenticationBusinessException {

String refIdHash = securityManager.hash(idvid);
Optional<OtpTransaction> otpEntityOpt = otpRepo.findFirstByRefIdAndStatusCodeInAndGeneratedDtimesNotNullOrderByGeneratedDtimesDesc(refIdHash, QUERIED_STATUS_CODES);

if(otpEntityOpt.isPresent()) {
OtpTransaction otpEntity = otpEntityOpt.get();
requireOtpNotFrozen(otpEntity, false);
}

String otp = generateOTP(otpRequestDTO.getIndividualId());
LocalDateTime otpGenerationTime = DateUtils.getUTCCurrentDateTime();
String otpHash = IdAuthSecurityManager.digestAsPlainText((otpRequestDTO.getIndividualId()
+ EnvUtil.getKeySplitter() + otpRequestDTO.getTransactionID()
+ EnvUtil.getKeySplitter() + otp).getBytes());

OtpTransaction otpTxn;
if (otpEntityOpt.isPresent()
&& (otpTxn = otpEntityOpt.get()).getStatusCode().equals(IdAuthCommonConstants.ACTIVE_STATUS)) {
Expand All @@ -141,7 +141,7 @@ public boolean sendOtp(OtpRequestDTO otpRequestDTO, String idvid, String idvidTy
txn.setStatusCode(IdAuthCommonConstants.ACTIVE_STATUS);
otpRepo.save(txn);
}

String notificationProperty = null;
notificationProperty = otpRequestDTO
.getOtpChannel().stream().map(channel -> NotificationType.getNotificationTypeForChannel(channel)
Expand Down Expand Up @@ -219,18 +219,18 @@ public boolean validateOtp(String pinValue, String otpKey, String individualId)
if (otpEntityOpt.isEmpty()) {
throw new IdAuthenticationBusinessException(IdAuthenticationErrorConstants.OTP_REQUEST_REQUIRED);
}

OtpTransaction otpEntity = otpEntityOpt.get();
requireOtpNotFrozen(otpEntity, true);

if(otpEntity.getStatusCode().equals(IdAuthCommonConstants.UNFROZEN)) {
throw new IdAuthenticationBusinessException(IdAuthenticationErrorConstants.OTP_REQUEST_REQUIRED);
}

// At this point it should be active status alone.
// Increment the validation attempt count.
int attemptCount = otpEntity.getValidationRetryCount() == null ? 1 : otpEntity.getValidationRetryCount() + 1;

String otpHash = getOtpHash(pinValue, otpKey);
if (otpEntity.getOtpHash().equals(otpHash)) {
otpEntity.setUpdDTimes(DateUtils.getUTCCurrentDateTime());
Expand All @@ -247,6 +247,9 @@ public boolean validateOtp(String pinValue, String otpKey, String individualId)
otpEntity.setValidationRetryCount(attemptCount);
if (attemptCount >= numberOfValidationAttemptsAllowed) {
otpEntity.setStatusCode(IdAuthCommonConstants.FROZEN);
otpEntity.setUpdDTimes(DateUtils.getUTCCurrentDateTime());
otpRepo.save(otpEntity);
throw createOTPFrozenException();
}
otpEntity.setUpdDTimes(DateUtils.getUTCCurrentDateTime());
otpRepo.save(otpEntity);
Expand Down Expand Up @@ -294,5 +297,5 @@ private String getOtpHash(String pinValue, String otpKey) {
return IdAuthSecurityManager.digestAsPlainText(
(otpKey + EnvUtil.getKeySplitter() + pinValue).getBytes());
}

}
Loading

0 comments on commit 3cca8ef

Please sign in to comment.