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
use std::{path::PathBuf, sync::Arc};

use super::{TwAPI, BEARER_TOKEN, GUEST_ACTIVE_URL, LOGIN_URL, VERIFY_CREDENTIALS_URL};
use log::debug;
use env_logger;
use serde::Deserialize;
use serde_json::{self, json};
use eyre::{bail, Context, ContextCompat, Result};
use reqwest_cookie_store;

#[derive(Clone, Debug, thiserror::Error)]
#[error("Suspicious Login")]
pub struct SuspiciousLoginError(
    pub String,
    pub Flow, // error message, latest flow
);

#[derive(Deserialize, Debug, Clone)]
pub struct User {
    pub id: i64,
    pub id_str: String,
    pub name: String,
    pub screen_name: String,
}
#[derive(Deserialize, Debug, Clone)]
pub struct OpenAccount {
    pub user: Option<User>,
    pub next_link: Option<Link>,
    pub attribution_event: Option<String>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct Subtask {
    pub subtask_id: String,
    pub open_account: Option<OpenAccount>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct ApiError {
    pub code: i64,
    pub message: String,
}

#[derive(Deserialize, Debug, Clone)]
pub struct Flow {
    pub errors: Option<Vec<ApiError>>,
    pub flow_token: String,
    pub status: String,
    pub subtasks: Vec<Subtask>,
    pub js_instrumentation: Option<Insrumentation>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct Insrumentation {
    pub url: String,
    pub timeout_ms: i64,
    pub next_link: Link,
}

#[derive(Deserialize, Debug, Clone)]
pub struct Link {
    pub link_type: String,
    pub link_id: String,
}

#[derive(Deserialize, Debug, Clone)]
pub struct GuestToken {
    pub guest_token: String,
}

#[derive(Deserialize, Debug, Clone)]
pub struct VerifyCredentials {
    pub errors: Option<Vec<ApiError>>,
}

impl TwAPI {
    pub fn new(session_path: Option<PathBuf>) -> Result<TwAPI> {
        let _ = env_logger::try_init();
        let client_builder = reqwest::ClientBuilder::new();
        let cookie_store: Arc<reqwest_cookie_store::CookieStoreMutex>;
    
        if let Some(session_path) = session_path.as_ref().filter(|path| path.exists()) {
            let file = std::fs::File::open(session_path.to_str().unwrap())
                .map(std::io::BufReader::new)
                .unwrap();
            debug!("Load json session from {session_path:?}");
            let provider: reqwest_cookie_store::CookieStore = reqwest_cookie_store::CookieStore::load_json(file).unwrap();
            
            cookie_store = std::sync::Arc::new(reqwest_cookie_store::CookieStoreMutex::new(provider));
        } else {
            let provider: reqwest_cookie_store::CookieStore = reqwest_cookie_store::CookieStore::new(None);
            
            cookie_store = std::sync::Arc::new(reqwest_cookie_store::CookieStoreMutex::new(provider));
        }
    
        let client = client_builder
            .cookie_provider(std::sync::Arc::clone(&cookie_store))
            .build()
            .context("can't build cookie store")?;
    
        Ok(TwAPI {
            client,
            csrf_token: String::from(""),
            guest_token: String::from(""),
            cookie_store,
            session_path,
        })
    }
    
    async fn get_flow(&mut self, body: serde_json::Value) -> Result<Flow> {
        if self.guest_token.is_empty() {
            self.get_guest_token().await?
        }
        let res = self
            .client
            .post(LOGIN_URL)
            .header("Authorization", format!("Bearer {}", BEARER_TOKEN))
            .header("Content-Type", "application/json")
            .header("User-Agent", "TwitterAndroid/99")
            .header("X-Guest-Token", self.guest_token.replace("\"", ""))
            .header("X-Twitter-Auth-Type", "OAuth2Client")
            .header("X-Twitter-Active-User", "yes")
            .header("X-Twitter-Client-Language", "en")
            .json(&body)
            .send().await?;

        let cookies = res.cookies();
        for cookie in cookies {
            if cookie.name().eq("ct0") {
                self.csrf_token = cookie.value().to_string()
            }
        }
        let text = res.text().await?;
        debug!("text: {text}");
        let result: Flow = serde_json::from_str(text.as_str())?;
        return Ok(result);
    }

    pub async fn get_flow_token(
        &mut self,
        data: serde_json::Value,
    ) -> Result<Option<Flow>> {
        let res = self.get_flow(data).await;
        match res {
            Ok(info) => {
                if info.subtasks.len() > 0 {
                    let subtask_id = info.subtasks[0].subtask_id.as_str();
                    match subtask_id {
                        // "LoginEnterAlternateIdentifierSubtask"
                        "LoginAcid" | "LoginTwoFactorAuthChallenge" | "DenyLoginSubtask" => {
                            bail!("Auth error: {}", subtask_id);
                        }
                        _ => return Ok(Some(info)),
                    }
                }
                return Ok(Some(info));
            }
            Err(e) => {
                bail!("Request error: {}", e.to_string())
            },
        }
    }

    async fn get_guest_token(&mut self) -> Result<()> {
        let token = format!("Bearer {}", BEARER_TOKEN);
        let res = self
            .client
            .post(GUEST_ACTIVE_URL)
            .header("Authorization", token)
            .send().await?;
        let op = res.json::<serde_json::Value>().await?;
        let guest_token = op.get("guest_token").context("cant get guest_token")?;
        self.guest_token = guest_token.to_string();
        Ok(())
    }



    pub async fn before_password_steps(&mut self, username: String) -> Result<Flow> {
        let data = json!(
            {
                "flow_name": "login",
                "input_flow_data": {
                    "flow_context" : {
                        "debug_overrides": {},
                        "start_location": {
                            "location": "splash_screen"
                        }
                    }
                }
            }
        );
        let flow_token = self.get_flow_token(data).await?.context("cant get folow token")?.flow_token;

        // flow instrumentation step
        let data = json!(
            {
                "flow_token": flow_token,
                "subtask_inputs" : [{
                    "subtask_id": "LoginJsInstrumentationSubtask",
                    "js_instrumentation":{
                        "response": "{}",
                        "link": "next_link"
                    }
                }],
            }
        );
        let flow_token = self.get_flow_token(data).await?.context("cant get folow token")?.flow_token;

        // flow username step
        let data = json!(
            {
                "flow_token": flow_token,
                "subtask_inputs" : [{
                    "subtask_id": "LoginEnterUserIdentifierSSO",
                    "settings_list": {
                        "setting_responses" : [{
                            "key":           "user_identifier",
                            "response_data": {
                                "text_data" :{
                                    "result": username
                                }
                            }
                        }],
                        "link": "next_link"
                    }
                }]
            }
        );

        let flow = self.get_flow_token(data).await?.context("flow is none")?;
        // let token = flow.flow_token.to_owned();
        let subtask_id = flow.subtasks[0].subtask_id.clone();

        // asking for username because of suspicies log in
        if subtask_id == "LoginEnterAlternateIdentifierSubtask" {
            return Err(SuspiciousLoginError("".into(), flow).into());
        }
        Ok(flow)
    }

    pub async fn login(
        &mut self,
        username: &str,
        password: &str,
        confirmation: &str,
        latest_flow: Option<Flow>,
    ) -> Result<Option<Flow>> {
        // flow start

        let mut flow: Flow;
        if latest_flow.is_some() {
            debug!("taking latest flow");
            flow = latest_flow.context("latest flow is none")?;
            let subtask_id = flow.subtasks[0].subtask_id.clone();
            let data = json!({
                "flow_token": flow.flow_token,
                "subtask_inputs": [{"subtask_id": subtask_id, "enter_text": {"text": username,"link":"next_link"}}]
            });
            // self.handle_suspicies(token.clone(), subtask_id.clone());
            flow = self.get_flow_token(data).await.context("flow token is none")?.context("inner flow token is none")?;
        } else {
            flow = self.before_password_steps(username.into()).await?;
        }
        // flow password step
        let data = json!(
            {
                "flow_token": flow.flow_token,
                "subtask_inputs": [{
                    "subtask_id":     "LoginEnterPassword",
                    "enter_password": {
                        "password": password,
                        "link": "next_link"
                    },
                }]
            }
        );

        let flow_token = self.get_flow_token(data).await?.context("flow token is none in password step")?.flow_token;

        // flow duplication check
        let data = json!(
            {
                "flow_token": flow_token,
                "subtask_inputs": [{
                    "subtask_id":              "AccountDuplicationCheck",
                    "check_logged_in_account": {
                        "link": "AccountDuplicationCheck_false"
                    },
                }]
            }
        );
        let flow_token = self.get_flow_token(data).await;

        match flow_token {
            Err(e) => {
                let mut confirmation_subtask = "";
                for item in vec!["LoginAcid", "LoginTwoFactorAuthChallenge"] {
                    if e.to_string().contains(item) {
                        confirmation_subtask = item;
                        break;
                    }
                }
                if !confirmation_subtask.is_empty() {
                    if confirmation.is_empty() {
                        let msg = format!(
                            "confirmation data required for {}",
                            confirmation_subtask.to_owned()
                        );
                        bail!(msg)
                    }
                    let data = json!(
                        {
                            "flow_token": "",
                            "subtask_inputs": {
                                    "subtask_id": confirmation_subtask,
                                    "enter_text": {
                                        "text": confirmation,
                                        "link": "next_link",
                                    },
                            },
                        }
                    );
                    return self.get_flow_token(data).await;
                }
                Ok(None)
            }
            Ok(_) => return Ok(None),
        }
    }

    pub async fn is_logged_in(&mut self) -> Result<bool> {
        let req = self
            .client
            .get(VERIFY_CREDENTIALS_URL)
            .header("Authorization", format!("Bearer {}", BEARER_TOKEN))
            .header("X-CSRF-Token", self.csrf_token.to_owned())
            .build()
            .context("Cant build http request in logged in")?;
        let res = self.client.execute(req).await.context("failed execute request")?;
        let cookies = res.cookies();
        for cookie in cookies {
            if cookie.name().eq("ct0") {
                self.csrf_token = cookie.value().to_string()
            }
        }
        let text = res.text().await.context("res is not text")?;
        let res: VerifyCredentials = serde_json::from_str(&text).context("res is not diseralizable")?;
        Ok(res.errors.is_none())
    }

    pub fn save_session(&mut self) -> Result<()> {
        if let Some(path) = &self.session_path {
            let mut writer = std::fs::File::create(path)
            .map(std::io::BufWriter::new)
            .unwrap();
            let store = self.cookie_store.lock().unwrap();
            store.save_json(&mut writer).unwrap();
            Ok(())
        } else {
            bail!("Session path must set when creating API")
        }
    }
}