Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
50 lines
1.4 KiB
C++
50 lines
1.4 KiB
C++
#include "Cookies.hpp"
|
|
|
|
#include <cstdio>
|
|
|
|
namespace molo::example::fw {
|
|
|
|
namespace {
|
|
|
|
unsigned long long fnv1a(const std::string& s) {
|
|
unsigned long long h = 1469598103934665603ULL;
|
|
for (unsigned char c : s) {
|
|
h ^= c;
|
|
h *= 1099511628211ULL;
|
|
}
|
|
return h;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::string cookie_sign(const std::string& value, const std::string& secret) {
|
|
char buf[32];
|
|
std::snprintf(buf, sizeof(buf), "%016llx",
|
|
(unsigned long long)fnv1a(secret + "|" + value));
|
|
return value + "." + buf;
|
|
}
|
|
|
|
bool cookie_verify(const std::string& signed_value, const std::string& secret,
|
|
std::string& out_value) {
|
|
std::size_t dot = signed_value.rfind('.');
|
|
if (dot == std::string::npos) return false;
|
|
std::string value = signed_value.substr(0, dot);
|
|
return cookie_sign(value, secret) == signed_value
|
|
? (out_value = value, true)
|
|
: false;
|
|
}
|
|
|
|
std::string make_set_cookie(const std::string& name, const std::string& value,
|
|
const std::string& path, bool http_only,
|
|
bool secure, const std::string& same_site,
|
|
long max_age) {
|
|
std::string out = name + "=" + value + "; Path=" + path;
|
|
if (max_age >= 0) out += "; Max-Age=" + std::to_string(max_age);
|
|
if (http_only) out += "; HttpOnly";
|
|
if (secure) out += "; Secure";
|
|
out += "; SameSite=" + same_site;
|
|
return out;
|
|
}
|
|
|
|
} // namespace molo::example::fw
|