001    /**
002     *
003     * Copyright 2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.security.jaas;
018    
019    import java.io.Serializable;
020    import java.util.Arrays;
021    import javax.security.auth.DestroyFailedException;
022    import javax.security.auth.Destroyable;
023    import javax.security.auth.RefreshFailedException;
024    import javax.security.auth.Refreshable;
025    
026    
027    /**
028     * A username/password credential.  Used to store the username/password in the
029     * Subject's private credentials.
030     *
031     * @version $Revision: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
032     */
033    public class UsernamePasswordCredential implements Destroyable, Refreshable, Serializable {
034    
035        private String username;
036        private char[] password;
037        private boolean destroyed;
038    
039        public UsernamePasswordCredential(String username, char[] password) {
040            assert username != null;
041            assert password != null;
042    
043            this.username = username;
044            this.password = new char[password.length];
045            System.arraycopy(password, 0, this.password, 0, password.length);
046        }
047    
048        public String getUsername() {
049            return username;
050        }
051    
052        public char[] getPassword() {
053            return password;
054        }
055    
056        public void destroy() throws DestroyFailedException {
057            Arrays.fill(password, ' ');
058            
059            username = null;
060            password = null;
061            destroyed = true;
062        }
063    
064        public boolean isDestroyed() {
065            return destroyed;
066        }
067    
068        public void refresh() throws RefreshFailedException {
069        }
070    
071        public boolean isCurrent() {
072            return !destroyed;
073        }
074    
075        public boolean equals(Object o) {
076            if (this == o) return true;
077            if (!(o instanceof UsernamePasswordCredential)) return false;
078    
079            final UsernamePasswordCredential credential = (UsernamePasswordCredential) o;
080    
081            if (destroyed != credential.destroyed) return false;
082            if (!Arrays.equals(password, credential.password)) return false;
083            if (username != null ? !username.equals(credential.username) : credential.username != null) return false;
084    
085            return true;
086        }
087    
088        public int hashCode() {
089            int result;
090            result = (username != null ? username.hashCode() : 0);
091            result = 29 * result + (destroyed ? 1 : 0);
092            return result;
093        }
094    }