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();
}
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();
}
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);
}
Aggregations