|
| 1 | +package com.itbulls.learnit.onlinestore.web.owasp.bola; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import com.itbulls.learnit.onlinestore.core.facades.ProductFacade; |
| 6 | +import com.itbulls.learnit.onlinestore.core.facades.impl.DefaultProductFacade; |
| 7 | +import com.itbulls.learnit.onlinestore.persistence.dto.RoleDto; |
| 8 | +import com.itbulls.learnit.onlinestore.persistence.enteties.Product; |
| 9 | +import com.itbulls.learnit.onlinestore.persistence.enteties.User; |
| 10 | +import com.itbulls.learnit.onlinestore.web.controllers.SignInServlet; |
| 11 | + |
| 12 | +import jakarta.servlet.ServletException; |
| 13 | +import jakarta.servlet.annotation.WebServlet; |
| 14 | +import jakarta.servlet.http.HttpServlet; |
| 15 | +import jakarta.servlet.http.HttpServletRequest; |
| 16 | +import jakarta.servlet.http.HttpServletResponse; |
| 17 | + |
| 18 | +@WebServlet("/bola-product-demo-solution") |
| 19 | +public class SecureProductServlet extends HttpServlet { |
| 20 | + |
| 21 | + private ProductFacade productFacade = DefaultProductFacade.getInstance(); |
| 22 | + |
| 23 | + @Override |
| 24 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 25 | + String productGuid = req.getParameter("productGuid"); |
| 26 | + User loggedInUser = (User)req.getSession().getAttribute(SignInServlet.LOGGED_IN_USER_ATTR); |
| 27 | + Product product = productFacade.getProductByGuid(productGuid); |
| 28 | + |
| 29 | + if (product != null && product.getProductName() != null) { |
| 30 | + if (loggedInUser != null && loggedInUser.getRoleName().equals(RoleDto.ADMIN_ROLE_NAME)) { |
| 31 | + resp.getWriter().write("Product: " + product); |
| 32 | + } else { |
| 33 | + resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized access to read product"); |
| 34 | + } |
| 35 | + } else { |
| 36 | + resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Product not found"); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
0 commit comments