001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.api.login;
023
024import java.io.IOException;
025
026import javax.security.auth.callback.Callback;
027import javax.security.auth.callback.CallbackHandler;
028import javax.security.auth.callback.NameCallback;
029import javax.security.auth.callback.PasswordCallback;
030import javax.security.auth.callback.UnsupportedCallbackException;
031
032public class UserIdentificationInfoCallbackHandler implements CallbackHandler {
033
034    protected final UserIdentificationInfo userIdent;
035
036    public UserIdentificationInfoCallbackHandler(UserIdentificationInfo userIdent) {
037        this.userIdent = userIdent;
038    }
039
040    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
041
042        for (Callback c : callbacks) {
043            if (c instanceof NameCallback) {
044                String username = userIdent.getUserName();
045                NameCallback nc = (NameCallback) c;
046                nc.setName(username);
047            } else if (c instanceof PasswordCallback) {
048                PasswordCallback pc = (PasswordCallback) c;
049                char[] password = userIdent.getPassword().toCharArray();
050                pc.setPassword(password);
051            } else if (c instanceof UserIdentificationInfoCallback) {
052                UserIdentificationInfoCallback uic = (UserIdentificationInfoCallback) c;
053                uic.setUserInfo(userIdent);
054            } else if (c.getClass().getName().equals("org.jboss.security.auth.callback.SecurityAssociationCallback")) {
055                // we do nothing but do not raise error
056
057            } else {
058                throw new UnsupportedCallbackException(c, "Unrecognized Callback:" + c.getClass().getName());
059            }
060        }
061    }
062
063}