1 17 package org.alfresco.webservice.util; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.IOException ; 21 import java.rmi.RemoteException ; 22 23 import javax.security.auth.callback.Callback ; 24 import javax.security.auth.callback.CallbackHandler ; 25 import javax.security.auth.callback.UnsupportedCallbackException ; 26 27 import org.alfresco.webservice.authentication.AuthenticationFault; 28 import org.alfresco.webservice.authentication.AuthenticationResult; 29 import org.apache.axis.EngineConfiguration; 30 import org.apache.axis.configuration.FileProvider; 31 import org.apache.ws.security.WSPasswordCallback; 32 33 36 public class AuthenticationUtils implements CallbackHandler 37 { 38 39 private static final String WS_SECURITY_INFO = 40 "<deployment xmlns='http://xml.apache.org/axis/wsdd/' xmlns:java='http://xml.apache.org/axis/wsdd/providers/java'>" + 41 " <transport name='http' pivot='java:org.apache.axis.transport.http.HTTPSender'/>" + 42 " <globalConfiguration >" + 43 " <requestFlow >" + 44 " <handler type='java:org.apache.ws.axis.security.WSDoAllSender' >" + 45 " <parameter name='action' value='UsernameToken Timestamp'/>" + 46 " <parameter name='user' value='ticket'/>" + 47 " <parameter name='passwordCallbackClass' value='org.alfresco.webservice.util.AuthenticationUtils'/>" + 48 " <parameter name='passwordType' value='PasswordText'/>" + 49 " </handler>" + 50 " </requestFlow >" + 51 " </globalConfiguration>" + 52 "</deployment>"; 53 54 55 private static ThreadLocal <String > currentTicket = new ThreadLocal <String >(); 56 57 64 public static void startSession(String username, String password) 65 throws AuthenticationFault 66 { 67 try 68 { 69 AuthenticationResult result = WebServiceFactory.getAuthenticationService().startSession(username, password); 71 72 currentTicket.set(result.getTicket()); 74 } 75 catch (RemoteException exception) 76 { 77 if (exception instanceof AuthenticationFault) 78 { 79 throw (AuthenticationFault)exception; 81 } 82 else 83 { 84 throw new WebServiceException("Error starting session.", exception); 86 } 87 } 88 } 89 90 93 public static void endSession() 94 { 95 String ticket = currentTicket.get(); 96 if (ticket != null) 97 { 98 try 99 { 100 WebServiceFactory.getAuthenticationService().endSession(ticket); 101 currentTicket.remove(); 102 } 103 catch (RemoteException exception) 104 { 105 exception.printStackTrace(); 106 throw new WebServiceException("Error ending session.", exception); 107 } 108 } 109 } 110 111 public static String getCurrentTicket() 112 { 113 return currentTicket.get(); 114 } 115 116 121 public void handle(Callback [] callbacks) throws IOException , UnsupportedCallbackException 122 { 123 for (int i = 0; i < callbacks.length; i++) 124 { 125 if (callbacks[i] instanceof WSPasswordCallback) 126 { 127 WSPasswordCallback pc = (WSPasswordCallback)callbacks[i]; 128 pc.setPassword(currentTicket.get()); 129 } 130 else 131 { 132 throw new UnsupportedCallbackException (callbacks[i], "Unrecognized Callback"); 133 } 134 } 135 } 136 137 142 public static EngineConfiguration getEngineConfiguration() 143 { 144 return new FileProvider(new ByteArrayInputStream (WS_SECURITY_INFO.getBytes())); 145 } 146 } 147 | Popular Tags |