001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.api.login;
021
022import java.io.IOException;
023
024import javax.security.auth.callback.Callback;
025import javax.security.auth.callback.CallbackHandler;
026import javax.security.auth.callback.NameCallback;
027import javax.security.auth.callback.PasswordCallback;
028import javax.security.auth.callback.UnsupportedCallbackException;
029
030public class UserIdentificationInfoCallbackHandler implements CallbackHandler {
031
032    protected final UserIdentificationInfo userIdent;
033
034    public UserIdentificationInfoCallbackHandler(UserIdentificationInfo userIdent) {
035        this.userIdent = userIdent;
036    }
037
038    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
039
040        for (Callback c : callbacks) {
041            if (c instanceof NameCallback) {
042                String username = userIdent.getUserName();
043                NameCallback nc = (NameCallback) c;
044                nc.setName(username);
045            } else if (c instanceof PasswordCallback) {
046                PasswordCallback pc = (PasswordCallback) c;
047                char[] password = userIdent.getPassword().toCharArray();
048                pc.setPassword(password);
049            } else if (c instanceof UserIdentificationInfoCallback) {
050                UserIdentificationInfoCallback uic = (UserIdentificationInfoCallback) c;
051                uic.setUserInfo(userIdent);
052            } else if (c.getClass().getName().equals("org.jboss.security.auth.callback.SecurityAssociationCallback")) {
053                // we do nothing but do not raise error
054
055            } else {
056                throw new UnsupportedCallbackException(c, "Unrecognized Callback:" + c.getClass().getName());
057            }
058        }
059    }
060
061}