Search in sources :

Example 36 with AccessToken

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

the class OAuth2IntrospectTest method introspectAccessTokenKeyCloakWay.

@Test
public void introspectAccessTokenKeyCloakWay() {
    config = oauthIntrospect;
    fixture = fixtureKeycloak;
    oauth2.introspectToken(token, res -> {
        if (res.failed()) {
            fail(res.cause().getMessage());
        } else {
            AccessToken token = res.result();
            assertNotNull(token);
            JsonObject principal = token.principal();
            assertTrue(principal.getBoolean("active"));
            testComplete();
        }
    });
    await();
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 37 with AccessToken

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

the class OAuth2IntrospectTest method introspectAccessTokenGoogleWay.

@Test
public void introspectAccessTokenGoogleWay() {
    config = oauthIntrospect;
    fixture = fixtureGoogle;
    oauth2.introspectToken(token, res -> {
        if (res.failed()) {
            fail(res.cause().getMessage());
        } else {
            AccessToken token = res.result();
            assertNotNull(token);
            // make a copy because later we need to original data
            JsonObject principal = token.principal().copy();
            // clean time specific value
            principal.remove("expires_at");
            principal.remove("access_token");
            assertEquals(fixtureGoogle.getMap(), principal.getMap());
            token.isAuthorized("profile", res0 -> {
                if (res0.failed()) {
                    fail(res0.cause().getMessage());
                } else {
                    if (res0.result()) {
                        // Issue #142
                        // the test is a replay of the same test so all checks have
                        // been done above.
                        // the replay shows that the api can be used from the user object
                        // directly too
                        token.introspect(v -> {
                            if (v.failed()) {
                                fail(v.cause());
                            } else {
                                testComplete();
                            }
                        });
                    } else {
                        fail("Should be allowed");
                    }
                }
            });
        }
    });
    await();
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 38 with AccessToken

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

the class OAuth2UserInfoTest method getUserInfoWithParams.

@Test
public void getUserInfoWithParams() {
    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 {
            assertEquals(fixture, userInfo.result());
            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 39 with AccessToken

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

the class OAuth2TokenImpl method refresh.

/**
 * Refresh the access token
 *
 * @param handler - The callback function returning the results.
 */
@Override
public OAuth2TokenImpl refresh(Handler<AsyncResult<Void>> handler) {
    final JsonObject headers = new JsonObject();
    JsonObject tmp = provider.getConfig().getHeaders();
    if (tmp != null) {
        headers.mergeIn(tmp);
    }
    final JsonObject form = new JsonObject();
    form.put("grant_type", "refresh_token").put("refresh_token", opaqueRefreshToken()).put("client_id", provider.getConfig().getClientID());
    if (provider.getConfig().getClientSecretParameterName() != null) {
        form.put(provider.getConfig().getClientSecretParameterName(), provider.getConfig().getClientSecret());
    }
    headers.put("Content-Type", "application/x-www-form-urlencoded");
    final Buffer payload = Buffer.buffer(stringify(form));
    // specify preferred accepted accessToken type
    headers.put("Accept", "application/json,application/x-www-form-urlencoded;q=0.9");
    OAuth2API.fetch(provider, HttpMethod.POST, provider.getConfig().getTokenPath(), headers, payload, res -> {
        if (res.failed()) {
            handler.handle(Future.failedFuture(res.cause()));
            return;
        }
        final OAuth2Response reply = res.result();
        if (reply.body() == null || reply.body().length() == 0) {
            handler.handle(Future.failedFuture("No Body"));
            return;
        }
        JsonObject json;
        if (reply.is("application/json")) {
            try {
                json = reply.jsonObject();
            } catch (RuntimeException e) {
                handler.handle(Future.failedFuture(e));
                return;
            }
        } else if (reply.is("application/x-www-form-urlencoded") || reply.is("text/plain")) {
            try {
                json = queryToJSON(reply.body().toString());
            } catch (UnsupportedEncodingException | RuntimeException e) {
                handler.handle(Future.failedFuture(e));
                return;
            }
        } else {
            handler.handle(Future.failedFuture("Cannot handle accessToken type: " + reply.headers().get("Content-Type")));
            return;
        }
        try {
            if (json.containsKey("error")) {
                String description;
                Object error = json.getValue("error");
                if (error instanceof JsonObject) {
                    description = ((JsonObject) error).getString("message");
                } else {
                    // attempt to handle the error as a string
                    try {
                        description = json.getString("error_description", json.getString("error"));
                    } catch (RuntimeException e) {
                        description = error.toString();
                    }
                }
                handler.handle(Future.failedFuture(description));
            } else {
                OAuth2API.processNonStandardHeaders(json, reply, provider.getConfig().getScopeSeparator());
                token = json;
                init();
                handler.handle(Future.succeededFuture());
            }
        } catch (RuntimeException e) {
            handler.handle(Future.failedFuture(e));
        }
    });
    return this;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) OAuth2Response(io.vertx.ext.auth.oauth2.OAuth2Response) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject)

Example 40 with AccessToken

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

the class OAuth2TokenImpl method revoke.

/**
 * Revoke access or refresh token
 *
 * @param token_type - A String containing the type of token to revoke. Should be either "access_token" or "refresh_token".
 * @param handler    - The callback function returning the results.
 */
@Override
public OAuth2TokenImpl revoke(String token_type, Handler<AsyncResult<Void>> handler) {
    final String tokenValue = token.getString(token_type);
    if (tokenValue != null) {
        final JsonObject headers = new JsonObject();
        JsonObject tmp = provider.getConfig().getHeaders();
        if (tmp != null) {
            headers.mergeIn(tmp);
        }
        final JsonObject form = new JsonObject();
        form.put("token", tokenValue).put("token_type_hint", token_type);
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        final Buffer payload = Buffer.buffer(stringify(form));
        // specify preferred accepted accessToken type
        headers.put("Accept", "application/json,application/x-www-form-urlencoded;q=0.9");
        OAuth2API.fetch(provider, HttpMethod.POST, provider.getConfig().getRevocationPath(), headers, payload, res -> {
            if (res.failed()) {
                handler.handle(Future.failedFuture(res.cause()));
                return;
            }
            final OAuth2Response reply = res.result();
            if (reply.body() == null) {
                handler.handle(Future.failedFuture("No Body"));
                return;
            }
            // invalidate ourselves
            token.remove(token_type);
            if ("access_token".equals(token_type)) {
                accessToken = null;
            }
            handler.handle(Future.succeededFuture());
        });
    } else {
        handler.handle(Future.failedFuture("Invalid token: " + token_type));
    }
    return this;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) OAuth2Response(io.vertx.ext.auth.oauth2.OAuth2Response) JsonObject(io.vertx.core.json.JsonObject)

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