001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 1any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent Benoit
022: * --------------------------------------------------------------------------
023: * $Id: NoInputCallbackHandler.java 4804 2004-05-25 15:13:29Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.security.auth.callback;
026:
027: import java.io.IOException;
028: import javax.security.auth.callback.CallbackHandler;
029: import javax.security.auth.callback.UnsupportedCallbackException;
030: import javax.security.auth.callback.NameCallback;
031: import javax.security.auth.callback.PasswordCallback;
032: import javax.security.auth.callback.Callback;
033: import java.security.cert.Certificate;
034:
035: /**
036: * The username and password are given by the constructor No JNDI, datasource or
037: * file checks.
038: * @author Florent Benoit (initial developer)
039: * @author Alexandre Thaveau (add the use of certificates)
040: * @author Marc-Antoine Bourgeot (add the use of certificates)
041: */
042: public class NoInputCallbackHandler implements CallbackHandler {
043:
044: /**
045: * Username to use
046: */
047: private String username = null;
048:
049: /**
050: * Password to use
051: */
052: private String password = null;
053:
054: /**
055: * Certificate to use, optionnal
056: */
057: private Certificate cert = null;
058:
059: /**
060: * No default Constructor : must use the one with username and password
061: * @throws Exception if someone try to use it
062: */
063: public NoInputCallbackHandler() throws Exception {
064: throw new Exception(
065: "This class could only be used with the constructor NoInputCallbackHandler(String username, String password)");
066: }
067:
068: /**
069: * Constructor
070: * @param username username to store for the authentication
071: * @param password password to store for the authentication
072: */
073: public NoInputCallbackHandler(String username, String password) {
074: this .username = username;
075: this .password = password;
076: }
077:
078: /**
079: * Constructor
080: * @param username username to store for the authentication
081: * @param password password to store for the authentication
082: * @param cert the certificate for the authentication
083: */
084: public NoInputCallbackHandler(String username, String password,
085: Certificate cert) {
086: this (username, password);
087: this .cert = cert;
088: }
089:
090: /**
091: * Invoke an array of Callbacks.
092: * @param callbacks an array of <code>Callback</code> objects which
093: * contain the information requested by an underlying security
094: * service to be retrieved or displayed.
095: * @throws IOException if an input or output error occurs. <p>
096: * @throws UnsupportedCallbackException if the implementation of this method
097: * does not support one or more of the Callbacks specified in the
098: * <code>callbacks</code> parameter.
099: */
100: public void handle(Callback[] callbacks) throws IOException,
101: UnsupportedCallbackException {
102:
103: for (int i = 0; i < callbacks.length; i++) {
104: if (callbacks[i] instanceof NameCallback) {
105: // set the username to the username given in the constructor
106: NameCallback nc = (NameCallback) callbacks[i];
107: nc.setName(username);
108: } else if (callbacks[i] instanceof PasswordCallback) {
109: // set the password to the password given in the constructor
110: PasswordCallback pc = (PasswordCallback) callbacks[i];
111: pc.setPassword(password.toCharArray());
112: } else if (callbacks[i] instanceof CertificateCallback) {
113: CertificateCallback cc = (CertificateCallback) callbacks[i];
114: cc.setUserCertificate(cert);
115: } else {
116: throw new UnsupportedCallbackException(callbacks[i],
117: "Unrecognized Callback");
118: }
119: }
120: }
121: }
|