001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.keymanager.front.webapp;
019    
020    import java.io.Serializable;
021    import java.util.Arrays;
022    
023    /**
024     * Authentication information (username + password). Can be obtained in every
025     * REST service by sub-classing {@link AbstractService} and using
026     * {@link AbstractService#getAuth()} or {@link AbstractService#authenticate(String)}.
027     *
028     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
029     */
030    public class Auth
031    implements Serializable
032    {
033            private static final long serialVersionUID = 1L;
034    
035            private String userName;
036    
037            private char[] password;
038    
039            /**
040             * Create an empty instance.
041             */
042            public Auth() { }
043    
044            /**
045             * Create an instance with the given values.
046             * @param userName the user-name (might be <code>null</code>).
047             * @param password the password (might be <code>null</code>).
048             */
049            public Auth(String userName, char[] password)
050            {
051                    this.userName = userName;
052                    this.password = password;
053            }
054    
055            /**
056             * Get the user-name.
057             * @return the user-name or <code>null</code>.
058             */
059            public String getUserName() {
060                    return userName;
061            }
062    
063            /**
064             * Set the user-name.
065             * @param userName the user-name or <code>null</code>.
066             */
067            public void setUserName(String userName) {
068                    this.userName = userName;
069            }
070    
071            /**
072             * <p>
073             * Get the password.
074             * </p>
075             * <p>
076             * <b>Warning: the char-array returned by this method might be modified later</b> (overwritten with 0), e.g. if
077             * {@link #clear()} is called! If you want to use this char-array elsewhere, you must clone it immediately!
078             * </p>
079    
080             * @return the password or <code>null</code>.
081             */
082            public char[] getPassword() {
083                    return password;
084            }
085    
086            /**
087             * <p>
088             * Set the password.
089             * </p>
090             * <p>
091             * <b>Warning: the char-array passed to this method is modified</b> (overwritten with 0), if
092             * {@link #clear()} is called! If you want to use this char-array elsewhere, you must pass
093             * a clone here!
094             * </p>
095             * @param password the password or <code>null</code>.
096             */
097            public void setPassword(char[] password)
098            {
099                    this.password = password;
100            }
101    
102            /**
103             * Clear the sensitive data from this <code>Auth</code> instance. If the <code>password</code>
104             * is not <code>null</code>, it is overwritten with 0. This method is called by the
105             * {@link #finalize()} method of this class!
106             */
107            public void clear()
108            {
109                    if (password != null)
110                            Arrays.fill(password, (char)0);
111    
112                    password = null;
113            }
114    
115            @Override
116            protected void finalize() throws Throwable {
117                    clear();
118            }
119    }