1   /*
2    * $Id: ValidateUserTokenHandler.java 7976 2007-08-21 14:26:13Z dirk.olmes $ 
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.extras.wssecurity.validationHandler;
12  
13  import java.security.cert.X509Certificate;
14  import java.util.Vector;
15  
16  import org.apache.ws.security.WSConstants;
17  import org.apache.ws.security.WSSecurityEngineResult;
18  import org.apache.ws.security.WSUsernameTokenPrincipal;
19  import org.apache.ws.security.handler.WSHandlerConstants;
20  import org.apache.ws.security.handler.WSHandlerResult;
21  import org.codehaus.xfire.MessageContext;
22  import org.codehaus.xfire.handler.AbstractHandler;
23  import sun.security.x509.X500Name;
24  
25  public class ValidateUserTokenHandler extends AbstractHandler
26  {
27  
28      public void invoke (MessageContext context) throws Exception
29      {
30          Vector result = (Vector) context.getProperty(WSHandlerConstants.RECV_RESULTS);
31          for (int i = 0; i < result.size(); i++)
32          {
33              WSHandlerResult res = (WSHandlerResult) result.get(i);
34              for (int j = 0; j < res.getResults().size(); j++)
35              {
36                  WSSecurityEngineResult secRes = (WSSecurityEngineResult) res.getResults().get(j);
37                  int action = secRes.getAction();
38                  // USER TOKEN
39                  if ((action & WSConstants.UT) > 0)
40                  {
41                      WSUsernameTokenPrincipal principal = (WSUsernameTokenPrincipal) secRes
42                              .getPrincipal();
43                      // Set user property to user from UT to allow response encryption
44                      context.setProperty(WSHandlerConstants.ENCRYPTION_USER, principal.getName());
45                      System.out.print("User : " + principal.getName() + " password : "
46                                       + principal.getPassword() + "\n");
47                  }
48                  // SIGNATURE
49                  if ((action & WSConstants.SIGN) > 0)
50                  {
51                      X509Certificate cert = secRes.getCertificate();
52                      X500Name principal = (X500Name) secRes.getPrincipal();
53                      // Do something with cert
54                      System.out.print("Signature for : " + principal.getCommonName());
55                  }
56              }
57          }
58      }
59  }