View Javadoc

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