01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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: * The most basic Callbacks to be handled when using a LoginContext from JAAS, are the NameCallback and
30: * PasswordCallback. The acegi security framework provides the JaasNameCallbackHandler specifically tailored to
31: * handling the NameCallback. <br>
32: *
33: * @author Ray Krueger
34: * @version $Id: JaasNameCallbackHandler.java 1496 2006-05-23 13:38:33Z benalex $
35: *
36: * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/callback/Callback.html">Callback</a>
37: * @see <a
38: * href="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/callback/NameCallback.html">NameCallback</a>
39: */
40: public class JaasNameCallbackHandler implements
41: 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: if (callback instanceof NameCallback) {
57: NameCallback ncb = (NameCallback) callback;
58: String username = "";
59:
60: Object principal = authentication.getPrincipal();
61:
62: if (principal instanceof UserDetails) {
63: username = ((UserDetails) principal).getUsername();
64: } else {
65: username = principal.toString();
66: }
67:
68: ncb.setName(username);
69: }
70: }
71: }
|