-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathOtpRateLimiterService.java
More file actions
104 lines (91 loc) · 3.94 KB
/
OtpRateLimiterService.java
File metadata and controls
104 lines (91 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* AMRIT – Accessible Medical Records via Integrated Technology
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package com.iemr.common.service.otp;
import com.iemr.common.exception.OtpRateLimitException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;
/**
* Rate-limits OTP send/resend requests per mobile number using Redis counters.
*
* Limits (configurable via properties):
* otp.ratelimit.minute-limit – max OTPs per minute (default 3)
* otp.ratelimit.hour-limit – max OTPs per hour (default 10)
* otp.ratelimit.day-limit – max OTPs per day (default 20)
*
* Redis key pattern:
* rl:otp:min:{mobNo}:{minuteSlot} TTL 60 s
* rl:otp:hr:{mobNo}:{hourSlot} TTL 3600 s
* rl:otp:day:{mobNo}:{yyyyMMdd} TTL 86400 s
*/
@Component
public class OtpRateLimiterService {
private final StringRedisTemplate redis;
@Value("${otp.ratelimit.enabled:true}")
private boolean enabled;
@Value("${otp.ratelimit.minute-limit:3}")
private int minuteLimit;
@Value("${otp.ratelimit.hour-limit:10}")
private int hourLimit;
@Value("${otp.ratelimit.day-limit:20}")
private int dayLimit;
public OtpRateLimiterService(StringRedisTemplate redis) {
this.redis = redis;
}
/**
* Checks all three rate-limit windows for the given mobile number.
* Throws {@link OtpRateLimitException} if any limit is exceeded.
* No-op when otp.ratelimit.enabled=false.
*/
public void checkRateLimit(String mobNo) {
if (!enabled) return;
String today = LocalDate.now(ZoneId.of("Asia/Kolkata"))
.toString().replaceAll("-", ""); // yyyyMMdd
long minuteSlot = System.currentTimeMillis() / 60_000L;
long hourSlot = System.currentTimeMillis() / 3_600_000L;
String minKey = "rl:otp:min:" + mobNo + ":" + minuteSlot;
String hourKey = "rl:otp:hr:" + mobNo + ":" + hourSlot;
String dayKey = "rl:otp:day:" + mobNo + ":" + today;
if (incrementWithExpire(minKey, 60L) > minuteLimit) {
throw new OtpRateLimitException(
"OTP request limit exceeded. Maximum " + minuteLimit + " OTPs allowed per minute. Please try again later.");
}
if (incrementWithExpire(hourKey, 3600L) > hourLimit) {
throw new OtpRateLimitException(
"OTP request limit exceeded. Maximum " + hourLimit + " OTPs allowed per hour. Please try again later.");
}
if (incrementWithExpire(dayKey, 86400L) > dayLimit) {
throw new OtpRateLimitException(
"OTP request limit exceeded. Maximum " + dayLimit + " OTPs allowed per day. Please try again tomorrow.");
}
}
private long incrementWithExpire(String key, long ttlSeconds) {
Long value = redis.opsForValue().increment(key, 1L);
if (value != null && value == 1L) {
redis.expire(key, ttlSeconds, TimeUnit.SECONDS);
}
return value == null ? 0L : value;
}
}