Skip to content

Commit 97c5012

Browse files
committed
mod sign util
1 parent dd780d7 commit 97c5012

File tree

6 files changed

+114
-73
lines changed

6 files changed

+114
-73
lines changed

demo/openapi_cpp_test/openapi_cpp_test.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ class TestQuoteClient {
406406

407407

408408
static void test_quote(const std::shared_ptr<QuoteClient> quote_client) {
409-
TestQuoteClient::test_get_kline_quota(quote_client);
409+
TestQuoteClient::test_get_quote_delay(quote_client);
410410
}
411411
};
412412

@@ -595,44 +595,42 @@ class TestPushClient {
595595
};
596596

597597
int main(int argc, char* argv[]) {
598-
// // 初始化日志
599-
// START_EASYLOGGINGPP(argc, argv);
600-
//
601-
// 配置日志
598+
599+
// log settings
602600
el::Configurations defaultConf;
603601
defaultConf.setToDefault();
604-
605-
// 只显示WARNING和ERROR级别的日志
602+
606603
defaultConf.set(el::Level::Global, el::ConfigurationType::Enabled, "true");
607-
defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "false");
604+
defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "true");
608605
defaultConf.set(el::Level::Info, el::ConfigurationType::Enabled, "true");
609606
defaultConf.set(el::Level::Warning, el::ConfigurationType::Enabled, "true");
610607
defaultConf.set(el::Level::Error, el::ConfigurationType::Enabled, "true");
611-
612-
// 应用配置
608+
613609
el::Loggers::reconfigureLogger("default", defaultConf);
614610

615611
//Set Tiger OpenAPI SDK configuration
616612
bool sand_box = false;
617-
ClientConfig config = ClientConfig(false, U("../openapi_cpp_test/"));
613+
// ClientConfig config = ClientConfig(false, U("../openapi_cpp_test/"));
618614
// config.set_server_url(U("http://127.0.0.1:8085/gateway"));
619615
// config.set_server_public_key(SANDBOX_TIGER_PUBLIC_KEY);
616+
ClientConfig config = ClientConfig(false);
617+
620618
// config.private_key = U("");
621619
// config.tiger_id = U("");
622620
// config.account = U("");
623621
config.use_full_tick = true;
624622
//config.lang = U("en_US");
625623

626624
//Create a push client instance
627-
auto push_client = IPushClient::create_push_client(config);
625+
// auto push_client = IPushClient::create_push_client(config);
628626
//Run some push test cases
629-
TestPushClient::test_push_client(push_client, config);
627+
// TestPushClient::test_push_client(push_client, config);
630628

631629
/**
632630
* QuoteClient
633631
*/
634-
// std::shared_ptr<QuoteClient> quote_client = std::make_shared<QuoteClient>(config);
635-
// TestQuoteClient::test_quote(quote_client);
632+
std::shared_ptr<QuoteClient> quote_client = std::make_shared<QuoteClient>(config);
633+
TestQuoteClient::test_quote(quote_client);
636634

