Skip to content

Commit 7d7fc6a

Browse files
committed
feat: Add OAuth 2.0 support to C and C++ SDK
Add lb_config_from_oauth to C SDK and Config::from_oauth to C++ SDK, following the OAuth support added in f12dfb8 for other language SDKs.
1 parent f12dfb8 commit 7d7fc6a

4 files changed

Lines changed: 56 additions & 3 deletions

File tree

c/csrc/include/longport.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3902,6 +3902,19 @@ struct lb_config_t *lb_config_new(const char *app_key,
39023902
bool enable_print_quote_packages,
39033903
const char *log_path);
39043904

3905+
/**
3906+
* Create a new `Config` for OAuth 2.0 authentication
3907+
*
3908+
* OAuth 2.0 is the recommended authentication method that uses Bearer tokens
3909+
* and does not require app_secret or HMAC signatures.
3910+
*
3911+
* # Arguments
3912+
*
3913+
* - `client_id` - OAuth 2.0 client ID
3914+
* - `access_token` - OAuth 2.0 access token (Bearer prefix is optional)
3915+
*/
3916+
struct lb_config_t *lb_config_from_oauth(const char *client_id, const char *access_token);
3917+
39053918
/**
39063919
* Free the config object
39073920
*/

c/src/config.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
ffi::{CStr, c_void},
2+
ffi::{c_void, CStr},
33
os::raw::c_char,
44
sync::Arc,
55
};
@@ -8,8 +8,8 @@ use longport::Config;
88
use time::OffsetDateTime;
99

1010
use crate::{
11-
async_call::{CAsyncCallback, execute_async},
12-
error::{CError, set_error},
11+
async_call::{execute_async, CAsyncCallback},
12+
error::{set_error, CError},
1313
types::{CLanguage, CPushCandlestickMode, CString},
1414
};
1515

@@ -120,6 +120,30 @@ pub unsafe extern "C" fn lb_config_new(
120120
Box::into_raw(Box::new(CConfig(Arc::new(config))))
121121
}
122122

123+
/// Create a new `Config` for OAuth 2.0 authentication
124+
///
125+
/// OAuth 2.0 is the recommended authentication method that uses Bearer tokens
126+
/// and does not require app_secret or HMAC signatures.
127+
///
128+
/// # Arguments
129+
///
130+
/// - `client_id` - OAuth 2.0 client ID
131+
/// - `access_token` - OAuth 2.0 access token (Bearer prefix is optional)
132+
#[unsafe(no_mangle)]
133+
pub unsafe extern "C" fn lb_config_from_oauth(
134+
client_id: *const c_char,
135+
access_token: *const c_char,
136+
) -> *mut CConfig {
137+
let client_id = CStr::from_ptr(client_id)
138+
.to_str()
139+
.expect("invalid client id");
140+
let access_token = CStr::from_ptr(access_token)
141+
.to_str()
142+
.expect("invalid access token");
143+
let config = Config::from_oauth(client_id, access_token);
144+
Box::into_raw(Box::new(CConfig(Arc::new(config))))
145+
}
146+
123147
/// Free the config object
124148
#[unsafe(no_mangle)]
125149
pub unsafe extern "C" fn lb_config_free(config: *mut CConfig) {

cpp/include/config.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ class Config
5757

5858
operator const lb_config_t*() const;
5959

60+
/// Create a new `Config` for OAuth 2.0 authentication
61+
///
62+
/// OAuth 2.0 is the recommended authentication method that uses Bearer tokens
63+
/// and does not require app_secret or HMAC signatures.
64+
///
65+
/// @param client_id OAuth 2.0 client ID
66+
/// @param access_token OAuth 2.0 access token (Bearer prefix is optional)
67+
static Config from_oauth(const std::string& client_id,
68+
const std::string& access_token);
69+
6070
/// Create a new `Config` from the given environment variables
6171
///
6272
/// It first gets the environment variables from the `.env` file in the

cpp/src/config.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ Config::from_env(Config& config)
8181
return status;
8282
}
8383

84+
Config
85+
Config::from_oauth(const std::string& client_id, const std::string& access_token)
86+
{
87+
return Config(lb_config_from_oauth(client_id.c_str(), access_token.c_str()));
88+
}
89+
8490
void
8591
Config::refresh_access_token(int64_t expired_at,
8692
AsyncCallback<void*, std::string> callback)

0 commit comments

Comments
 (0)