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