Search in sources :

Example 1 with OAuth2ClientOptions

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

the class OAuth2AuthHandlerTest method testPasswordFlow.

@Test
public void testPasswordFlow() throws Exception {
    // lets mock a oauth2 server using code auth code flow
    OAuth2Auth oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.PASSWORD, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://localhost:10000"));
    final CountDownLatch latch = new CountDownLatch(1);
    HttpServer server = vertx.createHttpServer().requestHandler(req -> {
        if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
            req.setExpectMultipart(true).bodyHandler(buffer -> {
                final String queryString = buffer.toString();
                assertTrue(queryString.contains("username=paulo"));
                assertTrue(queryString.contains("password=bananas"));
                assertTrue(queryString.contains("grant_type=password"));
                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 -> req.response().end());
        } else {
            req.response().setStatusCode(400).end();
        }
    }).listen(10000, ready -> {
        if (ready.failed()) {
            throw new RuntimeException(ready.cause());
        }
        // ready
        latch.countDown();
    });
    latch.await();
    AuthHandler oauth2Handler = BasicAuthHandler.create(oauth2);
    // protect everything under /protected
    router.route("/protected/*").handler(oauth2Handler);
    // mount some handler under the protected zone
    router.route("/protected/somepage").handler(rc -> {
        assertNotNull(rc.user());
        rc.response().end("Welcome to the protected resource!");
    });
    testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString("paulo:bananas".getBytes())), res -> {
    // in this case we should get the resource
    }, 200, "OK", "Welcome to the protected resource!");
    testRequest(HttpMethod.GET, "/protected/somepage", 401, "Unauthorized");
    server.close();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Base64(java.util.Base64) 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) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) WebTestBase(io.vertx.ext.web.WebTestBase) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) HttpServer(io.vertx.core.http.HttpServer) CountDownLatch(java.util.concurrent.CountDownLatch) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) Test(org.junit.Test)

Example 2 with OAuth2ClientOptions

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

the class OAuth2FailureTest 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 {
                    assertEquals(config, queryToJSON(buffer.toString()));
                } catch (UnsupportedEncodingException e) {
                    fail(e);
                }
                req.response().setStatusCode(code).end();
            });
        } else {
            req.response().setStatusCode(400).end();
        }
    }).listen(8080, ready -> {
        if (ready.failed()) {
            throw new RuntimeException(ready.cause());
        }
        // ready
        latch.countDown();
    });
    latch.await();
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) HttpServer(io.vertx.core.http.HttpServer) Test(org.junit.Test) OAuth2FlowType(io.vertx.ext.auth.oauth2.OAuth2FlowType) VertxTestBase(io.vertx.test.core.VertxTestBase) UnknownHostException(java.net.UnknownHostException) CountDownLatch(java.util.concurrent.CountDownLatch) 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) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with OAuth2ClientOptions

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

the class OAuth2PasswordTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.PASSWORD, 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) 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 4 with OAuth2ClientOptions

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

the class AuthCodeImpl method authorizeURL.

/**
 * Redirect the user to the authorization page
 *
 * @param params - redirectURI: A String that represents the registered application URI where the user is redirected after authorization.
 *               scope:       A String that represents the application privileges.
 *               scopes:      A array of strings that will encoded as a single string "scope" following the provider requirements
 *               state:       A String that represents an optional opaque value used by the client to maintain state between the request and the callback.
 */
@Override
public String authorizeURL(JsonObject params) {
    final JsonObject query = params.copy();
    final OAuth2ClientOptions config = provider.getConfig();
    if (query.containsKey("scopes")) {
        // scopes have been passed as a list so the provider must generate the correct string for it
        query.put("scope", String.join(config.getScopeSeparator(), query.getJsonArray("scopes").getList()));
        query.remove("scopes");
    }
    query.put("response_type", "code");
    query.put("client_id", config.getClientID());
    return config.getSite() + config.getAuthorizationPath() + '?' + stringify(query);
}
Also used : OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) JsonObject(io.vertx.core.json.JsonObject)

Example 5 with OAuth2ClientOptions

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

the class OAuth2AuthCodeErrorTest 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 {
                    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) 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)

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