/
UnauthenticatedTest.java
150 lines (128 loc) · 5.95 KB
/
UnauthenticatedTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.security;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.security.authentication.DeferredAuthentication;
import org.eclipse.jetty.security.authentication.LoginAuthenticator;
import org.eclipse.jetty.server.Authentication;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.security.Constraint;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
public class UnauthenticatedTest
{
private LocalConnector connector;
private TestAuthenticator authenticator;
@BeforeEach
public void beforeEach() throws Exception
{
Server server = new Server();
connector = new LocalConnector(server);
server.addConnector(connector);
// Authenticator that always returns UNAUTHENTICATED.
authenticator = new TestAuthenticator();
// Add a security handler which requires paths under /requireAuth to be authenticated.
ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
Constraint requireAuthentication = new Constraint();
requireAuthentication.setRoles(new String[]{"**"});
requireAuthentication.setAuthenticate(true);
ConstraintMapping constraintMapping = new ConstraintMapping();
constraintMapping.setPathSpec("/requireAuth/*");
constraintMapping.setConstraint(requireAuthentication);
securityHandler.addConstraintMapping(constraintMapping);
securityHandler.setAuthenticator(authenticator);
securityHandler.setHandler(new AbstractHandler()
{
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
baseRequest.setHandled(true);
response.setStatus(HttpStatus.OK_200);
response.getWriter().println("authentication: " + baseRequest.getAuthentication());
}
});
server.setHandler(securityHandler);
server.start();
}
@Test
public void testUnauthenticated() throws Exception
{
TestAuthenticator.AUTHENTICATION.set(Authentication.UNAUTHENTICATED);
// Request to URI which doesn't require authentication can get through even though auth is UNAUTHENTICATED.
String response = connector.getResponse("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, containsString("authentication: UNAUTHENTICATED"));
// This URI requires just that the request is authenticated.
response = connector.getResponse("GET /requireAuth/test HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertThat(response, containsString("HTTP/1.1 401 Unauthorized"));
}
@Test
public void testDeferredAuth() throws Exception
{
TestAuthenticator.AUTHENTICATION.set(new DeferredAuthentication(authenticator));
// Request to URI which doesn't require authentication can get through even though auth is UNAUTHENTICATED.
String response = connector.getResponse("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, containsString("DeferredAuthentication"));
// This URI requires just that the request is authenticated. But DeferredAuthentication can bypass this.
response = connector.getResponse("GET /requireAuth/test HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, containsString("DeferredAuthentication"));
}
public static class TestAuthenticator extends LoginAuthenticator
{
static AtomicReference<Authentication> AUTHENTICATION = new AtomicReference<>();
@Override
public void setConfiguration(AuthConfiguration configuration)
{
// Do nothing.
}
@Override
public String getAuthMethod()
{
return this.getClass().getSimpleName();
}
@Override
public void prepareRequest(ServletRequest request)
{
// Do nothing.
}
@Override
public Authentication validateRequest(ServletRequest request, ServletResponse response, boolean mandatory) throws ServerAuthException
{
return AUTHENTICATION.get();
}
@Override
public boolean secureResponse(ServletRequest request, ServletResponse response, boolean mandatory, Authentication.User validatedUser) throws ServerAuthException
{
return true;
}
}
}