ORE Studio 0.0.4
Loading...
Searching...
No Matches
nats_client.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 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_NATS_SERVICE_NATS_CLIENT_HPP
21#define ORES_NATS_SERVICE_NATS_CLIENT_HPP
22
23#include <chrono>
24#include <functional>
25#include <memory>
26#include <optional>
27#include <span>
28#include <string>
29#include <string_view>
30#include "ores.logging/make_logger.hpp"
31#include "ores.nats/config/nats_options.hpp"
32#include "ores.nats/domain/headers.hpp"
33#include "ores.nats/domain/message.hpp"
34#include "ores.nats/service/session_expired_error.hpp"
35
36namespace ores::nats::service { class client; }
37
38namespace ores::nats::service {
39
73private:
74 inline static std::string_view logger_name = "ores.nats.service.nats_client";
75
76 [[nodiscard]] static auto& lg() {
77 using namespace ores::logging;
78 static auto instance = make_logger(logger_name);
79 return instance;
80 }
81
82public:
88 struct login_info {
89 std::string jwt;
90 std::string username;
91 std::string tenant_id;
92 std::string tenant_name;
93 };
94
105 using token_provider = std::function<std::string(bool force)>;
106
107 // -- Interactive path --
108
112 nats_client() = default;
113
119 void connect(config::nats_options opts);
120
124 void disconnect();
125
126 [[nodiscard]] bool is_connected() const noexcept;
127
131 void set_auth(login_info info);
132
136 void clear_auth();
137
142 [[nodiscard]] const login_info& auth() const;
143
144 // -- Service path --
145
153 nats_client(client& nats, token_provider provider);
154
155 // -- Common --
156
157 [[nodiscard]] bool is_logged_in() const noexcept;
158
164 [[nodiscard]] message request(std::string_view subject,
165 std::string_view json_body);
166
172 [[nodiscard]] message authenticated_request(std::string_view subject,
173 std::string_view json_body,
174 std::chrono::milliseconds timeout = std::chrono::seconds(30));
175
181 [[nodiscard]] message authenticated_request(std::string_view subject,
182 std::span<const std::byte> body,
183 std::chrono::milliseconds timeout = std::chrono::seconds(10));
184
197 [[nodiscard]] nats_client with_delegation(std::string token) const;
198
209 [[nodiscard]] nats_client with_correlation_id(std::string cid) const;
210
216 [[nodiscard]] std::shared_ptr<client> get_client() const;
217
218private:
219 [[nodiscard]] client& active_client() const;
220
221 [[nodiscard]] message do_authenticated_request(std::string_view subject,
222 std::span<const std::byte> body, std::chrono::milliseconds timeout);
223
224 // Interactive path state
225 std::shared_ptr<client> owned_client_;
226 std::optional<login_info> auth_;
227
228 // Service path state
229 client* external_client_ = nullptr;
230 token_provider token_provider_;
231
232 // Optional delegation token forwarded as X-Delegated-Authorization.
233 std::string delegation_token_;
234
235 // Optional correlation ID forwarded as Nats-Correlation-Id.
236 std::string correlation_id_;
237};
238
248[[nodiscard]] std::string extract_bearer(const ores::nats::message& msg);
249
250} // namespace ores::nats::service
251
252#endif
STL namespace.
ORE Studio - Graphical interface and data management for Open Source Risk Engine.
Definition image.hpp:29
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
@ client
Indicates that the span describes a request to some remote service. This is often the client-side of ...
Configuration for a NATS connection.
Definition nats_options.hpp:30
A received NATS message.
Definition message.hpp:40
NATS client: connection, pub/sub, request/reply, and JetStream.
Definition client.hpp:73
Authenticated NATS client for both interactive and service-to-service use.
Definition nats_client.hpp:72
const login_info & auth() const
Return current auth info.
Definition nats_client.cpp:76
nats_client with_correlation_id(std::string cid) const
Returns a new nats_client that forwards a Nats-Correlation-Id header on every authenticated_request c...
Definition nats_client.cpp:163
void clear_auth()
Clear authentication info on logout.
Definition nats_client.cpp:68
std::function< std::string(bool force)> token_provider
Callable that returns a current Bearer token.
Definition nats_client.hpp:105
nats_client()=default
Default-construct for interactive use. Call connect() next.
void set_auth(login_info info)
Store authentication info after a successful login.
Definition nats_client.cpp:64
void disconnect()
Disconnect and clear all auth state.
Definition nats_client.cpp:49
nats_client with_delegation(std::string token) const
Returns a new nats_client that shares this instance's underlying connection and token provider but in...
Definition nats_client.cpp:152
std::shared_ptr< client > get_client() const
Return the underlying client (interactive path only).
Definition nats_client.cpp:194
void connect(config::nats_options opts)
Connect to NATS and take ownership of the connection.
Definition nats_client.cpp:34
message authenticated_request(std::string_view subject, std::string_view json_body, std::chrono::milliseconds timeout=std::chrono::seconds(30))
Authenticated synchronous request — string body overload.
Definition nats_client.cpp:184
Login state for the interactive path.
Definition nats_client.hpp:88