Skip to content

Commit e4e6fb5

Browse files
author
Yaniv Inbar
committed
api issue 704: add GoogleIdTokenVerifier.verify that takes issuers & audiences parameters
https://code.google.com/p/google-api-java-client/issues/detail?id=704 switch to the new Builder pattern which simplifies implementation https://codereview.appspot.com/7086048/
1 parent cf13c8f commit e4e6fb5

12 files changed

Lines changed: 359 additions & 127 deletions

File tree

google-api-client-appengine/src/main/java/com/google/api/client/googleapis/extensions/appengine/auth/oauth2/AppIdentityCredential.java

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.google.appengine.api.appidentity.AppIdentityService;
2222
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
2323
import com.google.appengine.api.appidentity.AppIdentityServiceFailureException;
24-
import com.google.common.base.Preconditions;
2524
import com.google.common.collect.ImmutableList;
2625

2726
import java.io.IOException;
@@ -63,24 +62,42 @@ public class AppIdentityCredential implements HttpRequestInitializer, HttpExecut
6362
* @param scopes OAuth scopes
6463
*/
6564
public AppIdentityCredential(Iterable<String> scopes) {
66-
this(AppIdentityServiceFactory.getAppIdentityService(), ImmutableList.copyOf(scopes));
65+
this(new Builder(scopes));
6766
}
6867

6968
/**
7069
* @param scopes OAuth scopes
7170
*/
7271
public AppIdentityCredential(String... scopes) {
73-
this(AppIdentityServiceFactory.getAppIdentityService(), ImmutableList.copyOf(scopes));
72+
this(new Builder(scopes));
73+
}
74+
75+
/**
76+
* @param builder builder
77+
*
78+
* @since 1.14
79+
*/
80+
protected AppIdentityCredential(Builder builder) {
81+
// Lazily retrieved rather than setting as the default value in order to not add runtime
82+
// dependencies on AppIdentityServiceFactory unless it is actually being used.
83+
appIdentityService = builder.appIdentityService == null
84+
? AppIdentityServiceFactory.getAppIdentityService() : builder.appIdentityService;
85+
scopes = ImmutableList.copyOf(builder.scopes);
7486
}
7587

7688
/**
7789
* @param appIdentityService App Identity Service that provides the access token
7890
* @param scopes OAuth scopes
7991
*
8092
* @since 1.12
93+
* @deprecated (scheduled to be removed in 1.15) Use {@link #AppIdentityCredential(Builder)}
8194
*/
95+
@Deprecated
8296
protected AppIdentityCredential(AppIdentityService appIdentityService, List<String> scopes) {
83-
this.appIdentityService = Preconditions.checkNotNull(appIdentityService);
97+
// Lazily retrieved rather than setting as the default value in order to not add runtime
98+
// dependencies on AppIdentityServiceFactory unless it is actually being used.
99+
this.appIdentityService = appIdentityService == null
100+
? AppIdentityServiceFactory.getAppIdentityService() : appIdentityService;
84101
this.scopes = ImmutableList.copyOf(scopes);
85102
}
86103

@@ -133,11 +150,14 @@ public final List<String> getScopes() {
133150
*/
134151
public static class Builder {
135152

136-
/** App Identity Service that provides the access token. */
137-
private AppIdentityService appIdentityService;
153+
/**
154+
* App Identity Service that provides the access token or {@code null} to use
155+
* {@link AppIdentityServiceFactory#getAppIdentityService()}.
156+
*/
157+
AppIdentityService appIdentityService;
138158

139159
/** OAuth scopes. */
140-
private final ImmutableList<String> scopes;
160+
final ImmutableList<String> scopes;
141161

142162
/**
143163
* Returns an instance of a new builder.
@@ -158,33 +178,34 @@ public Builder(String... scopes) {
158178
}
159179

160180
/**
161-
* Sets the App Identity Service that provides the access token.
162-
* <p>
163-
* If not explicitly set, the {@link AppIdentityServiceFactory#getAppIdentityService()} method
164-
* will be used to provide the App Identity Service.
165-
* </p>
181+
* Returns the App Identity Service that provides the access token or {@code null} to use
182+
* {@link AppIdentityServiceFactory#getAppIdentityService()}.
183+
*
184+
* @since 1.14
185+
*/
186+
public final AppIdentityService getAppIdentityService() {
187+
return appIdentityService;
188+
}
189+
190+
/**
191+
* Sets the App Identity Service that provides the access token or {@code null} to use
192+
* {@link AppIdentityServiceFactory#getAppIdentityService()}.
166193
*
167194
* <p>
168195
* Overriding is only supported for the purpose of calling the super implementation and changing
169196
* the return type, but nothing else.
170197
* </p>
171198
*/
172199
public Builder setAppIdentityService(AppIdentityService appIdentityService) {
173-
this.appIdentityService = Preconditions.checkNotNull(appIdentityService);
200+
this.appIdentityService = appIdentityService;
174201
return this;
175202
}
176203

177204
/**
178205
* Returns a new {@link AppIdentityCredential}.
179206
*/
180207
public AppIdentityCredential build() {
181-
AppIdentityService appIdentityService = this.appIdentityService;
182-
if (appIdentityService == null) {
183-
// Lazily retrieved rather than setting as the default value in order to not add runtime
184-
// dependencies on AppIdentityServiceFactory unless it is actually being used.
185-
appIdentityService = AppIdentityServiceFactory.getAppIdentityService();
186-
}
187-
return new AppIdentityCredential(appIdentityService, scopes);
208+
return new AppIdentityCredential(this);
188209
}
189210
}
190211
}

google-api-client/src/main/java/com/google/api/client/googleapis/auth/oauth2/GoogleIdToken.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.io.IOException;
2424
import java.security.GeneralSecurityException;
25+
import java.util.Collection;
2526

2627
/**
2728
* Google ID tokens.
@@ -70,7 +71,11 @@ public GoogleIdToken(
7071

7172
/**
7273
* Verifies that this ID token is valid using {@link GoogleIdTokenVerifier#verify(GoogleIdToken)}.
74+
*
75+
* @deprecated (scheduled to be removed in 1.15) Use
76+
* {@link GoogleIdTokenVerifier#verify(GoogleIdToken, Collection, Collection)}
7377
*/
78+
@Deprecated
7479
public boolean verify(GoogleIdTokenVerifier verifier)
7580
throws GeneralSecurityException, IOException {
7681
return verifier.verify(this);

0 commit comments

Comments
 (0)