com.google.auth.oauth2.GoogleCredentials

Here are the examples of the java api class com.google.auth.oauth2.GoogleCredentials taken from open source projects.

1. AuthCredentials#createForJson()

Project: gcloud-java
File: AuthCredentials.java
/**
   * Creates Service Account Credentials given a stream for credentials in JSON format.
   *
   * <p>For details on how to obtain Service Account Credentials in JSON format see
   * <a href="https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts">Service
   * Account Authentication</a>.
   * </p>
   *
   * @param jsonCredentialStream stream for Service Account Credentials in JSON format
   * @return the credentials instance
   * @throws IOException if the credentials cannot be created from the stream
   */
public static ServiceAccountAuthCredentials createForJson(InputStream jsonCredentialStream) throws IOException {
    GoogleCredentials tempCredentials = GoogleCredentials.fromStream(jsonCredentialStream);
    if (tempCredentials instanceof ServiceAccountCredentials) {
        ServiceAccountCredentials tempServiceAccountCredentials = (ServiceAccountCredentials) tempCredentials;
        return new ServiceAccountAuthCredentials(tempServiceAccountCredentials.getClientEmail(), tempServiceAccountCredentials.getPrivateKey());
    }
    throw new IOException("The given JSON Credentials Stream is not for a service account credential.");
}

2. AppEngineCredentialsTest#createScoped_clonesWithScopes()

Project: google-auth-library-java
File: AppEngineCredentialsTest.java
@Test
public void createScoped_clonesWithScopes() throws IOException {
    final String expectedAccessToken = "ExpectedAccessToken";
    final Collection<String> emptyScopes = Collections.emptyList();
    MockAppIdentityService appIdentity = new MockAppIdentityService();
    appIdentity.setAccessTokenText(expectedAccessToken);
    GoogleCredentials credentials = new AppEngineCredentials(emptyScopes, appIdentity);
    assertTrue(credentials.createScopedRequired());
    try {
        credentials.getRequestMetadata(CALL_URI);
        fail("Should not be able to use credential without scopes.");
    } catch (Exception expected) {
    }
    assertEquals(0, appIdentity.getGetAccessTokenCallCount());
    GoogleCredentials scopedCredentials = credentials.createScoped(SCOPES);
    assertNotSame(credentials, scopedCredentials);
    Map<String, List<String>> metadata = scopedCredentials.getRequestMetadata(CALL_URI);
    assertEquals(1, appIdentity.getGetAccessTokenCallCount());
    assertContainsBearerToken(metadata, expectedAccessToken);
}

3. AbstractInteropTest#oauth2AuthToken()

Project: grpc-java
File: AbstractInteropTest.java
/** Sends a unary rpc with raw oauth2 access token credentials. */
public void oauth2AuthToken(String jsonKey, InputStream credentialsStream, String authScope) throws Exception {
    GoogleCredentials utilCredentials = GoogleCredentials.fromStream(credentialsStream);
    utilCredentials = utilCredentials.createScoped(Arrays.<String>asList(authScope));
    AccessToken accessToken = utilCredentials.refreshAccessToken();
    // TODO(madongfly): The Auth library may have something like AccessTokenCredentials in the
    // future, change to the official implementation then.
    OAuth2Credentials credentials = new OAuth2Credentials(accessToken) {

        @Override
        public AccessToken refreshAccessToken() throws IOException {
            throw new IOException("This credential is based on a certain AccessToken, " + "so you can not refresh AccessToken");
        }
    };
    TestServiceGrpc.TestServiceBlockingStub stub = blockingStub.withCallCredentials(MoreCallCredentials.from(credentials));
    final SimpleRequest request = SimpleRequest.newBuilder().setFillUsername(true).setFillOauthScope(true).build();
    final SimpleResponse response = stub.unaryCall(request);
    assertFalse(response.getUsername().isEmpty());
    assertTrue("Received username: " + response.getUsername(), jsonKey.contains(response.getUsername()));
    assertFalse(response.getOauthScope().isEmpty());
    assertTrue("Received oauth scope: " + response.getOauthScope(), authScope.contains(response.getOauthScope()));
}

4. AbstractInteropTest#serviceAccountCreds()

Project: grpc-java
File: AbstractInteropTest.java
/** Sends a large unary rpc with service account credentials. */
public void serviceAccountCreds(String jsonKey, InputStream credentialsStream, String authScope) throws Exception {
    // cast to ServiceAccountCredentials to double-check the right type of object was created.
    GoogleCredentials credentials = ServiceAccountCredentials.class.cast(GoogleCredentials.fromStream(credentialsStream));
    credentials = credentials.createScoped(Arrays.<String>asList(authScope));
    TestServiceGrpc.TestServiceBlockingStub stub = blockingStub.withCallCredentials(MoreCallCredentials.from(credentials));
    final SimpleRequest request = SimpleRequest.newBuilder().setFillUsername(true).setFillOauthScope(true).setResponseSize(314159).setResponseType(PayloadType.COMPRESSABLE).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[271828]))).build();
    final SimpleResponse response = stub.unaryCall(request);
    assertFalse(response.getUsername().isEmpty());
    assertTrue("Received username: " + response.getUsername(), jsonKey.contains(response.getUsername()));
    assertFalse(response.getOauthScope().isEmpty());
    assertTrue("Received oauth scope: " + response.getOauthScope(), authScope.contains(response.getOauthScope()));
    final SimpleResponse goldenResponse = SimpleResponse.newBuilder().setOauthScope(response.getOauthScope()).setUsername(response.getUsername()).setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[314159]))).build();
    assertEquals(goldenResponse, response);
}

5. AuthCredentials#createApplicationDefaults()

Project: gcloud-java
File: AuthCredentials.java
/**
   * Returns the Application Default Credentials.
   *
   * <p>Returns the Application Default Credentials which are credentials that identify and
   * authorize the whole application. This is the built-in service account if running on
   * Google Compute Engine or the credentials file can be read from the path in the environment
   * variable GOOGLE_APPLICATION_CREDENTIALS.
   * </p>
   *
   * @return the credentials instance
   * @throws IOException if the credentials cannot be created in the current environment
   */
public static AuthCredentials createApplicationDefaults() throws IOException {
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
    if (credentials instanceof ServiceAccountCredentials) {
        ServiceAccountCredentials serviceAccountCredentials = (ServiceAccountCredentials) credentials;
        return new ServiceAccountAuthCredentials(serviceAccountCredentials);
    }
    return new ApplicationDefaultAuthCredentials(credentials);
}