View Javadoc

1   // ========================================================================
2   // Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
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.Locale;
20  
21  import javax.servlet.ServletOutputStream;
22  import javax.servlet.ServletRequest;
23  import javax.servlet.ServletResponse;
24  import javax.servlet.http.Cookie;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.eclipse.jetty.security.Authenticator;
28  import org.eclipse.jetty.security.IdentityService;
29  import org.eclipse.jetty.security.ServerAuthException;
30  import org.eclipse.jetty.server.Authentication;
31  import org.eclipse.jetty.util.IO;
32  import org.eclipse.jetty.util.log.Log;
33  
34  public class DeferredAuthentication implements Authentication.Deferred
35  {
36      protected final Authenticator _authenticator;
37  
38      private IdentityService _identityService;
39      private Object _previousAssociation;
40  
41      public DeferredAuthentication(Authenticator authenticator)
42      {
43          if (authenticator == null)
44              throw new NullPointerException("No Authenticator");
45          this._authenticator = authenticator;
46      }
47      
48      /* ------------------------------------------------------------ */
49      /** Get the identityService.
50       * @return the identityService
51       */
52      public IdentityService getIdentityService()
53      {
54          return _identityService;
55      }
56  
57      /* ------------------------------------------------------------ */
58      /** Set the identityService.
59       * @param identityService the identityService to set
60       */
61      public void setIdentityService(IdentityService identityService)
62      {
63          _identityService = identityService;
64      }
65  
66      /* ------------------------------------------------------------ */
67      /**
68       * @see org.eclipse.jetty.server.Authentication.Deferred#authenticate(ServletRequest)
69       */
70      public Authentication authenticate(ServletRequest request)
71      {
72          try
73          {
74              Authentication authentication = _authenticator.validateRequest(request,__deferredResponse,true);
75              
76              if (authentication!=null && (authentication instanceof Authentication.User) && !(authentication instanceof Authentication.ResponseSent))
77              {
78                  if (_identityService!=null)
79                      _previousAssociation=_identityService.associate(((Authentication.User)authentication).getUserIdentity());
80                  return authentication;
81              }
82          }
83          catch (ServerAuthException e)
84          {
85              Log.debug(e);
86          }
87          return Authentication.UNAUTHENTICATED;
88      }
89      
90      /* ------------------------------------------------------------ */
91      /**
92       * @see org.eclipse.jetty.server.Authentication.Deferred#authenticate(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
93       */
94      public Authentication authenticate(ServletRequest request, ServletResponse response)
95      {
96          try
97          {
98              Authentication authentication = _authenticator.validateRequest(request,response,true);
99              if (authentication instanceof Authentication.User && _identityService!=null)
100                 _previousAssociation=_identityService.associate(((Authentication.User)authentication).getUserIdentity());
101             return authentication;
102         }
103         catch (ServerAuthException e)
104         {
105             Log.debug(e);
106         }
107         return Authentication.UNAUTHENTICATED;
108     }
109 
110     /* ------------------------------------------------------------ */
111     /**
112      * @see org.eclipse.jetty.server.Authentication.Deferred#login(java.lang.String, java.lang.String)
113      */
114     public Authentication login(String username, String password)
115     {
116         return null; // TODO implement
117     }
118 
119     /* ------------------------------------------------------------ */
120     public Object getPreviousAssociation()
121     {
122         return _previousAssociation;
123     }
124 
125     /* ------------------------------------------------------------ */
126     /**
127      * @param response
128      * @return true if this response is from a deferred call to {@link #authenticate(ServletRequest)}
129      */
130     public boolean isDeferred(HttpServletResponse response)
131     {
132         return response==__deferredResponse;
133     }
134     
135     /* ------------------------------------------------------------ */
136     /* ------------------------------------------------------------ */
137     /* ------------------------------------------------------------ */
138     static HttpServletResponse __deferredResponse = new HttpServletResponse()
139     {
140         public void addCookie(Cookie cookie)
141         {
142         }
143 
144         public void addDateHeader(String name, long date)
145         {
146         }
147 
148         public void addHeader(String name, String value)
149         {
150         }
151 
152         public void addIntHeader(String name, int value)
153         {
154         }
155 
156         public boolean containsHeader(String name)
157         {
158             return false;
159         }
160 
161         public String encodeRedirectURL(String url)
162         {
163             return null;
164         }
165 
166         public String encodeRedirectUrl(String url)
167         {
168             return null;
169         }
170 
171         public String encodeURL(String url)
172         {
173             return null;
174         }
175 
176         public String encodeUrl(String url)
177         {
178             return null;
179         }
180 
181         public void sendError(int sc) throws IOException
182         {
183         }
184 
185         public void sendError(int sc, String msg) throws IOException
186         {
187         }
188 
189         public void sendRedirect(String location) throws IOException
190         {
191         }
192 
193         public void setDateHeader(String name, long date)
194         {
195         }
196 
197         public void setHeader(String name, String value)
198         {
199         }
200 
201         public void setIntHeader(String name, int value)
202         {
203         }
204 
205         public void setStatus(int sc)
206         {
207         }
208 
209         public void setStatus(int sc, String sm)
210         {
211         }
212 
213         public void flushBuffer() throws IOException
214         {
215         }
216 
217         public int getBufferSize()
218         {
219             return 1024;
220         }
221 
222         public String getCharacterEncoding()
223         {
224             return null;
225         }
226 
227         public String getContentType()
228         {
229             return null;
230         }
231 
232         public Locale getLocale()
233         {
234             return null;
235         }
236 
237         public ServletOutputStream getOutputStream() throws IOException
238         {
239             return __nullOut;
240         }
241 
242         public PrintWriter getWriter() throws IOException
243         {
244             return IO.getNullPrintWriter();
245         }
246 
247         public boolean isCommitted()
248         {
249             return true;
250         }
251 
252         public void reset()
253         {
254         }
255 
256         public void resetBuffer()
257         {
258         }
259 
260         public void setBufferSize(int size)
261         {
262         }
263 
264         public void setCharacterEncoding(String charset)
265         {
266         }
267 
268         public void setContentLength(int len)
269         {
270         }
271 
272         public void setContentType(String type)
273         {
274         }
275 
276         public void setLocale(Locale loc)
277         {
278         }
279 
280     };
281 
282     /* ------------------------------------------------------------ */
283     /* ------------------------------------------------------------ */
284     /* ------------------------------------------------------------ */
285     private static ServletOutputStream __nullOut = new ServletOutputStream()
286     {
287         public void write(int b) throws IOException
288         {
289         }
290 
291         public void print(String s) throws IOException
292         {
293         }
294 
295         public void println(String s) throws IOException
296         {
297         }
298     };
299 
300     
301 }