Skip to content

Commit

Permalink
[ES-1929] [ES-1932] modified lua cleanup script, otp retrigger issue (#…
Browse files Browse the repository at this point in the history
…431)

* [MODIFIED] lua cleanup script

Signed-off-by: Zeeshan Mehboob <[email protected]>

* [MODIFIED] blocking identifer in validate trasaction

Signed-off-by: Zeeshan Mehboob <[email protected]>

---------

Signed-off-by: Zeeshan Mehboob <[email protected]>
  • Loading branch information
zesu22 authored Nov 15, 2024
1 parent 04c013f commit e025f57
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public class CacheUtilService {


private static final String CLEANUP_SCRIPT = "local hash_name = KEYS[1]\n" +
"local current_time = tonumber(ARGV[1])\n" +
"local time = redis.call(\"TIME\")\n" +
"local current_time = ( tonumber(time[1]) * 1000) + math.floor( tonumber(time[2]) / 1000)\n" +
"local verified_slot_cache_keys = {}\n" +
"local fields_to_delete = {}\n" +
"local delcount=0\n" +
Expand All @@ -62,7 +63,7 @@ public class CacheUtilService {
" for i = 1, #hash_data, 2 do\n" +
" local field = hash_data[i]\n" +
" local value = tonumber(hash_data[i + 1])\n" +
" if value and value < current_time then\n" +
" if value < current_time then\n" +
" local separator_index = string.find(field, \"###\")\n" +
" if separator_index then \n" +
" local key_part = string.sub(field, 1, separator_index - 1)\n" +
Expand Down Expand Up @@ -272,16 +273,14 @@ public void clearExpiredSlots() {
scriptHash = redisConnectionFactory.getConnection().scriptingCommands().scriptLoad(CLEANUP_SCRIPT.getBytes());
}
LockAssert.assertLocked();
Long currentTimeMillis = System.currentTimeMillis(); // Current time in millis
log.info("Running scheduled cleanup task - task to clear expired slots with script hash: {} {}", scriptHash,
SLOTS_CONNECTED);

long keysDeleted = redisConnectionFactory.getConnection().scriptingCommands().evalSha(
scriptHash,
ReturnType.INTEGER,
1, // Number of keys
SLOTS_CONNECTED.getBytes(), // The Redis hash name (key)
Longs.toByteArray(currentTimeMillis) // Current time in milliseconds
SLOTS_CONNECTED.getBytes() // The Redis hash name (key)
);
log.info("Running scheduled cleanup task - Keys Deleted count: {}", keysDeleted);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public GenerateChallengeResponse generateChallenge(GenerateChallengeRequest gene
}
else {
transaction = cacheUtilService.getChallengeGeneratedTransaction(transactionId);
validateTransaction(transaction, identifier, generateChallengeRequest);
validateTransaction(transaction, identifier, generateChallengeRequest, transactionId);
transaction.setVerificationAttempts(0);
}

Expand All @@ -124,10 +124,6 @@ public GenerateChallengeResponse generateChallenge(GenerateChallengeRequest gene
transaction.setLocale(generateChallengeRequest.getLocale());
cacheUtilService.createUpdateChallengeGeneratedTransaction(transactionId, transaction);

//Resend attempts exhausted, block the identifier for configured time.
if(transaction.getChallengeRetryAttempts() > resendAttempts)
cacheUtilService.blockIdentifier(transactionId, transaction.getIdentifier(), "blocked");

HashMap<String, String> hashMap = new LinkedHashMap<>();
hashMap.put("{challenge}", challenge);
notificationHelper.sendSMSNotification(generateChallengeRequest.getIdentifier(), transaction.getLocale(),
Expand Down Expand Up @@ -327,7 +323,7 @@ private void validateKBAChallenge(ProfileDto profileDto, VerifyChallengeRequest


private void validateTransaction(RegistrationTransaction transaction, String identifier,
GenerateChallengeRequest generateChallengeRequest) {
GenerateChallengeRequest generateChallengeRequest, String transactionId) {
if(transaction == null) {
log.error("generate-challenge failed: validate transaction null");
throw new InvalidTransactionException();
Expand All @@ -339,6 +335,8 @@ private void validateTransaction(RegistrationTransaction transaction, String ide
}

if(transaction.getChallengeRetryAttempts() > resendAttempts) {
//Resend attempts exhausted, block the identifier for configured time.
cacheUtilService.blockIdentifier(transactionId, transaction.getIdentifier(), "blocked");
log.error("generate-challenge failed: too many attempts");
throw new GenerateChallengeException(ErrorConstants.TOO_MANY_ATTEMPTS);
}
Expand Down

0 comments on commit e025f57

Please sign in to comment.