OAuth2AuthHandler.create(authProvider) .setupCallback(router.route("/callback")) .addAuthority("user:email") );
/** * Extra parameters needed to be passed while requesting a token. * @param extraParams extra optional parameters. * @return self */ public io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler extraParams(JsonObject extraParams) { delegate.extraParams(extraParams); return this; }
OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.create(oauth2, "http://localhost:8080/callback"); oauth2Handler.setupCallback(router.route());
@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"); }
/** * add the callback handler to a given route. * @param route a given route e.g.: `/callback` * @return self */ public io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler setupCallback(io.vertx.rxjava.ext.web.Route route) { delegate.setupCallback(route.getDelegate()); return this; }
public static void parseCredentials(io.vertx.ext.web.handler.OAuth2AuthHandler j_receiver, io.vertx.ext.web.RoutingContext context, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.util.Map<String, Object>>> handler) { j_receiver.parseCredentials(context, handler != null ? new io.vertx.core.Handler<io.vertx.core.AsyncResult<io.vertx.core.json.JsonObject>>() { public void handle(io.vertx.core.AsyncResult<io.vertx.core.json.JsonObject> ar) { handler.handle(ar.map(event -> io.vertx.core.impl.ConversionHelper.fromJsonObject(event))); } } : null); } public static io.vertx.ext.web.handler.OAuth2AuthHandler extraParams(io.vertx.ext.web.handler.OAuth2AuthHandler j_receiver, java.util.Map<String, Object> extraParams) {
/** * Add a required authority for this auth handler * @param authority the authority * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.AuthHandler addAuthority(String authority) { delegate.addAuthority(authority); return this; }
/** * Add a set of required authorities for this auth handler * @param authorities the set of authorities * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.AuthHandler addAuthorities(Set<String> authorities) { delegate.addAuthorities(authorities); return this; }
/** * Authorizes the given user against all added authorities. * @param user a user. * @param handler the handler for the result. */ public void authorize(io.vertx.rxjava.ext.auth.User user, Handler<AsyncResult<Void>> handler) { delegate.authorize(user.getDelegate(), handler); }
router.route().handler(OAuth2AuthHandler.create(oauth2, "http://localhost:8080/callback").setupCallback(router.route())); OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.create(oauth2, "http://localhost:8080/callback").setupCallback(router.route());
/** * 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; }
/** * add the callback handler to a given route. * @param route a given route e.g.: `/callback` * @return self */ public io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler setupCallback(io.vertx.rxjava.ext.web.Route route) { delegate.setupCallback(route.getDelegate()); return this; }
/** * Parses the credentials from the request into a JsonObject. The implementation should * be able to extract the required info for the auth provider in the format the provider * expects. * @param context the routing context * @param handler the handler to be called once the information is available. */ public void parseCredentials(io.vertx.rxjava.ext.web.RoutingContext context, Handler<AsyncResult<JsonObject>> handler) { delegate.parseCredentials(context.getDelegate(), handler); }
/** * Add a required authority for this auth handler * @param authority the authority * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.AuthHandler addAuthority(String authority) { delegate.addAuthority(authority); return this; }
/** * Add a set of required authorities for this auth handler * @param authorities the set of authorities * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.AuthHandler addAuthorities(Set<String> authorities) { delegate.addAuthorities(authorities); return this; }
/** * Authorizes the given user against all added authorities. * @param user a user. * @param handler the handler for the result. */ public void authorize(io.vertx.rxjava.ext.auth.User user, Handler<AsyncResult<Void>> handler) { delegate.authorize(user.getDelegate(), handler); }
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; }
/** * 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; }
/** * Extra parameters needed to be passed while requesting a token. * @param extraParams extra optional parameters. * @return self */ public io.vertx.rxjava.ext.web.handler.OAuth2AuthHandler extraParams(JsonObject extraParams) { delegate.extraParams(extraParams); return this; }
/** * Parses the credentials from the request into a JsonObject. The implementation should * be able to extract the required info for the auth provider in the format the provider * expects. * @param context the routing context * @param handler the handler to be called once the information is available. */ public void parseCredentials(io.vertx.rxjava.ext.web.RoutingContext context, Handler<AsyncResult<JsonObject>> handler) { delegate.parseCredentials(context.getDelegate(), handler); }