11 #include <oatpp/web/protocol/http/outgoing/ResponseFactory.hpp>
12 #include <oatpp/web/protocol/http/Http.hpp>
13 #include <json/json.h>
24 namespace DtAuthHelpers
29 inline std::shared_ptr<oatpp::web::protocol::http::outgoing::Response>
makeJsonResponse(
30 const oatpp::web::protocol::http::Status& status,
const Json::Value& body)
32 Json::StreamWriterBuilder writer;
33 writer[
"indentation"] =
"";
34 const std::string text = Json::writeString(writer, body);
35 auto resp = oatpp::web::protocol::http::outgoing::ResponseFactory::createResponse(
36 status, text.c_str());
37 resp->putHeader(
"Content-Type",
"application/json");
42 inline std::shared_ptr<oatpp::web::protocol::http::outgoing::Response>
makeJsonError(
43 const oatpp::web::protocol::http::Status& status,
const std::string& message)
46 body[
"error"] = message;
53 const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request,
57 const auto bodyStr = request->readBodyToString();
58 if (!bodyStr || bodyStr->empty())
60 errors =
"Request body is empty.";
63 Json::CharReaderBuilder builder;
64 std::istringstream ss(bodyStr->c_str());
65 return Json::parseFromStream(builder, ss, &out, &errors);
71 const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request)
73 const auto header = request->getHeader(
"Authorization");
78 const std::string value = header->c_str();
79 const std::string prefix =
"Bearer ";
80 if (value.size() <= prefix.size() || value.substr(0, prefix.size()) != prefix)
84 return value.substr(prefix.size());
94 const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request,
95 const std::string& permission)
106 oatpp::web::protocol::http::Status::CODE_401,
107 "Missing or invalid Authorization header. Use 'Authorization: Bearer <sessionKey>'.");
113 oatpp::web::protocol::http::Status::CODE_401,
"Invalid or expired session key.");
119 oatpp::web::protocol::http::Status::CODE_403,
120 "Permission denied. Required: " + permission);
135 const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request)
146 oatpp::web::protocol::http::Status::CODE_401,
147 "Valid session required. Authenticate via POST /api/auth/login.");
std::shared_ptr< oatpp::web::protocol::http::outgoing::Response > requireValidSession(const std::shared_ptr< oatpp::web::protocol::http::incoming::Request > &request)
Checks only that the request carries a valid session key, with no specific permission required...
Definition: authHelpers.h:134
static DtWebServerSettings & getInstance()
Returns the process-wide singleton instance.
bool validateSession(const std::string &sessionKey) const
Returns true if a session with the given key exists.
std::shared_ptr< oatpp::web::protocol::http::outgoing::Response > makeJsonResponse(const oatpp::web::protocol::http::Status &status, const Json::Value &body)
Builds a JSON response with the given HTTP status and body. The body is serialized to a compact JSON ...
Definition: authHelpers.h:29
IMGUI_API void Value(const char *prefix, bool b)
void touchSession(const std::string &sessionKey)
Updates the last-activity timestamp for the given session to the current time. Does nothing if the se...
std::shared_ptr< oatpp::web::protocol::http::outgoing::Response > makeJsonError(const oatpp::web::protocol::http::Status &status, const std::string &message)
Builds a minimal JSON error response.
Definition: authHelpers.h:42
std::shared_ptr< oatpp::web::protocol::http::outgoing::Response > requirePermission(const std::shared_ptr< oatpp::web::protocol::http::incoming::Request > &request, const std::string &permission)
Checks whether the request carries a valid session key with the required permission.
Definition: authHelpers.h:93
static DtSessionManager & getInstance()
Returns the process-wide singleton instance.
std::string extractSessionKey(const std::shared_ptr< oatpp::web::protocol::http::incoming::Request > &request)
Extracts the Bearer token from the Authorization header. Returns an empty string if the header is abs...
Definition: authHelpers.h:70
bool parseJsonBody(const std::shared_ptr< oatpp::web::protocol::http::incoming::Request > &request, Json::Value &out, std::string &errors)
Parses the JSON body from a request. Returns false and sets errors if the body is missing...
Definition: authHelpers.h:52