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