8#define ATL_TRANSPORT_LAYER_PRESENT 1
10#include <boost/json.hpp>
22namespace json = boost::json;
36template <
typename BrokerType>
40 static constexpr std::string_view
grant_type =
"client_credentials";
48 static_cast<BrokerType*
>(
this)->from_env_impl();
58 static_cast<BrokerType*
>(
this)->from_json_impl(filename);
71 static_cast<BrokerType*
>(
this)->from_json_text_impl(text, origin);
78 const std::string&
key() const noexcept {
86 const std::string&
secret() const noexcept {
98 value[BrokerType::key_field()] =
key_;
99 value[BrokerType::secret_field()] =
secret_;
100 return json::serialize(value);
108 return "grant_type=client_credentials&client_id=" + form_encode(
key_) +
"&client_secret=" + form_encode(
secret_);
122 static std::string form_encode(std::string_view value) {
123 static constexpr char hex[] =
"0123456789ABCDEF";
125 result.reserve(value.size());
126 for (
const unsigned char ch : value) {
127 const bool unreserved = (ch >=
'A' && ch <=
'Z') || (ch >=
'a' && ch <=
'z') || (ch >=
'0' && ch <=
'9') ||
128 ch ==
'-' || ch ==
'.' || ch ==
'_' || ch ==
'~';
130 result.push_back(
static_cast<char>(ch));
132 result.push_back(
'%');
133 result.push_back(hex[ch >> 4]);
134 result.push_back(hex[ch & 0x0F]);
149template <
typename BrokerType>
167 const std::string_view field = BrokerType::token_field();
168 const auto token = document.if_contains(field);
169 if (!token || !token->is_string()) {
170 throw std::runtime_error(
"token response has no " + std::string(field) +
" string");
174 expires_at_ = std::chrono::system_clock::time_point{};
175 if (
const auto value = document.if_contains(
"expires_in")) {
176 std::optional<std::int64_t> expires_in;
177 if (value->is_int64()) {
178 expires_in = value->as_int64();
179 }
else if (value->is_uint64()) {
180 expires_in =
static_cast<std::int64_t
>(value->as_uint64());
181 }
else if (value->is_string()) {
182 expires_in = parse_seconds(value->as_string());
185 expires_at_ = std::chrono::system_clock::now() + std::chrono::seconds(*expires_in);
188 static_cast<BrokerType*
>(
this)->from_json_impl(document);
189 if (
expires_at_ == std::chrono::system_clock::time_point{}) {
190 throw std::runtime_error(
191 "token response states no expiry, neither expires_in nor a broker-specific absolute one"
201 bool valid(std::chrono::seconds margin = std::chrono::minutes(5)) const noexcept {
209 static std::optional<std::int64_t> parse_seconds(std::string_view text) {
210 std::int64_t seconds{0};
211 const auto parsed = std::from_chars(text.data(), text.data() + text.size(), seconds);
212 if (parsed.ec != std::errc{} || parsed.ptr != text.data() + text.size()) {
OAuth client credentials (an app key and secret), the base of each broker's credentials class.
void from_env()
Loads the credentials from the broker's environment variables.
std::string to_form_urlencoded_body() const
Builds a form-encoded token-request body (grant_type, client_id, client_secret).
const std::string & key() const noexcept
Returns the app key (client id).
static constexpr std::string_view grant_type
The OAuth grant type sent in a token request.
std::string key_
The app key (client id).
void from_json_text(std::string_view text, const std::string &origin)
Loads the credentials from JSON text in the broker's format, as from_json reads a file's contents,...
std::string secret_
The app secret.
void from_json(const std::string &filename)
Loads the credentials from a JSON file, in the broker's format.
std::string to_jsonbody() const
Builds a JSON token-request body (grant type, key and secret under the broker's field names).
const std::string & secret() const noexcept
Returns the app secret.
HTTP and WebSocket transport, OAuth tokens, and request pacing.
The library: broker-neutral types (core), and the modules below.
An OAuth access token and its expiry, the base of each broker's token-response class.
bool valid(std::chrono::seconds margin=std::chrono::minutes(5)) const noexcept
Tests whether the token is set and stays valid for at least a margin.
std::chrono::system_clock::time_point expires_at_
When the token expires.
void from_json(const json::object &document)
Reads a token response.
std::string access_token_
The bearer token; do not log it.