01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.security.auth.callback;
23:
24: import javax.security.auth.callback.Callback;
25: import javax.security.auth.callback.CallbackHandler;
26: import javax.security.auth.callback.NameCallback;
27: import javax.security.auth.callback.PasswordCallback;
28: import javax.security.auth.callback.UnsupportedCallbackException;
29:
30: /** A simple implementation of CallbackHandler that sets a username and
31: password in the handle(Callback[]) method to that passed in to
32: the constructor. This is suitable for environments that need non-interactive
33: JAAS logins.
34:
35: @see javax.security.auth.callback.CallbackHandler
36: @see #handle(Callback[])
37:
38: @author Scott.Stark@jboss.org
39: @version $Revision: 57203 $
40: */
41: public class UsernamePasswordHandler implements CallbackHandler {
42: private transient String username;
43: private transient char[] password;
44: private transient Object credential;
45:
46: /** Initialize the UsernamePasswordHandler with the usernmae
47: and password to use.
48: */
49: public UsernamePasswordHandler(String username, char[] password) {
50: this .username = username;
51: this .password = password;
52: this .credential = password;
53: }
54:
55: public UsernamePasswordHandler(String username, Object credential) {
56: this .username = username;
57: this .credential = credential;
58: }
59:
60: /** Sets any NameCallback name property to the instance username,
61: sets any PasswordCallback password property to the instance, and any
62: password.
63: @exception UnsupportedCallbackException, thrown if any callback of
64: type other than NameCallback or PasswordCallback are seen.
65: */
66: public void handle(Callback[] callbacks)
67: throws UnsupportedCallbackException {
68: for (int i = 0; i < callbacks.length; i++) {
69: Callback c = callbacks[i];
70: if (c instanceof NameCallback) {
71: NameCallback nc = (NameCallback) c;
72: nc.setName(username);
73: } else if (c instanceof PasswordCallback) {
74: PasswordCallback pc = (PasswordCallback) c;
75: if (password == null) {
76: // We were given an opaque Object credential but a char[] is requested?
77: if (credential != null) {
78: String tmp = credential.toString();
79: password = tmp.toCharArray();
80: }
81: }
82: pc.setPassword(password);
83: } else if (c instanceof ObjectCallback) {
84: ObjectCallback oc = (ObjectCallback) c;
85: oc.setCredential(credential);
86: } else {
87: throw new UnsupportedCallbackException(callbacks[i],
88: "Unrecognized Callback");
89: }
90: }
91: }
92: }
|