|
11 | 11 | import jakarta.servlet.ServletException; |
12 | 12 | import jakarta.servlet.http.HttpServletRequest; |
13 | 13 | import jakarta.servlet.http.HttpServletResponse; |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.*; |
14 | 16 | import org.springframework.security.core.context.SecurityContextHolder; |
15 | 17 | import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; |
16 | 18 | import org.springframework.web.filter.OncePerRequestFilter; |
17 | 19 |
|
18 | | -import java.io.IOException; |
19 | | -import java.util.*; |
20 | | - |
21 | 20 | public class Auth0AuthenticationFilter extends OncePerRequestFilter { |
22 | 21 |
|
23 | | - private final AuthClient authClient; |
| 22 | + private final AuthClient authClient; |
24 | 23 |
|
25 | | - private final Auth0Properties auth0Properties; |
| 24 | + private final Auth0Properties auth0Properties; |
26 | 25 |
|
27 | | - public Auth0AuthenticationFilter(AuthClient authClient, Auth0Properties auth0Properties) { |
28 | | - this.authClient = authClient; |
29 | | - this.auth0Properties = auth0Properties; |
30 | | - } |
| 26 | + public Auth0AuthenticationFilter(AuthClient authClient, Auth0Properties auth0Properties) { |
| 27 | + this.authClient = authClient; |
| 28 | + this.auth0Properties = auth0Properties; |
| 29 | + } |
31 | 30 |
|
32 | | - @Override |
33 | | - protected void doFilterInternal( |
34 | | - HttpServletRequest request, |
35 | | - HttpServletResponse response, |
36 | | - FilterChain chain |
37 | | - ) throws ServletException, IOException { |
| 31 | + @Override |
| 32 | + protected void doFilterInternal( |
| 33 | + HttpServletRequest request, HttpServletResponse response, FilterChain chain) |
| 34 | + throws ServletException, IOException { |
38 | 35 |
|
39 | | - try { |
| 36 | + try { |
40 | 37 |
|
41 | | - Map<String, String> headers = extractHeaders(request); |
| 38 | + Map<String, String> headers = extractHeaders(request); |
42 | 39 |
|
43 | | - String authorizationHeader = headers.get("authorization"); |
44 | | - if (authorizationHeader == null || authorizationHeader.trim().isEmpty()) { |
45 | | - chain.doFilter(request, response); |
46 | | - return; |
47 | | - } |
| 40 | + String authorizationHeader = headers.get("authorization"); |
| 41 | + if (authorizationHeader == null || authorizationHeader.trim().isEmpty()) { |
| 42 | + chain.doFilter(request, response); |
| 43 | + return; |
| 44 | + } |
48 | 45 |
|
49 | | - HttpRequestInfo requestInfo = extractRequestInfo(request); |
| 46 | + HttpRequestInfo requestInfo = extractRequestInfo(request); |
50 | 47 |
|
51 | | - AuthenticationContext ctx = authClient.verifyRequest(headers, requestInfo); |
| 48 | + AuthenticationContext ctx = authClient.verifyRequest(headers, requestInfo); |
52 | 49 |
|
53 | | - Auth0AuthenticationToken authentication = new Auth0AuthenticationToken(ctx); |
54 | | - authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); |
| 50 | + Auth0AuthenticationToken authentication = new Auth0AuthenticationToken(ctx); |
| 51 | + authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); |
55 | 52 |
|
56 | | - SecurityContextHolder.getContext().setAuthentication(authentication); |
| 53 | + SecurityContextHolder.getContext().setAuthentication(authentication); |
57 | 54 |
|
58 | | - chain.doFilter(request, response); |
| 55 | + chain.doFilter(request, response); |
59 | 56 |
|
60 | | - } catch (BaseAuthException ex) { |
61 | | - response.setStatus(ex.getStatusCode()); |
| 57 | + } catch (BaseAuthException ex) { |
| 58 | + response.setStatus(ex.getStatusCode()); |
62 | 59 |
|
63 | | - Map<String, String> exceptionHeaders = ex.getHeaders(); |
64 | | - String wwwAuthenticate = exceptionHeaders.get("WWW-Authenticate"); |
| 60 | + Map<String, String> exceptionHeaders = ex.getHeaders(); |
| 61 | + String wwwAuthenticate = exceptionHeaders.get("WWW-Authenticate"); |
65 | 62 |
|
66 | | - if (wwwAuthenticate != null) { |
67 | | - response.addHeader("WWW-Authenticate", wwwAuthenticate); |
68 | | - } |
69 | | - SecurityContextHolder.clearContext(); |
70 | | - } |
| 63 | + if (wwwAuthenticate != null) { |
| 64 | + response.addHeader("WWW-Authenticate", wwwAuthenticate); |
| 65 | + } |
| 66 | + SecurityContextHolder.clearContext(); |
71 | 67 | } |
| 68 | + } |
72 | 69 |
|
73 | | - Map<String, String> extractHeaders(HttpServletRequest request) |
74 | | - throws MissingAuthorizationException { |
| 70 | + Map<String, String> extractHeaders(HttpServletRequest request) |
| 71 | + throws MissingAuthorizationException { |
75 | 72 |
|
76 | | - List<String> authHeaders = Collections.list(request.getHeaders("Authorization")); |
77 | | - if (authHeaders != null && authHeaders.size() > 1) { |
78 | | - String firstValue = authHeaders.get(0); |
| 73 | + List<String> authHeaders = Collections.list(request.getHeaders("Authorization")); |
| 74 | + if (authHeaders != null && authHeaders.size() > 1) { |
| 75 | + String firstValue = authHeaders.get(0); |
79 | 76 |
|
80 | | - MissingAuthorizationException ex = new MissingAuthorizationException(); |
| 77 | + MissingAuthorizationException ex = new MissingAuthorizationException(); |
81 | 78 |
|
82 | | - String[] parts = firstValue.trim().split("\\s+", 2); |
| 79 | + String[] parts = firstValue.trim().split("\\s+", 2); |
83 | 80 |
|
84 | | - DPoPMode dpopMode = auth0Properties.getDpopMode(); |
85 | | - if (dpopMode == null) { |
86 | | - dpopMode = DPoPMode.ALLOWED; // default fallback |
87 | | - } |
| 81 | + DPoPMode dpopMode = auth0Properties.getDpopMode(); |
| 82 | + if (dpopMode == null) { |
| 83 | + dpopMode = DPoPMode.ALLOWED; // default fallback |
| 84 | + } |
88 | 85 |
|
89 | | - List<String> challenges = WWWAuthenticateBuilder.buildChallenges( |
90 | | - ex.getErrorCode(), |
91 | | - ex.getErrorDescription(), |
92 | | - dpopMode, |
93 | | - parts[0].toLowerCase(Locale.ROOT) |
94 | | - ); |
| 86 | + List<String> challenges = |
| 87 | + WWWAuthenticateBuilder.buildChallenges( |
| 88 | + ex.getErrorCode(), |
| 89 | + ex.getErrorDescription(), |
| 90 | + dpopMode, |
| 91 | + parts[0].toLowerCase(Locale.ROOT)); |
95 | 92 |
|
96 | | - if (!challenges.isEmpty()) { |
97 | | - ex.addHeader("WWW-Authenticate", String.join(", ", challenges)); |
98 | | - } |
| 93 | + if (!challenges.isEmpty()) { |
| 94 | + ex.addHeader("WWW-Authenticate", String.join(", ", challenges)); |
| 95 | + } |
99 | 96 |
|
100 | | - throw ex; |
101 | | - } |
102 | | - |
103 | | - Map<String, String> headers = new HashMap<>(); |
104 | | - Enumeration<String> names = request.getHeaderNames(); |
105 | | - |
106 | | - if (names != null) { |
107 | | - while (names.hasMoreElements()) { |
108 | | - String name = names.nextElement(); |
109 | | - headers.put( |
110 | | - name.toLowerCase(Locale.ROOT), |
111 | | - request.getHeader(name) |
112 | | - ); |
113 | | - } |
114 | | - } |
115 | | - |
116 | | - return headers; |
| 97 | + throw ex; |
117 | 98 | } |
118 | 99 |
|
119 | | - HttpRequestInfo extractRequestInfo(HttpServletRequest request) { |
120 | | - String htu = buildHtu(request); |
121 | | - return new HttpRequestInfo(request.getMethod(), htu, null); |
| 100 | + Map<String, String> headers = new HashMap<>(); |
| 101 | + Enumeration<String> names = request.getHeaderNames(); |
| 102 | + |
| 103 | + if (names != null) { |
| 104 | + while (names.hasMoreElements()) { |
| 105 | + String name = names.nextElement(); |
| 106 | + headers.put(name.toLowerCase(Locale.ROOT), request.getHeader(name)); |
| 107 | + } |
122 | 108 | } |
123 | 109 |
|
124 | | - static String buildHtu(HttpServletRequest request) { |
125 | | - String scheme = request.getScheme().toLowerCase(Locale.ROOT); |
126 | | - String host = request.getServerName().toLowerCase(Locale.ROOT); |
| 110 | + return headers; |
| 111 | + } |
127 | 112 |
|
128 | | - int port = request.getServerPort(); |
129 | | - boolean defaultPort = |
130 | | - (scheme.equals("http") && port == 80) || |
131 | | - (scheme.equals("https") && port == 443); |
| 113 | + HttpRequestInfo extractRequestInfo(HttpServletRequest request) { |
| 114 | + String htu = buildHtu(request); |
| 115 | + return new HttpRequestInfo(request.getMethod(), htu, null); |
| 116 | + } |
132 | 117 |
|
133 | | - StringBuilder htu = new StringBuilder(); |
134 | | - htu.append(scheme).append("://").append(host); |
| 118 | + static String buildHtu(HttpServletRequest request) { |
| 119 | + String scheme = request.getScheme().toLowerCase(Locale.ROOT); |
| 120 | + String host = request.getServerName().toLowerCase(Locale.ROOT); |
135 | 121 |
|
136 | | - if (!defaultPort) { |
137 | | - htu.append(":").append(port); |
138 | | - } |
| 122 | + int port = request.getServerPort(); |
| 123 | + boolean defaultPort = |
| 124 | + (scheme.equals("http") && port == 80) || (scheme.equals("https") && port == 443); |
139 | 125 |
|
140 | | - htu.append(request.getRequestURI()); |
| 126 | + StringBuilder htu = new StringBuilder(); |
| 127 | + htu.append(scheme).append("://").append(host); |
141 | 128 |
|
142 | | - return htu.toString(); |
| 129 | + if (!defaultPort) { |
| 130 | + htu.append(":").append(port); |
143 | 131 | } |
| 132 | + |
| 133 | + htu.append(request.getRequestURI()); |
| 134 | + |
| 135 | + return htu.toString(); |
| 136 | + } |
144 | 137 | } |
0 commit comments