Search in sources :

Example 11 with OAuth2ClientOptions

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

the class OAuth2ClientTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.CLIENT, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://localhost:8080"));
    final CountDownLatch latch = new CountDownLatch(1);
    server = vertx.createHttpServer().requestHandler(req -> {
        if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
            req.setExpectMultipart(true).bodyHandler(buffer -> {
                try {
                    assertEquals(config, queryToJSON(buffer.toString()));
                } catch (UnsupportedEncodingException e) {
                    fail(e);
                }
                req.response().putHeader("Content-Type", "application/json").end(fixture.encode());
            });
        } else {
            req.response().setStatusCode(400).end();
        }
    }).listen(8080, ready -> {
        if (ready.failed()) {
            throw new RuntimeException(ready.cause());
        }
        // ready
        latch.countDown();
    });
    latch.await();
}
Also used : HttpServer(io.vertx.core.http.HttpServer) Test(org.junit.Test) OAuth2API.stringify(io.vertx.ext.auth.oauth2.impl.OAuth2API.stringify) OAuth2FlowType(io.vertx.ext.auth.oauth2.OAuth2FlowType) VertxTestBase(io.vertx.test.core.VertxTestBase) CountDownLatch(java.util.concurrent.CountDownLatch) URLEncoder(java.net.URLEncoder) User(io.vertx.ext.auth.User) HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) AccessToken(io.vertx.ext.auth.oauth2.AccessToken) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) OAuth2API.queryToJSON(io.vertx.ext.auth.oauth2.impl.OAuth2API.queryToJSON) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 12 with OAuth2ClientOptions

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

the class OAuth2ErrorsTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://localhost:8080"));
    final CountDownLatch latch = new CountDownLatch(1);
    server = vertx.createHttpServer().requestHandler(req -> {
        if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
            req.setExpectMultipart(true).bodyHandler(buffer -> req.response().putHeader("Content-Type", "application/json").end(fixture.encode()));
        } else {
            req.response().setStatusCode(400).end();
        }
    }).listen(8080, ready -> {
        if (ready.failed()) {
            throw new RuntimeException(ready.cause());
        }
        // ready
        latch.countDown();
    });
    latch.await();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) HttpMethod(io.vertx.core.http.HttpMethod) HttpServer(io.vertx.core.http.HttpServer) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) OAuth2FlowType(io.vertx.ext.auth.oauth2.OAuth2FlowType) VertxTestBase(io.vertx.test.core.VertxTestBase) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 13 with OAuth2ClientOptions

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

the class OAuth2FailureTest method unknownHost.

@Test
public void unknownHost() {
    OAuth2Auth auth = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://zlouklfoux.net.com.info.pimpo.molo"));
    auth.authenticate(tokenConfig, res -> {
        if (res.failed()) {
            assertThat(res.cause(), instanceOf(UnknownHostException.class));
            testComplete();
        } else {
            fail("Should have failed");
        }
    });
    await();
}
Also used : UnknownHostException(java.net.UnknownHostException) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) Test(org.junit.Test)

Example 14 with OAuth2ClientOptions

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

the class OAuth2TokenImpl method introspect.

@Override
public AccessToken introspect(String tokenType, Handler<AsyncResult<Void>> handler) {
    final JsonObject headers = new JsonObject();
    final OAuth2ClientOptions config = provider.getConfig();
    if (config.isUseBasicAuthorizationHeader()) {
        String basic = config.getClientID() + ":" + config.getClientSecret();
        headers.put("Authorization", "Basic " + Base64.getEncoder().encodeToString(basic.getBytes()));
    }
    JsonObject tmp = config.getHeaders();
    if (tmp != null) {
        headers.mergeIn(tmp);
    }
    final JsonObject form = new JsonObject().put("token", token.getString(tokenType)).put("token_type_hint", tokenType);
    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, config.getIntrospectionPath(), 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 {
                // RFC7662 dictates that there is a boolean active field (however tokeninfo implementations do not return this)
                if (json.containsKey("active") && !json.getBoolean("active", false)) {
                    handler.handle(Future.failedFuture("Inactive Token"));
                    return;
                }
                // validate client id
                if (json.containsKey("client_id") && !json.getString("client_id", "").equals(config.getClientID())) {
                    handler.handle(Future.failedFuture("Wrong client_id"));
                    return;
                }
                // RFC7662 dictates that there is a boolean active field (however tokeninfo implementations do not return this)
                if (json.containsKey("active") && !json.getBoolean("active", false)) {
                    handler.handle(Future.failedFuture("Inactive Token"));
                    return;
                }
                // validate client id
                if (json.containsKey("client_id") && !json.getString("client_id", "").equals(provider.getConfig().getClientID())) {
                    handler.handle(Future.failedFuture("Wrong client_id"));
                    return;
                }
                try {
                    processNonStandardHeaders(json, reply, config.getScopeSeparator());
                    // reset the access token
                    token.mergeIn(json);
                    init();
                    if (expired()) {
                        handler.handle(Future.failedFuture("Expired token"));
                        return;
                    }
                    handler.handle(Future.succeededFuture());
                } catch (RuntimeException e) {
                    handler.handle(Future.failedFuture(e));
                }
            }
        } 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) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject)

