webull-rs 0.1.0

A Rust client for the Webull trading API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use crate::error::{WebullError, WebullResult};
use crate::config::WebullConfig;
use crate::utils::crypto::{encrypt_password, generate_signature, generate_timestamp};
use crate::utils::serialization::{from_json, to_json};
use chrono::{DateTime, Utc};
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::sync::Mutex;

/// Credentials for authentication.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Credentials {
    /// Username for authentication
    pub username: String,

    /// Password for authentication
    pub password: String,
}

/// Access token for API requests.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccessToken {
    /// The access token
    pub token: String,

    /// When the token expires
    pub expires_at: DateTime<Utc>,

    /// The refresh token
    pub refresh_token: Option<String>,
}

/// Interface for storing and retrieving tokens.
pub trait TokenStore: Send + Sync {
    /// Get the current access token.
    fn get_token(&self) -> WebullResult<Option<AccessToken>>;

    /// Store an access token.
    fn store_token(&self, token: AccessToken) -> WebullResult<()>;

    /// Clear the stored token.
    fn clear_token(&self) -> WebullResult<()>;
}

/// In-memory token store.
#[derive(Debug, Default)]
pub struct MemoryTokenStore {
    token: Mutex<Option<AccessToken>>,
}

impl TokenStore for MemoryTokenStore {
    fn get_token(&self) -> WebullResult<Option<AccessToken>> {
        Ok(self.token.lock().unwrap().clone())
    }

    fn store_token(&self, token: AccessToken) -> WebullResult<()> {
        *self.token.lock().unwrap() = Some(token);
        Ok(())
    }

    fn clear_token(&self) -> WebullResult<()> {
        *self.token.lock().unwrap() = None;
        Ok(())
    }
}

/// Manager for authentication.
pub struct AuthManager {
    /// Credentials for authentication
    credentials: Option<Credentials>,

    /// Token store
    pub token_store: Box<dyn TokenStore>,

    /// Configuration
    config: WebullConfig,

    /// HTTP client
    client: reqwest::Client,
}

impl AuthManager {
    /// Create a new authentication manager.
    pub fn new(
        config: WebullConfig,
        token_store: Box<dyn TokenStore>,
        client: reqwest::Client,
    ) -> Self {
        Self {
            credentials: None,
            token_store,
            config,
            client,
        }
    }

    /// Authenticate with username and password.
    pub async fn authenticate(&mut self, username: &str, password: &str) -> WebullResult<AccessToken> {
        // Store credentials for potential token refresh
        self.credentials = Some(Credentials {
            username: username.to_string(),
            password: password.to_string(),
        });

        // Encrypt the password
        let encrypted_password = encrypt_password(password, &self.config.api_secret.clone().unwrap_or_default())?;

        // Create the request body
        let body = json!({
            "username": username,
            "password": encrypted_password,
            "deviceId": self.config.device_id.clone().unwrap_or_default(),
            "deviceName": "Rust API Client",
            "deviceType": "Web",
        });

        // Create headers
        let mut headers = HeaderMap::new();
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

        // Add API key if available
        if let Some(api_key) = &self.config.api_key {
            headers.insert("api-key", HeaderValue::from_str(api_key).unwrap());
        }

        // Generate timestamp and signature
        let timestamp = generate_timestamp();
        let signature = if let Some(api_secret) = &self.config.api_secret {
            let message = format!("{}{}", timestamp, to_json(&body)?);
            generate_signature(api_secret, &message)?
        } else {
            String::new()
        };

        // Add timestamp and signature to headers
        headers.insert("timestamp", HeaderValue::from_str(&timestamp).unwrap());
        headers.insert("signature", HeaderValue::from_str(&signature).unwrap());

        // Send the request
        let response = self.client.post(format!("{}/api/passport/login/v5/account", self.config.base_url))
            .headers(headers)
            .json(&body)
            .send()
            .await
            .map_err(|e| WebullError::NetworkError(e))?;

        // Check for errors
        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await.unwrap_or_else(|_| "Unknown error".to_string());

            if status.as_u16() == 401 {
                return Err(WebullError::Unauthorized);
            } else if status.as_u16() == 429 {
                return Err(WebullError::RateLimitExceeded);
            } else {
                return Err(WebullError::ApiError {
                    code: status.as_u16().to_string(),
                    message: text,
                });
            }
        }

