VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
authHelpers.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2026 MAK Technologies, Inc.
3  * All rights reserved.
4  ******************************************************************************/
5 
6 #pragma once
7 
8 #include "sessionManager.h"
9 #include "webServerSettings.h"
10 
11 #include <oatpp/web/protocol/http/outgoing/ResponseFactory.hpp>
12 #include <oatpp/web/protocol/http/Http.hpp>
13 #include <json/json.h>
14 
15 #include <memory>
16 #include <sstream>
17 #include <string>
18 
23 
24 namespace DtAuthHelpers
25 {
26 
29 inline std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> makeJsonResponse(
30  const oatpp::web::protocol::http::Status& status, const Json::Value& body)
31 {
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");
38  return resp;
39 }
40 
42 inline std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> makeJsonError(
43  const oatpp::web::protocol::http::Status& status, const std::string& message)
44 {
45  Json::Value body;
46  body["error"] = message;
47  return makeJsonResponse(status, body);
48 }
49 
52 inline bool parseJsonBody(
53  const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request,
54  Json::Value& out,
55  std::string& errors)
56 {
57  const auto bodyStr = request->readBodyToString();
58  if (!bodyStr || bodyStr->empty())
59  {
60  errors = "Request body is empty.";
61  return false;
62  }
63  Json::CharReaderBuilder builder;
64  std::istringstream ss(bodyStr->c_str());
65  return Json::parseFromStream(builder, ss, &out, &errors);
66 }
67 
70 inline std::string extractSessionKey(
71  const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request)
72 {
73  const auto header = request->getHeader("Authorization");
74  if (!header)
75  {
76  return {};
77  }
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)
81  {
82  return {};
83  }
84  return value.substr(prefix.size());
85 }
86 
93 inline std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> requirePermission(
94  const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request,
95  const std::string& permission)
96 {
97  if (!DtWebServerSettings::getInstance().accessControlEnabled)
98  {
99  return nullptr;
100  }
101 
102  const std::string key = extractSessionKey(request);
103  if (key.empty())
104  {
105  return makeJsonError(
106  oatpp::web::protocol::http::Status::CODE_401,
107  "Missing or invalid Authorization header. Use 'Authorization: Bearer <sessionKey>'.");
108  }
109 
110  if (!DtSessionManager::getInstance().validateSession(key))
111  {
112  return makeJsonError(
113  oatpp::web::protocol::http::Status::CODE_401, "Invalid or expired session key.");
114  }
115 
116  if (!DtSessionManager::getInstance().hasPermission(key, permission))
117  {
118  return makeJsonError(
119  oatpp::web::protocol::http::Status::CODE_403,
120  "Permission denied. Required: " + permission);
121  }
122 
124  return nullptr;
125 }
126 
134 inline std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> requireValidSession(
135  const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request)
136 {
137  if (!DtWebServerSettings::getInstance().accessControlEnabled)
138  {
139  return nullptr;
140  }
141 
142  const std::string key = extractSessionKey(request);
143  if (key.empty() || !DtSessionManager::getInstance().validateSession(key))
144  {
145  return makeJsonError(
146  oatpp::web::protocol::http::Status::CODE_401,
147  "Valid session required. Authenticate via POST /api/auth/login.");
148  }
149 
151  return nullptr;
152 }
153 
154 } // namespace DtAuthHelpers
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

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)