View Javadoc

1   /*
2    * $Id: MuleCallbackHandler.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.jaas;
12  
13  import org.mule.umo.security.UMOAuthentication;
14  
15  import java.io.IOException;
16  
17  import javax.security.auth.callback.Callback;
18  import javax.security.auth.callback.CallbackHandler;
19  import javax.security.auth.callback.NameCallback;
20  import javax.security.auth.callback.PasswordCallback;
21  import javax.security.auth.callback.UnsupportedCallbackException;
22  
23  /**
24   * @author Marie.Rizzo
25   */
26  public class MuleCallbackHandler implements CallbackHandler
27  {
28      private UMOAuthentication authentication;
29      private String username;
30      private String password;
31  
32      /**
33       * @param authentication
34       */
35      public MuleCallbackHandler(UMOAuthentication authentication)
36      {
37          this.authentication = authentication;
38          this.username = (String)this.authentication.getPrincipal();
39          this.password = (String)this.authentication.getCredentials();
40      }
41  
42      /**
43       * The handle() method handles the callbacks to be passed to the Jaas security.
44       * It makes use of two types of callbacks: the NameCallback and the
45       * PasswordCallback.
46       * 
47       * @param callbacks
48       * @throws IOException
49       * @throws UnsupportedCallbackException
50       */
51      public final void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
52      {
53          for (int i = 0; i < callbacks.length; i++)
54          {
55              if (callbacks[i] instanceof NameCallback)
56              {
57                  NameCallback nameCb = (NameCallback)callbacks[i];
58                  nameCb.setName(username);
59              }
60              else if (callbacks[i] instanceof PasswordCallback)
61              {
62                  PasswordCallback passCb = (PasswordCallback)callbacks[i];
63                  passCb.setPassword(password.toCharArray());
64              }
65              else
66              {
67                  throw (new UnsupportedCallbackException(callbacks[i], "Callback class not supported"));
68              }
69          }
70      }
71  }