Skip to content

Commit 62bbc54

Browse files
WIP: Jetty 12
1 parent 5057d97 commit 62bbc54

File tree

4 files changed

+60
-72
lines changed

4 files changed

+60
-72
lines changed

its/pom.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,20 @@
6868
<scope>test</scope>
6969
</dependency>
7070
<dependency>
71-
<groupId>org.eclipse.jetty</groupId>
71+
<groupId>org.eclipse.jetty.ee10</groupId>
7272
<artifactId>jetty-ee10-servlet</artifactId>
7373
<version>${jetty.version}</version>
7474
<scope>test</scope>
7575
</dependency>
7676
<dependency>
77-
<groupId>org.eclipse.jetty</groupId>
78-
<artifactId>jetty-proxy</artifactId>
77+
<groupId>org.eclipse.jetty.ee10</groupId>
78+
<artifactId>jetty-ee10-proxy</artifactId>
79+
<version>${jetty.version}</version>
80+
<scope>test</scope>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.eclipse.jetty.ee9</groupId>
84+
<artifactId>jetty-ee9-security</artifactId>
7985
<version>${jetty.version}</version>
8086
<scope>test</scope>
8187
</dependency>

its/src/test/java/com/sonar/it/scanner/msbuild/sonarqube/ProxyTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@
3131
import java.io.IOException;
3232
import java.util.List;
3333
import java.util.concurrent.ConcurrentLinkedDeque;
34-
import org.eclipse.jetty.client.api.Request;
35-
import org.eclipse.jetty.proxy.ProxyServlet;
36-
import org.eclipse.jetty.security.ConstraintMapping;
37-
import org.eclipse.jetty.security.ConstraintSecurityHandler;
34+
import org.eclipse.jetty.client.Request;
35+
import org.eclipse.jetty.ee10.proxy.ProxyServlet;
36+
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
37+
import org.eclipse.jetty.ee10.servlet.ServletHandler;
38+
import org.eclipse.jetty.ee10.servlet.security.ConstraintMapping;
39+
import org.eclipse.jetty.ee10.servlet.security.ConstraintSecurityHandler;
40+
import org.eclipse.jetty.ee9.security.Authenticator;
41+
import org.eclipse.jetty.security.Constraint;
3842
import org.eclipse.jetty.security.HashLoginService;
3943
import org.eclipse.jetty.security.SecurityHandler;
4044
import org.eclipse.jetty.security.UserStore;
@@ -44,10 +48,6 @@
4448
import org.eclipse.jetty.server.Server;
4549
import org.eclipse.jetty.server.ServerConnector;
4650
import org.eclipse.jetty.server.handler.DefaultHandler;
47-
import org.eclipse.jetty.server.handler.HandlerCollection;
48-
import org.eclipse.jetty.servlet.ServletContextHandler;
49-
import org.eclipse.jetty.servlet.ServletHandler;
50-
import org.eclipse.jetty.util.security.Constraint;
5151
import org.eclipse.jetty.util.security.Credential;
5252
import org.eclipse.jetty.util.thread.QueuedThreadPool;
5353
import org.junit.jupiter.api.AfterEach;
@@ -125,7 +125,7 @@ private static void startProxy(boolean needProxyAuth) throws Exception {
125125
httpConfig.setSendDateHeader(false);
126126

127127
// Handler Structure
128-
HandlerCollection handlers = new HandlerCollection();
128+
Handler.Sequence handlers = new Handler.Sequence();
129129
handlers.setHandlers(new Handler[]{proxyHandler(needProxyAuth), new DefaultHandler()});
130130
server.setHandler(handlers);
131131

@@ -154,10 +154,10 @@ private static SecurityHandler basicAuth(String realm) {
154154
l.setUserStore(userStore);
155155
l.setName(realm);
156156

157-
Constraint constraint = new Constraint();
158-
constraint.setName(Constraint.__BASIC_AUTH);
159-
constraint.setRoles(new String[]{"user"});
160-
constraint.setAuthenticate(true);
157+
Constraint constraint = new Constraint.Builder()
158+
.name(Authenticator.BASIC_AUTH)
159+
.roles(new String[]{"user"})
160+
.build();
161161

162162
ConstraintMapping cm = new ConstraintMapping();
163163
cm.setConstraint(constraint);
@@ -186,7 +186,7 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
186186
super.service(request, response);
187187
}
188188

189-
@Override
189+
@Override
190190
protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest) {
191191
super.sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
192192
}

