Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JettyAuthHandler.java
Go to the documentation of this file.
1package dev.rafex.ether.http.jetty12;
2
3/*-
4 * #%L
5 * ether-http-jetty12
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.time.Instant;
30import java.util.ArrayList;
31import java.util.List;
32import java.util.Objects;
33
34import org.eclipse.jetty.http.pathmap.PathSpec;
35import org.eclipse.jetty.server.Handler;
36import org.eclipse.jetty.server.Request;
37import org.eclipse.jetty.server.Response;
38import org.eclipse.jetty.util.Callback;
39
40import dev.rafex.ether.http.core.AuthPolicy;
41import dev.rafex.ether.http.jetty12.response.JettyApiErrorResponses;
42import dev.rafex.ether.http.jetty12.security.TokenVerificationResult;
43import dev.rafex.ether.http.jetty12.security.TokenVerifier;
44import dev.rafex.ether.json.JsonCodec;
45
46public final class JettyAuthHandler extends Handler.Wrapper {
47
48 public static final String REQ_ATTR_AUTH = "auth";
49
50 record Rule(String method, PathSpec pathSpec) {
51 }
52
53 private final TokenVerifier tokenVerifier;
54 private final JettyApiErrorResponses errorResponses;
55 private final List<Rule> publicRules = new ArrayList<>();
56 private final List<PathSpec> protectedPrefixes = new ArrayList<>();
57
58 public JettyAuthHandler(final Handler delegate, final TokenVerifier tokenVerifier, final JsonCodec jsonCodec) {
59 super(delegate);
60 this.tokenVerifier = Objects.requireNonNull(tokenVerifier);
61 this.errorResponses = new JettyApiErrorResponses(Objects.requireNonNull(jsonCodec));
62 }
63
64 public JettyAuthHandler publicPath(final String method, final String pathSpec) {
65 publicRules.add(new Rule(method.toUpperCase(), PathSpec.from(pathSpec)));
66 return this;
67 }
68
69 public JettyAuthHandler protectedPrefix(final String pathSpec) {
70 protectedPrefixes.add(PathSpec.from(pathSpec));
71 return this;
72 }
73
74 public JettyAuthHandler authPolicy(final AuthPolicy policy) {
75 if (policy == null) {
76 return this;
77 }
78 if (policy.type() == AuthPolicy.Type.PUBLIC_PATH) {
79 return publicPath(policy.method(), policy.pathSpec());
80 }
81 return protectedPrefix(policy.pathSpec());
82 }
83
84 public JettyAuthHandler authPolicies(final List<AuthPolicy> policies) {
85 if (policies == null) {
86 return this;
87 }
88 for (final var policy : policies) {
89 authPolicy(policy);
90 }
91 return this;
92 }
93
94 @Override
95 public boolean handle(final Request request, final Response response, final Callback callback) throws Exception {
96 final var method = request.getMethod().toUpperCase();
97 final var path = request.getHttpURI() != null ? request.getHttpURI().getPath() : null;
98 if (path == null) {
99 errorResponses.badRequest(response, callback, "missing_path");
100 return true;
101 }
102
103 if (isPublic(method, path) || !isProtected(path)) {
104 return super.handle(request, response, callback);
105 }
106
107 final var authz = request.getHeaders().get("authorization");
108 if (authz == null || !authz.startsWith("Bearer ")) {
109 errorResponses.unauthorized(response, callback, "missing_bearer_token");
110 return true;
111 }
112
113 final var token = authz.substring("Bearer ".length()).trim();
114 final var verification = tokenVerifier.verify(token, Instant.now().getEpochSecond());
115 if (!verification.ok()) {
116 final var code = verification.code() == null || verification.code().isBlank() ? "invalid_token"
117 : verification.code();
118 errorResponses.unauthorized(response, callback, code);
119 return true;
120 }
121
122 request.setAttribute(REQ_ATTR_AUTH, verification.context());
123 return super.handle(request, response, callback);
124 }
125
126 private boolean isPublic(final String method, final String path) {
127 for (final var rule : publicRules) {
128 if (rule.method().equals(method) && rule.pathSpec().matches(path)) {
129 return true;
130 }
131 }
132 return false;
133 }
134
135 private boolean isProtected(final String path) {
136 for (final var p : protectedPrefixes) {
137 if (p.matches(path)) {
138 return true;
139 }
140 }
141 return false;
142 }
143}
boolean handle(final Request request, final Response response, final Callback callback)
JettyAuthHandler authPolicy(final AuthPolicy policy)
JettyAuthHandler authPolicies(final List< AuthPolicy > policies)
JettyAuthHandler publicPath(final String method, final String pathSpec)
JettyAuthHandler(final Handler delegate, final TokenVerifier tokenVerifier, final JsonCodec jsonCodec)
JettyAuthHandler protectedPrefix(final String pathSpec)