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.test.cmp2.audit.interfaces;
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: import javax.security.auth.login.LoginContext;
30: import javax.security.auth.login.LoginException;
31:
32: /**
33: * A callback handler for login with user and password.
34: *
35: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
36: * @version $Revision: 57211 $
37: */
38: public class ApplicationCallbackHandler implements CallbackHandler {
39: // Attributes ----------------------------------------------------
40:
41: private String user;
42: private char[] password;
43:
44: // Constructor ---------------------------------------------------
45:
46: private ApplicationCallbackHandler(String user, String password) {
47: this .user = user;
48: this .password = password.toCharArray();
49: }
50:
51: // Public --------------------------------------------------------
52:
53: public void handle(Callback[] callbacks)
54: throws UnsupportedCallbackException {
55: for (int i = 0; i < callbacks.length; i++) {
56: if (callbacks[i] instanceof NameCallback) {
57: NameCallback nameCallback = (NameCallback) callbacks[i];
58: nameCallback.setName(user);
59: } else if (callbacks[i] instanceof PasswordCallback) {
60: PasswordCallback passwordCallback = (PasswordCallback) callbacks[i];
61: passwordCallback.setPassword(password);
62: } else {
63: throw new UnsupportedCallbackException(callbacks[i],
64: "Unsupported callback");
65: }
66: }
67: }
68:
69: // Static --------------------------------------------------------
70:
71: /**
72: * Login using the "client-login" config
73: */
74: public static LoginContext login(String user, String password)
75: throws LoginException {
76: return login("client-login", user, password);
77: }
78:
79: /**
80: * Login using specific login configuration
81: * @param config - name of login config to use
82: * @param user
83: * @param password
84: * @return
85: * @throws LoginException
86: */
87: public static LoginContext login(String config, String user,
88: String password) throws LoginException {
89: ApplicationCallbackHandler handler = new ApplicationCallbackHandler(
90: user, password);
91: LoginContext result = new LoginContext(config, handler);
92: result.login();
93: return result;
94: }
95: }
|