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 <optional>
26#include <expected>
27#include <boost/uuid/uuid.hpp>
28#include "ores.utility/log/make_logger.hpp"
29#include "ores.comms/messaging/message_types.hpp"
30
31namespace ores::comms::service {
32
37 boost::uuids::uuid account_id;
38 bool is_admin;
39};
40
50private:
51 inline static std::string_view logger_name =
52 "ores.comms.service.auth_session_service";
53
54 static auto& lg() {
55 using namespace ores::utility::log;
56 static auto instance = make_logger(logger_name);
57 return instance;
58 }
59
60public:
61 auth_session_service() = default;
62
69 [[nodiscard]] std::optional<session_info>
70 get_session(const std::string& remote_address) const;
71
78 [[nodiscard]] bool is_authenticated(const std::string& remote_address) const;
79
86 [[nodiscard]] bool is_admin(const std::string& remote_address) const;
87
94 void store_session(const std::string& remote_address, session_info info);
95
101 void remove_session(const std::string& remote_address);
102
106 void clear_all_sessions();
107
122 [[nodiscard]] std::expected<void, messaging::error_code>
123 authorize_request(messaging::message_type type,
124 const std::string& remote_address) const;
125
126private:
130 static bool requires_authentication(messaging::message_type type);
131
135 static bool requires_admin(messaging::message_type type);
136
137 mutable std::mutex session_mutex_;
138 std::map<std::string, session_info> sessions_;
139};
140
141}
142
143#endif
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30
Information about an authenticated session.
Definition auth_session_service.hpp:36
Centralized authentication session management service.
Definition auth_session_service.hpp:49
bool is_admin(const std::string &remote_address) const
Check if a remote address has an admin session.
Definition auth_session_service.cpp:43
void clear_all_sessions()
Remove all sessions (e.g., on server shutdown).
Definition auth_session_service.cpp:71
bool is_authenticated(const std::string &remote_address) const
Check if a remote address has an authenticated session.
Definition auth_session_service.cpp:38
std::expected< void, messaging::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:120
std::optional< session_info > get_session(const std::string &remote_address) const
Get session for a remote address.
Definition auth_session_service.cpp:29
void remove_session(const std::string &remote_address)
Remove session for a remote address.
Definition auth_session_service.cpp:61
void store_session(const std::string &remote_address, session_info info)
Store session for a remote address.
Definition auth_session_service.cpp:52