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.json.JsonCodec;
41import dev.rafex.ether.http.core.AuthPolicy;
42
43public final class JettyAuthHandler extends Handler.Wrapper {
44
45 public static final String REQ_ATTR_AUTH = "auth";
46
47 record Rule(String method, PathSpec pathSpec) {
48 }
49
50 private final TokenVerifier tokenVerifier;
51 private final JettyApiErrorResponses errorResponses;
52 private final List<Rule> publicRules = new ArrayList<>();
53 private final List<PathSpec> protectedPrefixes = new ArrayList<>();
54
55 public JettyAuthHandler(final Handler delegate, final TokenVerifier tokenVerifier, final JsonCodec jsonCodec) {
56 super(delegate);
57 this.tokenVerifier = Objects.requireNonNull(tokenVerifier);
58 this.errorResponses = new JettyApiErrorResponses(Objects.requireNonNull(jsonCodec));
59 }
60
61 public JettyAuthHandler publicPath(final String method, final String pathSpec) {
62 publicRules.add(new Rule(method.toUpperCase(), PathSpec.from(pathSpec)));
63 return this;
64 }
65
66 public JettyAuthHandler protectedPrefix(final String pathSpec) {
67 protectedPrefixes.add(PathSpec.from(pathSpec));
68 return this;
69 }
70
71 public JettyAuthHandler authPolicy(final AuthPolicy policy) {
72 if (policy == null) {
73 return this;
74 }
75 if (policy.type() == AuthPolicy.Type.PUBLIC_PATH) {
76 return publicPath(policy.method(), policy.pathSpec());
77 }
78 return protectedPrefix(policy.pathSpec());
79 }
80
81 public JettyAuthHandler authPolicies(final List<AuthPolicy> policies) {
82 if (policies == null) {
83 return this;
84 }
85 for (final var policy : policies) {
86 authPolicy(policy);
87 }
88 return this;
89 }
90
91 @Override
92 public boolean handle(final Request request, final Response response, final Callback callback) throws Exception {
93 final var method = request.getMethod().toUpperCase();
94 final var path = request.getHttpURI() != null ? request.getHttpURI().getPath() : null;
95 if (path == null) {
96 errorResponses.badRequest(response, callback, "missing_path");
97 return true;
98 }
99
100 if (isPublic(method, path) || !isProtected(path)) {
101 return super.handle(request, response, callback);
102 }
103
104 final var authz = request.getHeaders().get("authorization");
105 if (authz == null || !authz.startsWith("Bearer ")) {
106 errorResponses.unauthorized(response, callback, "missing_bearer_token");
107 return true;
108 }
109
110 final var token = authz.substring("Bearer ".length()).trim();
111 final var verification = tokenVerifier.verify(token, Instant.now().getEpochSecond());
112 if (!verification.ok()) {
113 final var code = verification.code() == null || verification.code().isBlank() ? "invalid_token"
114 : verification.code();
115 errorResponses.unauthorized(response, callback, code);
116 return true;
117 }
118
119 request.setAttribute(REQ_ATTR_AUTH, verification.context());
120 return super.handle(request, response, callback);
121 }
122
123 private boolean isPublic(final String method, final String path) {
124 for (final var rule : publicRules) {
125 if (rule.method().equals(method) && rule.pathSpec().matches(path)) {
126 return true;
127 }
128 }
129 return false;
130 }
131
132 private boolean isProtected(final String path) {
133 for (final var p : protectedPrefixes) {
134 if (p.matches(path)) {
135 return true;
136 }
137 }
138 return false;
139 }
140}
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)