Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2c1dd62614a73b09451b0ba37bf6b7801d2bc31a (plain) (blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*******************************************************************************
 * Copyright (c) Jan. 26, 2019 Liferay, Inc.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Liferay, Inc. - tests
 ******************************************************************************/

package org.eclipse.equinox.http.servlet.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME;
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH;
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT;
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.equinox.http.servlet.testbase.BaseTest;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.service.http.context.ServletContextHelper;

public class AuthenticationTest extends BaseTest {

	@Test
	public void test_forwardSecurity() throws Exception {
		final List<String> callStack = new ArrayList<>();
		setup(callStack);

		// request without auth -> no servlet invoked
		requestAdvisor.request("context1/servlet?forward=true");
		assertTrue(callStack.isEmpty());

		// request with auth and forward -> servlet invoked
		requestAdvisor.request("context1/servlet?forward=true&auth=true");
		assertEquals(6, callStack.size());
		assertEquals(Arrays.asList("handle1", "servlet/context1", "handle1", "servlet/context1", "finish1", "finish1"), callStack);
		callStack.clear();
	}

	@Test
	public void test_handleFinishSecurity() throws Exception {
		final List<String> callStack = new ArrayList<>();
		setup(callStack);

		// request without auth -> no servlet invoked
		requestAdvisor.request("context1/servlet");
		assertTrue(callStack.isEmpty());

		// request with auth -> servlet invoked
		requestAdvisor.request("context1/servlet?auth=true");
		assertEquals(3, callStack.size());
		assertEquals(Arrays.asList("handle1", "servlet/context1", "finish1"), callStack);
		callStack.clear();

		// request to context2, no auth required
		requestAdvisor.request("context2/servlet");
		assertEquals(3, callStack.size());
		assertEquals(Arrays.asList("handle2", "servlet/context2", "finish2"), callStack);
	}

	private static final String	AUTH_PAR	= "auth";

	private static final String	REC_PAR		= "rec";

	private void setup(final List<String> callStack) throws Exception {
		final BundleContext context = getBundleContext();

		// setup context 1
		final Dictionary<String,Object> ctx1Props = new Hashtable<String,Object>();
		ctx1Props.put(HTTP_WHITEBOARD_CONTEXT_NAME, "context1");
		ctx1Props.put(HTTP_WHITEBOARD_CONTEXT_PATH, "/context1");
		registrations.add(context.registerService(
			ServletContextHelper.class,
			new ServletContextHelper() {

				@Override
				public boolean handleSecurity(final HttpServletRequest request, final HttpServletResponse response)
					throws IOException {

					if (request.getParameter(AUTH_PAR) != null) {
						callStack.add("handle1");
						return true;
					}
					return false;
				}

				@Override
				public void finishSecurity(final HttpServletRequest request, final HttpServletResponse response) {
					callStack.add("finish1");
				}

			},
			ctx1Props)
		);

		// setup context 2
		final Dictionary<String,Object> ctx2Props = new Hashtable<String,Object>();
		ctx2Props.put(HTTP_WHITEBOARD_CONTEXT_NAME, "context2");
		ctx2Props.put(HTTP_WHITEBOARD_CONTEXT_PATH, "/context2");
		registrations.add(context.registerService(
			ServletContextHelper.class,
			new ServletContextHelper() {

				@Override
				public boolean handleSecurity(final HttpServletRequest request, final HttpServletResponse response)
					throws IOException {

					callStack.add("handle2");
					return true;
				}

				@Override
				public void finishSecurity(final HttpServletRequest request, final HttpServletResponse response) {
					callStack.add("finish2");
				}

			},
			ctx2Props)
		);

		// servlet for both contexts
		@SuppressWarnings("serial")
		class AServlet extends HttpServlet {

			@Override
			protected void service(HttpServletRequest request,
					HttpServletResponse response)
				throws ServletException, IOException {

				callStack.add("servlet" + request.getContextPath());

				if (request.getContextPath().equals("/context1")
						&& request.getAttribute(REC_PAR) == null) {
					if (request.getParameter("forward") != null) {
						request.setAttribute(REC_PAR, "true");
						request.getRequestDispatcher("/servlet")
									.forward(request, response);
						return;
					} else if (request.getParameter("include") != null) {
						request.setAttribute(REC_PAR, "true");
						request.getRequestDispatcher("/servlet")
									.include(request, response);
					} else if (request.getParameter("throw") != null) {
						callStack.add("throw");
						throw new ServletException("throw");
					}
				}
				response.setStatus(200);
			}

		};

		final Dictionary<String,Object> servletProps = new Hashtable<String,Object>();

		servletProps.put(HTTP_WHITEBOARD_SERVLET_PATTERN, new String[] {"/servlet"});
		servletProps.put(HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=context1)");
		registrations.add(context.registerService(Servlet.class, new AServlet(), servletProps));

		servletProps.put(HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=context2)");
		registrations.add(context.registerService(Servlet.class, new AServlet(), servletProps));
	}

}

Back to the top