Joaquín Reñé
2025-10-07 146a0fb8b0e90f9196e569152f649baf60d6cc8f
securis/src/main/java/net/curisit/securis/AuthFilter.java
....@@ -1,3 +1,6 @@
1
+/*
2
+ * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
3
+ */
14 package net.curisit.securis;
25
36 import java.io.IOException;
....@@ -17,16 +20,63 @@
1720 import org.apache.logging.log4j.LogManager;
1821 import org.apache.logging.log4j.Logger;
1922
23
+/**
24
+* AuthFilter
25
+* <p>
26
+* Simple authentication/role wrapper for development and lightweight scenarios.
27
+* If a request parameter <code>user</code> or a session attribute <code>user</code>
28
+* is present, this filter wraps the current request with a custom {@link Principal}
29
+* and an ad-hoc role. The role assignment is temporary and follows the rule:
30
+* <ul>
31
+* <li>user == "advance" → role "advance"</li>
32
+* <li>otherwise → role "normal"</li>
33
+* </ul>
34
+* If no user is present, the request continues unmodified.
35
+*
36
+* <p><b>Security note:</b> This filter trusts a user name coming from a request parameter,
37
+* which must not be used in production. Replace with a proper authentication mechanism
38
+* (e.g., JWT, container security, SSO) and derive roles from authoritative claims.
39
+*
40
+* @author JRA
41
+* Last reviewed by JRA on Oct 6, 2025.
42
+*/
2043 @ApplicationScoped
2144 @WebFilter(urlPatterns = "/*")
2245 public class AuthFilter implements Filter {
2346
2447 private static final Logger LOG = LogManager.getLogger(AuthFilter.class);
2548
49
+ // ---------------------------------------------------------------------
50
+ // Lifecycle
51
+ // ---------------------------------------------------------------------
52
+
53
+ /**
54
+ * init<p>
55
+ * Filter initialization hook (unused).
56
+ */
2657 @Override
2758 public void init(FilterConfig fc) throws ServletException {
2859 }
2960
61
+ // ---------------------------------------------------------------------
62
+ // Filtering
63
+ // ---------------------------------------------------------------------
64
+
65
+
66
+ /**
67
+ * doFilter
68
+ * <p>
69
+ * If a user is detected (request param or session attribute), wrap the request to:
70
+ * <ul>
71
+ * <li>Expose a {@link Principal} with the provided username.</li>
72
+ * <li>Report a single role through {@link HttpServletRequest#isUserInRole(String)}.</li>
73
+ * </ul>
74
+ * Otherwise, pass-through.
75
+ *
76
+ * @param sr incoming request
77
+ * @param sr1 outgoing response
78
+ * @param fc filter chain
79
+ */
3080 @Override
3181 public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException {
3282 HttpServletRequest req = (HttpServletRequest) sr;
....@@ -46,21 +96,46 @@
4696
4797 }
4898
99
+ /**
100
+ * destroy<p>
101
+ * Filter destruction hook (unused).
102
+ */
49103 @Override
50104 public void destroy() {
51105 }
52106
107
+ // ---------------------------------------------------------------------
108
+ // Wrapper
109
+ // ---------------------------------------------------------------------
110
+
111
+ /**
112
+ * UserRoleRequestWrapper
113
+ * <p>
114
+ * Wrapper that overrides role checks and the user principal when a synthetic user is present.
115
+ */
53116 private class UserRoleRequestWrapper extends HttpServletRequestWrapper {
54117
55118 private String role;
56119 private String user;
57120
121
+ /**
122
+ * Constructor
123
+ * <p>
124
+ * @param role single role to expose via {@link #isUserInRole(String)}
125
+ * @param user user name to expose via {@link #getUserPrincipal()}
126
+ * @param request original request to wrap
127
+ */
58128 public UserRoleRequestWrapper(String role, String user, HttpServletRequest request) {
59129 super(request);
60130 this.role = role;
61131 this.user = user;
62132 }
63133
134
+ /**
135
+ * isUserInRole
136
+ * <p>
137
+ * Returns {@code true} if the requested role equals the configured synthetic role.
138
+ */
64139 @Override
65140 public boolean isUserInRole(String role) {
66141 LOG.info("isUserRole METHOD: {}, {}", role, this.role);
....@@ -70,6 +145,11 @@
70145 return this.role.equals(role);
71146 }
72147
148
+ /**
149
+ * getUserPrincipal
150
+ * <p>
151
+ * Returns a minimal {@link Principal} with the configured user name; delegates otherwise.
152
+ */
73153 @Override
74154 public Principal getUserPrincipal() {
75155 if (this.user == null) {