Boost C++ Libraries

PrevUpHomeNext

Function template guard

boost::taar::handler::guard — Run a check before a route's handler.

Synopsis

// In header: <boost/taar/handler/guard.hpp>


template<typename Check, typename Inner> auto guard(Check check, Inner inner);

Description

auto require_admin = [](http::request_header<> const& req, taar::matcher::context& ctx)
{
    if (req[http::field::authorization] != "Bearer admin-token")
        return taar::reject(http::status::forbidden);
    ctx.attributes.set(principal {"admin"});
    return taar::proceed();
};

session.register_request_handler(
    method == http::verb::delete_ && target == "/tasks/{id}",
    taar::handler::guard(require_admin,
        taar::handler::rest(&delete_task, path_arg("id"), attribute_arg<principal>())));

Parameters:

check

A callable with the interceptor signature returning taar::interceptor_result or an awaitable of it. It must not be a generic lambda (its parameters are inspected to inject contexts).

inner

The handler to run when the check lets the request proceed.


PrevUpHomeNext