637635
/**
638636
* TradeClient

include/tigerapi/client_config.h

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ using namespace std;
1919
namespace TIGER_API {
2020
class OPENAPI_EXPORT ClientConfig {
2121
public:
22-
explicit ClientConfig(bool sandbox_debug = false) :
23-
sandbox_debug(sandbox_debug) {
22+
explicit ClientConfig(bool sandbox_debug = false) :
23+
sandbox_debug(sandbox_debug) {
2424
if (sandbox_debug) {
2525
LOG(WARNING) << U("SANDBOX IS NOT SUPPORTED") << endl;
2626
// server_url = SANDBOX_TIGER_SERVER_URL;
@@ -31,17 +31,17 @@ namespace TIGER_API {
3131
// ClientConfig() : sandbox_debug(false) {};
3232

3333
ClientConfig(utility::string_t tiger_id, utility::string_t private_key, utility::string_t account) :
34-
tiger_id(std::move(tiger_id)),
35-
private_key(std::move(private_key)),
36-
account(std::move(account)) {};
34+
tiger_id(std::move(tiger_id)),
35+
private_key(std::move(private_key)),
36+
account(std::move(account)) {};
3737

3838
ClientConfig(utility::string_t tiger_id, utility::string_t private_key, utility::string_t account,
39-
bool sandbox_debug = false, utility::string_t lang = U("en_US")) :
40-
tiger_id(std::move(tiger_id)),
41-
private_key(std::move(private_key)),
42-
account(std::move(account)),
43-
sandbox_debug(sandbox_debug),
44-
lang(lang) {
39+
bool sandbox_debug = false, utility::string_t lang = U("en_US")) :
40+
tiger_id(std::move(tiger_id)),
41+
private_key(std::move(private_key)),
42+
account(std::move(account)),
43+
sandbox_debug(sandbox_debug),
44+
lang(lang) {
4545
if (sandbox_debug) {
4646
LOG(WARNING) << U("SANDBOX IS NOT SUPPORTED") << endl;
4747
// server_url = SANDBOX_TIGER_SERVER_URL;
@@ -51,10 +51,11 @@ namespace TIGER_API {
5151
}
5252
};
5353

54-
explicit ClientConfig(bool sandbox_debug, const utility::string_t props_path) :
55-
sandbox_debug(sandbox_debug),
56-
props_path(props_path) {
54+
explicit ClientConfig(bool sandbox_debug, const utility::string_t props_path) :
55+
sandbox_debug(sandbox_debug),
56+
props_path(props_path) {
5757
load_props();
58+
load_token();
5859
};
5960

6061
utility::string_t tiger_id;
@@ -66,7 +67,7 @@ namespace TIGER_API {
6667
utility::string_t device_id = Utils::get_device_id();
6768
utility::string_t secret_key;
6869
bool use_full_tick = false;
69-
utility::string_t socket_ca_certs;
70+
utility::string_t socket_ca_certs;
7071
unsigned int send_interval = 10 * 1000;
7172
unsigned int receive_interval = 10 * 1000;
7273
utility::string_t license;
@@ -107,23 +108,26 @@ namespace TIGER_API {
107108
this->server_public_key = key;
108109
}
109110

110-
const utility::string_t& get_server_url() {
111+
void set_token(const utility::string_t &token) {
112+
this->token = token;
113+
}
114+
115+
const utility::string_t &get_server_url() {
111116
return this->server_url;
112117
}
113118

114-
const utility::string_t& get_server_pub_key() {
119+
const utility::string_t &get_server_pub_key() {
115120
return this->server_public_key;
116121
}
117122

118-
const utility::string_t& get_socket_url() {
123+
const utility::string_t &get_socket_url() {
119124
return this->socket_url;
120125
}
121126

122-
const utility::string_t& get_socket_port() {
127+
const utility::string_t &get_socket_port() {
123128
return this->socket_port;
124129
}
125130

126-
127131

128132
private:
129133
bool sandbox_debug = false;
@@ -154,7 +158,7 @@ namespace TIGER_API {
154158
}
155159
if (private_key.empty()) {
156160
private_key = props.get_property(U("private_key_pk1"));
157-
}
161+
}
158162
if (account.empty()) {
159163
account = props.get_property(U("account"));
160164
}
@@ -171,14 +175,15 @@ namespace TIGER_API {
171175
socket_url = SANDBOX_TIGER_SOCKET_HOST;
172176
socket_port = SANDBOX_TIGER_SOCKET_PORT;
173177
}
174-
175-
} catch (const std::exception& e) {
178+
179+
} catch (const std::exception &e) {
176180
LOG(ERROR) << U("Failed to load properties file: ") << Utils::str8to16(e.what()) << endl;
177181
}
178-
LOG(INFO) << U("Loaded properties file successfully, tiger_id: ") << tiger_id << " account: " << account << endl;
182+
LOG(INFO) << U("Loaded properties file successfully, tiger_id: ") << tiger_id << " account: " << account
183+
<< endl;
179184
}
180185

181-
utility::string_t get_props_path(const utility::string_t& filename) const {
186+
utility::string_t get_props_path(const utility::string_t &filename) const {
182187
if (!props_path.empty()) {
183188
if (Utils::is_directory(props_path)) {
184189
return Utils::path_join(props_path, filename);
@@ -195,45 +200,52 @@ namespace TIGER_API {
195200

196201
void load_token() {
197202
utility::string_t full_path = get_token_path();
198-
if (!full_path.empty()) {
199-
try {
200-
std::ifstream file(Utils::str16to8(full_path));
201-
if (!file.is_open()) {
202-
LOG(ERROR) << U("Failed to open token file: ") << full_path << endl;
203-
return;
204-
}
205-
206-
Properties props;
207-
props.load(file);
208-
209-
// get token value
210-
token = props.get_property(U("token"));
211-
212-
} catch (const std::exception& e) {
213-
LOG(ERROR) << U("Failed to load token file: ") << Utils::str8to16(e.what()) << endl;
203+
if (full_path.empty()) {
204+
return;
205+
}
206+
// if not exist, return
207+
if (!Utils::is_file(full_path)) {
208+
LOG(DEBUG) << U("Token file not found: ") << full_path << endl;
209+
return;
210+
}
211+
try {
212+
std::ifstream file(Utils::str16to8(full_path));
213+
if (!file.is_open()) {
214+
LOG(ERROR) << U("Failed to open token file: ") << full_path << endl;
215+
return;
214216
}
217+
218+
Properties props;
219+
props.load(file);
220+
221+
// get token value
222+
token = props.get_property(U("token"));
223+
LOG(INFO) << U("Loaded token successfully, token: ") << token << endl;
224+
} catch (const std::exception &e) {
225+
LOG(ERROR) << U("Failed to load token file: ") << Utils::str8to16(e.what()) << endl;
215226
}
216227
}
217228

218-
void save_token(const utility::string_t& new_token) {
229+
void save_token(const utility::string_t &new_token) {
219230
utility::string_t full_path = get_token_path();
220-
if (!full_path.empty()) {
221-
try {
222-
Properties props;
231+
if (full_path.empty()) {
232+
return;
233+
}
234+
try {
235+
Properties props;
223236
props.set_property(U("token"), new_token);
224-
237+
225238
std::ofstream file(Utils::str16to8(full_path));
226239
if (!file.is_open()) {
227240
LOG(ERROR) << U("Failed to open token file for writing: ") << full_path << endl;
228241
return;
229242
}
230-
231-
props.store(file);
232-
token = new_token;
233-
234-
} catch (const std::exception& e) {
235-
LOG(ERROR) << U("Failed to save token file: ") << Utils::str8to16(e.what()) << endl;
236-
}
243+
244+
props.store(file);
245+
token = new_token;
246+
LOG(INFO) << U("Saved token successfully, token: ") << token << endl;
247+
} catch (const std::exception &e) {
248+
LOG(ERROR) << U("Failed to save token file: ") << Utils::str8to16(e.what()) << endl;
237249
}
238250
}
239251
};

include/tigerapi/sign_util.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,22 @@ namespace TIGER_API {
6767
auto str_transfer_content = Utils::str16to8(context);
6868
unsigned int context_size = str_transfer_content.size();
6969
SHA1((const unsigned char *) str_transfer_content.c_str(), context_size, hash);
70+
7071
RSA *rsa = create_rsa((utility::char_t *) key.c_str(), true);
72+
if (rsa == nullptr) {
73+
throw std::runtime_error(U("RSA creation failed, please check your private key"));
74+
}
75+
7176
int ret = RSA_sign(NID_sha1, hash, SHA_DIGEST_LENGTH,
72-
encrypted, &encrypted_length, rsa);
73-
if (ret == 1) {
74-
utility::string_t s(encrypted, encrypted + encrypted_length);
75-
return s;
77+
encrypted, &encrypted_length, rsa);
78+
79+
if (ret != 1) {
80+
throw std::runtime_error(U("RSA_sign failed"));
7681
}
77-
return U("");
82+
83+
utility::string_t s(encrypted, encrypted + encrypted_length);
84+
RSA_free(rsa);
85+
return s;
7886
}
7987

8088
int sha1_verify(const utility::string_t &context, const utility::string_t &sign, const utility::string_t &key) {
@@ -90,6 +98,9 @@ namespace TIGER_API {
9098
SHA1((const unsigned char *) context_s.c_str(), context_s.size(), hash);
9199

92100
RSA *rsa = create_rsa((utility::char_t *) key.c_str(), false);
101+
if (rsa == nullptr) {
102+
throw std::runtime_error("RSA creation failed, please check your private key");
103+
}
93104
int ret = -1;
94105
if (vec_base64_decode.size() > 0) {
95106
ret = RSA_verify(NID_sha1, hash, SHA_DIGEST_LENGTH,

include/tigerapi/utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ namespace TIGER_API {
4545
static utility::string_t double_to_string(double num, int precision = 1);
4646

4747
static bool is_directory(const utility::string_t& path);
48+
49+
static bool is_file(const utility::string_t& path);
50+
4851
static utility::string_t path_dirname(const utility::string_t& path);
52+
4953
static utility::string_t path_join(const utility::string_t& path1, const utility::string_t& path2);
5054
};
5155
}

src/tiger_client.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ namespace TIGER_API {
6262
request.headers().add(U("Accept"), U("application/json"));
6363
request.headers().set_content_type(U("application/json; charset=UTF-8"));
6464
request.headers().add(P_USER_AGENT, P_SDK_VERSION_PREFIX + U(PROJECT_VERSION));
65+
if (!client_config.token.empty()) {
66+
request.headers().add(U("Authorization"), client_config.token);
67+
}
6568

6669
/************************** set request body ***************************/
6770

src/utils.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,19 @@ namespace TIGER_API {
224224
}
225225
}
226226

227+
bool Utils::is_file(const utility::string_t& path) {
228+
try {
229+
#ifdef _WIN32
230+
fs::path fs_path(path);
231+
#else
232+
fs::path fs_path(utility::conversions::to_utf8string(path));
233+
#endif
234+
return fs::is_regular_file(fs_path);
235+
} catch (const std::exception&) {
236+
return false;
237+
}
238+
}
239+
227240
utility::string_t Utils::path_dirname(const utility::string_t& path) {
228241
try {
229242
#ifdef _WIN32

0 commit comments

Comments
 (0)