3#include "../core/types.hpp"
5#include <openssl/evp.h>
45 virtual ~Lock() =
default;
70 virtual std::optional<BrokerError>
publish(
const std::string& identity,
const StoredToken& token) = 0;
78 virtual std::optional<BrokerError>
discard(
const std::string& identity,
const std::string& access_token) = 0;
85 virtual std::optional<BrokerError>
remove(
const std::string& identity) = 0;
98 error.
message_ = std::string(reason);
112 error.
message_ =
"the access token stopped being usable during the call; nothing was sent with a new one";
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) {
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]);
138namespace token_store_detail {
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()) {
161 auto held = store.lock(identity);
163 return std::move(held.error());
165 auto loaded = store.load(identity);
167 return std::move(loaded.error());
169 if (loaded.value()) {
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))) {
177 auto issued = request();
180 client.forget_token();
181 return std::move(issued.error());
183 if (
auto error = store.publish(identity, StoredToken{issued.value().access_token_, issued.value().expires_at_})) {
184 client.forget_token();
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()) {
206 if (
const auto lock = store.lock(identity)) {
207 store.discard(identity, carried.access_token_);
Either a value or a BrokerError.
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.
The error a broker or transport operation returns.
std::string message_
A human-readable description, for logs; do not parse it.
ErrorCategory category_
The kind of failure; the field to branch on.
std::string broker_code_
The broker's own error code, verbatim (for example KIS's EGW00201).
bool reached_broker_
Whether a request was sent before this error.
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.