Example 15 with OAuth2ClientOptions

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

the class OAuth2AccessTokenTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://localhost:8080"));
    final CountDownLatch latch = new CountDownLatch(1);
    server = vertx.createHttpServer().requestHandler(req -> {
        if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
            req.setExpectMultipart(true).bodyHandler(buffer -> {
                try {
                    JsonObject expectedRequest = config;
                    assertEquals(expectedRequest, queryToJSON(buffer.toString()));
                } catch (UnsupportedEncodingException e) {
                    fail(e);
                }
                req.response().putHeader("Content-Type", "application/json").end(fixture.encode());
            });
        } else if (req.method() == HttpMethod.POST && "/oauth/revoke".equals(req.path())) {
            req.setExpectMultipart(true).bodyHandler(buffer -> {
                // Revoke does not pass auth details
                JsonObject expectedRequest = removeAuthDetails(config);
                try {
                    assertEquals(expectedRequest, queryToJSON(buffer.toString()));
                } catch (UnsupportedEncodingException e) {
                    fail(e);
                }
                req.response().end();
            });
        } else if (req.method() == HttpMethod.POST && "/oauth/introspect".equals(req.path())) {
            req.setExpectMultipart(true).bodyHandler(buffer -> {
                try {
                    assertEquals(config, queryToJSON(buffer.toString()));
                } catch (UnsupportedEncodingException e) {
                    fail(e);
                }
                req.response().putHeader("Content-Type", "application/json").end(fixtureIntrospect.encode());
            });
        } else {
            req.response().setStatusCode(400).end();
        }
    }).listen(8080, ready -> {
        if (ready.failed()) {
            throw new RuntimeException(ready.cause());
        }
        // ready
        latch.countDown();
    });
    latch.await();
}
Also used : HttpServer(io.vertx.core.http.HttpServer) Test(org.junit.Test) OAuth2FlowType(io.vertx.ext.auth.oauth2.OAuth2FlowType) VertxTestBase(io.vertx.test.core.VertxTestBase) CountDownLatch(java.util.concurrent.CountDownLatch) User(io.vertx.ext.auth.User) HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) OAuth2API(io.vertx.ext.auth.oauth2.impl.OAuth2API) AccessToken(io.vertx.ext.auth.oauth2.AccessToken) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) JsonObject(io.vertx.core.json.JsonObject) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

OAuth2ClientOptions (io.vertx.ext.auth.oauth2.OAuth2ClientOptions)14 JsonObject (io.vertx.core.json.JsonObject)11 OAuth2Auth (io.vertx.ext.auth.oauth2.OAuth2Auth)11 Test (org.junit.Test)10 HttpMethod (io.vertx.core.http.HttpMethod)9 HttpServer (io.vertx.core.http.HttpServer)9 OAuth2FlowType (io.vertx.ext.auth.oauth2.OAuth2FlowType)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 VertxTestBase (io.vertx.test.core.VertxTestBase)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)5 User (io.vertx.ext.auth.User)4 OAuth2API.queryToJSON (io.vertx.ext.auth.oauth2.impl.OAuth2API.queryToJSON)4 OAuth2API.stringify (io.vertx.ext.auth.oauth2.impl.OAuth2API.stringify)4 URLEncoder (java.net.URLEncoder)3 PubSecKeyOptions (io.vertx.ext.auth.PubSecKeyOptions)2 OAuth2API (io.vertx.ext.auth.oauth2.impl.OAuth2API)2 WebTestBase (io.vertx.ext.web.WebTestBase)2 UnknownHostException (java.net.UnknownHostException)2 Base64 (java.util.Base64)2