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