001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.auth;
016    
017    import java.io.Serializable;
018    
019    import javax.security.auth.callback.Callback;
020    import javax.security.auth.callback.CallbackHandler;
021    import javax.security.auth.callback.NameCallback;
022    import javax.security.auth.callback.PasswordCallback;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class PortalCallbackHandler implements CallbackHandler, Serializable {
028    
029            public PortalCallbackHandler(String name, String password) {
030                    _name = name;
031                    _password = password;
032            }
033    
034            @Override
035            public void handle(Callback[] callbacks) {
036                    for (int i = 0; i < callbacks.length; i++) {
037                            if (callbacks[i] instanceof NameCallback) {
038                                    NameCallback nameCallback = (NameCallback)callbacks[i];
039                                    nameCallback.setName(_name);
040                            }
041                            else if (callbacks[i] instanceof PasswordCallback) {
042                                    PasswordCallback passCallback = (PasswordCallback)callbacks[i];
043                                    passCallback.setPassword(_password.toCharArray());
044                            }
045                    }
046            }
047    
048            private String _name;
049            private String _password;
050    
051    }