|
| 1 | +package com.itbulls.learnit.onlinestore.web.owasp.urc.solution; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.time.Instant; |
| 5 | +import java.time.temporal.ChronoUnit; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Random; |
| 8 | +import java.util.concurrent.ConcurrentHashMap; |
| 9 | + |
| 10 | +import com.itbulls.learnit.onlinestore.core.facades.UserFacade; |
| 11 | +import com.itbulls.learnit.onlinestore.core.facades.impl.DefaultUserFacade; |
| 12 | +import com.itbulls.learnit.onlinestore.persistence.enteties.User; |
| 13 | + |
| 14 | +import jakarta.servlet.ServletException; |
| 15 | +import jakarta.servlet.annotation.WebServlet; |
| 16 | +import jakarta.servlet.http.HttpServlet; |
| 17 | +import jakarta.servlet.http.HttpServletRequest; |
| 18 | +import jakarta.servlet.http.HttpServletResponse; |
| 19 | + |
| 20 | +@WebServlet("/urc-solution-reset-password") |
| 21 | +public class UrcSolutionPasswordResetServlet extends HttpServlet { |
| 22 | + |
| 23 | + private static final int MAX_REQUESTS = 5; // Max requests per time window |
| 24 | + private static final int TIME_WINDOW_IN_MINUTES = 30; // Time window in minutes |
| 25 | + private static final Map<String, RateLimitInfo> rateLimitMap = new ConcurrentHashMap<>(); |
| 26 | + |
| 27 | + @Override |
| 28 | + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 29 | + String email = req.getParameter("email"); |
| 30 | + |
| 31 | + // Check rate limit |
| 32 | + String clientIdentifier = email; // You can use email as a client identifier for rate limiting |
| 33 | + RateLimitInfo info = rateLimitMap.getOrDefault(clientIdentifier, new RateLimitInfo()); |
| 34 | + |
| 35 | + if (info.isExpired() || info.getCount() < MAX_REQUESTS) { |
| 36 | + if (info.isExpired()) { |
| 37 | + info.reset(TIME_WINDOW_IN_MINUTES); |
| 38 | + } |
| 39 | + info.increment(); |
| 40 | + rateLimitMap.put(clientIdentifier, info); |
| 41 | + |
| 42 | + // Assuming these services are injected or instantiated |
| 43 | + UserFacade userService = new DefaultUserFacade(); |
| 44 | + User user = userService.getUserByEmail(email); |
| 45 | + if (user == null) { |
| 46 | + resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 47 | + resp.getWriter().write("Invalid email"); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // Generate a reset code |
| 52 | + String resetCode = generateResetCode(); |
| 53 | + userService.updateUser(user); |
| 54 | + |
| 55 | + // Send the code to user's email |
| 56 | + // NotificationService notificationService = new NotificationService(); |
| 57 | + // notificationService.sendResetCode(email, resetCode); |
| 58 | + |
| 59 | + resp.setStatus(HttpServletResponse.SC_OK); |
| 60 | + resp.getWriter().write("Reset code sent to your email"); |
| 61 | + |
| 62 | + } else { |
| 63 | + resp.setContentType("text/plain"); |
| 64 | + resp.getWriter().write("Rate limit exceeded. Try again later."); |
| 65 | + resp.setStatus(429); // Too Many Requests |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private String generateResetCode() { |
| 70 | + Random random = new Random(); |
| 71 | + return String.format("%04d", random.nextInt(10000)); |
| 72 | + } |
| 73 | + |
| 74 | + private static class RateLimitInfo { |
| 75 | + private Instant expiry; |
| 76 | + private int count; |
| 77 | + |
| 78 | + void reset(int minutes) { |
| 79 | + this.expiry = Instant.now().plus(minutes, ChronoUnit.MINUTES); |
| 80 | + this.count = 0; |
| 81 | + } |
| 82 | + |
| 83 | + boolean isExpired() { |
| 84 | + return Instant.now().isAfter(expiry); |
| 85 | + } |
| 86 | + |
| 87 | + void increment() { |
| 88 | + count++; |
| 89 | + } |
| 90 | + |
| 91 | + int getCount() { |
| 92 | + return count; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments