-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathOAuth2Credentials.java
696 lines (606 loc) · 22.4 KB
/
OAuth2Credentials.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
* Copyright 2015, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.auth.oauth2;
import com.google.api.client.util.Clock;
import com.google.auth.Credentials;
import com.google.auth.RequestMetadataCallback;
import com.google.auth.http.AuthHttpConstants;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.AbstractFuture;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListenableFutureTask;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.net.URI;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
/** Base type for Credentials using OAuth2. */
public class OAuth2Credentials extends Credentials {
private static final long serialVersionUID = 4556936364828217687L;
static final Duration DEFAULT_EXPIRATION_MARGIN = Duration.ofMinutes(3);
static final Duration DEFAULT_REFRESH_MARGIN = Duration.ofMinutes(3).plusSeconds(45);
private static final ImmutableMap<String, List<String>> EMPTY_EXTRA_HEADERS = ImmutableMap.of();
@VisibleForTesting private final Duration expirationMargin;
@VisibleForTesting private final Duration refreshMargin;
// byte[] is serializable, so the lock variable can be final
@VisibleForTesting final Object lock = new byte[0];
private volatile OAuthValue value = null;
@VisibleForTesting transient RefreshTask refreshTask;
// Change listeners are not serialized
private transient List<CredentialsChangedListener> changeListeners;
// Until we expose this to the users it can remain transient and non-serializable
@VisibleForTesting transient Clock clock = Clock.SYSTEM;
/**
* Returns the credentials instance from the given access token.
*
* @param accessToken the access token
* @return the credentials instance
*/
public static OAuth2Credentials create(AccessToken accessToken) {
return OAuth2Credentials.newBuilder().setAccessToken(accessToken).build();
}
/** Default constructor. */
protected OAuth2Credentials() {
this(null);
}
/**
* Constructor with explicit access token.
*
* @param accessToken initial or temporary access token
*/
protected OAuth2Credentials(AccessToken accessToken) {
this(accessToken, DEFAULT_REFRESH_MARGIN, DEFAULT_EXPIRATION_MARGIN);
}
protected OAuth2Credentials(
AccessToken accessToken, Duration refreshMargin, Duration expirationMargin) {
if (accessToken != null) {
this.value = OAuthValue.create(accessToken, EMPTY_EXTRA_HEADERS);
}
this.refreshMargin = Preconditions.checkNotNull(refreshMargin, "refreshMargin");
Preconditions.checkArgument(!refreshMargin.isNegative(), "refreshMargin can't be negative");
this.expirationMargin = Preconditions.checkNotNull(expirationMargin, "expirationMargin");
Preconditions.checkArgument(
!expirationMargin.isNegative(), "expirationMargin can't be negative");
}
@Override
public String getAuthenticationType() {
return "OAuth2";
}
@Override
public boolean hasRequestMetadata() {
return true;
}
@Override
public boolean hasRequestMetadataOnly() {
return true;
}
/**
* Returns the cached access token.
*
* <p>If not set, you should call {@link #refresh()} to fetch and cache an access token.
*
* @return The cached access token.
*/
public final AccessToken getAccessToken() {
OAuthValue localState = value;
if (localState != null) {
return localState.temporaryAccess;
}
return null;
}
/** Returns the credentials' refresh margin. */
@VisibleForTesting
Duration getRefreshMargin() {
return this.refreshMargin;
}
/** Returns the credentials' expiration margin. */
@VisibleForTesting
Duration getExpirationMargin() {
return this.expirationMargin;
}
@Override
public void getRequestMetadata(
final URI uri, Executor executor, final RequestMetadataCallback callback) {
Futures.addCallback(
asyncFetch(executor),
new FutureCallbackToMetadataCallbackAdapter(callback),
MoreExecutors.directExecutor());
}
/**
* Provide the request metadata by ensuring there is a current access token and providing it as an
* authorization bearer token.
*/
@Override
public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException {
return unwrapDirectFuture(asyncFetch(MoreExecutors.directExecutor())).requestMetadata;
}
/**
* Request a new token regardless of the current token state. If the current token is not expired,
* it will still be returned during the refresh.
*/
@Override
public void refresh() throws IOException {
AsyncRefreshResult refreshResult = getOrCreateRefreshTask();
refreshResult.executeIfNew(MoreExecutors.directExecutor());
unwrapDirectFuture(refreshResult.task);
}
/**
* Refresh these credentials only if they have expired or are expiring imminently.
*
* @throws IOException during token refresh.
*/
public void refreshIfExpired() throws IOException {
// asyncFetch will ensure that the token is refreshed
unwrapDirectFuture(asyncFetch(MoreExecutors.directExecutor()));
}
/**
* Attempts to get a fresh token.
*
* <p>If a fresh token is already available, it will be immediately returned. Otherwise a refresh
* will be scheduled using the passed in executor. While a token is being freshed, a stale value
* will be returned.
*/
private ListenableFuture<OAuthValue> asyncFetch(Executor executor) {
AsyncRefreshResult refreshResult = null;
// fast and common path: skip the lock if the token is fresh
// The inherent race condition here is a non-issue: even if the value gets replaced after the
// state check, the new token will still be fresh.
if (getState() == CacheState.FRESH) {
return Futures.immediateFuture(value);
}
// Schedule a refresh as necessary
synchronized (lock) {
if (getState() != CacheState.FRESH) {
refreshResult = getOrCreateRefreshTask();
}
}
// Execute the refresh if necessary. This should be done outside of the lock to avoid blocking
// metadata requests during a stale refresh.
if (refreshResult != null) {
refreshResult.executeIfNew(executor);
}
synchronized (lock) {
// Immediately resolve the token token if its not expired, or wait for the refresh task to
// complete
if (getState() != CacheState.EXPIRED) {
return Futures.immediateFuture(value);
} else if (refreshResult != null) {
return refreshResult.task;
} else {
// Should never happen
return Futures.immediateFailedFuture(
new IllegalStateException("Credentials expired, but there is no task to refresh"));
}
}
}
/**
* Atomically creates a single flight refresh token task.
*
* <p>Only a single refresh task can be scheduled at a time. If there is an existing task, it will
* be returned for subsequent invocations. However if a new task is created, it is the
* responsibility of the caller to execute it. The task will clear the single flight slow upon
* completion.
*/
private AsyncRefreshResult getOrCreateRefreshTask() {
synchronized (lock) {
if (refreshTask != null) {
return new AsyncRefreshResult(refreshTask, false);
}
final ListenableFutureTask<OAuthValue> task =
ListenableFutureTask.create(
new Callable<OAuthValue>() {
@Override
public OAuthValue call() throws Exception {
return OAuthValue.create(refreshAccessToken(), getAdditionalHeaders());
}
});
refreshTask = new RefreshTask(task, new RefreshTaskListener(task));
return new AsyncRefreshResult(refreshTask, true);
}
}
/**
* Async callback for committing the result from a token refresh.
*
* <p>The result will be stored, listeners are invoked and the single flight slot is cleared.
*/
private void finishRefreshAsync(ListenableFuture<OAuthValue> finishedTask) {
synchronized (lock) {
try {
this.value = Futures.getDone(finishedTask);
for (CredentialsChangedListener listener : changeListeners) {
listener.onChanged(this);
}
} catch (Exception e) {
// noop
} finally {
if (this.refreshTask != null && this.refreshTask.getTask() == finishedTask) {
this.refreshTask = null;
}
}
}
}
/**
* Unwraps the value from the future.
*
* <p>Under most circumstances, the underlying future will already be resolved by the
* DirectExecutor. In those cases, the error stacktraces will be rooted in the caller's call tree.
* However, in some cases when async and sync usage is mixed, it's possible that a blocking call
* will await an async future. In those cases, the stacktrace will be orphaned and be rooted in a
* thread of whatever executor the async call used. This doesn't affect correctness and is
* extremely unlikely.
*/
private static <T> T unwrapDirectFuture(ListenableFuture<T> future) throws IOException {
try {
return future.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Interrupted while asynchronously refreshing the access token", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof IOException) {
throw (IOException) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else {
throw new IOException("Unexpected error refreshing access token", cause);
}
}
}
/** Computes the effective credential state in relation to the current time. */
private CacheState getState() {
OAuthValue localValue = value;
if (localValue == null) {
return CacheState.EXPIRED;
}
Date expirationTime = localValue.temporaryAccess.getExpirationTime();
if (expirationTime == null) {
return CacheState.FRESH;
}
Duration remaining = Duration.ofMillis(expirationTime.getTime() - clock.currentTimeMillis());
if (remaining.compareTo(expirationMargin) <= 0) {
return CacheState.EXPIRED;
}
if (remaining.compareTo(refreshMargin) <= 0) {
return CacheState.STALE;
}
return CacheState.FRESH;
}
/**
* Method to refresh the access token according to the specific type of credentials.
*
* <p>Throws IllegalStateException if not overridden since direct use of OAuth2Credentials is only
* for temporary or non-refreshing access tokens.
*
* @return never
* @throws IllegalStateException always. OAuth2Credentials does not support refreshing the access
* token. An instance with a new access token or a derived type that supports refreshing
* should be used instead.
*/
public AccessToken refreshAccessToken() throws IOException {
throw new IllegalStateException(
"OAuth2Credentials instance does not support refreshing the"
+ " access token. An instance with a new access token should be used, or a derived type"
+ " that supports refreshing.");
}
/**
* Provide additional headers to return as request metadata.
*
* @return additional headers
*/
protected Map<String, List<String>> getAdditionalHeaders() {
return EMPTY_EXTRA_HEADERS;
}
/**
* Adds a listener that is notified when the Credentials data changes.
*
* <p>This is called when token content changes, such as when the access token is refreshed. This
* is typically used by code caching the access token.
*
* @param listener the listener to be added
*/
public final void addChangeListener(CredentialsChangedListener listener) {
synchronized (lock) {
if (changeListeners == null) {
changeListeners = new ArrayList<>();
}
changeListeners.add(listener);
}
}
/**
* Removes a listener that was added previously.
*
* @param listener The listener to be removed.
*/
public final void removeChangeListener(CredentialsChangedListener listener) {
synchronized (lock) {
if (changeListeners != null) {
changeListeners.remove(listener);
}
}
}
/**
* Listener for changes to credentials.
*
* <p>This is called when token content changes, such as when the access token is refreshed. This
* is typically used by code caching the access token.
*/
public interface CredentialsChangedListener {
/**
* Notifies that the credentials have changed.
*
* <p>This is called when token content changes, such as when the access token is refreshed.
* This is typically used by code caching the access token.
*
* @param credentials The updated credentials instance
* @throws IOException My be thrown by listeners if saving credentials fails.
*/
void onChanged(OAuth2Credentials credentials) throws IOException;
}
@Override
public int hashCode() {
return Objects.hashCode(value);
}
@Nullable
protected Map<String, List<String>> getRequestMetadataInternal() {
OAuthValue localValue = value;
if (localValue != null) {
return localValue.requestMetadata;
}
return null;
}
@Override
public String toString() {
OAuthValue localValue = value;
Map<String, List<String>> requestMetadata = null;
AccessToken temporaryAccess = null;
if (localValue != null) {
requestMetadata = localValue.requestMetadata;
temporaryAccess = localValue.temporaryAccess;
}
return MoreObjects.toStringHelper(this)
.add("requestMetadata", requestMetadata)
.add("temporaryAccess", temporaryAccess)
.toString();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof OAuth2Credentials)) {
return false;
}
OAuth2Credentials other = (OAuth2Credentials) obj;
return Objects.equals(this.value, other.value);
}
private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
input.defaultReadObject();
clock = Clock.SYSTEM;
refreshTask = null;
}
@SuppressWarnings("unchecked")
protected static <T> T newInstance(String className) throws IOException, ClassNotFoundException {
try {
return (T) Class.forName(className).newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IOException(e);
}
}
/**
* Returns the first service provider from the given service loader.
*
* @param clazz The class of the service provider to load.
* @param defaultInstance The default instance to return if no service providers are found.
* @param <T> The type of the service provider.
* @return The first service provider from the service loader, or the {@code defaultInstance} if
* no service providers are found.
*/
public static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance) {
return Iterables.getFirst(ServiceLoader.load(clazz), defaultInstance);
}
public static Builder newBuilder() {
return new Builder();
}
public Builder toBuilder() {
return new Builder(this);
}
/** Stores an immutable snapshot of the accesstoken owned by {@link OAuth2Credentials} */
static class OAuthValue implements Serializable {
private final AccessToken temporaryAccess;
private final Map<String, List<String>> requestMetadata;
static OAuthValue create(AccessToken token, Map<String, List<String>> additionalHeaders) {
return new OAuthValue(
token,
ImmutableMap.<String, List<String>>builder()
.put(
AuthHttpConstants.AUTHORIZATION,
ImmutableList.of(OAuth2Utils.BEARER_PREFIX + token.getTokenValue()))
.putAll(additionalHeaders)
.build());
}
private OAuthValue(AccessToken temporaryAccess, Map<String, List<String>> requestMetadata) {
this.temporaryAccess = temporaryAccess;
this.requestMetadata = requestMetadata;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof OAuthValue)) {
return false;
}
OAuthValue other = (OAuthValue) obj;
return Objects.equals(this.requestMetadata, other.requestMetadata)
&& Objects.equals(this.temporaryAccess, other.temporaryAccess);
}
@Override
public int hashCode() {
return Objects.hash(temporaryAccess, requestMetadata);
}
}
enum CacheState {
FRESH,
STALE,
EXPIRED;
}
static class FutureCallbackToMetadataCallbackAdapter implements FutureCallback<OAuthValue> {
private final RequestMetadataCallback callback;
public FutureCallbackToMetadataCallbackAdapter(RequestMetadataCallback callback) {
this.callback = callback;
}
@Override
public void onSuccess(@Nullable OAuthValue value) {
callback.onSuccess(value.requestMetadata);
}
@Override
public void onFailure(Throwable throwable) {
// refreshAccessToken will be invoked in an executor, so if it fails unwrap the underlying
// error
if (throwable instanceof ExecutionException) {
throwable = throwable.getCause();
}
callback.onFailure(throwable);
}
}
/**
* Result from {@link com.google.auth.oauth2.OAuth2Credentials#getOrCreateRefreshTask()}.
*
* <p>Contains the the refresh task and a flag indicating if the task is newly created. If the
* task is newly created, it is the caller's responsibility to execute it.
*/
static class AsyncRefreshResult {
private final RefreshTask task;
private final boolean isNew;
AsyncRefreshResult(RefreshTask task, boolean isNew) {
this.task = task;
this.isNew = isNew;
}
void executeIfNew(Executor executor) {
if (isNew) {
executor.execute(task);
}
}
}
@VisibleForTesting
class RefreshTaskListener implements Runnable {
private ListenableFutureTask<OAuthValue> task;
RefreshTaskListener(ListenableFutureTask<OAuthValue> task) {
this.task = task;
}
@Override
public void run() {
finishRefreshAsync(task);
}
}
class RefreshTask extends AbstractFuture<OAuthValue> implements Runnable {
private final ListenableFutureTask<OAuthValue> task;
private final RefreshTaskListener listener;
RefreshTask(ListenableFutureTask<OAuthValue> task, RefreshTaskListener listener) {
this.task = task;
this.listener = listener;
// Update Credential state first
task.addListener(listener, MoreExecutors.directExecutor());
// Then notify the world
Futures.addCallback(
task,
new FutureCallback<OAuthValue>() {
@Override
public void onSuccess(OAuthValue result) {
RefreshTask.this.set(result);
}
@Override
public void onFailure(Throwable t) {
RefreshTask.this.setException(t);
}
},
MoreExecutors.directExecutor());
}
public ListenableFutureTask<OAuthValue> getTask() {
return this.task;
}
@Override
public void run() {
task.run();
}
}
public static class Builder {
private AccessToken accessToken;
private Duration refreshMargin = DEFAULT_REFRESH_MARGIN;
private Duration expirationMargin = DEFAULT_EXPIRATION_MARGIN;
protected Builder() {}
protected Builder(OAuth2Credentials credentials) {
this.accessToken = credentials.getAccessToken();
this.refreshMargin = credentials.refreshMargin;
this.expirationMargin = credentials.expirationMargin;
}
@CanIgnoreReturnValue
public Builder setAccessToken(AccessToken token) {
this.accessToken = token;
return this;
}
@CanIgnoreReturnValue
public Builder setRefreshMargin(Duration refreshMargin) {
this.refreshMargin = refreshMargin;
return this;
}
public Duration getRefreshMargin() {
return refreshMargin;
}
@CanIgnoreReturnValue
public Builder setExpirationMargin(Duration expirationMargin) {
this.expirationMargin = expirationMargin;
return this;
}
public Duration getExpirationMargin() {
return expirationMargin;
}
public AccessToken getAccessToken() {
return accessToken;
}
public OAuth2Credentials build() {
return new OAuth2Credentials(accessToken, refreshMargin, expirationMargin);
}
}
}