Search in sources :

Example 11 with OAuth2Auth

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

the class OAuth2AuthHandlerTest method testAuthCodeFlow.

@Test
public void testAuthCodeFlow() throws Exception {
    // lets mock a oauth2 server using code auth code flow
    OAuth2Auth oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, 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 -> 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();
    // create a oauth2 handler on our domain to the callback: "http://localhost:8080/callback"
    OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.create(oauth2, "http://localhost:8080/callback");
    // setup the callback handler for receiving the callback
    oauth2Handler.setupCallback(router.route());
    // 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", null, resp -> {
        // in this case we should get a redirect
        redirectURL = resp.getHeader("Location");
        assertNotNull(redirectURL);
    }, 302, "Found", null);
    // fake the redirect
    testRequest(HttpMethod.GET, "/callback?state=/protected/somepage&code=1", null, resp -> {
    }, 200, "OK", "Welcome to the protected resource!");
    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 12 with OAuth2Auth

use of io.vertx.ext.auth.oauth2.OAuth2Auth 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 13 with OAuth2Auth

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

the class OAuth2UserSerializationTest method loadUser.

@Test
public void loadUser() {
    OAuth2Auth provider = KeycloakAuth.create(Vertx.vertx(), OAuth2FlowType.AUTH_CODE, keycloakConfig);
    OAuth2TokenImpl user = new OAuth2TokenImpl();
    System.out.println(keycloakToken.length());
    user.readFromBuffer(0, Buffer.buffer().appendInt(0).appendInt(keycloakToken.length()).appendString(keycloakToken));
    user.setAuthProvider(provider);
}
Also used : OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl) Test(org.junit.Test)

Aggregations

OAuth2Auth (io.vertx.ext.auth.oauth2.OAuth2Auth)11 Test (org.junit.Test)6 JsonObject (io.vertx.core.json.JsonObject)5 OAuth2ClientOptions (io.vertx.ext.auth.oauth2.OAuth2ClientOptions)5 HttpMethod (io.vertx.core.http.HttpMethod)2 HttpServer (io.vertx.core.http.HttpServer)2 PubSecKeyOptions (io.vertx.ext.auth.PubSecKeyOptions)2 OAuth2FlowType (io.vertx.ext.auth.oauth2.OAuth2FlowType)2 WebTestBase (io.vertx.ext.web.WebTestBase)2 Base64 (java.util.Base64)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 JsonArray (io.vertx.core.json.JsonArray)1 User (io.vertx.ext.auth.User)1 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)1 OAuth2AuthProviderImpl (io.vertx.ext.auth.oauth2.impl.OAuth2AuthProviderImpl)1 OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)1 Router (io.vertx.ext.web.Router)1 UnknownHostException (java.net.UnknownHostException)1 Ignore (org.junit.Ignore)1