ORE Studio 0.0.4
Loading...
Searching...
No Matches
auth_session_service.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_COMMS_SERVICE_AUTH_SESSION_SERVICE_HPP
21#define ORES_COMMS_SERVICE_AUTH_SESSION_SERVICE_HPP
22
23#include <map>
24#include <mutex>
25#include <memory>
26#include <optional>
27#include <expected>
28#include <boost/uuid/uuid.hpp>
29#include "ores.logging/make_logger.hpp"
30#include "ores.comms/messaging/message_types.hpp"
31#include "ores.comms/service/session_data.hpp"
32
33namespace ores::comms::service {
34
44 boost::uuids::uuid account_id;
45};
46
54 std::string client_identifier;
55 std::uint16_t client_version_major = 0;
56 std::uint16_t client_version_minor = 0;
57};
58
68private:
69 inline static std::string_view logger_name =
70 "ores.comms.service.auth_session_service";
71
72 static auto& lg() {
73 using namespace ores::logging;
74 static auto instance = make_logger(logger_name);
75 return instance;
76 }
77
78public:
79 auth_session_service() = default;
80
87 [[nodiscard]] std::optional<session_info>
88 get_session(const std::string& remote_address) const;
89
96 [[nodiscard]] bool is_authenticated(const std::string& remote_address) const;
97
105 void store_session(const std::string& remote_address, session_info info);
106
113 void store_session_data(const std::string& remote_address,
114 std::shared_ptr<session_data> session);
115
122 [[nodiscard]] std::shared_ptr<session_data>
123 get_session_data(const std::string& remote_address) const;
124
132 void update_session_bytes(const std::string& remote_address,
133 std::uint64_t bytes_sent, std::uint64_t bytes_received);
134
141 std::shared_ptr<session_data>
142 remove_session(const std::string& remote_address);
143
149 std::vector<std::shared_ptr<session_data>> clear_all_sessions();
150
154 [[nodiscard]] std::vector<std::shared_ptr<session_data>>
155 get_all_sessions() const;
156
166 void store_client_info(const std::string& remote_address, client_info info);
167
174 [[nodiscard]] std::optional<client_info>
175 get_client_info(const std::string& remote_address) const;
176
184 void remove_client_info(const std::string& remote_address);
185
201 [[nodiscard]] std::expected<void, ores::utility::serialization::error_code>
202 authorize_request(messaging::message_type type,
203 const std::string& remote_address) const;
204
205private:
209 static bool requires_authentication(messaging::message_type type);
210
211 mutable std::mutex session_mutex_;
212 std::map<std::string, std::shared_ptr<session_data>> sessions_;
213
214 mutable std::mutex client_info_mutex_;
215 std::map<std::string, client_info> client_infos_;
216};
217
218}
219
220#endif
Main server application for ORE Studio.
Definition application.hpp:30
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Lightweight session info for backward compatibility.
Definition auth_session_service.hpp:43
Client information captured during handshake.
Definition auth_session_service.hpp:53
Centralized authentication session management service.
Definition auth_session_service.hpp:67
void update_session_bytes(const std::string &remote_address, std::uint64_t bytes_sent, std::uint64_t bytes_received)
Update byte counters for an active session.
Definition auth_session_service.cpp:77
std::expected< void, ores::utility::serialization::error_code > authorize_request(messaging::message_type type, const std::string &remote_address) const
Check if a request is authorized based on message type and session.
Definition auth_session_service.cpp:183
bool is_authenticated(const std::string &remote_address) const
Check if a remote address has an authenticated session.
Definition auth_session_service.cpp:52
std::vector< std::shared_ptr< session_data > > clear_all_sessions()
Remove all sessions (e.g., on server shutdown).
Definition auth_session_service.cpp:103
std::optional< client_info > get_client_info(const std::string &remote_address) const
Get client info for a remote address.
Definition auth_session_service.cpp:139
std::shared_ptr< session_data > remove_session(const std::string &remote_address)
Remove session for a remote address.
Definition auth_session_service.cpp:88
std::optional< session_info > get_session(const std::string &remote_address) const
Get session for a remote address.
Definition auth_session_service.cpp:30
std::vector< std::shared_ptr< session_data > > get_all_sessions() const
Get all active sessions.
Definition auth_session_service.cpp:118
std::shared_ptr< session_data > get_session_data(const std::string &remote_address) const
Get full session data for a remote address.
Definition auth_session_service.cpp:43
void remove_client_info(const std::string &remote_address)
Remove client info for a remote address.
Definition auth_session_service.cpp:148
void store_client_info(const std::string &remote_address, client_info info)
Store client info from handshake.
Definition auth_session_service.cpp:128
void store_session(const std::string &remote_address, session_info info)
Store session for a remote address (legacy interface).
Definition auth_session_service.cpp:57
void store_session_data(const std::string &remote_address, std::shared_ptr< session_data > session)
Store full session data for a remote address.
Definition auth_session_service.cpp:68