001package co.codewizards.cloudstore.rest.server.auth;
002
003import java.io.Serializable;
004import java.util.Arrays;
005
006import com.google.common.util.concurrent.AbstractService;
007
008/**
009 * Authentication information (username + password). Can be obtained in every
010 * REST service by sub-classing {@link AbstractService} and using
011 * {@link AbstractService#getAuth()} or {@link AbstractService#authenticate(String)}.
012 *
013 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
014 */
015public class Auth
016implements Serializable
017{
018        private static final long serialVersionUID = 1L;
019
020        private String userName;
021
022        private char[] password;
023
024        /**
025         * Create an empty instance.
026         */
027        public Auth() { }
028
029        /**
030         * Create an instance with the given values.
031         * @param userName the user-name (might be <code>null</code>).
032         * @param password the password (might be <code>null</code>).
033         */
034        public Auth(String userName, char[] password)
035        {
036                this.userName = userName;
037                this.password = password;
038        }
039
040        /**
041         * Get the user-name.
042         * @return the user-name or <code>null</code>.
043         */
044        public String getUserName() {
045                return userName;
046        }
047
048        /**
049         * Set the user-name.
050         * @param userName the user-name or <code>null</code>.
051         */
052        public void setUserName(String userName) {
053                this.userName = userName;
054        }
055
056        /**
057         * <p>
058         * Get the password.
059         * </p>
060         * <p>
061         * <b>Warning: the char-array returned by this method might be modified later</b> (overwritten with 0), e.g. if
062         * {@link #clear()} is called! If you want to use this char-array elsewhere, you must clone it immediately!
063         * </p>
064
065         * @return the password or <code>null</code>.
066         */
067        public char[] getPassword() {
068                return password;
069        }
070
071        /**
072         * <p>
073         * Set the password.
074         * </p>
075         * <p>
076         * <b>Warning: the char-array passed to this method is modified</b> (overwritten with 0), if
077         * {@link #clear()} is called! If you want to use this char-array elsewhere, you must pass
078         * a clone here!
079         * </p>
080         * @param password the password or <code>null</code>.
081         */
082        public void setPassword(char[] password)
083        {
084                this.password = password;
085        }
086
087        /**
088         * Clear the sensitive data from this <code>Auth</code> instance. If the <code>password</code>
089         * is not <code>null</code>, it is overwritten with 0. This method is called by the
090         * {@link #finalize()} method of this class!
091         */
092        public void clear()
093        {
094                if (password != null)
095                        Arrays.fill(password, (char)0);
096
097                password = null;
098        }
099
100        @Override
101        protected void finalize() throws Throwable {
102                clear();
103        }
104}