Java Code Examples for io.vertx.ext.auth.oauth2.OAuth2Auth#create()

The following examples show how to use io.vertx.ext.auth.oauth2.OAuth2Auth#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IBMCloudAuth.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
/**
 * Create a OAuth2Auth provider for IBM Cloud
 *
 * @param region            the region to use
 * @param clientId          the client id given to you by IBM Cloud
 * @param clientSecret      the client secret given to you by IBM Cloud
 * @param guid              the guid of your application given to you by IBM Cloud
 * @param httpClientOptions custom http client options
 */
static OAuth2Auth create(Vertx vertx, String region, String clientId, String clientSecret, String guid, HttpClientOptions httpClientOptions) {
  if (region == null) {
    throw new IllegalStateException("region cannot be null");
  }

  return
    OAuth2Auth.create(vertx, new OAuth2Options()
      .setHttpClientOptions(httpClientOptions)
      .setFlow(OAuth2FlowType.AUTH_CODE)
      .setClientID(clientId)
      .setClientSecret(clientSecret)
      .setTenant(guid)
      .setSite("https://" + region + ".appid.cloud.ibm.com/oauth/v4/{tenant}")
      .setTokenPath("/token")
      .setAuthorizationPath("/authorization")
      .setJwkPath("/publickeys")
      .setUserInfoPath("/userinfo"));
}
 
Example 2
Source File: AmazonCognitoAuth.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
/**
 * Create a OAuth2Auth provider for Amazon Cognito
 *
 * @param region            the region to use
 * @param clientId          the client id given to you by Amazon Cognito
 * @param clientSecret      the client secret given to you by Amazon Cognito
 * @param userPoolId        the userPoolId of your application given to you by Amazon Cognito
 * @param httpClientOptions custom http client options
 */
static OAuth2Auth create(Vertx vertx, String region, String clientId, String clientSecret, String userPoolId, HttpClientOptions httpClientOptions) {
  if (region == null) {
    throw new IllegalStateException("region cannot be null");
  }

  return
    OAuth2Auth.create(vertx, new OAuth2Options()
      .setHttpClientOptions(httpClientOptions)
      .setFlow(OAuth2FlowType.AUTH_CODE)
      .setClientID(clientId)
      .setClientSecret(clientSecret)
      .setTenant(userPoolId)
      .setSite("https://cognito-idp." + region + ".amazonaws.com/{tenant}")
      .setTokenPath("/oauth2/token")
      .setAuthorizationPath("/oauth2/authorize")
      .setUserInfoPath("/oauth2/userInfo")
      .setJwkPath("/.well-known/jwks.json")
      .setLogoutPath("/logout")
      .setScopeSeparator("+"));
}
 
Example 3
Source File: GoogleAuth.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
/**
 * Create a OAuth2Auth provider for Google Service Account (Server to Server)
 *
 * @param serviceAccountJson the configuration json file from your Google API page
 * @param httpClientOptions  custom http client options
 */
static OAuth2Auth create(Vertx vertx, JsonObject serviceAccountJson, HttpClientOptions httpClientOptions) {
  return
    OAuth2Auth.create(vertx, new OAuth2Options()
      .setHttpClientOptions(httpClientOptions)
      .setFlow(OAuth2FlowType.AUTH_JWT)
      .setClientID(serviceAccountJson.getString("client_id"))
      .setSite("https://accounts.google.com")
      .setTokenPath(serviceAccountJson.getString("token_uri"))
      .addPubSecKey(new PubSecKeyOptions()
        .setAlgorithm("RS256")
        .setBuffer(serviceAccountJson.getString("private_key")))
      .setJWTOptions(new JWTOptions()
        .setAlgorithm("RS256")
        .setExpiresInMinutes(60)
        .addAudience(serviceAccountJson.getString("token_uri"))
        .setIssuer(serviceAccountJson.getString("client_email"))));
}
 
Example 4
Source File: AzureADAuth.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
/**
 * Create a OAuth2Auth provider for Microsoft Azure Active Directory
 *
 * @param clientId          the client id given to you by Azure
 * @param clientSecret      the client secret given to you by Azure
 * @param guid              the guid of your application given to you by Azure
 * @param httpClientOptions custom http client options
 */
