Coverage Report - org.acegisecurity.providers.jaas.JaasNameCallbackHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
JaasNameCallbackHandler
90% 
100% 
3
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 2  
  *
 3  
  * Licensed under the Apache License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  *     http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 
 16  
 package org.acegisecurity.providers.jaas;
 17  
 
 18  
 import org.acegisecurity.Authentication;
 19  
 
 20  
 import org.acegisecurity.userdetails.UserDetails;
 21  
 
 22  
 import java.io.IOException;
 23  
 
 24  
 import javax.security.auth.callback.Callback;
 25  
 import javax.security.auth.callback.NameCallback;
 26  
 import javax.security.auth.callback.UnsupportedCallbackException;
 27  
 
 28  
 
 29  
 /**
 30  
  * The most basic Callbacks to be handled when using a LoginContext from JAAS, are the NameCallback and
 31  
  * PasswordCallback. The acegi security framework provides the JaasNameCallbackHandler specifically tailored to
 32  
  * handling the NameCallback. <br>
 33  
  *
 34  
  * @author Ray Krueger
 35  
  * @version $Id: JaasNameCallbackHandler.java 1496 2006-05-23 13:38:33Z benalex $
 36  
  *
 37  
  * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/callback/Callback.html">Callback</a>
 38  
  * @see <a
 39  
  *      href="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/callback/NameCallback.html">NameCallback</a>
 40  
  */
 41  12
 public class JaasNameCallbackHandler implements JaasAuthenticationCallbackHandler {
 42  
     //~ Methods ========================================================================================================
 43  
 
 44  
     /**
 45  
      * If the callback passed to the 'handle' method is an instance of NameCallback, the
 46  
      * JaasNameCallbackHandler will call, callback.setName(authentication.getPrincipal().toString()).
 47  
      *
 48  
      * @param callback
 49  
      * @param authentication
 50  
      *
 51  
      * @throws IOException
 52  
      * @throws UnsupportedCallbackException
 53  
      */
 54  
     public void handle(Callback callback, Authentication authentication)
 55  
         throws IOException, UnsupportedCallbackException {
 56  18
         if (callback instanceof NameCallback) {
 57  6
             NameCallback ncb = (NameCallback) callback;
 58  6
             String username = "";
 59  
 
 60  6
             Object principal = authentication.getPrincipal();
 61  
 
 62  6
             if (principal instanceof UserDetails) {
 63  0
                 username = ((UserDetails) principal).getUsername();
 64  
             } else {
 65  6
                 username = principal.toString();
 66  
             }
 67  
 
 68  6
             ncb.setName(username);
 69  
         }
 70  18
     }
 71  
 }