Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
Csrf.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <string_view>
4#include <memory>
7#include <functional>
8
9namespace middleware {
10
15class Csrf {
16public:
23 Csrf(const std::string& cookie_name = "csrf_token", const std::string& header_name = "X-CSRF-Token");
24
33 bool operator()(http::HttpRequest& req, std::shared_ptr<http::ResponseWriter> writer);
34
40 static std::string generate_random_token();
41
42private:
43 std::string cookie_name_;
44 std::string header_name_;
45};
46
55inline auto csrf_protection(const std::string& cookie_name = "csrf_token", const std::string& header_name = "X-CSRF-Token") {
56 return [c = std::make_shared<Csrf>(cookie_name, header_name)](http::HttpRequest& req, std::shared_ptr<http::ResponseWriter> writer) -> bool {
57 return (*c)(req, writer);
58 };
59}
60
61} // namespace middleware
Cross-Site Request Forgery (CSRF) protection middleware.
Definition Csrf.hpp:15
static std::string generate_random_token()
Generates a random CSRF token.
Definition Csrf.cpp:10
bool operator()(http::HttpRequest &req, std::shared_ptr< http::ResponseWriter > writer)
Middleware execution operator.
Definition Csrf.cpp:26
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:55
Definition Compress.hpp:4
Represents an incoming HTTP request.
Definition HttpRequest.hpp:22