View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.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       * @see org.eclipse.jetty.server.Authentication.Deferred#authenticate(ServletRequest)
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       * @see org.eclipse.jetty.server.Authentication.Deferred#authenticate(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
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      * @see org.eclipse.jetty.server.Authentication.Deferred#login(java.lang.String, java.lang.String)
115      */
116     @Override
117     public Authentication login(String username, Object password, ServletRequest request)
118     {
119         UserIdentity identity = _authenticator.login(username, password, request);
120         if (identity != null)
121         {
122             IdentityService identity_service = _authenticator.getLoginService().getIdentityService();
123             UserAuthentication authentication = new UserAuthentication("API",identity);
124             if (identity_service != null)
125                 _previousAssociation=identity_service.associate(identity);
126             return authentication;
127         }
128         return null;
129     }
130 
131     /* ------------------------------------------------------------ */
132     public Object getPreviousAssociation()
133     {
134         return _previousAssociation;
135     }
136 
137     /* ------------------------------------------------------------ */
138     /**
139      * @param response
140      * @return true if this response is from a deferred call to {@link #authenticate(ServletRequest)}
141      */
142     public static boolean isDeferred(HttpServletResponse response)
143     {
144         return response==__deferredResponse;
145     }
146 
147     /* ------------------------------------------------------------ */
148     /* ------------------------------------------------------------ */
149     /* ------------------------------------------------------------ */
150     final static HttpServletResponse __deferredResponse = new HttpServletResponse()
151     {
152         @Override
153         public void addCookie(Cookie cookie)
154         {
155         }
156 
157         @Override
158         public void addDateHeader(String name, long date)
159         {
160         }
161 
162         @Override
163         public void addHeader(String name, String value)
164         {
165         }
166 
167         @Override
168         public void addIntHeader(String name, int value)
169         {
170         }
171 
172         @Override
173         public boolean containsHeader(String name)
174         {
175             return false;
176         }
177 
178         @Override
179         public String encodeRedirectURL(String url)
180         {
181             return null;
182         }
183 
184         @Override
185         public String encodeRedirectUrl(String url)
186         {
187             return null;
188         }
189 
190         @Override
191         public String encodeURL(String url)
192         {
193             return null;
194         }
195 
196         @Override
197         public String encodeUrl(String url)
198         {
199             return null;
200         }
201 
202         @Override
203         public void sendError(int sc) throws IOException
204         {
205         }
206 
207         @Override
208         public void sendError(int sc, String msg) throws IOException
209         {
210         }
211 
212         @Override
213         public void sendRedirect(String location) throws IOException
214         {
215         }
216 
217         @Override
218         public void setDateHeader(String name, long date)
219         {
220         }
221 
222         @Override
223         public void setHeader(String name, String value)
224         {
225         }
226 
227         @Override
228         public void setIntHeader(String name, int value)
229         {
230         }
231 
232         @Override
233         public void setStatus(int sc)
234         {
235         }
236 
237         @Override
238         public void setStatus(int sc, String sm)
239         {
240         }
241 
242         @Override
243         public void flushBuffer() throws IOException
244         {
245         }
246 
247         @Override
248         public int getBufferSize()
249         {
250             return 1024;
251         }
252 
253         @Override
254         public String getCharacterEncoding()
255         {
256             return null;
257         }
258 
259         @Override
260         public String getContentType()
261         {
262             return null;
263         }
264 
265         @Override
266         public Locale getLocale()
267         {
268             return null;
269         }
270 
271         @Override
272         public ServletOutputStream getOutputStream() throws IOException
273         {
274             return __nullOut;
275         }
276 
277         @Override
278         public PrintWriter getWriter() throws IOException
279         {
280             return IO.getNullPrintWriter();
281         }
282 
283         @Override
284         public boolean isCommitted()
285         {
286             return true;
287         }
288 
289         @Override
290         public void reset()
291         {
292         }
293 
294         @Override
295         public void resetBuffer()
296         {
297         }
298 
299         @Override
300         public void setBufferSize(int size)
301         {
302         }
303 
304         @Override
305         public void setCharacterEncoding(String charset)
306         {
307         }
308 
309         @Override
310         public void setContentLength(int len)
311         {
312         }
313 
314         @Override
315         public void setContentType(String type)
316         {
317         }
318 
319         @Override
320         public void setLocale(Locale loc)
321         {
322         }
323 
324         @Override
325 	public Collection<String> getHeaderNames()
326 	{
327 	    return Collections.emptyList();
328 	}
329 
330 	@Override
331 	public String getHeader(String arg0)
332 	{
333 	    return null;
334 	}
335 
336 	@Override
337 	public Collection<String> getHeaders(String arg0)
338 	{
339             return Collections.emptyList();
340 	}
341 
342 	@Override
343 	public int getStatus()
344 	{
345 	    return 0;
346 	}
347 
348     };
349 
350     /* ------------------------------------------------------------ */
351     /* ------------------------------------------------------------ */
352     /* ------------------------------------------------------------ */
353     private static ServletOutputStream __nullOut = new ServletOutputStream()
354     {
355         public void write(int b) throws IOException
356         {
357         }
358 
359         public void print(String s) throws IOException
360         {
361         }
362 
363         public void println(String s) throws IOException
364         {
365         }
366     };
367 
368 
369 }