1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.eclipse.jetty.security.authentication;
21
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Locale;
27
28 import javax.servlet.ServletOutputStream;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.http.Cookie;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.eclipse.jetty.security.IdentityService;
35 import org.eclipse.jetty.security.LoginService;
36 import org.eclipse.jetty.security.ServerAuthException;
37 import org.eclipse.jetty.security.UserAuthentication;
38 import org.eclipse.jetty.server.Authentication;
39 import org.eclipse.jetty.server.UserIdentity;
40 import org.eclipse.jetty.util.IO;
41 import org.eclipse.jetty.util.log.Log;
42 import org.eclipse.jetty.util.log.Logger;
43
44 public class DeferredAuthentication implements Authentication.Deferred
45 {
46 private static final Logger LOG = Log.getLogger(DeferredAuthentication.class);
47 protected final LoginAuthenticator _authenticator;
48 private Object _previousAssociation;
49
50
51 public DeferredAuthentication(LoginAuthenticator authenticator)
52 {
53 if (authenticator == null)
54 throw new NullPointerException("No Authenticator");
55 this._authenticator = authenticator;
56 }
57
58
59
60
61
62 @Override
63 public Authentication authenticate(ServletRequest request)
64 {
65 try
66 {
67 Authentication authentication = _authenticator.validateRequest(request,__deferredResponse,true);
68
69 if (authentication!=null && (authentication instanceof Authentication.User) && !(authentication instanceof Authentication.ResponseSent))
70 {
71 LoginService login_service= _authenticator.getLoginService();
72 IdentityService identity_service=login_service.getIdentityService();
73
74 if (identity_service!=null)
75 _previousAssociation=identity_service.associate(((Authentication.User)authentication).getUserIdentity());
76
77 return authentication;
78 }
79 }
80 catch (ServerAuthException e)
81 {
82 LOG.debug(e);
83 }
84
85 return this;
86 }
87
88
89
90
91
92 @Override
93 public Authentication authenticate(ServletRequest request, ServletResponse response)
94 {
95 try
96 {
97 LoginService login_service= _authenticator.getLoginService();
98 IdentityService identity_service=login_service.getIdentityService();
99
100 Authentication authentication = _authenticator.validateRequest(request,response,true);
101 if (authentication instanceof Authentication.User && identity_service!=null)
102 _previousAssociation=identity_service.associate(((Authentication.User)authentication).getUserIdentity());
103 return authentication;
104 }
105 catch (ServerAuthException e)
106 {
107 LOG.debug(e);
108 }
109 return this;
110 }
111
112
113
114
115
116 @Override
117 public Authentication login(String username, Object password, ServletRequest request)
118 {
119 if (username == null)
120 return null;
121
122 UserIdentity identity = _authenticator.login(username, password, request);
123 if (identity != null)
124 {
125 IdentityService identity_service = _authenticator.getLoginService().getIdentityService();
126 UserAuthentication authentication = new UserAuthentication("API",identity);
127 if (identity_service != null)
128 _previousAssociation=identity_service.associate(identity);
129 return authentication;
130 }
131 return null;
132 }
133
134
135 public Object getPreviousAssociation()
136 {
137 return _previousAssociation;
138 }
139
140
141
142
143
144
145 public static boolean isDeferred(HttpServletResponse response)
146 {
147 return response==__deferredResponse;
148 }
149
150
151
152
153 final static HttpServletResponse __deferredResponse = new HttpServletResponse()
154 {
155 @Override
156 public void addCookie(Cookie cookie)
157 {
158 }
159
160 @Override
161 public void addDateHeader(String name, long date)
162 {
163 }
164
165 @Override
166 public void addHeader(String name, String value)
167 {
168 }
169
170 @Override
171 public void addIntHeader(String name, int value)
172 {
173 }
174
175 @Override
176 public boolean containsHeader(String name)
177 {
178 return false;
179 }
180
181 @Override
182 public String encodeRedirectURL(String url)
183 {
184 return null;
185 }
186
187 @Override
188 public String encodeRedirectUrl(String url)
189 {
190 return null;
191 }
192
193 @Override
194 public String encodeURL(String url)
195 {
196 return null;
197 }
198
199 @Override
200 public String encodeUrl(String url)
201 {
202 return null;
203 }
204
205 @Override
206 public void sendError(int sc) throws IOException
207 {
208 }
209
210 @Override
211 public void sendError(int sc, String msg) throws IOException
212 {
213 }
214
215 @Override
216 public void sendRedirect(String location) throws IOException
217 {
218 }
219
220 @Override
221 public void setDateHeader(String name, long date)
222 {
223 }
224
225 @Override
226 public void setHeader(String name, String value)
227 {
228 }
229
230 @Override
231 public void setIntHeader(String name, int value)
232 {
233 }
234
235 @Override
236 public void setStatus(int sc)
237 {
238 }
239
240 @Override
241 public void setStatus(int sc, String sm)
242 {
243 }
244
245 @Override
246 public void flushBuffer() throws IOException
247 {
248 }
249
250 @Override
251 public int getBufferSize()
252 {
253 return 1024;
254 }
255
256 @Override
257 public String getCharacterEncoding()
258 {
259 return null;
260 }
261
262 @Override
263 public String getContentType()
264 {
265 return null;
266 }
267
268 @Override
269 public Locale getLocale()
270 {
271 return null;
272 }
273
274 @Override
275 public ServletOutputStream getOutputStream() throws IOException
276 {
277 return __nullOut;
278 }
279
280 @Override
281 public PrintWriter getWriter() throws IOException
282 {
283 return IO.getNullPrintWriter();
284 }
285
286 @Override
287 public boolean isCommitted()
288 {
289 return true;
290 }
291
292 @Override
293 public void reset()
294 {
295 }
296
297 @Override
298 public void resetBuffer()
299 {
300 }
301
302 @Override
303 public void setBufferSize(int size)
304 {
305 }
306
307 @Override
308 public void setCharacterEncoding(String charset)
309 {
310 }
311
312 @Override
313 public void setContentLength(int len)
314 {
315 }
316
317 @Override
318 public void setContentType(String type)
319 {
320 }
321
322 @Override
323 public void setLocale(Locale loc)
324 {
325 }
326
327 @Override
328 public Collection<String> getHeaderNames()
329 {
330 return Collections.emptyList();
331 }
332
333 @Override
334 public String getHeader(String arg0)
335 {
336 return null;
337 }
338
339 @Override
340 public Collection<String> getHeaders(String arg0)
341 {
342 return Collections.emptyList();
343 }
344
345 @Override
346 public int getStatus()
347 {
348 return 0;
349 }
350
351 };
352
353
354
355
356 private static ServletOutputStream __nullOut = new ServletOutputStream()
357 {
358 public void write(int b) throws IOException
359 {
360 }
361
362 public void print(String s) throws IOException
363 {
364 }
365
366 public void println(String s) throws IOException
367 {
368 }
369 };
370
371
372 }