Java Code Examples for com.google.auth.oauth2.GoogleCredentials#refresh()

The following examples show how to use com.google.auth.oauth2.GoogleCredentials#refresh() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GoogleIdTokenAuthTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void testServiceAccountCredentialsWithAccessToken() throws IOException, GeneralSecurityException {
  Assume.assumeNotNull(credentials);
  final GoogleCredentials serviceAccountCredentials;
  final URI keyUri = URI.create("gs://styx-oss-test/styx-test-user.json");
  try (InputStream is = Files.newInputStream(Paths.get(keyUri))) {
    serviceAccountCredentials = GoogleCredentials.fromStream(is)
        .createScoped(ImmutableList.of(
            "https://www.googleapis.com/auth/cloud-platform",
            "https://www.googleapis.com/auth/userinfo.email"));
  }
  serviceAccountCredentials.refresh();
  final GoogleCredentials accessTokenCredentials = GoogleCredentials.newBuilder()
      .setAccessToken(serviceAccountCredentials.getAccessToken())
      .build();
  assertThat(canAcquireIdToken(accessTokenCredentials), is(true));
}
 
Example 2
Source File: GoogleIdTokenAuthTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void testServiceAccountCredentialsWithAccessTokenFailsIfMissingEmailScope() throws IOException,
                                                                                          GeneralSecurityException {
  Assume.assumeNotNull(credentials);
  final GoogleCredentials serviceAccountCredentials;
  final URI keyUri = URI.create("gs://styx-oss-test/styx-test-user.json");
  try (InputStream is = Files.newInputStream(Paths.get(keyUri))) {
    serviceAccountCredentials = GoogleCredentials.fromStream(is)
        .createScoped(ImmutableList.of(
            "https://www.googleapis.com/auth/cloud-platform"));
  }
  serviceAccountCredentials.refresh();
  final GoogleCredentials accessTokenCredentials = GoogleCredentials.newBuilder()
      .setAccessToken(serviceAccountCredentials.getAccessToken())
      .build();
  final GoogleIdTokenAuth idTokenAuth = GoogleIdTokenAuth.of(accessTokenCredentials);
  try {
    idTokenAuth.getToken("http://styx.foo.bar");
    fail();
  } catch (IOException e) {
    assertThat(e.getMessage(), is("Unable to look up principal email, credentials missing email scope?"));
  }
}
 
Example 3
Source File: TestOnlyImplFirebaseTrampolines.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public static String getToken(FirebaseApp app, boolean forceRefresh) {
  GoogleCredentials credentials = app.getOptions().getCredentials();
  try {
    if (forceRefresh) {
      credentials.refresh();
    } else {
      credentials.getRequestMetadata();
    }

    AccessToken token = credentials.getAccessToken();
    return token.getTokenValue();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 4
Source File: GoogleIdTokenAuth.java    From styx with Apache License 2.0 4 votes vote down vote up
private static AccessToken accessToken(GoogleCredentials credentials) throws IOException {
  if (credentials.getAccessToken() == null) {
    credentials.refresh();
  }
  return credentials.getAccessToken();
}
 
Example 5
Source File: ContainerRegistryAuthSupplier.java    From docker-client with Apache License 2.0 4 votes vote down vote up
@Override
public void refresh(final GoogleCredentials credentials) throws IOException {
  credentials.refresh();
}