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