Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
Csrf.hpp
Go to the documentation of this file.
1#pragma once
3#include <string>
4#include <string_view>
5#include <memory>
8#include <functional>
9
10namespace orbit::middleware {
11
16class Csrf {
17public:
24 Csrf(const std::string& cookie_name = "csrf_token", const std::string& header_name = "X-CSRF-Token");
25
34 bool operator()(http::HttpRequest& req, std::shared_ptr<http::ResponseWriter> writer);
35
41 static std::string generate_random_token();
42
43private:
44 std::string cookie_name_;
45 std::string header_name_;
46};
47
56inline auto csrf_protection(const std::string& cookie_name = "csrf_token", const std::string& header_name = "X-CSRF-Token") {
57 return [c = std::make_shared<Csrf>(cookie_name, header_name)](http::HttpRequest& req, std::shared_ptr<http::ResponseWriter> writer) -> bool {
58 return (*c)(req, writer);
59 };
60}
61
62} // namespace middleware
Cross-Site Request Forgery (CSRF) protection middleware.
Definition Csrf.hpp:16
static std::string generate_random_token()
Generates a random CSRF token.
bool operator()(http::HttpRequest &req, std::shared_ptr< http::ResponseWriter > writer)
Middleware execution operator.
std::string cookie_name_
Definition Csrf.hpp:44
Csrf(const std::string &cookie_name="csrf_token", const std::string &header_name="X-CSRF-Token")
Constructs a new Csrf middleware instance.
std::string header_name_
Definition Csrf.hpp:45
auto csrf_protection(const std::string &cookie_name="csrf_token", const std::string &header_name="X-CSRF-Token")
Helper to create a CSRF protection middleware handler.
Definition Csrf.hpp:56
Definition HttpRequest.hpp:19
Represents an incoming HTTP request.
Definition HttpRequest.hpp:54