OAuth2AuthHandler.create(authProvider)
OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.create(oauth2, "http://localhost:8080/callback");
@Test public void testBearerOnly() 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")); OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.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", 401, "Unauthorized"); // Now try again with fake credentials testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Bearer 4adc339e0"), 401, "Unauthorized", "Unauthorized"); }
/** * Create a OAuth2 auth handler without host pinning. * Most providers will not look to the redirect url but always redirect to * the preconfigured callback. So this factory does not provide a callback url. * @param authProvider the auth provider to use * @return the auth handler */ public static io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler create(io.vertx.rxjava.ext.auth.oauth2.OAuth2Auth authProvider) { io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler ret = io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler.newInstance(io.vertx.ext.web.handler.OAuth2AuthHandler.create(authProvider.getDelegate())); return ret; }
/** * Create a OAuth2 auth handler with host pinning * @param authProvider the auth provider to use * @param callbackURL the callback URL you entered in your provider admin console, usually it should be something like: `https://myserver:8888/callback` * @return the auth handler */ public static io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler create(io.vertx.rxjava.ext.auth.oauth2.OAuth2Auth authProvider, String callbackURL) { io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler ret = io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler.newInstance(io.vertx.ext.web.handler.OAuth2AuthHandler.create(authProvider.getDelegate(), callbackURL)); return ret; }
/** * Create a OAuth2 auth handler without host pinning. * Most providers will not look to the redirect url but always redirect to * the preconfigured callback. So this factory does not provide a callback url. * @param authProvider the auth provider to use * @return the auth handler */ public static io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler create(io.vertx.rxjava.ext.auth.oauth2.OAuth2Auth authProvider) { io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler ret = io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler.newInstance(io.vertx.ext.web.handler.OAuth2AuthHandler.create(authProvider.getDelegate())); return ret; }
/** * Create a OAuth2 auth handler with host pinning * @param authProvider the auth provider to use * @param callbackURL the callback URL you entered in your provider admin console, usually it should be something like: `https://myserver:8888/callback` * @return the auth handler */ public static io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler create(io.vertx.rxjava.ext.auth.oauth2.OAuth2Auth authProvider, String callbackURL) { io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler ret = io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler.newInstance(io.vertx.ext.web.handler.OAuth2AuthHandler.create(authProvider.getDelegate(), callbackURL)); return ret; }
private static OAuth2AuthHandler standardAuth(Vertx vertx, Router router, VertxEngineConfig apimanConfig, JsonObject authConfig, OAuth2FlowType flowType) { String proto = apimanConfig.isSSL() ? "https://" : "http://"; int port = apimanConfig.getPort(ApiVerticle.VERTICLE_TYPE); String hostname = Optional.of(apimanConfig.getPublicEndpoint()).orElse(apimanConfig.getHostname()); String redirect = proto + hostname + ":" + port; // Redirect back here to *after* auth. // Set up KC OAuth2 Authentication OAuth2AuthHandler auth = OAuth2AuthHandler.create(KeycloakAuth.create(vertx, flowType, authConfig), redirect); // Callback can be anything (as long as it's not already used by something else). auth.setupCallback(router.get("/callback")); return auth; }
OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.create(oauth);
OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.create(oauth2, "http://localhost:8080/callback");
@Test public void testBearerOnly() 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")); OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.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", 401, "Unauthorized"); // Now try again with fake credentials testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Bearer 4adc339e0"), 401, "Unauthorized", "Unauthorized"); }
OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.create(oauth);