Search in sources :

Example 46 with AccessToken

use of com.google.auth.oauth2.AccessToken in project vertx-auth by vert-x3.

the class AuthCodeImpl method getToken.

/**
 * Returns the Access Token object.
 *
 * @param params  - code:        Authorization code (from previous step).
 *                redirectURI: A String that represents the callback uri.
 * @param handler - The handler returning the results.
 */
@Override
public void getToken(JsonObject params, Handler<AsyncResult<AccessToken>> handler) {
    getToken("authorization_code", params, res -> {
        if (res.failed()) {
            handler.handle(Future.failedFuture(res.cause()));
            return;
        }
        AccessToken token;
        try {
            token = new OAuth2TokenImpl(provider, res.result());
        } catch (RuntimeException e) {
            handler.handle(Future.failedFuture(e));
            return;
        }
        handler.handle(Future.succeededFuture(token));
    });
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)

Example 47 with AccessToken

use of com.google.auth.oauth2.AccessToken in project vertx-auth by vert-x3.

the class PasswordImpl method getToken.

/**
 * Returns the Access Token object.
 *
 * @param params - username: A string that represents the registered username.
 *                 password: A string that represents the registered password.
 *                 scope:    A String that represents the application privileges.
 * @param handler - The handler function returning the results.
 */
@Override
public void getToken(JsonObject params, Handler<AsyncResult<AccessToken>> handler) {
    getToken("password", params, res -> {
        if (res.failed()) {
            handler.handle(Future.failedFuture(res.cause()));
            return;
        }
        AccessToken token;
        try {
            token = new OAuth2TokenImpl(provider, res.result());
        } catch (RuntimeException e) {
            handler.handle(Future.failedFuture(e));
            return;
        }
        handler.handle(Future.succeededFuture(token));
    });
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)

Example 48 with AccessToken

use of com.google.auth.oauth2.AccessToken in project vertx-auth by vert-x3.

the class OAuth2AccessTokenTest method tokenShouldNotBeExpired.

@Test
public void tokenShouldNotBeExpired() {
    config = oauthConfig;
    oauth2.authenticate(tokenConfig, res -> {
        if (res.failed()) {
            fail(res.cause().getMessage());
        } else {
            AccessToken token = (AccessToken) res.result();
            assertFalse(token.expired());
            testComplete();
        }
    });
    await();
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) Test(org.junit.Test)

Example 49 with AccessToken

use of com.google.auth.oauth2.AccessToken in project vertx-auth by vert-x3.

the class OAuth2AccessTokenTest method tokenShouldBeExpiredWhenExpirationDateIsInThePast.

@Test
public void tokenShouldBeExpiredWhenExpirationDateIsInThePast() {
    config = oauthConfig;
    oauth2.authenticate(tokenConfig, res -> {
        if (res.failed()) {
            fail(res.cause().getMessage());
        } else {
            AccessToken token = (AccessToken) res.result();
            // hack the token to set the expires_at (to yesterday)
            token.principal().put("expires_at", System.currentTimeMillis() - 24 * 60 * 60 * 1000);
            assertTrue(token.expired());
            testComplete();
        }
    });
    await();
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) Test(org.junit.Test)

Example 50 with AccessToken

use of com.google.auth.oauth2.AccessToken in project helios by spotify.

the class AuthenticatingHttpConnector method connect.

@Override
public HttpURLConnection connect(final URI uri, final String method, final byte[] entity, final Map<String, List<String>> headers) throws HeliosException {
    final Endpoint endpoint = endpointIterator.next();
    // convert the URI whose hostname portion is a domain name into a URI where the host is an IP
    // as we expect there to be several different IP addresses besides a common domain name
    final URI ipUri;
    try {
        ipUri = toIpUri(endpoint, uri);
    } catch (URISyntaxException e) {
        throw new HeliosException(e);
    }
    try {
        log.debug("connecting to {}", ipUri);
        final Optional<AccessToken> accessTokenOpt = accessTokenSupplier.get();
        if (accessTokenOpt.isPresent()) {
            final String token = accessTokenOpt.get().getTokenValue();
            headers.put("Authorization", singletonList("Bearer " + token));
            log.debug("Add Authorization header with bearer token");
        }
        if (clientCertificatePath.isPresent()) {
            // prioritize using the certificate file if set
            return connectWithCertificateFile(ipUri, method, entity, headers);
        } else if (agentProxy.isPresent() && !identities.isEmpty()) {
            // ssh-agent based authentication
            return connectWithIdentities(identities, ipUri, method, entity, headers);
        } else {
            // no authentication
            return doConnect(ipUri, method, entity, headers);
        }
    } catch (ConnectException | SocketTimeoutException | UnknownHostException e) {
        // UnknownHostException happens if we can't resolve hostname into IP address.
        // UnknownHostException's getMessage method returns just the hostname which is a
        // useless message, so log the exception class name to provide more info.
        log.debug(e.toString());
        throw new HeliosException("Unable to connect to master: " + ipUri, e);
    } catch (IOException e) {
        throw new HeliosException("Unexpected error connecting to " + ipUri, e);
    }
}
Also used : HeliosException(com.spotify.helios.common.HeliosException) SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) AccessToken(com.google.auth.oauth2.AccessToken) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) ConnectException(java.net.ConnectException)

Aggregations

Test (org.junit.Test)40 AccessToken (com.google.auth.oauth2.AccessToken)17 URI (java.net.URI)14 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)13 MockTokenServerTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory)12 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)8 JsonObject (io.vertx.core.json.JsonObject)8 Date (java.util.Date)8 OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)7 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)6 MockHttpTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory)6 IOException (java.io.IOException)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 Feature (javax.ws.rs.core.Feature)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 HttpRequest (com.google.api.client.http.HttpRequest)3 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)3