/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/DeferredAuthentication.java

https://github.com/dekellum/jetty · Java · 341 lines · 234 code · 59 blank · 48 comment · 13 complexity · 00e50433cee99174a06ab4ce39c54c1f MD5 · raw file

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