Search in sources :

Example 11 with AccessToken

use of io.vertx.ext.auth.oauth2.AccessToken in project vertx-auth by vert-x3.

the class OAuth2UserInfoTest method getUserInfo.

@Test
public void getUserInfo() {
    final AccessToken accessToken = new OAuth2TokenImpl((OAuth2AuthProviderImpl) oauth2, new JsonObject("{\"access_token\":\"eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJhdXRob3JpemF0aW9uIjp7InBlcm1pc3Npb25zIjpbeyJyZXNvdXJjZV9zZXRfaWQiOiJkMmZlOTg0My02NDYyLTRiZmMtYmFiYS1iNTc4N2JiNmUwZTciLCJyZXNvdXJjZV9zZXRfbmFtZSI6IkhlbGxvIFdvcmxkIFJlc291cmNlIn1dfSwianRpIjoiZDYxMDlhMDktNzhmZC00OTk4LWJmODktOTU3MzBkZmQwODkyLTE0NjQ5MDY2Nzk0MDUiLCJleHAiOjk5OTk5OTk5OTksIm5iZiI6MCwiaWF0IjoxNDY0OTA2NjcxLCJzdWIiOiJmMTg4OGY0ZC01MTcyLTQzNTktYmUwYy1hZjMzODUwNWQ4NmMiLCJ0eXAiOiJrY19ldHQiLCJhenAiOiJoZWxsby13b3JsZC1hdXRoei1zZXJ2aWNlIn0\",\"active\":true,\"scope\":\"scopeA scopeB\",\"client_id\":\"client-id\",\"username\":\"username\",\"token_type\":\"bearer\",\"expires_at\":99999999999000}"));
    accessToken.userInfo(userInfo -> {
        if (userInfo.failed()) {
            fail(userInfo.cause().getMessage());
        } else {
            testComplete();
        }
    });
    await();
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) JsonObject(io.vertx.core.json.JsonObject) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl) Test(org.junit.Test)

Example 12 with AccessToken

use of io.vertx.ext.auth.oauth2.AccessToken in project vertx-auth by vert-x3.

the class OAuth2TokenImpl method userInfo.

@Override
public AccessToken userInfo(Handler<AsyncResult<JsonObject>> callback) {
    final JsonObject headers = new JsonObject();
    final JsonObject extraParams = provider.getConfig().getUserInfoParameters();
    String path = provider.getConfig().getUserInfoPath();
    if (extraParams != null) {
        path += "?" + OAuth2API.stringify(extraParams);
    }
    headers.put("Authorization", "Bearer " + token.getString("access_token"));
    // specify preferred accepted accessToken type
    headers.put("Accept", "application/json,application/x-www-form-urlencoded;q=0.9");
    OAuth2API.fetch(provider, HttpMethod.GET, path, headers, null, fetch -> {
        if (fetch.failed()) {
            callback.handle(Future.failedFuture(fetch.cause()));
            return;
        }
        final OAuth2Response reply = fetch.result();
        // userInfo is expected to be an object
        JsonObject userInfo;
        if (reply.is("application/json")) {
            try {
                // userInfo is expected to be an object
                userInfo = reply.jsonObject();
            } catch (RuntimeException e) {
                callback.handle(Future.failedFuture(e));
                return;
            }
        } else if (reply.is("application/x-www-form-urlencoded") || reply.is("text/plain")) {
            try {
                // attempt to convert url encoded string to json
                userInfo = OAuth2API.queryToJSON(reply.body().toString());
            } catch (RuntimeException | UnsupportedEncodingException e) {
                callback.handle(Future.failedFuture(e));
                return;
            }
        } else {
            callback.handle(Future.failedFuture("Cannot handle Content-Type: " + reply.headers().get("Content-Type")));
            return;
        }
        OAuth2API.processNonStandardHeaders(token, reply, provider.getConfig().getScopeSeparator());
        // re-init to reparse the authorities
        init();
        callback.handle(Future.succeededFuture(userInfo));
    });
    return this;
}
Also used : OAuth2Response(io.vertx.ext.auth.oauth2.OAuth2Response) JsonObject(io.vertx.core.json.JsonObject)

Example 13 with AccessToken

use of io.vertx.ext.auth.oauth2.AccessToken in project vertx-auth by vert-x3.

the class ClientImpl method getToken.

/**
 * Returns the Access Token object.
 *
 * @param params - scope: A String that represents the application privileges.
 * @param handler - The handler returning the results.
 */
@Override
public void getToken(JsonObject params, Handler<AsyncResult<AccessToken>> handler) {
    getToken("client_credentials", 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 14 with AccessToken

use of io.vertx.ext.auth.oauth2.AccessToken in project vertx-auth by vert-x3.

the class OAuth2AccessTokenTest method shouldRevokeAToken.

@Test
public void shouldRevokeAToken() {
    config = oauthConfig;
    oauth2.authenticate(tokenConfig, res -> {
        if (res.failed()) {
            fail(res.cause().getMessage());
        } else {
            AccessToken token = (AccessToken) res.result();
            // refresh the token
            config = revokeConfig;
            token.revoke("refresh_token", v -> {
                if (v.failed()) {
                    fail(v.cause().getMessage());
                } else {
                    testComplete();
                }
            });
        }
    });
    await();
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) Test(org.junit.Test)

Example 15 with AccessToken

use of io.vertx.ext.auth.oauth2.AccessToken in project vertx-auth by vert-x3.

the class OAuth2AccessTokenTest method whenRefreshingTokenShouldGetNewAccessToken.

@Test
public void whenRefreshingTokenShouldGetNewAccessToken() {
    config = oauthConfig;
    oauth2.authenticate(tokenConfig, res -> {
        if (res.failed()) {
            fail(res.cause().getMessage());
        } else {
            AccessToken token = (AccessToken) res.result();
            final long origTTl = token.principal().getLong("expires_at");
            // refresh the token
            config = refreshConfig;
            token.refresh(v -> {
                if (v.failed()) {
                    fail(v.cause().getMessage());
                } else {
                    assertTrue(origTTl < token.principal().getLong("expires_at"));
                    testComplete();
                }
            });
        }
    });
    await();
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)25 AccessToken (com.google.auth.oauth2.AccessToken)22 JsonObject (io.vertx.core.json.JsonObject)13 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)13 Date (java.util.Date)10 IOException (java.io.IOException)9 OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)8 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)7 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)5 OAuth2Response (io.vertx.ext.auth.oauth2.OAuth2Response)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 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)3 Buffer (io.vertx.core.buffer.Buffer)3 URI (java.net.URI)3 WebTarget (javax.ws.rs.client.WebTarget)3