static OAuth2Auth create(Vertx vertx, String clientId, String clientSecret, String guid, HttpClientOptions httpClientOptions) {
  return
    OAuth2Auth.create(vertx, new OAuth2Options()
      .setHttpClientOptions(httpClientOptions)
      .setFlow(OAuth2FlowType.AUTH_CODE)
      .setClientID(clientId)
      .setClientSecret(clientSecret)
      .setTenant(guid)
      .setSite("https://login.windows.net/{tenant}")
      .setTokenPath("/oauth2/token")
      .setAuthorizationPath("/oauth2/authorize")
      .setScopeSeparator(",")
      .setExtraParameters(
        new JsonObject().put("resource", "{tenant}")));
}
 
Example 5
Source File: OAuth2ClientTest.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  oauth2 = OAuth2Auth.create(vertx, new OAuth2Options()
    .setFlow(OAuth2FlowType.CLIENT)
    .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())) {
      assertEquals("Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ=", req.getHeader("Authorization"));
      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();
}
 
Example 6
Source File: OAuth2FailureTest.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  dns = new FakeDNSServer().store(question -> Collections.emptySet());
  dns.start();
  super.setUp();
  oauth2 = OAuth2Auth.create(vertx, new OAuth2Options()
    .setFlow(OAuth2FlowType.AUTH_CODE)
    .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())) {
      assertEquals("Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ=", req.getHeader("Authorization"));
      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();
}
 
Example 7
Source File: OAuth2ErrorsTest.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  oauth2 = OAuth2Auth.create(vertx, new OAuth2Options()
    .setFlow(OAuth2FlowType.AUTH_CODE)
      .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();
}
 
Example 8
Source File: OAuth2PasswordTest.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  oauth2 = OAuth2Auth.create(vertx, new OAuth2Options()
    .setFlow(OAuth2FlowType.PASSWORD)
    .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())) {
      assertEquals("Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ=", req.getHeader("Authorization"));
      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();
}
 
Example 9
Source File: LiveAuth.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
/**
 * Create a OAuth2Auth provider for live.com
 *
 * @param clientId          the client id given to you by live.com
 * @param clientSecret      the client secret given to you by live.com
 * @param httpClientOptions custom http client options
 */
static OAuth2Auth create(Vertx vertx, String clientId, String clientSecret, HttpClientOptions httpClientOptions) {
  return
    OAuth2Auth.create(vertx, new OAuth2Options()
      .setHttpClientOptions(httpClientOptions)
      .setFlow(OAuth2FlowType.AUTH_CODE)
      .setClientID(clientId)
      .setClientSecret(clientSecret)
      .setSite("https://login.live.com")
      .setTokenPath("/oauth20_token.srf")
      .setAuthorizationPath("/oauth20_authorize.srf")
      .setScopeSeparator(" "));
}
 
Example 10
Source File: OAuth2AuthCodeErrorTest.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  oauth2 = OAuth2Auth.create(vertx, new OAuth2Options()
    .setFlow(OAuth2FlowType.AUTH_CODE)
    .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())) {
      assertEquals("Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ=", req.getHeader("Authorization"));
      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();
}
 
Example 11
Source File: ShopifyAuth.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
/**
 * Create a OAuth2Auth provider for Shopify
 *
 * @param clientId          the client id given to you by Shopify
 * @param clientSecret      the client secret given to you by Shopify
 * @param shop              your shop name
 * @param httpClientOptions custom http client options
 */
static OAuth2Auth create(Vertx vertx, String clientId, String clientSecret, String shop, HttpClientOptions httpClientOptions) {
  return
    OAuth2Auth.create(vertx, new OAuth2Options()
      .setHttpClientOptions(httpClientOptions)
      .setFlow(OAuth2FlowType.AUTH_CODE)
      .setClientID(clientId)
      .setClientSecret(clientSecret)
      .setTenant(shop)
      .setSite("https://{tenant}.myshopify.com")
      .setTokenPath("/admin/oauth/access_token")
      .setAuthorizationPath("/admin/oauth/authorize")
      .setUserInfoPath("/admin/shop.json")
      .setScopeSeparator(","));
}
 
Example 12
Source File: CloudFoundryAuth.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
/**
 * Create a OAuth2Auth provider for CloudFoundry UAA
 *
 * @param clientId          the client id given to you by CloudFoundry UAA
 * @param clientSecret      the client secret given to you by CloudFoundry UAA
 * @param uuaURL            the url to your UUA server instance
 * @param httpClientOptions custom http client options
 */
