com.google.auth.oauth2.ServiceAccountCredentials

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

1. GoogleAuthLibraryCallCredentialsTests#serviceAccountToJwt()

View license
@Test
public void serviceAccountToJwt() throws Exception {
    KeyPair pair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
    ServiceAccountCredentials credentials = new ServiceAccountCredentials(null, "[email protected]", pair.getPrivate(), null, null) {

        @Override
        public AccessToken refreshAccessToken() {
            throw new AssertionError();
        }
    };
    GoogleAuthLibraryCallCredentials callCredentials = new GoogleAuthLibraryCallCredentials(credentials);
    callCredentials.applyRequestMetadata(method, attrs, executor, applier);
    assertEquals(1, runPendingRunnables());
    verify(applier).apply(headersCaptor.capture());
    Metadata headers = headersCaptor.getValue();
    String[] authorization = Iterables.toArray(headers.getAll(AUTHORIZATION), String.class);
    assertEquals(1, authorization.length);
    assertTrue(authorization[0], authorization[0].startsWith("Bearer "));
    // JWT is reasonably long. Normal tokens aren't.
    assertTrue(authorization[0], authorization[0].length() > 300);
}

2. GoogleAuthLibraryCallCredentialsTests#serviceAccountWithScopeNotToJwt()

View license
@Test
public void serviceAccountWithScopeNotToJwt() throws Exception {
    final AccessToken token = new AccessToken("allyourbase", new Date(Long.MAX_VALUE));
    KeyPair pair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
    ServiceAccountCredentials credentials = new ServiceAccountCredentials(null, "[email protected]", pair.getPrivate(), null, Arrays.asList("somescope")) {

        @Override
        public AccessToken refreshAccessToken() {
            return token;
        }
    };
    GoogleAuthLibraryCallCredentials callCredentials = new GoogleAuthLibraryCallCredentials(credentials);
    callCredentials.applyRequestMetadata(method, attrs, executor, applier);
    assertEquals(1, runPendingRunnables());
    verify(applier).apply(headersCaptor.capture());
    Metadata headers = headersCaptor.getValue();
    Iterable<String> authorization = headers.getAll(AUTHORIZATION);
    assertArrayEquals(new String[] { "Bearer allyourbase" }, Iterables.toArray(authorization, String.class));
}

3. AbstractInteropTest#jwtTokenCreds()

Project: grpc-java
Source File: AbstractInteropTest.java
View license
/** Test JWT-based auth. */
public void jwtTokenCreds(InputStream serviceAccountJson) throws Exception {
    final SimpleRequest request = SimpleRequest.newBuilder().setResponseType(PayloadType.COMPRESSABLE).setResponseSize(314159).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[271828]))).setFillUsername(true).build();
    ServiceAccountCredentials credentials = (ServiceAccountCredentials) GoogleCredentials.fromStream(serviceAccountJson);
    TestServiceGrpc.TestServiceBlockingStub stub = blockingStub.withCallCredentials(MoreCallCredentials.from(credentials));
    SimpleResponse response = stub.unaryCall(request);
    assertEquals(credentials.getClientEmail(), response.getUsername());
    assertEquals(314159, response.getPayload().getBody().size());
}

4. AuthCredentialsTest#testServiceAccountFromJson()

View license
@Test
public void testServiceAccountFromJson() throws IOException, SignatureException {
    ServiceAccountAuthCredentials serviceAccountAuthCredentials = AuthCredentials.createForJson(new ByteArrayInputStream(JSON_KEY.getBytes()));
    ServiceAccountCredentials credentials = serviceAccountAuthCredentials.credentials();
    assertEquals(SERVICE_ACCOUNT, serviceAccountAuthCredentials.account());
    assertEquals(SERVICE_ACCOUNT, credentials.getClientEmail());
    assertEquals(privateKey, credentials.getPrivateKey());
    assertArrayEquals(signedBytes, serviceAccountAuthCredentials.sign(BYTES_TO_SIGN));
}

5. AuthCredentialsTest#testServiceAccountFromKey()

View license
@Test
public void testServiceAccountFromKey() throws IOException, SignatureException {
    ServiceAccountAuthCredentials serviceAccountAuthCredentials = AuthCredentials.createFor(SERVICE_ACCOUNT, privateKey);
    ServiceAccountCredentials credentials = serviceAccountAuthCredentials.credentials();
    assertEquals(SERVICE_ACCOUNT, serviceAccountAuthCredentials.account());
    assertEquals(SERVICE_ACCOUNT, credentials.getClientEmail());
    assertEquals(privateKey, credentials.getPrivateKey());
    assertArrayEquals(signedBytes, serviceAccountAuthCredentials.sign(BYTES_TO_SIGN));
}