        // Parse the response
        let response_text = response.text().await
            .map_err(|e| WebullError::NetworkError(e))?;

        #[derive(Debug, Deserialize)]
        struct LoginResponse {
            access_token: String,
            refresh_token: String,
            token_type: String,
            expires_in: i64,
        }

        let login_response: LoginResponse = from_json(&response_text)?;

        // Create the token
        let token = AccessToken {
            token: login_response.access_token,
            expires_at: Utc::now() + chrono::Duration::seconds(login_response.expires_in),
            refresh_token: Some(login_response.refresh_token),
        };

        // Store the token
        self.token_store.store_token(token.clone())?;

        Ok(token)
    }

    /// Handle multi-factor authentication.
    pub async fn multi_factor_auth(&mut self, mfa_code: &str) -> WebullResult<AccessToken> {
        // Check if we have credentials
        let credentials = self.credentials.as_ref()
            .ok_or_else(|| WebullError::InvalidRequest("No credentials available for MFA".to_string()))?;

        // Create the request body
        let body = json!({
            "username": credentials.username,
            "verificationCode": mfa_code,
            "deviceId": self.config.device_id.clone().unwrap_or_default(),
        });

        // Create headers
        let mut headers = HeaderMap::new();
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

        // Add API key if available
        if let Some(api_key) = &self.config.api_key {
            headers.insert("api-key", HeaderValue::from_str(api_key).unwrap());
        }

        // Generate timestamp and signature
        let timestamp = generate_timestamp();
        let signature = if let Some(api_secret) = &self.config.api_secret {
            let message = format!("{}{}", timestamp, to_json(&body)?);
            generate_signature(api_secret, &message)?
        } else {
            String::new()
        };

        // Add timestamp and signature to headers
        headers.insert("timestamp", HeaderValue::from_str(&timestamp).unwrap());
        headers.insert("signature", HeaderValue::from_str(&signature).unwrap());

        // Send the request
        let response = self.client.post(format!("{}/api/passport/verificationCode/verify", self.config.base_url))
            .headers(headers)
            .json(&body)
            .send()
            .await
            .map_err(|e| WebullError::NetworkError(e))?;

        // Check for errors
        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await.unwrap_or_else(|_| "Unknown error".to_string());

            if status.as_u16() == 401 {
                return Err(WebullError::Unauthorized);
            } else if status.as_u16() == 429 {
                return Err(WebullError::RateLimitExceeded);
            } else {
                return Err(WebullError::ApiError {
                    code: status.as_u16().to_string(),
                    message: text,
                });
            }
        }

        // Parse the response
        let response_text = response.text().await
            .map_err(|e| WebullError::NetworkError(e))?;

        #[derive(Debug, Deserialize)]
        struct MfaResponse {
            access_token: String,
            refresh_token: String,
            token_type: String,
            expires_in: i64,
        }

        let mfa_response: MfaResponse = from_json(&response_text)?;

        // Create the token
        let token = AccessToken {
            token: mfa_response.access_token,
            expires_at: Utc::now() + chrono::Duration::seconds(mfa_response.expires_in),
            refresh_token: Some(mfa_response.refresh_token),
        };

        // Store the token
        self.token_store.store_token(token.clone())?;

        Ok(token)
    }

    /// Refresh the access token.
    pub async fn refresh_token(&mut self) -> WebullResult<AccessToken> {
        // Get the current token
        let current_token = self.token_store.get_token()?
            .ok_or_else(|| WebullError::InvalidRequest("No token available for refresh".to_string()))?;

        // Check if we have a refresh token
        let refresh_token = current_token.refresh_token
            .ok_or_else(|| WebullError::InvalidRequest("No refresh token available".to_string()))?;

        // Create the request body
        let body = json!({
            "refreshToken": refresh_token,
            "deviceId": self.config.device_id.clone().unwrap_or_default(),
        });

        // Create headers
        let mut headers = HeaderMap::new();
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

        // Add API key if available
        if let Some(api_key) = &self.config.api_key {
            headers.insert("api-key", HeaderValue::from_str(api_key).unwrap());
        }

        // Generate timestamp and signature
        let timestamp = generate_timestamp();
        let signature = if let Some(api_secret) = &self.config.api_secret {
            let message = format!("{}{}", timestamp, to_json(&body)?);
            generate_signature(api_secret, &message)?
        } else {
            String::new()
        };

        // Add timestamp and signature to headers
        headers.insert("timestamp", HeaderValue::from_str(&timestamp).unwrap());
        headers.insert("signature", HeaderValue::from_str(&signature).unwrap());

        // Send the request
        let response = self.client.post(format!("{}/api/passport/refreshToken", self.config.base_url))
            .headers(headers)
            .json(&body)
            .send()
            .await
            .map_err(|e| WebullError::NetworkError(e))?;

        // Check for errors
        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await.unwrap_or_else(|_| "Unknown error".to_string());

            if status.as_u16() == 401 {
                return Err(WebullError::Unauthorized);
            } else if status.as_u16() == 429 {
                return Err(WebullError::RateLimitExceeded);
            } else {
                return Err(WebullError::ApiError {
                    code: status.as_u16().to_string(),
                    message: text,
                });
            }
        }

        // Parse the response
        let response_text = response.text().await
            .map_err(|e| WebullError::NetworkError(e))?;

        #[derive(Debug, Deserialize)]
        struct RefreshResponse {
            access_token: String,
            refresh_token: String,
            token_type: String,
            expires_in: i64,
        }

        let refresh_response: RefreshResponse = from_json(&response_text)?;

        // Create the token
        let token = AccessToken {
            token: refresh_response.access_token,
            expires_at: Utc::now() + chrono::Duration::seconds(refresh_response.expires_in),
            refresh_token: Some(refresh_response.refresh_token),
        };

        // Store the token
        self.token_store.store_token(token.clone())?;

        Ok(token)
    }

    /// Get the current access token.
    pub async fn get_token(&self) -> WebullResult<AccessToken> {
        match self.token_store.get_token()? {
            Some(token) => {
                // Check if token is expired
                if token.expires_at <= Utc::now() {
                    return Err(WebullError::Unauthorized);
                }
                Ok(token)
            }
            None => Err(WebullError::Unauthorized),
        }
    }

    /// Revoke the current token.
    pub async fn revoke_token(&mut self) -> WebullResult<()> {
        // Get the current token
        let current_token = match self.token_store.get_token()? {
            Some(token) => token,
            None => {
                // No token to revoke
                self.credentials = None;
                return Ok(());
            }
        };

        // Create the request body
        let body = json!({
            "accessToken": current_token.token,
            "deviceId": self.config.device_id.clone().unwrap_or_default(),
        });

        // Create headers
        let mut headers = HeaderMap::new();
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
        headers.insert(AUTHORIZATION, HeaderValue::from_str(&format!("Bearer {}", current_token.token)).unwrap());

        // Add API key if available
        if let Some(api_key) = &self.config.api_key {
            headers.insert("api-key", HeaderValue::from_str(api_key).unwrap());
        }

        // Generate timestamp and signature
        let timestamp = generate_timestamp();
        let signature = if let Some(api_secret) = &self.config.api_secret {
            let message = format!("{}{}", timestamp, to_json(&body)?);
            generate_signature(api_secret, &message)?
        } else {
            String::new()
        };

        // Add timestamp and signature to headers
        headers.insert("timestamp", HeaderValue::from_str(&timestamp).unwrap());
        headers.insert("signature", HeaderValue::from_str(&signature).unwrap());

        // Send the request
        let response = self.client.post(format!("{}/api/passport/logout", self.config.base_url))
            .headers(headers)
            .json(&body)
            .send()
            .await
            .map_err(|e| WebullError::NetworkError(e))?;

        // Check for errors
        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await.unwrap_or_else(|_| "Unknown error".to_string());

            if status.as_u16() == 401 {
                // Token is already invalid, so we can just clear it
            } else if status.as_u16() == 429 {
                return Err(WebullError::RateLimitExceeded);
            } else {
                return Err(WebullError::ApiError {
                    code: status.as_u16().to_string(),
                    message: text,
                });
            }
        }

        // Clear the token and credentials
        self.token_store.clear_token()?;
        self.credentials = None;

        Ok(())
    }
}