/root/projects/web-service-client/source/java/org/alfresco/webservice/util/AuthenticationUtils.java

https://github.com/alfresco-mirror/alfresco-mirror · Java · 211 lines · 124 code · 19 blank · 68 comment · 11 complexity · 46e28c1c2734bb95e24bef6be43c5904 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2010 Alfresco Software Limited.
  3. *
  4. * This file is part of Alfresco
  5. *
  6. * Alfresco is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Alfresco is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.alfresco.webservice.util;
  20. import java.io.ByteArrayInputStream;
  21. import java.io.IOException;
  22. import java.rmi.RemoteException;
  23. import javax.security.auth.callback.Callback;
  24. import javax.security.auth.callback.CallbackHandler;
  25. import javax.security.auth.callback.UnsupportedCallbackException;
  26. import org.alfresco.webservice.authentication.AuthenticationFault;
  27. import org.alfresco.webservice.authentication.AuthenticationResult;
  28. import org.apache.axis.EngineConfiguration;
  29. import org.apache.axis.configuration.FileProvider;
  30. import org.apache.ws.security.WSPasswordCallback;
  31. /**
  32. * @author Roy Wetherall
  33. */
  34. public class AuthenticationUtils implements CallbackHandler
  35. {
  36. /** WS security information */
  37. private static final String WS_SECURITY_INFO =
  38. "<deployment xmlns='http://xml.apache.org/axis/wsdd/' xmlns:java='http://xml.apache.org/axis/wsdd/providers/java'>" +
  39. " <transport name='http' pivot='java:org.apache.axis.transport.http.HTTPSender'/>" +
  40. " <globalConfiguration >" +
  41. " <requestFlow >" +
  42. " <handler type='java:org.apache.ws.axis.security.WSDoAllSender' >" +
  43. " <parameter name='action' value='UsernameToken Timestamp'/>" +
  44. " <parameter name='user' value='ticket'/>" +
  45. " <parameter name='passwordCallbackClass' value='org.alfresco.webservice.util.AuthenticationUtils'/>" +
  46. " <parameter name='passwordType' value='PasswordText'/>" +
  47. " </handler>" +
  48. " <handler name='cookieHandler' type='java:org.alfresco.webservice.util.CookieHandler' />" +
  49. " </requestFlow >" +
  50. " </globalConfiguration>" +
  51. "</deployment>";
  52. /** Thread local containing the current authentication details */
  53. private static ThreadLocal<AuthenticationDetails> authenticationDetails = new ThreadLocal<AuthenticationDetails>();
  54. /**
  55. * Start a session
  56. *
  57. * @param username
  58. * @param password
  59. * @throws AuthenticationFault
  60. */
  61. public static void startSession(String username, String password)
  62. throws AuthenticationFault
  63. {
  64. try
  65. {
  66. // Start the session
  67. AuthenticationResult result = WebServiceFactory.getAuthenticationService().startSession(username, password);
  68. // Store the ticket for use later
  69. authenticationDetails.set(new AuthenticationDetails(result.getUsername(), result.getTicket(), result.getSessionid()));
  70. }
  71. catch (RemoteException exception)
  72. {
  73. if (exception instanceof AuthenticationFault)
  74. {
  75. // Rethrow the authentication exception
  76. throw (AuthenticationFault)exception;
  77. }
  78. else
  79. {
  80. // Throw the exception as a wrapped runtime exception
  81. throw new WebServiceException("Error starting session.", exception);
  82. }
  83. }
  84. }
  85. /**
  86. * Start a session
  87. *
  88. * @param username
  89. * @param password
  90. * @param timeoutInterval timeout interval
  91. * @throws AuthenticationFault
  92. */
  93. public static void startSession(String username, String password, long timeoutInterval)
  94. throws AuthenticationFault
  95. {
  96. startSession(username, password);
  97. AuthenticationDetails ad = getAuthenticationDetails();
  98. ad.setTimeoutInterval(timeoutInterval);
  99. }
  100. public static void setAuthenticationDetails(AuthenticationDetails authenticationDetails)
  101. {
  102. AuthenticationUtils.authenticationDetails.set(authenticationDetails);
  103. }
  104. /**
  105. * @return if timeoutInterval is not set return false.
  106. */
  107. public static boolean isCurrentTicketTimedOut()
  108. {
  109. boolean to = getAuthenticationDetails().isTimedOut();
  110. if (to)
  111. endSession();
  112. return to;
  113. }
  114. /**
  115. * Ends the current session
  116. */
  117. public static void endSession()
  118. {
  119. AuthenticationDetails authenticationDetails = AuthenticationUtils.authenticationDetails.get();
  120. if (authenticationDetails != null)
  121. {
  122. try
  123. {
  124. WebServiceFactory.getAuthenticationService().endSession(authenticationDetails.getTicket());
  125. AuthenticationUtils.authenticationDetails.remove();
  126. }
  127. catch (RemoteException exception)
  128. {
  129. exception.printStackTrace();
  130. throw new WebServiceException("Error ending session.", exception);
  131. }
  132. }
  133. }
  134. /**
  135. * Get the ticket for the current authentication details on the current thread
  136. *
  137. * @return String the ticket
  138. */
  139. public static String getTicket()
  140. {
  141. String result = null;
  142. AuthenticationDetails authDetails = AuthenticationUtils.authenticationDetails.get();
  143. if (authDetails != null)
  144. {
  145. result = authDetails.getTicket();
  146. }
  147. return result;
  148. }
  149. /**
  150. * Get the authentication details for the current thread
  151. *
  152. * @return the authentication details
  153. */
  154. public static AuthenticationDetails getAuthenticationDetails()
  155. {
  156. return AuthenticationUtils.authenticationDetails.get();
  157. }
  158. /**
  159. * The implementation of the passwrod call back used by the WS Security
  160. *
  161. * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
  162. */
  163. public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
  164. {
  165. for (int i = 0; i < callbacks.length; i++)
  166. {
  167. if (callbacks[i] instanceof WSPasswordCallback)
  168. {
  169. WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];
  170. String ticket = AuthenticationUtils.getTicket();
  171. if (ticket == null)
  172. {
  173. throw new WebServiceException("Ticket could not be found when calling callback handler.");
  174. }
  175. pc.setPassword(ticket);
  176. }
  177. else
  178. {
  179. throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
  180. }
  181. }
  182. }
  183. /**
  184. * Gets the engine configuration used to create the web service references
  185. *
  186. * @return EngineConfiguration the engine configuration
  187. */
  188. public static EngineConfiguration getEngineConfiguration()
  189. {
  190. return new FileProvider(new ByteArrayInputStream(WS_SECURITY_INFO.getBytes()));
  191. }
  192. }