View Javadoc

1   /*
2    * $Id: MuleSecurityManagerCallbackHandler.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.cxf.support;
12  
13  import org.mule.api.security.SecurityException;
14  import org.mule.api.security.SecurityProviderNotFoundException;
15  import org.mule.security.DefaultMuleAuthentication;
16  import org.mule.security.MuleCredentials;
17  
18  import java.io.IOException;
19  
20  import javax.security.auth.callback.Callback;
21  import javax.security.auth.callback.CallbackHandler;
22  import javax.security.auth.callback.UnsupportedCallbackException;
23  
24  import org.apache.ws.security.WSPasswordCallback;
25  import org.apache.ws.security.WSSecurityException;
26  
27  public class MuleSecurityManagerCallbackHandler implements CallbackHandler
28  {
29      private org.mule.api.security.SecurityManager securityManager;
30  
31      public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
32      {
33          WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
34          
35          if (pc.getUsage() == WSPasswordCallback.USERNAME_TOKEN
36                          || pc.getUsage() == WSPasswordCallback.USERNAME_TOKEN_UNKNOWN)
37          {
38              DefaultMuleAuthentication auth = new DefaultMuleAuthentication(
39                  new MuleCredentials(pc.getIdentifer(), pc.getPassword().toCharArray()));
40              
41              try
42              {
43                  securityManager.authenticate(auth);
44                  
45                  pc.setPassword(pc.getPassword());
46              }
47              catch (SecurityException e)
48              {
49                  throw new WSSecurityException(WSSecurityException.FAILED_AUTHENTICATION, null, null, e);
50              }
51              catch (SecurityProviderNotFoundException e)
52              {
53                  throw new WSSecurityException(WSSecurityException.FAILED_AUTHENTICATION, null, null, e);
54              }
55          }
56      }
57  
58      public void setSecurityManager(org.mule.api.security.SecurityManager securityManager)
59      {
60          this.securityManager = securityManager;
61      }
62  
63  }
64  
65