Search in sources :

Example 16 with AccessToken

use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project google-auth-library-java by googleapis.

the class AppEngineCredentialsTest method refreshAccessToken_sameAs.

@Test
void refreshAccessToken_sameAs() throws IOException {
    String expectedAccessToken = "ExpectedAccessToken";
    MockAppIdentityService appIdentity = new MockAppIdentityService();
    appIdentity.setAccessTokenText(expectedAccessToken);
    appIdentity.setExpiration(new Date(System.currentTimeMillis() + 60L * 60L * 100L));
    AppEngineCredentials credentials = AppEngineCredentials.newBuilder().setScopes(SCOPES).setAppIdentityService(appIdentity).build();
    AccessToken accessToken = credentials.refreshAccessToken();
    assertEquals(appIdentity.getAccessTokenText(), accessToken.getTokenValue());
    assertEquals(appIdentity.getExpiration(), accessToken.getExpirationTime());
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) Date(java.util.Date) BaseSerializationTest(com.google.auth.oauth2.BaseSerializationTest) Test(org.junit.jupiter.api.Test)

Example 17 with AccessToken

use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project psoxy by Worklytics.

the class OAuthAccessTokenSourceAuthStrategy method getCredentials.

@Override
public Credentials getCredentials(Optional<String> userToImpersonateIgnored) {
    String token = config.getConfigPropertyOrError(ConfigProperty.ACCESS_TOKEN);
    // Some date into far future. Expiration is required
    Instant expire = clock.instant().plus(365L, ChronoUnit.DAYS);
    AccessToken accessToken = new AccessToken(token, Date.from(expire));
    // OAuth2Credentials tried to refresh and fail
    OAuth2CredentialsWithRefresh.Builder builder = OAuth2CredentialsWithRefresh.newBuilder();
    builder.setAccessToken(accessToken);
    // refresh does nothing, just return the same token
    builder.setRefreshHandler(() -> accessToken);
    return builder.build();
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) Instant(java.time.Instant) OAuth2CredentialsWithRefresh(com.google.auth.oauth2.OAuth2CredentialsWithRefresh)

Example 18 with AccessToken

use of com.microsoft.identity.common.internal.providers.oauth2.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.asList(authScope));
    AccessToken accessToken = utilCredentials.refreshAccessToken();
    OAuth2Credentials credentials = OAuth2Credentials.create(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) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Example 19 with AccessToken

use of com.microsoft.identity.common.internal.providers.oauth2.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 20 with AccessToken

use of com.microsoft.identity.common.internal.providers.oauth2.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));
    OAuth2Credentials credentials = new OAuth2Credentials() {

        @Override
        public AccessToken refreshAccessToken() throws IOException {
            return token;
        }
    };
    GoogleAuthLibraryCallCredentials callCredentials = new GoogleAuthLibraryCallCredentials(credentials);
    callCredentials.applyRequestMetadata(new RequestInfoImpl(SecurityLevel.NONE), 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)

Aggregations

AccessToken (com.google.auth.oauth2.AccessToken)78 Test (org.junit.Test)44 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)33 Date (java.util.Date)23 IOException (java.io.IOException)20 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)16 Instant (java.time.Instant)10 Client (javax.ws.rs.client.Client)10 AccessToken (org.glassfish.jersey.client.oauth1.AccessToken)10 ConsumerCredentials (org.glassfish.jersey.client.oauth1.ConsumerCredentials)10 JsonObject (io.vertx.core.json.JsonObject)9 URI (java.net.URI)9 Feature (javax.ws.rs.core.Feature)8 JerseyTest (org.glassfish.jersey.test.JerseyTest)8 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)6 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)6 Credential (io.cdap.cdap.proto.security.Credential)6 InputStreamReader (java.io.InputStreamReader)6 Clock (java.time.Clock)6 WebTarget (javax.ws.rs.client.WebTarget)6