Search in sources :

Example 21 with OAuth2Options

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

the class OAuth2AuthHandlerTest method testAuthCodeFlowSubRouter.

@Test
public void testAuthCodeFlowSubRouter() throws Exception {
    // lets mock a oauth2 server using code auth code flow
    OAuth2Auth oauth2 = OAuth2Auth.create(vertx, new OAuth2Options().setClientId("client-id").setFlow(OAuth2FlowType.AUTH_CODE).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();
    Router subRouter = Router.router(vertx);
    router.mountSubRouter("/secret", subRouter);
    // create a oauth2 handler on our domain to the callback: "http://localhost:8080/secret/callback"
    OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.create(vertx, oauth2, "http://localhost:8080/secret/callback");
    // setup the callback handler for receiving the callback
    oauth2Handler.setupCallback(subRouter.route("/callback"));
    // protect everything under /protected
    subRouter.route("/protected/*").handler(oauth2Handler);
    // mount some handler under the protected zone
    subRouter.route("/protected/somepage").handler(rc -> {
        assertNotNull(rc.user());
        rc.response().end("Welcome to the protected resource!");
    });
    testRequest(HttpMethod.GET, "/secret/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, "/secret/callback?state=/secret/protected/somepage&code=1", null, resp -> {
    }, 200, "OK", "Welcome to the protected resource!");
    server.close();
}
Also used : JWK(io.vertx.ext.auth.impl.jose.JWK) MessageDigest(java.security.MessageDigest) HttpServer(io.vertx.core.http.HttpServer) OAuth2Options(io.vertx.ext.auth.oauth2.OAuth2Options) Router(io.vertx.ext.web.Router) Test(org.junit.Test) OAuth2FlowType(io.vertx.ext.auth.oauth2.OAuth2FlowType) AtomicReference(java.util.concurrent.atomic.AtomicReference) StandardCharsets(java.nio.charset.StandardCharsets) PubSecKeyOptions(io.vertx.ext.auth.PubSecKeyOptions) JWTOptions(io.vertx.ext.auth.JWTOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Base64(java.util.Base64) SessionStore(io.vertx.ext.web.sstore.SessionStore) HttpMethod(io.vertx.core.http.HttpMethod) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JsonObject(io.vertx.core.json.JsonObject) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) JWT(io.vertx.ext.auth.impl.jose.JWT) WebTestBase(io.vertx.ext.web.WebTestBase) HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) CountDownLatch(java.util.concurrent.CountDownLatch) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) OAuth2Options(io.vertx.ext.auth.oauth2.OAuth2Options) Test(org.junit.Test)

Example 22 with OAuth2Options

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

the class WebExamples method example59.

public void example59(Vertx vertx, Router router) {
    // create an OAuth2 provider, clientID and clientSecret
    // should be requested to Google
    OAuth2Auth authProvider = OAuth2Auth.create(vertx, new OAuth2Options().setClientId("CLIENT_ID").setClientSecret("CLIENT_SECRET").setFlow(OAuth2FlowType.AUTH_CODE).setSite("https://accounts.google.com").setTokenPath("https://www.googleapis.com/oauth2/v3/token").setAuthorizationPath("/o/oauth2/auth"));
    // create a oauth2 handler on our domain: "http://localhost:8080"
    OAuth2AuthHandler oauth2 = OAuth2AuthHandler.create(vertx, authProvider, "http://localhost:8080");
    // these are the scopes
    oauth2.withScope("profile");
    // setup the callback handler for receiving the Google callback
    oauth2.setupCallback(router.get("/callback"));
    // protect everything under /protected
    router.route("/protected/*").handler(oauth2);
    // mount some handler under the protected zone
    router.route("/protected/somepage").handler(ctx -> ctx.response().end("Welcome to the protected resource!"));
    // welcome page
    router.get("/").handler(ctx -> ctx.response().putHeader("content-type", "text/html").end("Hello<br><a href=\"/protected/somepage\">Protected by Google</a>"));
}
Also used : OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) OAuth2Options(io.vertx.ext.auth.oauth2.OAuth2Options)

Aggregations

OAuth2Options (io.vertx.ext.auth.oauth2.OAuth2Options)22 OAuth2Auth (io.vertx.ext.auth.oauth2.OAuth2Auth)21 Test (org.junit.Test)19 JsonObject (io.vertx.core.json.JsonObject)18 OAuth2FlowType (io.vertx.ext.auth.oauth2.OAuth2FlowType)17 CountDownLatch (java.util.concurrent.CountDownLatch)17 HttpMethod (io.vertx.core.http.HttpMethod)16 JWTOptions (io.vertx.ext.auth.JWTOptions)10 Future (io.vertx.core.Future)9 Buffer (io.vertx.core.buffer.Buffer)9 PubSecKeyOptions (io.vertx.ext.auth.PubSecKeyOptions)9 JWK (io.vertx.ext.auth.impl.jose.JWK)9 JWT (io.vertx.ext.auth.impl.jose.JWT)9 SessionStore (io.vertx.ext.web.sstore.SessionStore)9 Future.failedFuture (io.vertx.core.Future.failedFuture)8 Future.succeededFuture (io.vertx.core.Future.succeededFuture)8 AUTHORIZATION (io.vertx.core.http.HttpHeaders.AUTHORIZATION)8 HttpServer (io.vertx.core.http.HttpServer)8 Oauth2Credentials (io.vertx.ext.auth.oauth2.Oauth2Credentials)8 Router (io.vertx.ext.web.Router)8