/
Credential.java
827 lines (749 loc) · 26.8 KB
/
Credential.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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*
* Copyright (c) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.api.client.auth.oauth2;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpExecuteInterceptor;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpStatusCodes;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.HttpUnsuccessfulResponseHandler;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.Clock;
import com.google.api.client.util.Lists;
import com.google.api.client.util.Objects;
import com.google.api.client.util.Preconditions;
import com.google.api.client.util.store.DataStoreFactory;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Thread-safe OAuth 2.0 helper for accessing protected resources using an access token, as well as
* optionally refreshing the access token when it expires using a refresh token.
*
* <p>
* Sample usage:
* </p>
*
* <pre>
public static Credential createCredentialWithAccessTokenOnly(
HttpTransport transport, JsonFactory jsonFactory, TokenResponse tokenResponse) {
return new Credential(BearerToken.authorizationHeaderAccessMethod()).setFromTokenResponse(
tokenResponse);
}
public static Credential createCredentialWithRefreshToken(
HttpTransport transport, JsonFactory jsonFactory, TokenResponse tokenResponse) {
return new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()).setTransport(
transport)
.setJsonFactory(jsonFactory)
.setTokenServerUrl(
new GenericUrl("https://server.example.com/token"))
.setClientAuthentication(new BasicAuthentication("s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw"))
.build()
.setFromTokenResponse(tokenResponse);
}
* </pre>
*
* <p>
* If you need to persist the access token in a data store, use {@link DataStoreFactory} and
* {@link Builder#addRefreshListener(CredentialRefreshListener)} with
* {@link DataStoreCredentialRefreshListener}.
* </p>
*
* <p>
* If you have a custom request initializer, request execute interceptor, or unsuccessful response
* handler, take a look at the sample usage for {@link HttpExecuteInterceptor} and
* {@link HttpUnsuccessfulResponseHandler}, which are interfaces that this class also implements.
* </p>
*
* @since 1.7
* @author Yaniv Inbar
*/
public class Credential
implements
HttpExecuteInterceptor,
HttpRequestInitializer,
HttpUnsuccessfulResponseHandler {
static final Logger LOGGER = Logger.getLogger(Credential.class.getName());
/**
* Method of presenting the access token to the resource server as specified in <a
* href="http://tools.ietf.org/html/rfc6749#section-7">Accessing Protected Resources</a>.
*/
public interface AccessMethod {
/**
* Intercept the HTTP request during {@link Credential#intercept(HttpRequest)} right before the
* HTTP request executes by providing the access token.
*
* @param request HTTP request
* @param accessToken access token
*/
void intercept(HttpRequest request, String accessToken) throws IOException;
/**
* Retrieve the original access token in the HTTP request, as provided in
* {@link #intercept(HttpRequest, String)}.
*
* @param request HTTP request
* @return original access token or {@code null} for none
*/
String getAccessTokenFromRequest(HttpRequest request);
}
/** Lock on the token response information. */
private final Lock lock = new ReentrantLock();
/**
* Method of presenting the access token to the resource server (for example
* {@link BearerToken.AuthorizationHeaderAccessMethod}).
*/
private final AccessMethod method;
/** Clock used to provide the currentMillis. */
private final Clock clock;
/** Access token issued by the authorization server. */
private String accessToken;
/**
* Expected expiration time in milliseconds based on {@link #setExpiresInSeconds} or {@code null}
* for none.
*/
private Long expirationTimeMilliseconds;
/**
* Refresh token which can be used to obtain new access tokens using the same authorization grant
* or {@code null} for none.
*/
private String refreshToken;
/** HTTP transport for executing refresh token request or {@code null} for none. */
private final HttpTransport transport;
/** Client authentication or {@code null} for none. */
private final HttpExecuteInterceptor clientAuthentication;
/**
* JSON factory to use for parsing response for refresh token request or {@code null} for none.
*/
private final JsonFactory jsonFactory;
/** Encoded token server URL or {@code null} for none. */
private final String tokenServerEncodedUrl;
/** Unmodifiable collection of listeners for refresh token results. */
private final Collection<CredentialRefreshListener> refreshListeners;
/**
* HTTP request initializer for refresh token requests to the token server or {@code null} for
* none.
*/
private final HttpRequestInitializer requestInitializer;
/**
* Constructor with the ability to access protected resources, but not refresh tokens.
*
* <p>
* To use with the ability to refresh tokens, use {@link Builder}.
* </p>
*
* @param method method of presenting the access token to the resource server (for example
* {@link BearerToken.AuthorizationHeaderAccessMethod})
*/
public Credential(AccessMethod method) {
this(new Builder(method));
}
/**
* @param builder credential builder
*
* @since 1.14
*/
protected Credential(Builder builder) {
method = Preconditions.checkNotNull(builder.method);
transport = builder.transport;
jsonFactory = builder.jsonFactory;
tokenServerEncodedUrl = builder.tokenServerUrl == null ? null : builder.tokenServerUrl.build();
clientAuthentication = builder.clientAuthentication;
requestInitializer = builder.requestInitializer;
refreshListeners = Collections.unmodifiableCollection(builder.refreshListeners);
clock = Preconditions.checkNotNull(builder.clock);
}
/**
* {@inheritDoc}
* <p>
* Default implementation is to try to refresh the access token if there is no access token or if
* we are 1 minute away from expiration. If token server is unavailable, it will try to use the
* access token even if has expired. If a 4xx error is encountered while refreshing the token,
* {@link TokenResponseException} is thrown. If successful, it will call {@link #getMethod()} and
* {@link AccessMethod#intercept}.
* </p>
*
* <p>
* Subclasses may override.
* </p>
*/
public void intercept(HttpRequest request) throws IOException {
lock.lock();
try {
Long expiresIn = getExpiresInSeconds();
// check if token will expire in a minute
if (accessToken == null || expiresIn != null && expiresIn <= 60) {
refreshToken();
if (accessToken == null) {
// nothing we can do without an access token
return;
}
}
method.intercept(request, accessToken);
} finally {
lock.unlock();
}
}
/**
* {@inheritDoc}
* <p>
* Default implementation checks if {@code WWW-Authenticate} exists and contains a "Bearer" value
* (see <a href="http://tools.ietf.org/html/rfc6750#section-3.1">rfc6750 section 3.1</a> for more
* details). If so, it calls {@link #refreshToken} in case the error code contains
* {@code invalid_token}. If there is no "Bearer" in {@code WWW-Authenticate} and the status code
* is {@link HttpStatusCodes#STATUS_CODE_UNAUTHORIZED} it calls {@link #refreshToken}. If
* {@link #executeRefreshToken()} throws an I/O exception, this implementation will log the
* exception and return {@code false}. Subclasses may override.
* </p>
*/
public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry) {
boolean refreshToken = false;
boolean bearer = false;
List<String> authenticateList = response.getHeaders().getAuthenticateAsList();
// TODO(peleyal): this logic should be implemented as a pluggable interface, in the same way we
// implement different AccessMethods
// if authenticate list is not null we will check if one of the entries contains "Bearer"
if (authenticateList != null) {
for (String authenticate : authenticateList) {
if (authenticate.startsWith(BearerToken.AuthorizationHeaderAccessMethod.HEADER_PREFIX)) {
// mark that we found a "Bearer" value, and check if there is a invalid_token error
bearer = true;
refreshToken = BearerToken.INVALID_TOKEN_ERROR.matcher(authenticate).find();
break;
}
}
}
// if "Bearer" wasn't found, we will refresh the token, if we got 401
if (!bearer) {
refreshToken = response.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED;
}
if (refreshToken) {
try {
lock.lock();
try {
// need to check if another thread has already refreshed the token
return !Objects.equal(accessToken, method.getAccessTokenFromRequest(request))
|| refreshToken();
} finally {
lock.unlock();
}
} catch (IOException exception) {
LOGGER.log(Level.SEVERE, "unable to refresh token", exception);
}
}
return false;
}
public void initialize(HttpRequest request) throws IOException {
request.setInterceptor(this);
request.setUnsuccessfulResponseHandler(this);
}
/** Returns the access token or {@code null} for none. */
public final String getAccessToken() {
lock.lock();
try {
return accessToken;
} finally {
lock.unlock();
}
}
/**
* Sets the access token.
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*
* @param accessToken access token or {@code null} for none
*/
public Credential setAccessToken(String accessToken) {
lock.lock();
try {
this.accessToken = accessToken;
} finally {
lock.unlock();
}
return this;
}
/**
* Return the method of presenting the access token to the resource server (for example
* {@link BearerToken.AuthorizationHeaderAccessMethod}).
*/
public final AccessMethod getMethod() {
return method;
}
/**
* Returns the clock used for expiration checks by this Credential. Mostly used for unit-testing.
* @since 1.9
*/
public final Clock getClock() {
return clock;
}
/** Return the HTTP transport for executing refresh token request or {@code null} for none. */
public final HttpTransport getTransport() {
return transport;
}
/**
* Returns the JSON factory to use for parsing response for refresh token request or {@code null}
* for none.
*/
public final JsonFactory getJsonFactory() {
return jsonFactory;
}
/** Returns the encoded authorization server URL or {@code null} for none. */
public final String getTokenServerEncodedUrl() {
return tokenServerEncodedUrl;
}
/**
* Returns the refresh token associated with the access token to be refreshed or {@code null} for
* none.
*/
public final String getRefreshToken() {
lock.lock();
try {
return refreshToken;
} finally {
lock.unlock();
}
}
/**
* Sets the refresh token.
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*
* @param refreshToken refresh token or {@code null} for none
*/
public Credential setRefreshToken(String refreshToken) {
lock.lock();
try {
if (refreshToken != null) {
Preconditions.checkArgument(jsonFactory != null && transport != null
&& clientAuthentication != null && tokenServerEncodedUrl != null,
"Please use the Builder and call setJsonFactory, setTransport, setClientAuthentication"
+ " and setTokenServerUrl/setTokenServerEncodedUrl");
}
this.refreshToken = refreshToken;
} finally {
lock.unlock();
}
return this;
}
/**
* Expected expiration time in milliseconds relative to the
* {@link System#currentTimeMillis() Java epoch}, or {@code null} for none.
*/
public final Long getExpirationTimeMilliseconds() {
lock.lock();
try {
return expirationTimeMilliseconds;
} finally {
lock.unlock();
}
}
/**
* Sets the expected expiration time in milliseconds relative to the
* {@link System#currentTimeMillis() Java epoch}, or {@code null} for none.
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*/
public Credential setExpirationTimeMilliseconds(Long expirationTimeMilliseconds) {
lock.lock();
try {
this.expirationTimeMilliseconds = expirationTimeMilliseconds;
} finally {
lock.unlock();
}
return this;
}
/**
* Returns the remaining lifetime in seconds of the access token (for example 3600 for an hour
* from now, or -3600 if expired an hour ago) or {@code null} if unknown.
*/
public final Long getExpiresInSeconds() {
lock.lock();
try {
if (expirationTimeMilliseconds == null) {
return null;
}
return (expirationTimeMilliseconds - clock.currentTimeMillis()) / 1000;
} finally {
lock.unlock();
}
}
/**
* Sets the lifetime in seconds of the access token (for example 3600 for an hour from now)
* or {@code null} for none.
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*
* @param expiresIn lifetime in seconds of the access token (for example 3600 for an hour from
* now) or {@code null} for none
*/
public Credential setExpiresInSeconds(Long expiresIn) {
return setExpirationTimeMilliseconds(
expiresIn == null ? null : clock.currentTimeMillis() + expiresIn * 1000);
}
/** Returns the client authentication or {@code null} for none. */
public final HttpExecuteInterceptor getClientAuthentication() {
return clientAuthentication;
}
/**
* Returns the HTTP request initializer for refresh token requests to the token server or
* {@code null} for none.
*/
public final HttpRequestInitializer getRequestInitializer() {
return requestInitializer;
}
/**
* Request a new access token from the authorization endpoint.
*
* <p>
* On success, it will call {@link #setFromTokenResponse(TokenResponse)}, call
* {@link CredentialRefreshListener#onTokenResponse} with the token response, and return
* {@code true}. On error, it will call {@link #setAccessToken(String)} and
* {@link #setExpiresInSeconds(Long)} with {@code null}, call
* {@link CredentialRefreshListener#onTokenErrorResponse} with the token error response, and
* return {@code false}. If a 4xx error is encountered while refreshing the token,
* {@link TokenResponseException} is thrown.
* </p>
*
* <p>
* If there is no refresh token, it will quietly return {@code false}.
* </p>
*
* @return whether a new access token was successfully retrieved
*/
public final boolean refreshToken() throws IOException {
lock.lock();
try {
try {
TokenResponse tokenResponse = executeRefreshToken();
if (tokenResponse != null) {
setFromTokenResponse(tokenResponse);
for (CredentialRefreshListener refreshListener : refreshListeners) {
refreshListener.onTokenResponse(this, tokenResponse);
}
return true;
}
} catch (TokenResponseException e) {
boolean statusCode4xx = 400 <= e.getStatusCode() && e.getStatusCode() < 500;
// check if it is a normal error response
if (e.getDetails() != null && statusCode4xx) {
// We were unable to get a new access token (e.g. it may have been revoked), we must now
// indicate that our current token is invalid.
setAccessToken(null);
setExpiresInSeconds(null);
}
for (CredentialRefreshListener refreshListener : refreshListeners) {
refreshListener.onTokenErrorResponse(this, e.getDetails());
}
if (statusCode4xx) {
throw e;
}
}
return false;
} finally {
lock.unlock();
}
}
/**
* Sets the {@link #setAccessToken access token}, {@link #setRefreshToken refresh token} (if
* available), and {@link #setExpiresInSeconds expires-in time} based on the values from the token
* response.
*
* <p>
* It does not call the refresh listeners.
* </p>
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*
* @param tokenResponse successful token response
*/
public Credential setFromTokenResponse(TokenResponse tokenResponse) {
setAccessToken(tokenResponse.getAccessToken());
// handle case of having a refresh token previous, but no refresh token in current
// response
if (tokenResponse.getRefreshToken() != null) {
setRefreshToken(tokenResponse.getRefreshToken());
}
setExpiresInSeconds(tokenResponse.getExpiresInSeconds());
return this;
}
/**
* Executes a request for new credentials from the token server.
*
* <p>
* The default implementation calls {@link RefreshTokenRequest#execute()} using the
* {@link #getTransport()}, {@link #getJsonFactory()}, {@link #getRequestInitializer()},
* {@link #getTokenServerEncodedUrl()}, {@link #getRefreshToken()}, and the
* {@link #getClientAuthentication()}. If {@link #getRefreshToken()} is {@code null}, it instead
* returns {@code null}.
* </p>
*
* <p>
* Subclasses may override for a different implementation. Implementations can assume proper
* thread synchronization is already taken care of inside {@link #refreshToken()}.
* </p>
*
* @return successful response from the token server or {@code null} if it is not possible to
* refresh the access token
* @throws TokenResponseException if an error response was received from the token server
*/
protected TokenResponse executeRefreshToken() throws IOException {
if (refreshToken == null) {
return null;
}
return new RefreshTokenRequest(transport, jsonFactory, new GenericUrl(tokenServerEncodedUrl),
refreshToken).setClientAuthentication(clientAuthentication)
.setRequestInitializer(requestInitializer).execute();
}
/** Returns the unmodifiable collection of listeners for refresh token results. */
public final Collection<CredentialRefreshListener> getRefreshListeners() {
return refreshListeners;
}
/**
* Credential builder.
*
* <p>
* Implementation is not thread-safe.
* </p>
*/
public static class Builder {
/**
* Method of presenting the access token to the resource server (for example
* {@link BearerToken.AuthorizationHeaderAccessMethod}).
*/
final AccessMethod method;
/**
* HTTP transport for executing refresh token request or {@code null} if not refreshing tokens.
*/
HttpTransport transport;
/**
* JSON factory to use for parsing response for refresh token request or {@code null} if not
* refreshing tokens.
*/
JsonFactory jsonFactory;
/** Token server URL or {@code null} if not refreshing tokens. */
GenericUrl tokenServerUrl;
/** Clock used for expiration checks. */
Clock clock = Clock.SYSTEM;
/**
* Client authentication or {@code null} for none (see
* {@link TokenRequest#setClientAuthentication(HttpExecuteInterceptor)}).
*/
HttpExecuteInterceptor clientAuthentication;
/**
* HTTP request initializer for refresh token requests to the token server or {@code null} for
* none.
*/
HttpRequestInitializer requestInitializer;
/** Listeners for refresh token results. */
Collection<CredentialRefreshListener> refreshListeners = Lists.newArrayList();
/**
* @param method method of presenting the access token to the resource server (for example
* {@link BearerToken.AuthorizationHeaderAccessMethod})
*/
public Builder(AccessMethod method) {
this.method = Preconditions.checkNotNull(method);
}
/** Returns a new credential instance. */
public Credential build() {
return new Credential(this);
}
/**
* Returns the method of presenting the access token to the resource server (for example
* {@link BearerToken.AuthorizationHeaderAccessMethod}).
*/
public final AccessMethod getMethod() {
return method;
}
/**
* Returns the HTTP transport for executing refresh token request or {@code null} if not
* refreshing tokens.
*/
public final HttpTransport getTransport() {
return transport;
}
/**
* Sets the HTTP transport for executing refresh token request or {@code null} if not refreshing
* tokens.
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*/
public Builder setTransport(HttpTransport transport) {
this.transport = transport;
return this;
}
/**
* Returns the clock to use for expiration checks or {@link Clock#SYSTEM} as default.
* @since 1.9
*/
public final Clock getClock() {
return clock;
}
/**
* Sets the clock to use for expiration checks.
*
* <p>
* The default value is Clock.SYSTEM.
* </p>
*
* @since 1.9
*/
public Builder setClock(Clock clock) {
this.clock = Preconditions.checkNotNull(clock);
return this;
}
/**
* Returns the JSON factory to use for parsing response for refresh token request or
* {@code null} if not refreshing tokens.
*/
public final JsonFactory getJsonFactory() {
return jsonFactory;
}
/**
* Sets the JSON factory to use for parsing response for refresh token request or {@code null}
* if not refreshing tokens.
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*/
public Builder setJsonFactory(JsonFactory jsonFactory) {
this.jsonFactory = jsonFactory;
return this;
}
/** Returns the token server URL or {@code null} if not refreshing tokens. */
public final GenericUrl getTokenServerUrl() {
return tokenServerUrl;
}
/**
* Sets the token server URL or {@code null} if not refreshing tokens.
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*/
public Builder setTokenServerUrl(GenericUrl tokenServerUrl) {
this.tokenServerUrl = tokenServerUrl;
return this;
}
/**
* Sets the encoded token server URL or {@code null} if not refreshing tokens.
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*/
public Builder setTokenServerEncodedUrl(String tokenServerEncodedUrl) {
this.tokenServerUrl =
tokenServerEncodedUrl == null ? null : new GenericUrl(tokenServerEncodedUrl);
return this;
}
/**
* Returns the client authentication or {@code null} for none (see
* {@link TokenRequest#setClientAuthentication(HttpExecuteInterceptor)}).
*/
public final HttpExecuteInterceptor getClientAuthentication() {
return clientAuthentication;
}
/**
* Sets the client authentication or {@code null} for none (see
* {@link TokenRequest#setClientAuthentication(HttpExecuteInterceptor)}).
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*/
public Builder setClientAuthentication(HttpExecuteInterceptor clientAuthentication) {
this.clientAuthentication = clientAuthentication;
return this;
}
/**
* Returns the HTTP request initializer for refresh token requests to the token server or
* {@code null} for none.
*/
public final HttpRequestInitializer getRequestInitializer() {
return requestInitializer;
}
/**
* Sets the HTTP request initializer for refresh token requests to the token server or
* {@code null} for none.
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*/
public Builder setRequestInitializer(HttpRequestInitializer requestInitializer) {
this.requestInitializer = requestInitializer;
return this;
}
/**
* Adds a listener for refresh token results.
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*
* @param refreshListener refresh listener
*/
public Builder addRefreshListener(CredentialRefreshListener refreshListener) {
refreshListeners.add(Preconditions.checkNotNull(refreshListener));
return this;
}
/** Returns the listeners for refresh token results. */
public final Collection<CredentialRefreshListener> getRefreshListeners() {
return refreshListeners;
}
/**
* Sets the listeners for refresh token results.
*
* <p>
* Overriding is only supported for the purpose of calling the super implementation and changing
* the return type, but nothing else.
* </p>
*/
public Builder setRefreshListeners(Collection<CredentialRefreshListener> refreshListeners) {
this.refreshListeners = Preconditions.checkNotNull(refreshListeners);
return this;
}
}
}