Skip to content

Commit b46a0a6

Browse files
author
Yaniv Inbar
committed
Added constructors to AppIdentityCredential to allow a client to provide AppIdentityService non-statically. Thanks Miles Chaston!
1 parent 4f95930 commit b46a0a6

File tree

1 file changed

+104
-6
lines changed
  • google-api-client-appengine/src/main/java/com/google/api/client/googleapis/extensions/appengine/auth/oauth2

1 file changed

+104
-6
lines changed

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

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
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.collect.Lists;
24+
import com.google.common.base.Preconditions;
25+
import com.google.common.collect.ImmutableList;
2526

2627
import java.io.IOException;
2728
import java.util.List;
@@ -52,21 +53,35 @@ public static HttpRequestFactory createRequestFactory(
5253
*/
5354
public class AppIdentityCredential implements HttpRequestInitializer, HttpExecuteInterceptor {
5455

56+
/** App Identity Service that provides the access token. */
57+
private final AppIdentityService appIdentityService;
58+
5559
/** OAuth scopes. */
56-
private final List<String> scopes;
60+
private final ImmutableList<String> scopes;
5761

5862
/**
5963
* @param scopes OAuth scopes
6064
*/
6165
public AppIdentityCredential(Iterable<String> scopes) {
62-
this.scopes = Lists.newArrayList(scopes.iterator());
66+
this(AppIdentityServiceFactory.getAppIdentityService(), ImmutableList.copyOf(scopes));
6367
}
6468

6569
/**
6670
* @param scopes OAuth scopes
6771
*/
6872
public AppIdentityCredential(String... scopes) {
69-
this.scopes = Lists.newArrayList(scopes);
73+
this(AppIdentityServiceFactory.getAppIdentityService(), ImmutableList.copyOf(scopes));
74+
}
75+
76+
/**
77+
* @param appIdentityService App Identity Service that provides the access token
78+
* @param scopes OAuth scopes
79+
*
80+
* @since 1.12
81+
*/
82+
protected AppIdentityCredential(AppIdentityService appIdentityService, List<String> scopes) {
83+
this.appIdentityService = Preconditions.checkNotNull(appIdentityService);
84+
this.scopes = ImmutableList.copyOf(scopes);
7085
}
7186

7287
@Override
@@ -87,11 +102,94 @@ public void initialize(HttpRequest request) throws IOException {
87102
@Override
88103
public void intercept(HttpRequest request) throws Exception {
89104
try {
90-
String accessToken =
91-
AppIdentityServiceFactory.getAppIdentityService().getAccessToken(scopes).getAccessToken();
105+
String accessToken = appIdentityService.getAccessToken(scopes).getAccessToken();
92106
BearerToken.authorizationHeaderAccessMethod().intercept(request, accessToken);
93107
} catch (AppIdentityServiceFailureException e) {
94108
throw new Exception(e);
95109
}
96110
}
111+
112+
/**
113+
* Gets the App Identity Service that provides the access token.
114+
*
115+
* @since 1.12
116+
*/
117+
public final AppIdentityService getAppIdentityService() {
118+
return appIdentityService;
119+
}
120+
121+
/**
122+
* Gets the OAuth scopes.
123+
*
124+
* @since 1.12
125+
*/
126+
public final List<String> getScopes() {
127+
return scopes;
128+
}
129+
130+
/**
131+
* Builder for {@link AppIdentityCredential}.
132+
*
133+
* <p>
134+
* Implementation is not thread-safe.
135+
* </p>
136+
*
137+
* @since 1.12
138+
*/
139+
public static class Builder {
140+
141+
/** App Identity Service that provides the access token. */
142+
private AppIdentityService appIdentityService;
143+
144+
/** OAuth scopes. */
145+
private final ImmutableList<String> scopes;
146+
147+
/**
148+
* Returns an instance of a new builder.
149+
*
150+
* @param scopes OAuth scopes
151+
*/
152+
public Builder(Iterable<String> scopes) {
153+
this.scopes = ImmutableList.copyOf(scopes);
154+
}
155+
156+
/**
157+
* Returns an instance of a new builder.
158+
*
159+
* @param scopes OAuth scopes
160+
*/
161+
public Builder(String... scopes) {
162+
this.scopes = ImmutableList.copyOf(scopes);
163+
}
164+
165+
/**
166+
* Sets the App Identity Service that provides the access token.
167+
* <p>
168+
* If not explicitly set, the {@link AppIdentityServiceFactory#getAppIdentityService()} method
169+
* will be used to provide the App Identity Service.
170+
* </p>
171+
*
172+
* <p>
173+
* Overriding is only supported for the purpose of calling the super implementation and changing
174+
* the return type, but nothing else.
175+
* </p>
176+
*/
177+
public Builder setAppIdentityService(AppIdentityService appIdentityService) {
178+
this.appIdentityService = Preconditions.checkNotNull(appIdentityService);
179+
return this;
180+
}
181+
182+
/**
183+
* Returns a new {@link AppIdentityCredential}.
184+
*/
185+
public AppIdentityCredential build() {
186+
AppIdentityService appIdentityService = this.appIdentityService;
187+
if (appIdentityService == null) {
188+
// Lazily retrieved rather than setting as the default value in order to not add runtime
189+
// dependencies on AppIdentityServiceFactory unless it is actually being used.
190+
appIdentityService = AppIdentityServiceFactory.getAppIdentityService();
191+
}
192+
return new AppIdentityCredential(appIdentityService, scopes);
193+
}
194+
}
97195
}

0 commit comments

Comments
 (0)