static OAuth2Auth create(Vertx vertx, String clientId, String clientSecret, String uuaURL, HttpClientOptions httpClientOptions) {
  return
    OAuth2Auth.create(vertx, new OAuth2Options()
      .setHttpClientOptions(httpClientOptions)
      .setFlow(OAuth2FlowType.AUTH_CODE)
      .setClientID(clientId)
      .setClientSecret(clientSecret)
      .setSite(uuaURL)
      .setTokenPath("/oauth/token")
      .setAuthorizationPath("/oauth/authorize")
      .setScopeSeparator(" "));
}
 
Example 13
Source File: LinkedInAuth.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
/**
 * Create a OAuth2Auth provider for LinkedIn
 *
 * @param clientId          the client id given to you by LinkedIn
 * @param clientSecret      the client secret given to you by LinkedIn
 * @param httpClientOptions custom http client options
 */
static OAuth2Auth create(Vertx vertx, String clientId, String clientSecret, HttpClientOptions httpClientOptions) {
  return
    OAuth2Auth.create(vertx, new OAuth2Options()
      .setHttpClientOptions(httpClientOptions)
      .setFlow(OAuth2FlowType.AUTH_CODE)
      .setClientID(clientId)
      .setClientSecret(clientSecret)
      .setSite("https://www.linkedin.com")
      .setTokenPath("/oauth/v2/accessToken")
      .setAuthorizationPath("/oauth/v2/authorization")
      .setUserInfoPath("/people/~")
      .setScopeSeparator(" "));
}
 
Example 14
Source File: HerokuAuth.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
/**
 * Create a OAuth2Auth provider for Heroku
 *
 * @param clientId          the client id given to you by Heroku
 * @param clientSecret      the client secret given to you by Heroku
 * @param httpClientOptions custom http client options
 */
static OAuth2Auth create(Vertx vertx, String clientId, String clientSecret, HttpClientOptions httpClientOptions) {
  return
    OAuth2Auth.create(vertx, new OAuth2Options()
      .setHttpClientOptions(httpClientOptions)
      .setFlow(OAuth2FlowType.AUTH_CODE)
      .setClientID(clientId)
      .setClientSecret(clientSecret)
      .setSite("https://id.heroku.com")
      .setTokenPath("/oauth/token")
      .setAuthorizationPath("/oauth/authorize")
      .setScopeSeparator(" "));
}
 
Example 15
Source File: FoursquareAuth.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
/**
 * Create a OAuth2Auth provider for Foursquare
 *
 * @param clientId          the client id given to you by Foursquare
 * @param clientSecret      the client secret given to you by Foursquare
 * @param httpClientOptions custom http client options
 */
static OAuth2Auth create(Vertx vertx, String clientId, String clientSecret, HttpClientOptions httpClientOptions) {
  return
    OAuth2Auth.create(vertx, new OAuth2Options()
      .setHttpClientOptions(httpClientOptions)
      .setClientID(clientId)
      .setClientSecret(clientSecret)
      .setFlow(OAuth2FlowType.AUTH_CODE)
      .setSite("https://foursquare.com")
      .setTokenPath("/oauth2/access_token")
      .setAuthorizationPath("/oauth2/authenticate")
      .setUserInfoPath("/users/self"));
}
 
Example 16
Source File: BoxAuth.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
/**
 * Create a OAuth2Auth provider for App.net
 *
 * @param clientId          the client id given to you by box.com
 * @param clientSecret      the client secret given to you by box.com
 * @param httpClientOptions custom http client options
 */
static OAuth2Auth create(Vertx vertx, String clientId, String clientSecret, HttpClientOptions httpClientOptions) {
  return
    OAuth2Auth.create(vertx, new OAuth2Options()
      .setHttpClientOptions(httpClientOptions)
      .setFlow(OAuth2FlowType.AUTH_CODE)
      .setClientID(clientId)
      .setClientSecret(clientSecret)
      .setSite("https://account.box.com")
      .setTokenPath("/api/oauth2/token")
      .setAuthorizationPath("/api/oauth2/authorize")
      .setUserInfoPath("/users/me")
      .setScopeSeparator(" "));
}
 
