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