001 /*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License"). You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at
010 * trunk/ds/resource/legal-notices/cddl.txt
011 * or http://www.opensource.org/licenses/cddl1.php.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/ds/resource/legal-notices/cddl.txt. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2010-2015 UnboundID Corp.
026 */
027 package com.unboundid.directory.sdk.broker.types;
028
029 import com.unboundid.util.Validator;
030
031 import java.util.Date;
032 import java.util.Set;
033
034 /**
035 * Abstract class for an OAuth 2 token.
036 */
037 public abstract class Token
038 {
039 // The application ID that has been authorized.
040 private final String applicationId;
041 // The set of scope IDs for which the application has requested or authorized.
042 private final Set<String> scopeIds;
043 // The time that this token was generated.
044 private final Date generateTimestamp;
045 // The maximum length of time in seconds that this token will be
046 // considered valid.
047 private final long maxValiditySeconds;
048 // A value of this token that will be exposed to clients.
049 private volatile String value;
050
051 /**
052 * Construct a new Token instance.
053 *
054 * @param applicationId The application ID with which this token is
055 * associated. Must not be null.
056 * @param scopeIds The set of scopes for which the client has been authorized.
057 * Must not be null.
058 * @param generateTimestamp The time that this token was generated. Must not
059 * be null.
060 * @param maxValiditySeconds The maximum length of time in seconds that this
061 * token will be considered valid. Must not be null.
062 */
063 protected Token(final String applicationId, final Set<String> scopeIds,
064 final Date generateTimestamp,
065 final long maxValiditySeconds)
066 {
067 Validator.ensureNotNull(applicationId, scopeIds, generateTimestamp,
068 maxValiditySeconds);
069 this.scopeIds = scopeIds;
070 this.applicationId = applicationId;
071 this.generateTimestamp = generateTimestamp;
072 this.maxValiditySeconds = maxValiditySeconds;
073 }
074
075 /**
076 * Retrieves the application ID with which this token is associated.
077 *
078 * @return The application ID with which this token is associated.
079 */
080 public String getApplicationId()
081 {
082 return applicationId;
083 }
084
085 /**
086 * Retrieves the set of scope IDs for which the client has been authorized.
087 *
088 * @return The set of scope IDs for which the client has been authorized.
089 */
090 public Set<String> getScopeIds()
091 {
092 return scopeIds;
093 }
094
095 /**
096 * Retrieves the time that this token was generated.
097 *
098 * @return The time that this token was generated.
099 */
100 public Date getGenerateTimestamp()
101 {
102 return generateTimestamp;
103 }
104
105 /**
106 * Retrieves the maximum length of time in seconds that this code will be
107 * considered valid.
108 *
109 * @return The maximum length of time in seconds that this code will be
110 * considered valid.
111 */
112 public long getMaxValiditySeconds()
113 {
114 return maxValiditySeconds;
115 }
116
117 /**
118 * Retrieves the string value of this token that should be returned to
119 * the client.
120 *
121 * @return The string value of this token that should be returned to
122 * the client.
123 */
124 public String getValue()
125 {
126 return value;
127 }
128
129 /**
130 * Sets the string value of this token that should be returned to the
131 * client.
132 *
133 * @param value The string value of this token that should be returned
134 * to the client.
135 */
136 public void setValue(final String value)
137 {
138 Validator.ensureNotNull(value);
139 this.value = value;
140 }
141
142 /**
143 * Indicates whether this token is expired.
144 *
145 * @return {@code true} if this token is expired, or
146 * {@code false} if not.
147 */
148 public boolean isExpired()
149 {
150 final long generateTS = generateTimestamp.getTime();
151 final long expirationTime = (generateTS + (1000L * maxValiditySeconds));
152 final long currentTime = System.currentTimeMillis();
153 return currentTime > expirationTime;
154 }
155
156 /**
157 * Retrieves a hash code for this token.
158 *
159 * @return A hash code for this token.
160 */
161 @Override
162 public int hashCode()
163 {
164 final int prime = 31;
165 int result = 1;
166 result = prime * result + (applicationId != null ?
167 applicationId.hashCode() : 0);
168 result = prime * result + (generateTimestamp != null ?
169 generateTimestamp.hashCode() : 0);
170 result = prime * result + (int) (maxValiditySeconds ^
171 (maxValiditySeconds >>> 32));
172 return result;
173 }
174
175 /**
176 * Indicates whether the provided object is equal to this token.
177 *
178 * @param o The object for which to make the determination.
179 *
180 * @return {@code true} if the provided object is equal to this authorization
181 * code, or {@code false} if not.
182 */
183 @Override
184 public boolean equals(final Object o)
185 {
186 if (this == o)
187 {
188 return true;
189 }
190 if (o == null || getClass() != o.getClass())
191 {
192 return false;
193 }
194
195 Token that = (Token) o;
196
197 if (maxValiditySeconds != that.maxValiditySeconds)
198 {
199 return false;
200 }
201 if (applicationId != null ?
202 !applicationId.equals(that.applicationId) :
203 that.applicationId != null)
204 {
205 return false;
206 }
207 if (generateTimestamp != null ?
208 !generateTimestamp.equals(that.generateTimestamp) :
209 that.generateTimestamp != null)
210 {
211 return false;
212 }
213 if (value != null ? !value.equals(that.value) : that.value != null)
214 {
215 return false;
216 }
217
218 return true;
219 }
220
221 /**
222 * Retrieves a string representation of this token.
223 *
224 * @return A string representation of this token.
225 */
226 @Override()
227 public String toString()
228 {
229 final StringBuilder buffer = new StringBuilder();
230 toString(buffer);
231 return buffer.toString();
232 }
233
234 /**
235 * Appends a string representation of this token to the provided
236 * buffer.
237 *
238 * @param buffer The buffer to which the string representation should be
239 * appended.
240 */
241 protected abstract void toString(final StringBuilder buffer);
242 }