its/src/test/java/com/sonar/it/scanner/msbuild/utils/HttpsReverseProxy.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,18 @@
2424
import java.net.InetAddress;
2525
import java.nio.file.Path;
2626
import java.nio.file.Paths;
27+
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
28+
import org.eclipse.jetty.ee10.servlet.ServletHandler;
29+
import org.eclipse.jetty.ee10.servlet.ServletHolder;
2730
import org.eclipse.jetty.http.HttpVersion;
28-
import org.eclipse.jetty.proxy.ProxyServlet;
31+
import org.eclipse.jetty.ee10.proxy.ProxyServlet;
2932
import org.eclipse.jetty.server.Handler;
3033
import org.eclipse.jetty.server.HttpConfiguration;
3134
import org.eclipse.jetty.server.HttpConnectionFactory;
3235
import org.eclipse.jetty.server.Server;
3336
import org.eclipse.jetty.server.ServerConnector;
3437
import org.eclipse.jetty.server.SslConnectionFactory;
3538
import org.eclipse.jetty.server.handler.DefaultHandler;
36-
import org.eclipse.jetty.server.handler.HandlerCollection;
37-
import org.eclipse.jetty.servlet.ServletContextHandler;
38-
import org.eclipse.jetty.servlet.ServletHandler;
39-
import org.eclipse.jetty.servlet.ServletHolder;
4039
import org.eclipse.jetty.util.ssl.SslContextFactory;
4140
import org.eclipse.jetty.util.thread.QueuedThreadPool;
4241
import org.slf4j.Logger;
@@ -79,7 +78,7 @@ public void start() throws Exception {
7978
httpConfig.setSendDateHeader(false);
8079

8180
// Handler Structure
82-
HandlerCollection handlers = new HandlerCollection();
81+
var handlers = new Handler.Sequence();
8382
handlers.setHandlers(new Handler[]{proxyHandler(), new DefaultHandler()});
8483
server.setHandler(handlers);
8584

its/src/test/java/com/sonar/it/scanner/msbuild/utils/ProxyAuthenticator.java

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,23 @@
4141
import jakarta.servlet.ServletResponse;
4242
import jakarta.servlet.http.HttpServletRequest;
4343
import jakarta.servlet.http.HttpServletResponse;
44+
import org.eclipse.jetty.client.Authentication;
45+
import org.eclipse.jetty.ee9.security.UserAuthentication;
46+
import org.eclipse.jetty.ee9.security.authentication.DeferredAuthentication;
4447
import org.eclipse.jetty.http.HttpHeader;
48+
import org.eclipse.jetty.security.AuthenticationState;
49+
import org.eclipse.jetty.security.Authenticator;
4550
import org.eclipse.jetty.security.ServerAuthException;
46-
import org.eclipse.jetty.security.UserAuthentication;
51+
import org.eclipse.jetty.security.UserIdentity;
4752
import org.eclipse.jetty.security.authentication.BasicAuthenticator;
48-
import org.eclipse.jetty.security.authentication.DeferredAuthentication;
4953
import org.eclipse.jetty.security.authentication.LoginAuthenticator;
50-
import org.eclipse.jetty.server.Authentication;
51-
import org.eclipse.jetty.server.Authentication.User;
52-
import org.eclipse.jetty.server.UserIdentity;
53-
import org.eclipse.jetty.util.security.Constraint;
5454

5555
import java.io.IOException;
5656
import java.nio.charset.StandardCharsets;
5757
import java.util.Base64;
58+
import org.eclipse.jetty.server.Request;
59+
import org.eclipse.jetty.server.Response;
60+
import org.eclipse.jetty.util.Callback;
5861

5962
/**
6063
* Inspired from {@link BasicAuthenticator} but adapted for proxy auth.
@@ -65,63 +68,43 @@ public ProxyAuthenticator() {
6568
}
6669

6770
/* ------------------------------------------------------------ */
71+
6872
/**
69-
* @see org.eclipse.jetty.security.Authenticator#getAuthMethod()
73+
* @see Authenticator#getAuthenticationType()
7074
*/
7175
@Override
72-
public String getAuthMethod() {
73-
return Constraint.__BASIC_AUTH;
76+
public String getAuthenticationType() {
77+
return Authenticator.BASIC_AUTH;
7478
}
7579

7680
/* ------------------------------------------------------------ */
81+
7782
/**
78-
* @see org.eclipse.jetty.security.Authenticator#validateRequest(ServletRequest, ServletResponse, boolean)
83+
* @see Authenticator#validateRequest(Request, Response, Callback)
7984
*/
8085
@Override
81-
public Authentication validateRequest(ServletRequest req, ServletResponse res, boolean mandatory) throws ServerAuthException {
82-
HttpServletRequest request = (HttpServletRequest) req;
83-
HttpServletResponse response = (HttpServletResponse) res;
84-
String credentials = request.getHeader(HttpHeader.PROXY_AUTHORIZATION.asString());
85-
86-
try {
87-
if (!mandatory)
88-
return new DeferredAuthentication(this);
86+
public AuthenticationState validateRequest(Request req, Response res, Callback callback) {
87+
String credentials = req.getHeaders().get(HttpHeader.PROXY_AUTHORIZATION);
88+
if (credentials != null) {
89+
int space = credentials.indexOf(' ');
90+
if (space > 0) {
91+
String method = credentials.substring(0, space);
92+
if ("basic".equalsIgnoreCase(method)) {
93+
credentials = credentials.substring(space + 1);
94+
credentials = new String(Base64.getDecoder().decode(credentials), StandardCharsets.ISO_8859_1);
95+
int i = credentials.indexOf(':');
96+
if (i > 0) {
97+
String username = credentials.substring(0, i);
98+
String password = credentials.substring(i + 1);
8999

90-
if (credentials != null) {
91-
int space = credentials.indexOf(' ');
92-
if (space > 0) {
93-
String method = credentials.substring(0, space);
94-
if ("basic".equalsIgnoreCase(method)) {
95-
credentials = credentials.substring(space + 1);
96-
credentials = new String(Base64.getDecoder().decode(credentials), StandardCharsets.ISO_8859_1);
97-
int i = credentials.indexOf(':');
98-
if (i > 0) {
99-
String username = credentials.substring(0, i);
100-
String password = credentials.substring(i + 1);
101-
102-
UserIdentity user = login(username, password, request);
103-
if (user != null) {
104-
return new UserAuthentication(getAuthMethod(), user);
105-
}
106-
}
100+
return AuthenticationState.login(username, password, req, res);
107101
}
108102
}
109103
}
110-
111-
if (DeferredAuthentication.isDeferred(response))
112-
return Authentication.UNAUTHENTICATED;
113-
114-
response.setHeader(HttpHeader.PROXY_AUTHENTICATE.asString(), "basic realm=\"" + _loginService.getName() + '"');
115-
response.sendError(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
116-
return Authentication.SEND_CONTINUE;
117-
} catch (IOException e) {
118-
throw new ServerAuthException(e);
119104
}
120-
}
121105

122-
@Override
123-
public boolean secureResponse(ServletRequest req, ServletResponse res, boolean mandatory, User validatedUser) throws ServerAuthException {
124-
return true;
106+
res.getHeaders().add(HttpHeader.PROXY_AUTHENTICATE.asString(), "basic realm=\"" + _loginService.getName() + '"');
107+
res.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
108+
return AuthenticationState.CHALLENGE;
125109
}
126-
127110
}

0 commit comments

Comments
 (0)