2 // ========================================================================
3 // Copyright (c) 1995-2016 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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
20 package org.eclipse.jetty.security.authentication;
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;
28 import javax.servlet.ServletOutputStream;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.WriteListener;
32 import javax.servlet.http.Cookie;
33 import javax.servlet.http.HttpServletResponse;
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;
45 public class DeferredAuthentication implements Authentication.Deferred
47 private static final Logger LOG = Log.getLogger(DeferredAuthentication.class);
48 protected final LoginAuthenticator _authenticator;
49 private Object _previousAssociation;
51 /* ------------------------------------------------------------ */
52 public DeferredAuthentication(LoginAuthenticator authenticator)
54 if (authenticator == null)
55 throw new NullPointerException("No Authenticator");
56 this._authenticator = authenticator;
59 /* ------------------------------------------------------------ */
61 * @see org.eclipse.jetty.server.Authentication.Deferred#authenticate(ServletRequest)
64 public Authentication authenticate(ServletRequest request)
68 Authentication authentication = _authenticator.validateRequest(request,__deferredResponse,true);
70 if (authentication!=null && (authentication instanceof Authentication.User) && !(authentication instanceof Authentication.ResponseSent))
72 LoginService login_service= _authenticator.getLoginService();
73 IdentityService identity_service=login_service.getIdentityService();
75 if (identity_service!=null)
76 _previousAssociation=identity_service.associate(((Authentication.User)authentication).getUserIdentity());
78 return authentication;
81 catch (ServerAuthException e)
89 /* ------------------------------------------------------------ */
91 * @see org.eclipse.jetty.server.Authentication.Deferred#authenticate(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
94 public Authentication authenticate(ServletRequest request, ServletResponse response)
98 LoginService login_service= _authenticator.getLoginService();
99 IdentityService identity_service=login_service.getIdentityService();
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;
106 catch (ServerAuthException e)
113 /* ------------------------------------------------------------ */
115 * @see org.eclipse.jetty.server.Authentication.Deferred#login(String, Object, ServletRequest)
118 public Authentication login(String username, Object password, ServletRequest request)
120 if (username == null)
123 UserIdentity identity = _authenticator.login(username, password, request);
124 if (identity != null)
126 IdentityService identity_service = _authenticator.getLoginService().getIdentityService();
127 UserAuthentication authentication = new UserAuthentication("API",identity);
128 if (identity_service != null)
129 _previousAssociation=identity_service.associate(identity);
130 return authentication;
135 /* ------------------------------------------------------------ */
136 public Object getPreviousAssociation()
138 return _previousAssociation;
141 /* ------------------------------------------------------------ */
144 * @return true if this response is from a deferred call to {@link #authenticate(ServletRequest)}
146 public static boolean isDeferred(HttpServletResponse response)
148 return response==__deferredResponse;
151 /* ------------------------------------------------------------ */
152 /* ------------------------------------------------------------ */
153 /* ------------------------------------------------------------ */
154 final static HttpServletResponse __deferredResponse = new HttpServletResponse()
157 public void addCookie(Cookie cookie)
162 public void addDateHeader(String name, long date)
167 public void addHeader(String name, String value)
172 public void addIntHeader(String name, int value)
177 public boolean containsHeader(String name)
183 public String encodeRedirectURL(String url)
189 public String encodeRedirectUrl(String url)
195 public String encodeURL(String url)
201 public String encodeUrl(String url)
207 public void sendError(int sc) throws IOException
212 public void sendError(int sc, String msg) throws IOException
217 public void sendRedirect(String location) throws IOException
222 public void setDateHeader(String name, long date)
227 public void setHeader(String name, String value)
232 public void setIntHeader(String name, int value)
237 public void setStatus(int sc)
242 public void setStatus(int sc, String sm)
247 public void flushBuffer() throws IOException
252 public int getBufferSize()
258 public String getCharacterEncoding()
264 public String getContentType()
270 public Locale getLocale()
276 public ServletOutputStream getOutputStream() throws IOException
282 public PrintWriter getWriter() throws IOException
284 return IO.getNullPrintWriter();
288 public boolean isCommitted()
299 public void resetBuffer()
304 public void setBufferSize(int size)
309 public void setCharacterEncoding(String charset)
314 public void setContentLength(int len)
318 public void setContentLengthLong(long len)
324 public void setContentType(String type)
329 public void setLocale(Locale loc)
334 public Collection<String> getHeaderNames()
336 return Collections.emptyList();
340 public String getHeader(String arg0)
346 public Collection<String> getHeaders(String arg0)
348 return Collections.emptyList();
352 public int getStatus()
360 /* ------------------------------------------------------------ */
361 /* ------------------------------------------------------------ */
362 /* ------------------------------------------------------------ */
363 private static ServletOutputStream __nullOut = new ServletOutputStream()
366 public void write(int b) throws IOException
371 public void print(String s) throws IOException
376 public void println(String s) throws IOException
382 public void setWriteListener(WriteListener writeListener)
388 public boolean isReady()