View Javadoc

1   /* Copyright 2004 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  import org.acegisecurity.userdetails.UserDetails;
20  
21  import java.io.IOException;
22  
23  import javax.security.auth.callback.Callback;
24  import javax.security.auth.callback.NameCallback;
25  import javax.security.auth.callback.UnsupportedCallbackException;
26  
27  
28  /***
29   * The most basic Callbacks to be handled when using a LoginContext from JAAS,
30   * are the NameCallback and PasswordCallback. The acegi security framework
31   * provides the JaasNameCallbackHandler specifically tailored to handling the
32   * NameCallback. <br>
33   *
34   * @author Ray Krueger
35   * @version $Id: JaasNameCallbackHandler.java,v 1.6 2005/11/29 13:10:08 benalex Exp $
36   *
37   * @see <a
38   *      href="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/callback/Callback.html">Callback</a>
39   * @see <a
40   *      href="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/callback/NameCallback.html">NameCallback</a>
41   */
42  public class JaasNameCallbackHandler
43      implements JaasAuthenticationCallbackHandler {
44      //~ Methods ================================================================
45  
46      /***
47       * If the callback passed to the 'handle' method is an instance of
48       * NameCallback, the JaasNameCallbackHandler will call,
49       * callback.setName(authentication.getPrincipal().toString()).
50       *
51       * @param callback
52       * @param authentication
53       *
54       * @throws IOException
55       * @throws UnsupportedCallbackException
56       */
57      public void handle(Callback callback, Authentication authentication)
58              throws IOException, UnsupportedCallbackException {
59  
60          if (callback instanceof NameCallback) {
61  
62              NameCallback ncb = (NameCallback) callback;
63              String username = "";
64  
65              Object principal = authentication.getPrincipal();
66              if (principal instanceof UserDetails) {
67                  username = ((UserDetails) principal).getUsername();
68              } else {
69                  username = principal.toString();
70              }
71  
72              ncb.setName(username);
73          }
74      }
75  }