Example 17
Source File: OAuth2KeyRotationTest.java    From vertx-auth with Apache License 2.0 4 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  oauth2 = OAuth2Auth.create(vertx, new OAuth2Options()
    .setFlow(OAuth2FlowType.AUTH_CODE)
    .setClientID("client-id")
    .setClientSecret("client-secret")
    .setJwkPath("/oauth/jwks")
    .setSite("http://localhost:8080"));

  final CountDownLatch latch = new CountDownLatch(1);

  server = vertx.createHttpServer()
    .connectionHandler(c -> connectionCounter++)
    .requestHandler(req -> {
      if (req.method() == HttpMethod.GET && "/oauth/jwks".equals(req.path())) {
        req.bodyHandler(buffer -> {
          if (cnt.compareAndSet(0, 1)) {
            then.set(System.currentTimeMillis());
            req.response()
              .putHeader("Content-Type", "application/json")
              // we expect a refresh within 5 sec
              .putHeader("Cache-Control", "public, max-age=5, must-revalidate, no-transform")
              .end(fixtureJwks.encode());
            return;
          }
          if (cnt.compareAndSet(1, 2)) {
            requestHandler.handle(req);
          } else {
            fail("Too many calls on the mock");
          }
        });
      } else {
        req.response().setStatusCode(400).end();
      }
    })
    .listen(8080, ready -> {
      if (ready.failed()) {
        throw new RuntimeException(ready.cause());
      }
      // ready
      latch.countDown();
    });

  connectionCounter = 0;
  latch.await();
}
 
Example 18
Source File: OAuth2AuthHandlerTest.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Test
public void testAuthCodeFlowBadSetup() throws Exception {

  // lets mock a oauth2 server using code auth code flow
  OAuth2Auth oauth2 = OAuth2Auth.create(vertx, new OAuth2Options()
    .setFlow(OAuth2FlowType.AUTH_CODE)
    .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();

  // protect everything. This has the bad sideffect that it will also shade the callback route which is computed
  // after this handler, the proper way to fix this would be create the route before
  router.route()
    .handler(
      OAuth2AuthHandler
        .create(vertx, oauth2, "http://localhost:8080/callback")
        .setupCallback(router.route()));

  // 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 -> {
  }, 500, "Internal Server Error", "Internal Server Error");

  // second attempt with proper config
  router.clear();

  // protect everything.
  OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler
    .create(vertx, oauth2, "http://localhost:8080/callback")
    .setupCallback(router.route());

  // now the callback is registered before as it should
  router.route().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();
}
 
Example 19
Source File: OAuth2AuthCodeTest.java    From vertx-auth with Apache License 2.0 4 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  oauth2 = OAuth2Auth.create(vertx, new OAuth2Options()
    .setFlow(OAuth2FlowType.AUTH_CODE)
    .setClientID("client-id")
    .setClientSecret("client-secret")
    .setJwkPath("/oauth/jwks")
    .setSite("http://localhost:8080"));

  final CountDownLatch latch = new CountDownLatch(1);

  server = vertx.createHttpServer()
    .connectionHandler(c -> connectionCounter++)
    .requestHandler(req -> {
      if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
        assertEquals("Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ=", req.getHeader("Authorization"));
        req.setExpectMultipart(true).bodyHandler(buffer -> {
          try {
            assertEquals(config, queryToJSON(buffer.toString()));
          } catch (UnsupportedEncodingException e) {
            fail(e);
          }
          req.response().putHeader("Content-Type", "application/json").end(fixtureTokens.encode());
        });
      } else if (req.method() == HttpMethod.GET && "/oauth/jwks".equals(req.path())) {
        req.bodyHandler(buffer -> {
          req.response().putHeader("Content-Type", "application/json").end(fixtureJwks.encode());
        });
      } else {
        req.response().setStatusCode(400).end();
      }
    })
    .listen(8080, ready -> {
      if (ready.failed()) {
        throw new RuntimeException(ready.cause());
      }
      // ready
      latch.countDown();
    });

  connectionCounter = 0;
  latch.await();
}
 
Example 20
Source File: OAuth2AuthHandlerTest.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Test
public void testAuthCodeFlow() 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();

  // create a oauth2 handler on our domain to the callback: "http://localhost:8080/callback"
  OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.create(vertx, 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();
}