Search in sources :

Example 21 with AccessToken

use of io.vertx.reactivex.ext.auth.oauth2.AccessToken in project java by kubernetes-client.

the class GCPAuthenticatorTest method testRefreshApplicationDefaultCredentials.

@Test
public void testRefreshApplicationDefaultCredentials() {
    Date fakeTokenExpiryDate = Date.from(Instant.parse(fakeTokenExpiry));
    Mockito.when(mockGC.getAccessToken()).thenReturn(new AccessToken(fakeToken, fakeTokenExpiryDate));
    final Map<String, Object> config = new HashMap<String, Object>() {
    };
    final Map<String, Object> result = gcpAuthenticator.refresh(config);
    assertEquals(fakeToken, result.get(GCPAuthenticator.ACCESS_TOKEN));
    assertEquals(fakeTokenExpiryDate, result.get(GCPAuthenticator.EXPIRY));
}
Also used : HashMap(java.util.HashMap) AccessToken(com.google.auth.oauth2.AccessToken) Date(java.util.Date) Test(org.junit.Test)

Example 22 with AccessToken

use of io.vertx.reactivex.ext.auth.oauth2.AccessToken in project curiostack by curioswitch.

the class AbstractAccessTokenProvider method refresh.

private CompletableFuture<AccessToken> refresh(Type type) {
    return fetchToken(type).handle((msg, t) -> {
        if (t != null) {
            throw new IllegalStateException("Failed to refresh GCP access token.", t);
        }
        final TokenResponse response;
        try {
            response = OBJECT_MAPPER.readValue(msg.content().array(), TokenResponse.class);
        } catch (IOException e) {
            throw new UncheckedIOException("Error parsing token refresh response.", e);
        }
        long expiresAtMilliseconds = clock.millis() + TimeUnit.SECONDS.toMillis(response.expiresIn());
        return new AccessToken(type == Type.ID_TOKEN ? response.idToken() : response.accessToken(), new Date(expiresAtMilliseconds));
    });
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Date(java.util.Date)

Example 23 with AccessToken

use of io.vertx.reactivex.ext.auth.oauth2.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)

Example 24 with AccessToken

use of io.vertx.reactivex.ext.auth.oauth2.AccessToken in project docker-client by spotify.

the class ContainerRegistryAuthSupplier method authFor.

@Override
public RegistryAuth authFor(final String imageName) throws DockerException {
    final String[] imageParts = imageName.split("/", 2);
    if (imageParts.length < 2 || !GCR_REGISTRIES.contains(imageParts[0])) {
        // not an image on GCR
        return null;
    }
    final AccessToken accessToken;
    try {
        accessToken = getAccessToken();
    } catch (IOException e) {
        throw new DockerException(e);
    }
    return authForAccessToken(accessToken);
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) AccessToken(com.google.auth.oauth2.AccessToken) IOException(java.io.IOException)

Example 25 with AccessToken

use of io.vertx.reactivex.ext.auth.oauth2.AccessToken in project docker-client by spotify.

the class ContainerRegistryAuthSupplierTest method testAuthForImage_TokenWithoutExpirationDoesNotCauseRefresh.

@Test
public void testAuthForImage_TokenWithoutExpirationDoesNotCauseRefresh() throws Exception {
    final AccessToken accessToken = new AccessToken(tokenValue, null);
    final GoogleCredentials credentials = new GoogleCredentials(accessToken);
    final ContainerRegistryAuthSupplier supplier = new ContainerRegistryAuthSupplier(credentials, clock, TimeUnit.SECONDS.toMillis(minimumExpirationSecs), refresher);
    assertThat(supplier.authFor("gcr.io/foobar/barfoo:latest"), matchesAccessToken(accessToken));
    verify(refresher, never()).refresh(credentials);
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)25 AccessToken (com.google.auth.oauth2.AccessToken)21 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)13 Date (java.util.Date)9 JsonObject (io.vertx.core.json.JsonObject)8 IOException (java.io.IOException)8 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)7 OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)7 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)5 Client (javax.ws.rs.client.Client)5 AccessToken (org.glassfish.jersey.client.oauth1.AccessToken)5 ConsumerCredentials (org.glassfish.jersey.client.oauth1.ConsumerCredentials)5 Metadata (io.grpc.Metadata)4 URI (java.net.URI)4 Feature (javax.ws.rs.core.Feature)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)3 WebTarget (javax.ws.rs.client.WebTarget)3 Response (javax.ws.rs.core.Response)3 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)3