@Override public GoogleCredentials createScoped(Collection<String> scopes) { return new AppEngineCredentials(scopes, this); }
private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException { input.defaultReadObject(); init(); }
@Test public void serialize() throws IOException, ClassNotFoundException { Collection<String> scopes = Collections.singleton("SomeScope"); AppEngineCredentials credentials = new TestAppEngineCredentials(scopes); GoogleCredentials deserializedCredentials = serializeAndDeserialize(credentials); assertEquals(credentials, deserializedCredentials); assertEquals(credentials.hashCode(), deserializedCredentials.hashCode()); assertEquals(credentials.toString(), deserializedCredentials.toString()); }
/** * Refresh the access token by getting it from the App Identity service. */ @Override public AccessToken refreshAccessToken() throws IOException { if (createScopedRequired()) { throw new IOException("AppEngineCredentials requires createScoped call before use."); } try { Object accessTokenResult = getAccessTokenResult.invoke(appIdentityService, scopes); String accessToken = (String) getAccessToken.invoke(accessTokenResult); Date expirationTime = (Date) getExpirationTime.invoke(accessTokenResult); return new AccessToken(accessToken, expirationTime); } catch (Exception e) { throw new IOException("Could not get the access token.", e); } }
private void init() throws IOException { try { Class<?> factoryClass = forName(APP_IDENTITY_SERVICE_FACTORY_CLASS); Method method = factoryClass.getMethod(GET_APP_IDENTITY_SERVICE_METHOD); this.appIdentityService = method.invoke(null); Class<?> serviceClass = forName(APP_IDENTITY_SERVICE_CLASS); Class<?> tokenResultClass = forName(GET_ACCESS_TOKEN_RESULT_CLASS); this.getAccessTokenResult = serviceClass.getMethod(GET_ACCESS_TOKEN_RESULT_METHOD, Iterable.class); this.getAccessToken = tokenResultClass.getMethod(GET_ACCESS_TOKEN_METHOD); this.getExpirationTime = tokenResultClass.getMethod(GET_EXPIRATION_TIME_METHOD); this.account = (String) serviceClass.getMethod(GET_SERVICE_ACCOUNT_NAME_METHOD) .invoke(appIdentityService); this.signForApp = serviceClass.getMethod(SIGN_FOR_APP_METHOD, byte[].class); Class<?> signingResultClass = forName(SIGNING_RESULT_CLASS); this.getSignature = signingResultClass.getMethod(GET_SIGNATURE_METHOD); } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { throw new IOException( "Application Default Credentials failed to create the Google App Engine service account" + " credentials. Check that the App Engine SDK is deployed.", ex); } }
@Test public void equals_false_scopes() throws IOException { final Collection<String> emptyScopes = Collections.emptyList(); Collection<String> scopes = Collections.singleton("SomeScope"); AppEngineCredentials credentials = new TestAppEngineCredentials(emptyScopes); AppEngineCredentials otherCredentials = new TestAppEngineCredentials(scopes); assertFalse(credentials.equals(otherCredentials)); assertFalse(otherCredentials.equals(credentials)); }
@Test public void toString_containsFields() throws IOException { String expectedToString = String.format( "TestAppEngineCredentials{scopes=[%s], scopesRequired=%b}", "SomeScope", false); Collection<String> scopes = Collections.singleton("SomeScope"); AppEngineCredentials credentials = new TestAppEngineCredentials(scopes); assertEquals(expectedToString, credentials.toString()); }
/** * Refresh the access token by getting it from the App Identity service. */ @Override public AccessToken refreshAccessToken() throws IOException { if (createScopedRequired()) { throw new IOException("AppEngineCredentials requires createScoped call before use."); } try { Object accessTokenResult = getAccessTokenResult.invoke(appIdentityService, scopes); String accessToken = (String) getAccessToken.invoke(accessTokenResult); Date expirationTime = (Date) getExpirationTime.invoke(accessTokenResult); return new AccessToken(accessToken, expirationTime); } catch (Exception e) { throw new IOException("Could not get the access token.", e); } }
private void init() throws IOException { try { Class<?> factoryClass = forName(APP_IDENTITY_SERVICE_FACTORY_CLASS); Method method = factoryClass.getMethod(GET_APP_IDENTITY_SERVICE_METHOD); this.appIdentityService = method.invoke(null); Class<?> serviceClass = forName(APP_IDENTITY_SERVICE_CLASS); Class<?> tokenResultClass = forName(GET_ACCESS_TOKEN_RESULT_CLASS); this.getAccessTokenResult = serviceClass.getMethod(GET_ACCESS_TOKEN_RESULT_METHOD, Iterable.class); this.getAccessToken = tokenResultClass.getMethod(GET_ACCESS_TOKEN_METHOD); this.getExpirationTime = tokenResultClass.getMethod(GET_EXPIRATION_TIME_METHOD); this.account = (String) serviceClass.getMethod(GET_SERVICE_ACCOUNT_NAME_METHOD) .invoke(appIdentityService); this.signForApp = serviceClass.getMethod(SIGN_FOR_APP_METHOD, byte[].class); Class<?> signingResultClass = forName(SIGNING_RESULT_CLASS); this.getSignature = signingResultClass.getMethod(GET_SIGNATURE_METHOD); } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { throw new IOException( "Application Default Credentials failed to create the Google App Engine service account" + " credentials. Check that the App Engine SDK is deployed.", ex); } }
@Override public GoogleCredentials createScoped(Collection<String> scopes) { return new AppEngineCredentials(scopes, this); }
private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException { input.defaultReadObject(); init(); }
private GoogleCredentials tryGetAppEngineCredential() throws IOException { // Checking for App Engine requires a class load, so check only once if (checkedAppEngine) { return null; } boolean onAppEngine = runningOnAppEngine(); checkedAppEngine = true; if (!onAppEngine) { return null; } return new AppEngineCredentials(Collections.<String>emptyList()); }
AppEngineCredentials(Collection<String> scopes) throws IOException { this.scopes = scopes == null ? ImmutableSet.<String>of() : ImmutableList.copyOf(scopes); this.scopesRequired = this.scopes.isEmpty(); init(); }
private GoogleCredentials tryGetAppEngineCredential() throws IOException { // Checking for App Engine requires a class load, so check only once if (checkedAppEngine) { return null; } boolean onAppEngine = runningOnAppEngine(); checkedAppEngine = true; if (!onAppEngine) { return null; } return new AppEngineCredentials(Collections.<String>emptyList()); }
AppEngineCredentials(Collection<String> scopes) throws IOException { this.scopes = scopes == null ? ImmutableSet.<String>of() : ImmutableList.copyOf(scopes); this.scopesRequired = this.scopes.isEmpty(); init(); }