001package co.codewizards.cloudstore.core.auth;
002
003import javax.xml.bind.annotation.XmlRootElement;
004
005import co.codewizards.cloudstore.core.dto.DateTime;
006
007@XmlRootElement
008public class AuthToken {
009        private String password;
010        private DateTime renewalDateTime;
011        private DateTime expiryDateTime;
012
013        private volatile boolean writable = true;
014
015        /**
016         * Gets the password, currently valid for authentication at the server.
017         * @return the current password. Never <code>null</code>.
018         */
019        public String getPassword() {
020                return password;
021        }
022        /**
023         * Sets the current password.
024         * @param password the current password. Should not be <code>null</code>.
025         * @throws IllegalStateException after {@link #makeUnmodifiable()} was invoked.
026         */
027        public void setPassword(String password) {
028                assertWritable();
029                this.password = password;
030        }
031
032        /**
033         * Gets the timestamp from which on a new token would be returned (but the old is still valid).
034         * @return the timestamp from which on a new token can be obtained by the client. Never <code>null</code>.
035         */
036        public DateTime getRenewalDateTime() {
037                return renewalDateTime;
038        }
039        /**
040         *
041         * @param renewalDateTime
042         * @throws IllegalStateException after {@link #makeUnmodifiable()} was invoked.
043         */
044        public void setRenewalDateTime(DateTime renewalDateTime) {
045                assertWritable();
046                this.renewalDateTime = renewalDateTime;
047        }
048
049        /**
050         * Gets the timestamp when the current token expires.
051         * <p>
052         * Trying to authenticate with the current {@link #getPassword() password} after this date + time
053         * will fail.
054         * @return the timestamp when the current token expires. Never <code>null</code>.
055         */
056        public DateTime getExpiryDateTime() {
057                return expiryDateTime;
058        }
059        /**
060         * Sets the timestamp when the current token expires.
061         * @param expiryDateTime the timestamp when the current token expires. Should not be <code>null</code>.
062         * @throws IllegalStateException after {@link #makeUnmodifiable()} was invoked.
063         */
064        public void setExpiryDateTime(DateTime expiryDateTime) {
065                assertWritable();
066                this.expiryDateTime = expiryDateTime;
067        }
068
069        /**
070         * Makes this instance immutable.
071         * <p>
072         * After this method was invoked, all setters throw an {@link IllegalStateException}.
073         */
074        public void makeUnmodifiable() {
075                this.writable = false;
076        }
077
078        /**
079         * Asserts that this instance is writable.
080         * <p>
081         * This method does nothing, if this instance may be modified. After {@link #makeUnmodifiable()} was
082         * invoked, this method throws an {@link IllegalStateException}.
083         * <p>
084         * All setters must call this method before modifying the object.
085         * @throws IllegalStateException after {@link #makeUnmodifiable()} was invoked.
086         */
087        private void assertWritable() {
088                if (!writable)
089                        throw new IllegalStateException("This AuthToken is not writable!");
090        }
091}