| 14792 |
manas |
1 |
/**
|
|
|
2 |
* Copyright 2010-present Facebook.
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
package com.facebook;
|
|
|
18 |
|
|
|
19 |
import android.os.Bundle;
|
|
|
20 |
import com.facebook.internal.Validate;
|
|
|
21 |
|
|
|
22 |
import java.util.ArrayList;
|
|
|
23 |
import java.util.Date;
|
|
|
24 |
import java.util.List;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* <p>
|
|
|
28 |
* A base class for implementations of a {@link Session Session} token cache.
|
|
|
29 |
* </p>
|
|
|
30 |
* <p>
|
|
|
31 |
* The Session constructor optionally takes a TokenCachingStrategy, from which it will
|
|
|
32 |
* attempt to load a cached token during construction. Also, whenever the
|
|
|
33 |
* Session updates its token, it will also save the token and associated state
|
|
|
34 |
* to the TokenCachingStrategy.
|
|
|
35 |
* </p>
|
|
|
36 |
* <p>
|
|
|
37 |
* This is the only mechanism supported for an Android service to use Session.
|
|
|
38 |
* The service can create a custom TokenCachingStrategy that returns the Session provided
|
|
|
39 |
* by an Activity through which the user logged in to Facebook.
|
|
|
40 |
* </p>
|
|
|
41 |
*/
|
|
|
42 |
public abstract class TokenCachingStrategy {
|
|
|
43 |
/**
|
|
|
44 |
* The key used by Session to store the token value in the Bundle during
|
|
|
45 |
* load and save.
|
|
|
46 |
*/
|
|
|
47 |
public static final String TOKEN_KEY = "com.facebook.TokenCachingStrategy.Token";
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* The key used by Session to store the expiration date value in the Bundle
|
|
|
51 |
* during load and save.
|
|
|
52 |
*/
|
|
|
53 |
public static final String EXPIRATION_DATE_KEY = "com.facebook.TokenCachingStrategy.ExpirationDate";
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* The key used by Session to store the last refresh date value in the
|
|
|
57 |
* Bundle during load and save.
|
|
|
58 |
*/
|
|
|
59 |
public static final String LAST_REFRESH_DATE_KEY = "com.facebook.TokenCachingStrategy.LastRefreshDate";
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* The key used by Session to store the user's id value in the Bundle during
|
|
|
63 |
* load and save.
|
|
|
64 |
*/
|
|
|
65 |
public static final String USER_FBID_KEY = "com.facebook.TokenCachingStrategy.UserFBID";
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* The key used by Session to store an enum indicating the source of the token
|
|
|
69 |
* in the Bundle during load and save.
|
|
|
70 |
*/
|
|
|
71 |
public static final String TOKEN_SOURCE_KEY = "com.facebook.TokenCachingStrategy.AccessTokenSource";
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* The key used by Session to store the list of permissions granted by the
|
|
|
75 |
* token in the Bundle during load and save.
|
|
|
76 |
*/
|
|
|
77 |
public static final String PERMISSIONS_KEY = "com.facebook.TokenCachingStrategy.Permissions";
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* The key used by Session to store the list of permissions declined by the user in the token in the Bundle
|
|
|
81 |
* during load and save.
|
|
|
82 |
*/
|
|
|
83 |
public static final String DECLINED_PERMISSIONS_KEY = "com.facebook.TokenCachingStrategy.DeclinedPermissions";
|
|
|
84 |
|
|
|
85 |
private static final long INVALID_BUNDLE_MILLISECONDS = Long.MIN_VALUE;
|
|
|
86 |
private static final String IS_SSO_KEY = "com.facebook.TokenCachingStrategy.IsSSO";
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Called during Session construction to get the token state. Typically this
|
|
|
90 |
* is loaded from a persistent store that was previously initialized via
|
|
|
91 |
* save. The caller may choose to keep a reference to the returned Bundle
|
|
|
92 |
* indefinitely. Therefore the TokenCachingStrategy should not store the returned Bundle
|
|
|
93 |
* and should return a new Bundle on every call to this method.
|
|
|
94 |
*
|
|
|
95 |
* @return A Bundle that represents the token state that was loaded.
|
|
|
96 |
*/
|
|
|
97 |
public abstract Bundle load();
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Called when a Session updates its token. This is passed a Bundle of
|
|
|
101 |
* values that should be stored durably for the purpose of being returned
|
|
|
102 |
* from a later call to load. Some implementations may choose to store
|
|
|
103 |
* bundle beyond the scope of this call, so the caller should keep no
|
|
|
104 |
* references to the bundle to ensure that it is not modified later.
|
|
|
105 |
*
|
|
|
106 |
* @param bundle
|
|
|
107 |
* A Bundle that represents the token state to be saved.
|
|
|
108 |
*/
|
|
|
109 |
public abstract void save(Bundle bundle);
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Called when a Session learns its token is no longer valid or during a
|
|
|
113 |
* call to {@link Session#closeAndClearTokenInformation
|
|
|
114 |
* closeAndClearTokenInformation} to clear the durable state associated with
|
|
|
115 |
* the token.
|
|
|
116 |
*/
|
|
|
117 |
public abstract void clear();
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Returns a boolean indicating whether a Bundle contains properties that
|
|
|
121 |
* could be a valid saved token.
|
|
|
122 |
*
|
|
|
123 |
* @param bundle
|
|
|
124 |
* A Bundle to check for token information.
|
|
|
125 |
* @return a boolean indicating whether a Bundle contains properties that
|
|
|
126 |
* could be a valid saved token.
|
|
|
127 |
*/
|
|
|
128 |
public static boolean hasTokenInformation(Bundle bundle) {
|
|
|
129 |
if (bundle == null) {
|
|
|
130 |
return false;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
String token = bundle.getString(TOKEN_KEY);
|
|
|
134 |
if ((token == null) || (token.length() == 0)) {
|
|
|
135 |
return false;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
long expiresMilliseconds = bundle.getLong(EXPIRATION_DATE_KEY, 0L);
|
|
|
139 |
if (expiresMilliseconds == 0L) {
|
|
|
140 |
return false;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
return true;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Gets the cached token value from a Bundle.
|
|
|
148 |
*
|
|
|
149 |
* @param bundle
|
|
|
150 |
* A Bundle in which the token value was stored.
|
|
|
151 |
* @return the cached token value, or null.
|
|
|
152 |
*
|
|
|
153 |
* @throws NullPointerException if the passed in Bundle is null
|
|
|
154 |
*/
|
|
|
155 |
public static String getToken(Bundle bundle) {
|
|
|
156 |
Validate.notNull(bundle, "bundle");
|
|
|
157 |
return bundle.getString(TOKEN_KEY);
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Puts the token value into a Bundle.
|
|
|
162 |
*
|
|
|
163 |
* @param bundle
|
|
|
164 |
* A Bundle in which the token value should be stored.
|
|
|
165 |
* @param value
|
|
|
166 |
* The String representing the token value, or null.
|
|
|
167 |
*
|
|
|
168 |
* @throws NullPointerException if the passed in Bundle or token value are null
|
|
|
169 |
*/
|
|
|
170 |
public static void putToken(Bundle bundle, String value) {
|
|
|
171 |
Validate.notNull(bundle, "bundle");
|
|
|
172 |
Validate.notNull(value, "value");
|
|
|
173 |
bundle.putString(TOKEN_KEY, value);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Gets the cached expiration date from a Bundle.
|
|
|
178 |
*
|
|
|
179 |
* @param bundle
|
|
|
180 |
* A Bundle in which the expiration date was stored.
|
|
|
181 |
* @return the cached expiration date, or null.
|
|
|
182 |
*
|
|
|
183 |
* @throws NullPointerException if the passed in Bundle is null
|
|
|
184 |
*/
|
|
|
185 |
public static Date getExpirationDate(Bundle bundle) {
|
|
|
186 |
Validate.notNull(bundle, "bundle");
|
|
|
187 |
return getDate(bundle, EXPIRATION_DATE_KEY);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Puts the expiration date into a Bundle.
|
|
|
192 |
*
|
|
|
193 |
* @param bundle
|
|
|
194 |
* A Bundle in which the expiration date should be stored.
|
|
|
195 |
* @param value
|
|
|
196 |
* The Date representing the expiration date.
|
|
|
197 |
*
|
|
|
198 |
* @throws NullPointerException if the passed in Bundle or date value are null
|
|
|
199 |
*/
|
|
|
200 |
public static void putExpirationDate(Bundle bundle, Date value) {
|
|
|
201 |
Validate.notNull(bundle, "bundle");
|
|
|
202 |
Validate.notNull(value, "value");
|
|
|
203 |
putDate(bundle, EXPIRATION_DATE_KEY, value);
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Gets the cached expiration date from a Bundle.
|
|
|
208 |
*
|
|
|
209 |
* @param bundle
|
|
|
210 |
* A Bundle in which the expiration date was stored.
|
|
|
211 |
* @return the long representing the cached expiration date in milliseconds
|
|
|
212 |
* since the epoch, or 0.
|
|
|
213 |
*
|
|
|
214 |
* @throws NullPointerException if the passed in Bundle is null
|
|
|
215 |
*/
|
|
|
216 |
public static long getExpirationMilliseconds(Bundle bundle) {
|
|
|
217 |
Validate.notNull(bundle, "bundle");
|
|
|
218 |
return bundle.getLong(EXPIRATION_DATE_KEY);
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* Puts the expiration date into a Bundle.
|
|
|
223 |
*
|
|
|
224 |
* @param bundle
|
|
|
225 |
* A Bundle in which the expiration date should be stored.
|
|
|
226 |
* @param value
|
|
|
227 |
* The long representing the expiration date in milliseconds
|
|
|
228 |
* since the epoch.
|
|
|
229 |
*
|
|
|
230 |
* @throws NullPointerException if the passed in Bundle is null
|
|
|
231 |
*/
|
|
|
232 |
public static void putExpirationMilliseconds(Bundle bundle, long value) {
|
|
|
233 |
Validate.notNull(bundle, "bundle");
|
|
|
234 |
bundle.putLong(EXPIRATION_DATE_KEY, value);
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
/**
|
|
|
238 |
* Gets the cached list of permissions from a Bundle.
|
|
|
239 |
*
|
|
|
240 |
* @param bundle
|
|
|
241 |
* A Bundle in which the list of permissions was stored.
|
|
|
242 |
* @return the cached list of permissions.
|
|
|
243 |
*
|
|
|
244 |
* @throws NullPointerException if the passed in Bundle is null
|
|
|
245 |
*/
|
|
|
246 |
public static List<String> getPermissions(Bundle bundle) {
|
|
|
247 |
Validate.notNull(bundle, "bundle");
|
|
|
248 |
return bundle.getStringArrayList(PERMISSIONS_KEY);
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
/**
|
|
|
252 |
* Puts the list of permissions into a Bundle.
|
|
|
253 |
*
|
|
|
254 |
* @param bundle
|
|
|
255 |
* A Bundle in which the list of permissions should be stored.
|
|
|
256 |
* @param value
|
|
|
257 |
* The List<String> representing the list of permissions,
|
|
|
258 |
* or null.
|
|
|
259 |
*
|
|
|
260 |
* @throws NullPointerException if the passed in Bundle or permissions list are null
|
|
|
261 |
*/
|
|
|
262 |
public static void putPermissions(Bundle bundle, List<String> value) {
|
|
|
263 |
Validate.notNull(bundle, "bundle");
|
|
|
264 |
Validate.notNull(value, "value");
|
|
|
265 |
|
|
|
266 |
ArrayList<String> arrayList;
|
|
|
267 |
if (value instanceof ArrayList<?>) {
|
|
|
268 |
arrayList = (ArrayList<String>) value;
|
|
|
269 |
} else {
|
|
|
270 |
arrayList = new ArrayList<String>(value);
|
|
|
271 |
}
|
|
|
272 |
bundle.putStringArrayList(PERMISSIONS_KEY, arrayList);
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
/**
|
|
|
276 |
* Puts the list of declined permissions into a Bundle.
|
|
|
277 |
*
|
|
|
278 |
* @param bundle
|
|
|
279 |
* A Bundle in which the list of permissions should be stored.
|
|
|
280 |
* @param value
|
|
|
281 |
* The List<String> representing the list of permissions,
|
|
|
282 |
* or null.
|
|
|
283 |
*
|
|
|
284 |
* @throws NullPointerException if the passed in Bundle or permissions list are null
|
|
|
285 |
*/
|
|
|
286 |
public static void putDeclinedPermissions(Bundle bundle, List<String> value) {
|
|
|
287 |
Validate.notNull(bundle, "bundle");
|
|
|
288 |
Validate.notNull(value, "value");
|
|
|
289 |
|
|
|
290 |
ArrayList<String> arrayList;
|
|
|
291 |
if (value instanceof ArrayList<?>) {
|
|
|
292 |
arrayList = (ArrayList<String>) value;
|
|
|
293 |
} else {
|
|
|
294 |
arrayList = new ArrayList<String>(value);
|
|
|
295 |
}
|
|
|
296 |
bundle.putStringArrayList(DECLINED_PERMISSIONS_KEY, arrayList);
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
|
|
|
300 |
/**
|
|
|
301 |
* Gets the cached enum indicating the source of the token from the Bundle.
|
|
|
302 |
*
|
|
|
303 |
* @param bundle
|
|
|
304 |
* A Bundle in which the enum was stored.
|
|
|
305 |
* @return enum indicating the source of the token
|
|
|
306 |
*
|
|
|
307 |
* @throws NullPointerException if the passed in Bundle is null
|
|
|
308 |
*/
|
|
|
309 |
public static AccessTokenSource getSource(Bundle bundle) {
|
|
|
310 |
Validate.notNull(bundle, "bundle");
|
|
|
311 |
if (bundle.containsKey(TokenCachingStrategy.TOKEN_SOURCE_KEY)) {
|
|
|
312 |
return (AccessTokenSource) bundle.getSerializable(TokenCachingStrategy.TOKEN_SOURCE_KEY);
|
|
|
313 |
} else {
|
|
|
314 |
boolean isSSO = bundle.getBoolean(TokenCachingStrategy.IS_SSO_KEY);
|
|
|
315 |
return isSSO ? AccessTokenSource.FACEBOOK_APPLICATION_WEB : AccessTokenSource.WEB_VIEW;
|
|
|
316 |
}
|
|
|
317 |
}
|
|
|
318 |
/**
|
|
|
319 |
* Puts the enum indicating the source of the token into a Bundle.
|
|
|
320 |
*
|
|
|
321 |
* @param bundle
|
|
|
322 |
* A Bundle in which the enum should be stored.
|
|
|
323 |
* @param value
|
|
|
324 |
* enum indicating the source of the token
|
|
|
325 |
*
|
|
|
326 |
* @throws NullPointerException if the passed in Bundle is null
|
|
|
327 |
*/
|
|
|
328 |
public static void putSource(Bundle bundle, AccessTokenSource value) {
|
|
|
329 |
Validate.notNull(bundle, "bundle");
|
|
|
330 |
bundle.putSerializable(TOKEN_SOURCE_KEY, value);
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
/**
|
|
|
334 |
* Gets the cached last refresh date from a Bundle.
|
|
|
335 |
*
|
|
|
336 |
* @param bundle
|
|
|
337 |
* A Bundle in which the last refresh date was stored.
|
|
|
338 |
* @return the cached last refresh Date, or null.
|
|
|
339 |
*
|
|
|
340 |
* @throws NullPointerException if the passed in Bundle is null
|
|
|
341 |
*/
|
|
|
342 |
public static Date getLastRefreshDate(Bundle bundle) {
|
|
|
343 |
Validate.notNull(bundle, "bundle");
|
|
|
344 |
return getDate(bundle, LAST_REFRESH_DATE_KEY);
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
/**
|
|
|
348 |
* Puts the last refresh date into a Bundle.
|
|
|
349 |
*
|
|
|
350 |
* @param bundle
|
|
|
351 |
* A Bundle in which the last refresh date should be stored.
|
|
|
352 |
* @param value
|
|
|
353 |
* The Date representing the last refresh date, or null.
|
|
|
354 |
*
|
|
|
355 |
* @throws NullPointerException if the passed in Bundle or date value are null
|
|
|
356 |
*/
|
|
|
357 |
public static void putLastRefreshDate(Bundle bundle, Date value) {
|
|
|
358 |
Validate.notNull(bundle, "bundle");
|
|
|
359 |
Validate.notNull(value, "value");
|
|
|
360 |
putDate(bundle, LAST_REFRESH_DATE_KEY, value);
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
/**
|
|
|
364 |
* Gets the cached last refresh date from a Bundle.
|
|
|
365 |
*
|
|
|
366 |
* @param bundle
|
|
|
367 |
* A Bundle in which the last refresh date was stored.
|
|
|
368 |
* @return the cached last refresh date in milliseconds since the epoch.
|
|
|
369 |
*
|
|
|
370 |
* @throws NullPointerException if the passed in Bundle is null
|
|
|
371 |
*/
|
|
|
372 |
public static long getLastRefreshMilliseconds(Bundle bundle) {
|
|
|
373 |
Validate.notNull(bundle, "bundle");
|
|
|
374 |
return bundle.getLong(LAST_REFRESH_DATE_KEY);
|
|
|
375 |
}
|
|
|
376 |
|
|
|
377 |
/**
|
|
|
378 |
* Puts the last refresh date into a Bundle.
|
|
|
379 |
*
|
|
|
380 |
* @param bundle
|
|
|
381 |
* A Bundle in which the last refresh date should be stored.
|
|
|
382 |
* @param value
|
|
|
383 |
* The long representing the last refresh date in milliseconds
|
|
|
384 |
* since the epoch.
|
|
|
385 |
*
|
|
|
386 |
* @throws NullPointerException if the passed in Bundle is null
|
|
|
387 |
*/
|
|
|
388 |
public static void putLastRefreshMilliseconds(Bundle bundle, long value) {
|
|
|
389 |
Validate.notNull(bundle, "bundle");
|
|
|
390 |
bundle.putLong(LAST_REFRESH_DATE_KEY, value);
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
static Date getDate(Bundle bundle, String key) {
|
|
|
394 |
if (bundle == null) {
|
|
|
395 |
return null;
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
long n = bundle.getLong(key, INVALID_BUNDLE_MILLISECONDS);
|
|
|
399 |
if (n == INVALID_BUNDLE_MILLISECONDS) {
|
|
|
400 |
return null;
|
|
|
401 |
}
|
|
|
402 |
|
|
|
403 |
return new Date(n);
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
static void putDate(Bundle bundle, String key, Date date) {
|
|
|
407 |
bundle.putLong(key, date.getTime());
|
|
|
408 |
}
|
|
|
409 |
}
|