Coverage Report - org.mule.module.jaas.MuleCallbackHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleCallbackHandler
94%
15/16
83%
5/6
3
 
 1  
 /*
 2  
  * $Id: MuleCallbackHandler.java 10789 2008-02-12 20:04:43Z dfeist $
 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.module.jaas;
 12  
 
 13  
 import org.mule.api.security.Authentication;
 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 Authentication authentication;
 26  
     private String username;
 27  
     private String password;
 28  
 
 29  
     /**
 30  
      * @param authentication
 31  
      */
 32  
     public MuleCallbackHandler(Authentication authentication)
 33  20
     {
 34  20
         this.authentication = authentication;
 35  20
         this.username = (String) this.authentication.getPrincipal();
 36  20
         this.password = (String) this.authentication.getCredentials();
 37  20
     }
 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  60
         for (int i = 0; i < callbacks.length; i++)
 51  
         {
 52  40
             if (callbacks[i] instanceof NameCallback)
 53  
             {
 54  20
                 NameCallback nameCb = (NameCallback) callbacks[i];
 55  20
                 nameCb.setName(username);
 56  20
             }
 57  20
             else if (callbacks[i] instanceof PasswordCallback)
 58  
             {
 59  20
                 PasswordCallback passCb = (PasswordCallback) callbacks[i];
 60  20
                 passCb.setPassword(password.toCharArray());
 61  20
             }
 62  
             else
 63  
             {
 64  0
                 throw (new UnsupportedCallbackException(callbacks[i], "Callback class not supported"));
 65  
             }
 66  
         }
 67  20
     }
 68  
 }