View Javadoc

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