Search in sources :

Example 1 with GoogleCredentials

use of com.google.auth.oauth2.GoogleCredentials in project grpc-java by grpc.

the class AbstractInteropTest method oauth2AuthToken.

/** 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()));
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) OAuth2Credentials(com.google.auth.oauth2.OAuth2Credentials) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Example 2 with GoogleCredentials

use of com.google.auth.oauth2.GoogleCredentials in project beam by apache.

the class BigqueryMatcher method getDefaultCredential.

private Credentials getDefaultCredential() {
    GoogleCredentials credential;
    try {
        credential = GoogleCredentials.getApplicationDefault();
    } catch (IOException e) {
        throw new RuntimeException("Failed to get application default credential.", e);
    }
    if (credential.createScopedRequired()) {
        Collection<String> bigqueryScope = Lists.newArrayList(BigqueryScopes.CLOUD_PLATFORM_READ_ONLY);
        credential = credential.createScoped(bigqueryScope);
    }
    return credential;
}
Also used : GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 3 with GoogleCredentials

use of com.google.auth.oauth2.GoogleCredentials in project grpc-java by grpc.

the class AbstractInteropTest method serviceAccountCreds.

/** 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);
}
Also used : SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) ByteString(com.google.protobuf.ByteString) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Aggregations

GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)3 ByteString (com.google.protobuf.ByteString)2 SimpleRequest (io.grpc.testing.integration.Messages.SimpleRequest)2 SimpleResponse (io.grpc.testing.integration.Messages.SimpleResponse)2 IOException (java.io.IOException)2 AccessToken (com.google.auth.oauth2.AccessToken)1 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)1 InterruptedIOException (java.io.InterruptedIOException)1