Search in sources :

Example 6 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project grpc-java by grpc.

the class ClientAuthInterceptorTest method testWithOAuth2Credential.

@Test
public void testWithOAuth2Credential() {
    final AccessToken token = new AccessToken("allyourbase", new Date(Long.MAX_VALUE));
    final OAuth2Credentials oAuth2Credentials = new OAuth2Credentials() {

        @Override
        public AccessToken refreshAccessToken() throws IOException {
            return token;
        }
    };
    interceptor = new ClientAuthInterceptor(oAuth2Credentials, executor);
    ClientCall<String, Integer> interceptedCall = interceptor.interceptCall(descriptor, CallOptions.DEFAULT, channel);
    Metadata headers = new Metadata();
    interceptedCall.start(listener, headers);
    assertEquals(listener, call.responseListener);
    assertEquals(headers, call.headers);
    Iterable<String> authorization = headers.getAll(AUTHORIZATION);
    Assert.assertArrayEquals(new String[] { "Bearer allyourbase" }, Iterables.toArray(authorization, String.class));
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) Metadata(io.grpc.Metadata) OAuth2Credentials(com.google.auth.oauth2.OAuth2Credentials) Date(java.util.Date) Test(org.junit.Test)

Example 7 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project grpc-java by grpc.

the class GoogleAuthLibraryCallCredentialsTest method oauth2Credential.

@Test
public void oauth2Credential() {
    final AccessToken token = new AccessToken("allyourbase", new Date(Long.MAX_VALUE));
    final OAuth2Credentials credentials = new OAuth2Credentials() {

        @Override
        public AccessToken refreshAccessToken() throws IOException {
            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));
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) Metadata(io.grpc.Metadata) OAuth2Credentials(com.google.auth.oauth2.OAuth2Credentials) Date(java.util.Date) Test(org.junit.Test)

Example 8 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken 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 9 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project grpc-java by grpc.

the class GoogleAuthLibraryCallCredentialsTest method serviceAccountWithScopeNotToJwt.

@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@example.com", 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));
}
Also used : KeyPair(java.security.KeyPair) AccessToken(com.google.auth.oauth2.AccessToken) Metadata(io.grpc.Metadata) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 Client (javax.ws.rs.client.Client)5 AccessToken (org.glassfish.jersey.client.oauth1.AccessToken)5 ConsumerCredentials (org.glassfish.jersey.client.oauth1.ConsumerCredentials)5 AccessToken (com.google.auth.oauth2.AccessToken)4 Feature (javax.ws.rs.core.Feature)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)3 Metadata (io.grpc.Metadata)3 URI (java.net.URI)3 Date (java.util.Date)3 WebTarget (javax.ws.rs.client.WebTarget)3 Response (javax.ws.rs.core.Response)3 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)3 IOException (java.io.IOException)2 OAuth1AuthorizationFlow (org.glassfish.jersey.client.oauth1.OAuth1AuthorizationFlow)2 OAuth1ServerFeature (org.glassfish.jersey.server.oauth1.OAuth1ServerFeature)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)1