@@ -43,6 +43,8 @@ private static class AppEngineAuthCredentials extends AuthCredentials {
4343 private static class AppEngineCredentials extends GoogleCredentials {
4444
4545 private final Object appIdentityService ;
46+ private final Method getAccessToken ;
47+ private final Method getAccessTokenResult ;
4648 private final Collection <String > scopes ;
4749 private final boolean scopesRequired ;
4850
@@ -52,17 +54,26 @@ private static class AppEngineCredentials extends GoogleCredentials {
5254 Class .forName ("com.google.appengine.api.appidentity.AppIdentityServiceFactory" );
5355 Method method = factoryClass .getMethod ("getAppIdentityService" );
5456 this .appIdentityService = method .invoke (null );
57+ Class <?> serviceClass =
58+ Class .forName ("com.google.appengine.api.appidentity.AppIdentityService" );
59+ Class <?> tokenResultClass = Class .forName (
60+ "com.google.appengine.api.appidentity.AppIdentityService$GetAccessTokenResult" );
61+ this .getAccessTokenResult = serviceClass .getMethod ("getAccessToken" , Iterable .class );
62+ this .getAccessToken = tokenResultClass .getMethod ("getAccessToken" );
5563 this .scopes = null ;
5664 this .scopesRequired = true ;
5765 } catch (Exception e ) {
5866 throw new RuntimeException ("Could not create AppEngineCredentials using reflection." );
5967 }
6068 }
6169
62- AppEngineCredentials (Collection <String > scopes , Object appIdentityService ) {
63- this .appIdentityService = appIdentityService ;
64- this .scopes = scopes ;
65- this .scopesRequired = (scopes == null || scopes .isEmpty ());
70+ AppEngineCredentials (Collection <String > scopes , Object appIdentityService ,
71+ Method getAccessToken , Method getAccessTokenResult ) {
72+ this .appIdentityService = appIdentityService ;
73+ this .getAccessToken = getAccessToken ;
74+ this .getAccessTokenResult = getAccessTokenResult ;
75+ this .scopes = scopes ;
76+ this .scopesRequired = (scopes == null || scopes .isEmpty ());
6677 }
6778
6879 /**
@@ -74,13 +85,7 @@ public AccessToken refreshAccessToken() throws IOException {
7485 throw new IOException ("AppEngineCredentials requires createScoped call before use." );
7586 }
7687 try {
77- Class <?> serviceClass =
78- Class .forName ("com.google.appengine.api.appidentity.AppIdentityService" );
79- Class <?> tokenResultClass = Class .forName (
80- "com.google.appengine.api.appidentity.AppIdentityService$GetAccessTokenResult" );
81- Method getAccessTokenResult = serviceClass .getMethod ("getAccessToken" , Iterable .class );
8288 Object accessTokenResult = getAccessTokenResult .invoke (appIdentityService , scopes );
83- Method getAccessToken = tokenResultClass .getMethod ("getAccessToken" );
8489 String accessToken = (String ) getAccessToken .invoke (accessTokenResult );
8590 return new AccessToken (accessToken , null );
8691 } catch (Exception e ) {
@@ -95,7 +100,8 @@ public boolean createScopedRequired() {
95100
96101 @ Override
97102 public GoogleCredentials createScoped (Collection <String > scopes ) {
98- return new AppEngineCredentials (scopes , appIdentityService );
103+ return new AppEngineCredentials (
104+ scopes , appIdentityService , getAccessToken , getAccessTokenResult );
99105 }
100106 }
101107
@@ -121,7 +127,7 @@ public boolean equals(Object obj) {
121127 }
122128
123129 @ Override
124- protected GoogleCredentials credentials () {
130+ public GoogleCredentials credentials () {
125131 return new AppEngineCredentials ();
126132 }
127133
@@ -176,7 +182,7 @@ public boolean equals(Object obj) {
176182 }
177183
178184 @ Override
179- protected GoogleCredentials credentials () {
185+ public GoogleCredentials credentials () {
180186 return new ServiceAccountCredentials (null , account , privateKey , null , null );
181187 }
182188
@@ -232,26 +238,17 @@ public boolean equals(Object obj) {
232238 }
233239
234240 @ Override
235- protected GoogleCredentials credentials () {
241+ public GoogleCredentials credentials () {
236242 return googleCredentials ;
237243 }
238244
239- public ServiceAccountAuthCredentials toServiceAccountCredentials () {
240- if (googleCredentials instanceof ServiceAccountCredentials ) {
241- ServiceAccountCredentials credentials = (ServiceAccountCredentials ) googleCredentials ;
242- return new ServiceAccountAuthCredentials (credentials .getClientEmail (),
243- credentials .getPrivateKey ());
244- }
245- return null ;
246- }
247-
248245 @ Override
249246 public RestorableState <AuthCredentials > capture () {
250247 return STATE ;
251248 }
252249 }
253250
254- protected abstract GoogleCredentials credentials ();
251+ public abstract GoogleCredentials credentials ();
255252
256253 public static AuthCredentials createForAppEngine () {
257254 return AppEngineAuthCredentials .INSTANCE ;
@@ -297,11 +294,12 @@ public static ServiceAccountAuthCredentials createFor(String account, PrivateKey
297294 * Account Authentication</a>.
298295 * </p>
299296 *
300- * @param jsonCredentialStream stream for Service Account Credentials in JSON format
297+ * @param jsonCredentialStream stream for Service Account Credentials or User Credentials in JSON
298+ * format
301299 * @return the credentials instance.
302300 * @throws IOException if the credentials cannot be created from the stream.
303301 */
304- public static ServiceAccountAuthCredentials createForJson (InputStream jsonCredentialStream )
302+ public static AuthCredentials createForJson (InputStream jsonCredentialStream )
305303 throws IOException {
306304 GoogleCredentials tempCredentials = GoogleCredentials .fromStream (jsonCredentialStream );
307305 if (tempCredentials instanceof ServiceAccountCredentials ) {
@@ -310,9 +308,9 @@ public static ServiceAccountAuthCredentials createForJson(InputStream jsonCreden
310308 return new ServiceAccountAuthCredentials (
311309 tempServiceAccountCredentials .getClientEmail (),
312310 tempServiceAccountCredentials .getPrivateKey ());
313- } else {
314- throw new IOException (
315- "The given JSON Credentials Stream is not a service account credential." );
316311 }
312+ throw new IOException (
313+ "The given JSON credentials stream could not be parsed as service account credentials or"
314+ + " user credentials." );
317315 }
318316}
0 commit comments