3#include <boost/json.hpp>
12#include "../../network/oauth.hpp"
13#include "../../network/token_store.hpp"
59static inline std::chrono::system_clock::time_point
parse_expiry(
const char* s) {
62 std::sscanf(s,
"%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6
70 const std::time_t utc = _mkgmtime(&tm) - 9 * 3600;
72 const std::time_t utc = timegm(&tm) - 9 * 3600;
74 return std::chrono::system_clock::from_time_t(utc);
110 const char*
key = std::getenv(
"KIS_APP_KEY");
111 const char*
secret = std::getenv(
"KIS_APP_SECRET");
113 throw std::runtime_error(
"Add KIS_APP_KEY and KIS_APP_SECRET environment variables.");
134 std::ifstream ifs(filename);
135 if (!ifs.is_open()) {
136 throw std::runtime_error(
"Cannot open KIS credential file: " + filename);
138 const std::string content(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>{});
151 boost::system::error_code ec;
152 const auto jv = json::parse(text, ec);
154 throw std::runtime_error(
"Failed to parse KIS credentials " + origin +
" — " + ec.message());
156 if (!jv.is_object()) {
157 throw std::runtime_error(
"Failed to parse KIS credentials " + origin +
" — not a JSON object");
159 const auto& doc = jv.as_object();
160 const json::value*
key = doc.if_contains(
"app_key");
161 const json::value*
secret = doc.if_contains(
"app_secret");
163 throw std::runtime_error(
"KIS credential " + origin +
" should contain app_key and app_secret as strings");
168 key_ =
key->get_string().c_str();
179 const char* demo = std::getenv(
"KIS_DEMO");
180 if (!demo || std::string_view(demo) ==
"1") {
183 if (std::string_view(demo) ==
"0") {
186 throw std::runtime_error(
187 std::string(
"KIS_DEMO is \"") + demo +
188 "\"; set it to 1 for the demo endpoints or 0 to trade "
189 "live, or leave it unset for demo."
201 const json::value* demo = doc.if_contains(
"demo");
205 if (!demo->is_bool()) {
206 throw std::runtime_error(
207 "KIS credential " + origin +
208 " has a \"demo\" that is not true or false; write "
209 "true for the demo endpoints or false to trade live, or leave it out for demo."
212 return demo->get_bool();
219 static constexpr std::string_view
key_field() noexcept {
244 return is_demo_ ?
"openapivts.koreainvestment.com" :
"openapi.koreainvestment.com";
260 return "ops.koreainvestment.com";
268 return is_demo_ ?
"31000" :
"21000";
276 return R
"({"grant_type":"client_credentials","appkey":")" + key_ + R"(","secretkey":")" + secret_ + R"("})";
296 return "access_token";
312 parse_expiry(doc.at(
"access_token_token_expired").as_string().c_str()) :
344 if (!doc.contains(
"approval_key")) {
345 throw std::runtime_error(
"WS approval key authentication failed");
357 const auto now = std::chrono::system_clock::now();
358 const bool never_authed =
last_auth_time_ == std::chrono::system_clock::time_point{};
359 const auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now -
last_auth_time_).count();
360 return !
approval_key_.empty() && !never_authed && elapsed < 86400;
OAuth client credentials (an app key and secret), the base of each broker's credentials class.
const std::string & key() const noexcept
const std::string & secret() const noexcept
The Korea Investment & Securities (KIS) Open API client.
constexpr const char * market_division_code(MarketTape tape) noexcept
Returns KIS's FID_COND_MRKT_DIV_CODE for a tape.
MarketTape
Which venue's prices KIS market-data requests read (see KisConfig::market_tape_).
@ CONSOLIDATED
KRX and NXT combined (UN).
static std::chrono::system_clock::time_point parse_expiry(const char *s)
Reads KIS's token expiry, YYYY-MM-DD HH:MM:SS in Korean time.
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.
KIS credentials (app key and secret), demo or real account, and the market-data tape.
MarketTape market_tape_
Which venue's prices every KIS market-data request reads (quotes, bars, depth, price bands,...
std::string token_identity() const
Names what a token of these credentials may not outlive, for a network::TokenStore.
bool is_demo_
Whether to use KIS's demo (모의투자) endpoints; true unless stated otherwise.
const char * ws_port() const noexcept
Returns the WebSocket port for the environment.
static bool demo_from_json(const json::object &doc, const std::string &origin)
Reads a credential document's "demo".
static constexpr std::string_view key_field() noexcept
Returns the token request's field for the app key.
void from_json_text_impl(std::string_view text, const std::string &origin)
Reads the credentials from JSON text in the file's format; called by from_json_text....
void from_json_impl(const std::string &filename)
Reads the credentials from a JSON file; called by from_json(filename). Nothing changes if it throws.
void from_env_impl()
Reads the credentials from KIS_APP_KEY and KIS_APP_SECRET, and the environment from KIS_DEMO (see dem...
static bool demo_from_env()
Reads KIS_DEMO.
static constexpr std::string_view secret_field() noexcept
Returns the token request's field for the app secret.
std::string ws_to_jsonbody() const
Builds the WebSocket approval-key request body (the secret under secretkey).
const char * rest_port() const noexcept
Returns the REST port for the environment.
const char * rest_host() const noexcept
Returns the REST host for the environment.
const char * ws_host() const noexcept
Returns the WebSocket host (the same for both environments).
void from_json_impl(const json::object &doc)
Reads KIS's stated expiry from a token response; called by from_json.
std::chrono::system_clock::time_point access_token_expired_
KIS's stated expiry (access_token_token_expired), which takes precedence over expires_in.
static constexpr std::string_view token_field() noexcept
Returns the token response's field for the token.
KisRest()
Constructs an empty token.
bool valid() const noexcept
Tests whether the token is set and stays valid for at least five more minutes.
KisWebsocket()
Constructs an empty key.
void from_json(const json::object &doc)
Reads an approval-key response.
bool valid() const noexcept
Tests whether the key is set and was received less than a day ago.
std::chrono::system_clock::time_point last_auth_time_
When it was received.
std::string approval_key_
The approval key.
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_