Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOSIP-31314 OTP validation security fix #1197

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading