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