algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
token_store.hpp
1#pragma once
2
3#include "../core/types.hpp"
4
5#include <openssl/evp.h>
6
7#include <chrono>
8#include <memory>
9#include <optional>
10#include <string>
11#include <string_view>
12#include <utility>
13
14namespace algotradelib {
15namespace network {
16
20 std::string access_token_;
22 std::chrono::system_clock::time_point expires_at_{};
23};
24
41public:
43 class Lock {
44 public:
45 virtual ~Lock() = default;
46 };
47
48 virtual ~TokenStore() = default;
49
55 virtual Result<std::unique_ptr<Lock>> lock(const std::string& identity) = 0;
56
62 virtual Result<std::optional<StoredToken>> load(const std::string& identity) = 0;
63
70 virtual std::optional<BrokerError> publish(const std::string& identity, const StoredToken& token) = 0;
71
78 virtual std::optional<BrokerError> discard(const std::string& identity, const std::string& access_token) = 0;
79
85 virtual std::optional<BrokerError> remove(const std::string& identity) = 0;
86};
87
93inline BrokerError token_store_error(std::string_view reason) {
94 BrokerError error;
96 error.reached_broker_ = false;
97 error.broker_code_ = "token_store";
98 error.message_ = std::string(reason);
99 return error;
100}
101
108 BrokerError error;
110 error.reached_broker_ = false;
111 error.broker_code_ = "token_unusable";
112 error.message_ = "the access token stopped being usable during the call; nothing was sent with a new one";
113 return error;
114}
115
123inline std::string token_identity(std::string_view broker, std::string_view environment, std::string_view key) {
124 unsigned char digest[EVP_MAX_MD_SIZE];
125 unsigned int length = 0;
126 if (EVP_Digest(key.data(), key.size(), digest, &length, EVP_sha256(), nullptr) != 1) {
127 return {};
128 }
129 static constexpr char hex[] = "0123456789abcdef";
130 std::string identity = std::string(broker) + '-' + std::string(environment) + '-';
131 for (unsigned int index = 0; index < length; ++index) {
132 identity.push_back(hex[digest[index] >> 4]);
133 identity.push_back(hex[digest[index] & 0x0F]);
134 }
135 return identity;
136}
137
138namespace token_store_detail {
139
155template <typename Client, typename Request>
156std::optional<BrokerError> prepare(TokenStore& store, const std::string& identity, Client& client, Request request) {
157 using Token = decltype(client.token());
158 if (client.token().valid()) {
159 return std::nullopt;
160 }
161 auto held = store.lock(identity);
162 if (!held) {
163 return std::move(held.error());
164 }
165 auto loaded = store.load(identity);
166 if (!loaded) {
167 return std::move(loaded.error());
168 }
169 if (loaded.value()) {
170 Token token;
171 token.access_token_ = std::move(loaded.value()->access_token_);
172 token.expires_at_ = loaded.value()->expires_at_;
173 if (client.adopt_token(std::move(token))) {
174 return std::nullopt;
175 }
176 }
177 auto issued = request();
178 if (!issued) {
179 // A token response read only in part can leave a token that looks valid; it was never published.
180 client.forget_token();
181 return std::move(issued.error());
182 }
183 if (auto error = store.publish(identity, StoredToken{issued.value().access_token_, issued.value().expires_at_})) {
184 client.forget_token();
185 return error;
186 }
187 return std::nullopt;
188}
189
201template <typename Token>
202void discard_refused(TokenStore& store, const std::string& identity, const Token& carried, const Token& held) {
203 if (carried.access_token_.empty() || !held.access_token_.empty()) {
204 return;
205 }
206 if (const auto lock = store.lock(identity)) {
207 store.discard(identity, carried.access_token_);
208 }
209}
210
211} // namespace token_store_detail
212} // namespace network
213} // namespace algotradelib
Either a value or a BrokerError.
Definition types.hpp:1058
Held while one caller works on one identity; destroying it releases the lock.
Where blocking broker clients on one key share its access token, across threads and processes.
virtual std::optional< BrokerError > publish(const std::string &identity, const StoredToken &token)=0
Stores a token for an identity, unless the stored one expires later.
virtual Result< std::unique_ptr< Lock > > lock(const std::string &identity)=0
Takes the lock of one identity, waiting a bounded time for another holder.
virtual std::optional< BrokerError > remove(const std::string &identity)=0
Removes an identity's token, whatever it is.
virtual std::optional< BrokerError > discard(const std::string &identity, const std::string &access_token)=0
Removes an identity's token if it is the given one.
virtual Result< std::optional< StoredToken > > load(const std::string &identity)=0
Reads the token stored for an identity.
HTTP and WebSocket transport, OAuth tokens, and request pacing.
BrokerError token_store_error(std::string_view reason)
Returns a store's error: AUTHENTICATION, not sent to the broker, code token_store.
BrokerError token_unusable_error()
Returns the error of a call whose token stopped being usable while it ran on a client with a store: A...
std::string token_identity(std::string_view broker, std::string_view environment, std::string_view key)
Names what a token may not outlive: the broker, the environment and the app key.
The library: broker-neutral types (core), and the modules below.
@ AUTHENTICATION
The credentials or token were refused.
Definition types.hpp:956
The error a broker or transport operation returns.
Definition types.hpp:1003
std::string message_
A human-readable description, for logs; do not parse it.
Definition types.hpp:1033
ErrorCategory category_
The kind of failure; the field to branch on.
Definition types.hpp:1005
std::string broker_code_
The broker's own error code, verbatim (for example KIS's EGW00201).
Definition types.hpp:1022
bool reached_broker_
Whether a request was sent before this error.
Definition types.hpp:1014
An access token as a TokenStore keeps it.
std::chrono::system_clock::time_point expires_at_
When it expires, as the client that obtained it computed it.
std::string access_token_
The bearer token; do not log it.