nab138_icloud_auth 0.1.10

A library to authenticate with Apple's GSA servers
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
use crate::{anisette::AnisetteData, Error};
use aes::{
    cipher::{block_padding::Pkcs7, consts::U16},
    Aes256,
};
use aes_gcm::{aead::KeyInit, AeadInPlace, AesGcm, Nonce};
use base64::{engine::general_purpose, Engine};
use cbc::cipher::{BlockDecryptMut, KeyIvInit};
use hmac::Mac;
use omnisette::{obf, AnisetteConfiguration};
use reqwest::{
    header::{HeaderMap, HeaderName, HeaderValue},
    Certificate, Client, ClientBuilder, Response,
};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use srp::{
    client::{SrpClient, SrpClientVerifier},
    groups::G_2048,
};
use std::str::FromStr;
use tokio::sync::Mutex;

const APPLE_ROOT: &[u8] = include_bytes!("./apple_root.der");

#[derive(Debug, Serialize, Deserialize)]
pub struct InitRequestBody {
    #[serde(rename = "A2k")]
    a_pub: plist::Value,
    cpd: plist::Dictionary,
    #[serde(rename = "o")]
    operation: String,
    ps: Vec<String>,
    #[serde(rename = "u")]
    username: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RequestHeader {
    #[serde(rename = "Version")]
    version: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InitRequest {
    #[serde(rename = "Header")]
    header: RequestHeader,
    #[serde(rename = "Request")]
    request: InitRequestBody,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ChallengeRequestBody {
    #[serde(rename = "M1")]
    m: plist::Value,
    cpd: plist::Dictionary,
    c: String,
    #[serde(rename = "o")]
    operation: String,
    #[serde(rename = "u")]
    username: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ChallengeRequest {
    #[serde(rename = "Header")]
    header: RequestHeader,
    #[serde(rename = "Request")]
    request: ChallengeRequestBody,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AuthTokenRequestBody {
    app: Vec<String>,
    c: plist::Value,
    cpd: plist::Dictionary,
    #[serde(rename = "o")]
    operation: String,
    t: String,
    u: String,
    checksum: plist::Value,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AuthTokenRequest {
    #[serde(rename = "Header")]
    header: RequestHeader,
    #[serde(rename = "Request")]
    request: AuthTokenRequestBody,
}

pub struct AppleAccount {
    //TODO: move this to omnisette
    pub anisette: Mutex<AnisetteData>,
    // pub spd:  Option<plist::Dictionary>,
    //mutable spd
    pub spd: Option<plist::Dictionary>,
    pub apple_id: String,
    pub client: Client,
}

#[derive(Clone, Debug)]
pub struct AppToken {
    pub app_tokens: plist::Dictionary,
    pub auth_token: String,
    pub app: String,
}
//Just make it return a custom enum, with LoggedIn(account: AppleAccount) or Needs2FA(FinishLoginDel: fn(i32) -> TFAResponse)
#[repr(C)]
#[derive(Debug)]
pub enum LoginState {
    LoggedIn,
    // NeedsSMS2FASent(Send2FAToDevices),
    NeedsDevice2FA,
    Needs2FAVerification,
    NeedsSMS2FA,
    NeedsSMS2FAVerification(VerifyBody),
    NeedsExtraStep(String),
    NeedsLogin,
}

#[derive(Serialize, Debug, Clone)]
struct VerifyCode {
    code: String,
}

#[derive(Serialize, Debug, Clone)]
struct PhoneNumber {
    id: u32,
}

#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct VerifyBody {
    phone_number: PhoneNumber,
    mode: String,
    security_code: Option<VerifyCode>,
}

#[repr(C)]
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TrustedPhoneNumber {
    pub number_with_dial_code: String,
    pub last_two_digits: String,
    pub push_mode: String,
    pub id: u32,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthenticationExtras {
    pub trusted_phone_numbers: Vec<TrustedPhoneNumber>,
    pub recovery_url: Option<String>,
    pub cant_use_phone_number_url: Option<String>,
    pub dont_have_access_url: Option<String>,
    pub recovery_web_url: Option<String>,
    pub repair_phone_number_url: Option<String>,
    pub repair_phone_number_web_url: Option<String>,
    #[serde(skip)]
    pub new_state: Option<LoginState>,
}

async fn parse_response(
    res: Result<Response, reqwest::Error>,
) -> Result<plist::Dictionary, crate::Error> {
    let res = res?.text().await?;
    let res: plist::Dictionary = plist::from_bytes(res.as_bytes())?;
    let res: plist::Value = res.get(obf!("Response")).unwrap().to_owned();
    match res {
        plist::Value::Dictionary(dict) => Ok(dict),
        _ => Err(crate::Error::Parse),
    }
}

impl AppleAccount {
    pub async fn new(
        config: AnisetteConfiguration,
        apple_id: String,
    ) -> Result<Self, crate::Error> {
        let anisette = AnisetteData::new(config).await?;
        Self::new_with_anisette(anisette, apple_id)
    }

    pub fn new_with_anisette(
        anisette: AnisetteData,
        apple_id: String,
    ) -> Result<Self, crate::Error> {
        let client = ClientBuilder::new()
            .add_root_certificate(Certificate::from_der(APPLE_ROOT)?)
            // uncomment when debugging w/ charles proxy
            // .danger_accept_invalid_certs(true)
            .http1_title_case_headers()
            .connection_verbose(true)
            .build()?;

        Ok(AppleAccount {
            client,
            anisette: Mutex::new(anisette),
            apple_id,
            spd: None,
        })
    }

    pub async fn login(
        appleid_closure: impl Fn() -> Result<(String, String), String>,
        tfa_closure: impl Fn() -> Result<String, String>,
        config: AnisetteConfiguration,
    ) -> Result<AppleAccount, Error> {
        let anisette = AnisetteData::new(config).await?;
        AppleAccount::login_with_anisette(appleid_closure, tfa_closure, anisette).await
    }

    pub async fn get_anisette(&self) -> AnisetteData {
        let mut locked = self.anisette.lock().await;
        if locked.needs_refresh() {
            *locked = locked.refresh().await.unwrap();
        }
        locked.clone()
    }

    pub async fn get_app_token(&self, app_name: &str) -> Result<AppToken, Error> {
        let spd = self.spd.as_ref().unwrap();
        let dsid = spd.get(obf!("adsid")).unwrap().as_string().unwrap();
        let auth_token = spd.get(obf!("GsIdmsToken")).unwrap().as_string().unwrap();

        let valid_anisette = self.get_anisette().await;

        let sk = spd.get(obf!("sk")).unwrap().as_data().unwrap();
        let c = spd.get(obf!("c")).unwrap().as_data().unwrap();

        let checksum = Self::create_checksum(&sk.to_vec(), dsid, app_name);

        let mut gsa_headers = HeaderMap::new();
        gsa_headers.insert(
            "Content-Type",
            HeaderValue::from_str("text/x-xml-plist").unwrap(),
        );
        gsa_headers.insert("Accept", HeaderValue::from_str("*/*").unwrap());
        gsa_headers.insert(
            "User-Agent",
            HeaderValue::from_str("akd/1.0 CFNetwork/978.0.7 Darwin/18.7.0").unwrap(),
        );
        gsa_headers.insert(
            HeaderName::from_str(&obf!("X-MMe-Client-Info")).unwrap(),
            HeaderValue::from_str(&valid_anisette.get_header(obf!("x-mme-client-info"))?).unwrap(),
        );

        let header = RequestHeader {
            version: "1.0.1".to_string(),
        };
        let body = AuthTokenRequestBody {
            cpd: valid_anisette.to_plist(true, false, false),
            app: vec![app_name.to_string()],
            c: plist::Value::Data(c.to_vec()),
            operation: obf!("apptokens").to_string(),
            t: auth_token.to_string(),
            u: dsid.to_string(),
            checksum: plist::Value::Data(checksum),
        };

        let packet = AuthTokenRequest {
            header: header.clone(),
            request: body,
        };

        let mut buffer = Vec::new();
        plist::to_writer_xml(&mut buffer, &packet)?;
        let buffer = String::from_utf8(buffer).unwrap();

        let res = self
            .client
            .post(obf!("https://gsa.apple.com/grandslam/GsService2"))
            .headers(gsa_headers.clone())
            .body(buffer)
            .send()
            .await;
        let res = parse_response(res).await?;
        let err_check = Self::check_error(&res);
        if err_check.is_err() {
            return Err(err_check.err().unwrap());
        }

        let encrypted_token = res
            .get(obf!("et"))
            .ok_or(Error::Parse)?
            .as_data()
            .ok_or(Error::Parse)?;

        if encrypted_token.len() < 3 + 16 + 16 {
            return Err(Error::Parse);
        }
        let header = &encrypted_token[0..3];
        if header != b"XYZ" {
            return Err(Error::AuthSrpWithMessage(
                0,
                "Encrypted token is in an unknown format.".to_string(),
            ));
        }
        let iv = &encrypted_token[3..19];
        let ciphertext_and_tag = &encrypted_token[19..];

        if sk.len() != 32 {
            return Err(Error::Parse);
        }
        if iv.len() != 16 {
            return Err(Error::Parse);
        }

        let key = aes_gcm::Key::<AesGcm<Aes256, U16>>::from_slice(sk);
        let cipher = AesGcm::<Aes256, U16>::new(key);
        let nonce = Nonce::<U16>::from_slice(iv);

        let mut buf = ciphertext_and_tag.to_vec();

        cipher
            .decrypt_in_place(nonce, header, &mut buf)
            .map_err(|_| {
                Error::AuthSrpWithMessage(
                    0,
                    "Failed to decrypt app token (AES-256/GCM aes-gcm).".to_string(),
                )
            })?;

        let decrypted_token: plist::Dictionary =
            plist::from_bytes(&buf).map_err(|_| Error::Parse)?;

        let t_val = decrypted_token.get("t").ok_or(Error::Parse)?;
        let app_tokens = t_val.as_dictionary().ok_or(Error::Parse)?;
        let app_token_dict = app_tokens.get(app_name).ok_or(Error::Parse)?;
        let app_token = app_token_dict.as_dictionary().ok_or(Error::Parse)?;
        let token = app_token
            .get(obf!("token"))
            .and_then(|v| v.as_string())
            .ok_or(Error::Parse)?;

        Ok(AppToken {
            app_tokens: app_tokens.clone(),
            auth_token: token.to_string(),
            app: app_name.to_string(),
        })
    }

    fn create_checksum(session_key: &Vec<u8>, dsid: &str, app_name: &str) -> Vec<u8> {
        <hmac::Hmac<Sha256> as hmac::Mac>::new_from_slice(session_key.as_slice())
            .unwrap()
            .chain_update(obf!("apptokens").as_bytes())
            .chain_update(dsid.as_bytes())
            .chain_update(app_name.as_bytes())
            .finalize()
            .into_bytes()
            .to_vec()
    }

    /// # Arguments
    ///
    /// * `appleid_closure` - A closure that takes no arguments and returns a tuple of the Apple ID and password
    /// * `tfa_closure` - A closure that takes no arguments and returns the 2FA code
    /// * `anisette` - AnisetteData
    /// # Examples
    ///
    /// ```
    /// use icloud_auth::AppleAccount;
    /// use omnisette::AnisetteData;
    ///
    /// let anisette = AnisetteData::new();
    /// let account = AppleAccount::login(
    ///   || Ok(("test@waffle.me", "password"))
    ///   || Ok("123123"),
    ///  anisette
    /// );
    /// ```
    /// Note: You would not provide the 2FA code like this, you would have to actually ask input for it.
    //TODO: add login_with_anisette and login, where login autodetcts anisette
    pub async fn login_with_anisette<
        F: Fn() -> Result<(String, String), String>,
        G: Fn() -> Result<String, String>,
    >(
        appleid_closure: F,
        tfa_closure: G,
        anisette: AnisetteData,
    ) -> Result<AppleAccount, Error> {
        let (username, password) = appleid_closure().map_err(|e| {
            Error::AuthSrpWithMessage(0, format!("Failed to get Apple ID credentials: {}", e))
        })?;
        let mut _self = AppleAccount::new_with_anisette(anisette, username.clone())?;

        let mut response = _self.login_email_pass(&username, &password).await?;
        loop {
            match response {
                LoginState::NeedsDevice2FA => response = _self.send_2fa_to_devices().await?,
                LoginState::Needs2FAVerification => {
                    response = _self
                        .verify_2fa(tfa_closure().map_err(|e| {
                            Error::AuthSrpWithMessage(0, format!("Failed to get 2FA code: {}", e))
                        })?)
                        .await?
                }
                LoginState::NeedsSMS2FA => response = _self.send_sms_2fa_to_devices(1).await?,
                LoginState::NeedsSMS2FAVerification(body) => {
                    response = _self
                        .verify_sms_2fa(
                            tfa_closure().map_err(|e| {
                                Error::AuthSrpWithMessage(
                                    0,
                                    format!("Failed to get SMS 2FA code: {}", e),
                                )
                            })?,
                            body,
                        )
                        .await?
                }
                LoginState::NeedsLogin => {
                    response = _self.login_email_pass(&username, &password).await?
                }
                LoginState::LoggedIn => return Ok(_self),
                LoginState::NeedsExtraStep(step) => {
                    if _self.get_pet().is_some() {
                        return Ok(_self);
                    } else {
                        return Err(Error::ExtraStep(step));
                    }
                }
            }
        }
    }

    pub fn get_pet(&self) -> Option<String> {
        Some(
            self.spd
                .as_ref()
                .unwrap()
                .get("t")?
                .as_dictionary()
                .unwrap()
                .get(obf!("com.apple.gs.idms.pet"))
                .unwrap()
                .as_dictionary()
                .unwrap()
                .get(obf!("token"))
                .unwrap()
                .as_string()
                .unwrap()
                .to_string(),
        )
    }

    pub fn get_name(&self) -> (String, String) {
        (
            self.spd
                .as_ref()
                .unwrap()
                .get(obf!("fn"))
                .unwrap()
                .as_string()
                .unwrap()
                .to_string(),
            self.spd
                .as_ref()
                .unwrap()
                .get(obf!("ln"))
                .unwrap()
                .as_string()
                .unwrap()
                .to_string(),
        )
    }

    pub async fn login_email_pass(
        &mut self,
        username: &str,
        password: &str,
    ) -> Result<LoginState, Error> {
        let srp_client = SrpClient::<Sha256>::new(&G_2048);
        let a: Vec<u8> = (0..32).map(|_| rand::random::<u8>()).collect();
        let a_pub = srp_client.compute_public_ephemeral(&a);

        let valid_anisette = self.get_anisette().await;

        let mut gsa_headers = HeaderMap::new();
        gsa_headers.insert(
            "Content-Type",
            HeaderValue::from_str("text/x-xml-plist").unwrap(),
        );
        gsa_headers.insert("Accept", HeaderValue::from_str("*/*").unwrap());
        gsa_headers.insert(
            "User-Agent",
            HeaderValue::from_str(obf!("akd/1.0 CFNetwork/978.0.7 Darwin/18.7.0")).unwrap(),
        );
        gsa_headers.insert(
            HeaderName::from_str(&obf!("X-MMe-Client-Info")).unwrap(),
            HeaderValue::from_str(&valid_anisette.get_header(obf!("x-mme-client-info"))?).unwrap(),
        );

        let header = RequestHeader {
            version: "1.0.1".to_string(),
        };
        let body = InitRequestBody {
            a_pub: plist::Value::Data(a_pub),
            cpd: valid_anisette.to_plist(true, false, false),
            operation: "init".to_string(),
            ps: vec![obf!("s2k").to_string(), obf!("s2k_fo").to_string()],
            username: username.to_string(),
        };

        let packet = InitRequest {
            header: header.clone(),
            request: body,
        };

        let mut buffer = Vec::new();
        plist::to_writer_xml(&mut buffer, &packet)?;
        let buffer = String::from_utf8(buffer).unwrap();

        // println!("{:?}", gsa_headers.clone());
        // println!("{:?}", buffer);

        let res = self
            .client
            .post(obf!("https://gsa.apple.com/grandslam/GsService2"))
            .headers(gsa_headers.clone())
            .body(buffer)
            .send()
            .await;

        let res = parse_response(res).await?;
        let err_check = Self::check_error(&res);
        if err_check.is_err() {
            return Err(err_check.err().unwrap());
        }
        // println!("{:?}", res);
        let salt = res.get(obf!("s")).unwrap().as_data().unwrap();
        let b_pub = res.get(obf!("B")).unwrap().as_data().unwrap();
        let iters = res.get(obf!("i")).unwrap().as_signed_integer().unwrap();
        let c = res.get(obf!("c")).unwrap().as_string().unwrap();

        let hashed_password = Sha256::digest(password.as_bytes());

        let mut password_buf = [0u8; 32];
        pbkdf2::pbkdf2::<hmac::Hmac<Sha256>>(
            &hashed_password,
            salt,
            iters as u32,
            &mut password_buf,
        );

        let verifier: SrpClientVerifier<Sha256> = srp_client
            .process_reply(&a, username.as_bytes(), &password_buf, salt, b_pub)
            .unwrap();

        let m = verifier.proof();

        let body = ChallengeRequestBody {
            m: plist::Value::Data(m.to_vec()),
            c: c.to_string(),
            cpd: valid_anisette.to_plist(true, false, false),
            operation: "complete".to_string(),
            username: username.to_string(),
        };

        let packet = ChallengeRequest {
            header,
            request: body,
        };

        let mut buffer = Vec::new();
        plist::to_writer_xml(&mut buffer, &packet)?;
        let buffer = String::from_utf8(buffer).unwrap();

        let res = self
            .client
            .post(obf!("https://gsa.apple.com/grandslam/GsService2"))
            .headers(gsa_headers.clone())
            .body(buffer)
            .send()
            .await;

        let res = parse_response(res).await?;
        let err_check = Self::check_error(&res);
        if err_check.is_err() {
            return Err(err_check.err().unwrap());
        }
        // println!("{:?}", res);
        let m2 = res.get(obf!("M2")).unwrap().as_data().unwrap();
        verifier.verify_server(m2).unwrap();

        let spd = res.get(obf!("spd")).unwrap().as_data().unwrap();
        let decrypted_spd = Self::decrypt_cbc(&verifier, spd);
        let decoded_spd: plist::Dictionary = plist::from_bytes(&decrypted_spd).unwrap();

        let status = res.get(obf!("Status")).unwrap().as_dictionary().unwrap();
        self.spd = Some(decoded_spd);

        if let Some(plist::Value::String(s)) = status.get(obf!("au")) {
            return match s.as_str() {
                "trustedDeviceSecondaryAuth" => Ok(LoginState::NeedsDevice2FA),
                "secondaryAuth" => Ok(LoginState::NeedsSMS2FA),
                _unk => Ok(LoginState::NeedsExtraStep(_unk.to_string())),
            };
        }

        Ok(LoginState::LoggedIn)
    }

    fn create_session_key(usr: &SrpClientVerifier<Sha256>, name: &str) -> Vec<u8> {
        <hmac::Hmac<Sha256> as hmac::Mac>::new_from_slice(usr.key())
            .unwrap()
            .chain_update(name.as_bytes())
            .finalize()
            .into_bytes()
            .to_vec()
    }

    fn decrypt_cbc(usr: &SrpClientVerifier<Sha256>, data: &[u8]) -> Vec<u8> {
        let extra_data_key = Self::create_session_key(usr, "extra data key:");
        let extra_data_iv = Self::create_session_key(usr, "extra data iv:");
        let extra_data_iv = &extra_data_iv[..16];

        cbc::Decryptor::<aes::Aes256>::new_from_slices(&extra_data_key, extra_data_iv)
            .unwrap()
            .decrypt_padded_vec_mut::<Pkcs7>(data)
            .unwrap()
    }

    pub async fn send_2fa_to_devices(&self) -> Result<LoginState, crate::Error> {
        let headers = self.build_2fa_headers(false);

        let res = self
            .client
            .get(obf!("https://gsa.apple.com/auth/verify/trusteddevice"))
            .headers(headers.await)
            .send()
            .await?;

        if !res.status().is_success() {
            return Err(Error::AuthSrp);
        }

        Ok(LoginState::Needs2FAVerification)
    }

    pub async fn send_sms_2fa_to_devices(&self, phone_id: u32) -> Result<LoginState, crate::Error> {
        let headers = self.build_2fa_headers(true);

        let body = VerifyBody {
            phone_number: PhoneNumber { id: phone_id },
            mode: "sms".to_string(),
            security_code: None,
        };

        let res = self
            .client
            .get(obf!("https://gsa.apple.com/auth"))
            .headers(headers.await)
            .send()
            .await?;

        if !res.status().is_success() {
            return Err(Error::AuthSrp);
        }

        Ok(LoginState::NeedsSMS2FAVerification(body))
    }

    pub async fn get_auth_extras(&self) -> Result<AuthenticationExtras, Error> {
        let headers = self.build_2fa_headers(true);

        let req = self
            .client
            .get(obf!("https://gsa.apple.com/auth"))
            .headers(headers.await)
            .header("Accept", "application/json")
            .send()
            .await?;
        let status = req.status().as_u16();
        let mut new_state = req.json::<AuthenticationExtras>().await?;
        if status == 201 {
            new_state.new_state = Some(LoginState::NeedsSMS2FAVerification(VerifyBody {
                phone_number: PhoneNumber {
                    id: new_state.trusted_phone_numbers.first().unwrap().id,
                },
                mode: "sms".to_string(),
                security_code: None,
            }));
        }

        Ok(new_state)
    }

    pub async fn verify_2fa(&self, code: String) -> Result<LoginState, Error> {
        // println!("Verifying 2fa Code {}", code.clone());
        let headers = self.build_2fa_headers(false);
        // println!("Recieved code: {}", code);
        let res = self
            .client
            .get(obf!("https://gsa.apple.com/grandslam/GsService2/validate"))
            .headers(headers.await)
            .header(
                HeaderName::from_str(obf!("security-code")).unwrap(),
                HeaderValue::from_str(&code).unwrap(),
            )
            .send()
            .await?;

        let res: plist::Dictionary = plist::from_bytes(res.text().await?.as_bytes())?;

        Self::check_error(&res)?;

        Ok(LoginState::NeedsLogin)
    }

    pub async fn verify_sms_2fa(
        &self,
        code: String,
        mut body: VerifyBody,
    ) -> Result<LoginState, Error> {
        let headers = self.build_2fa_headers(true).await;
        // println!("Recieved code: {}", code);

        body.security_code = Some(VerifyCode { code });

        let res = self
            .client
            .post(obf!("https://gsa.apple.com/auth/verify/phone/securitycode"))
            .headers(headers)
            .header("accept", "application/json")
            .json(&body)
            .send()
            .await?;

        if res.status() != 200 {
            return Err(Error::Bad2faCode);
        }

        Ok(LoginState::NeedsLogin)
    }

    fn check_error(res: &plist::Dictionary) -> Result<(), Error> {
        let res = match res.get("Status") {
            Some(plist::Value::Dictionary(d)) => d,
            _ => res,
        };

        if res.get(obf!("ec")).unwrap().as_signed_integer().unwrap() != 0 {
            return Err(Error::AuthSrpWithMessage(
                res.get(obf!("ec")).unwrap().as_signed_integer().unwrap(),
                res.get(obf!("em")).unwrap().as_string().unwrap().to_owned(),
            ));
        }

        Ok(())
    }

    pub async fn build_2fa_headers(&self, sms: bool) -> HeaderMap {
        let spd = self.spd.as_ref().unwrap();
        let dsid = spd.get(obf!("adsid")).unwrap().as_string().unwrap();
        let token = spd.get(obf!("GsIdmsToken")).unwrap().as_string().unwrap();

        let identity_token = general_purpose::STANDARD.encode(format!("{}:{}", dsid, token));

        let valid_anisette = self.get_anisette().await;

        let mut headers = HeaderMap::new();
        valid_anisette
            .generate_headers(false, true, true)
            .iter()
            .for_each(|(k, v)| {
                headers.append(
                    HeaderName::from_bytes(k.as_bytes()).unwrap(),
                    HeaderValue::from_str(v).unwrap(),
                );
            });

        if !sms {
            headers.insert(
                "Content-Type",
                HeaderValue::from_str("text/x-xml-plist").unwrap(),
            );
            headers.insert("Accept", HeaderValue::from_str("text/x-xml-plist").unwrap());
        }
        headers.insert("User-Agent", HeaderValue::from_str(obf!("Xcode")).unwrap());
        headers.insert("Accept-Language", HeaderValue::from_str("en-us").unwrap());
        headers.append(
            HeaderName::from_str(&obf!("X-Apple-Identity-Token")).unwrap(),
            HeaderValue::from_str(&identity_token).unwrap(),
        );

        headers.insert(
            "Loc",
            HeaderValue::from_str(&valid_anisette.get_header(obf!("x-apple-locale")).unwrap())
                .unwrap(),
        );

        headers
    }

    pub async fn send_request(
        &self,
        url: &str,
        body: Option<plist::Dictionary>,
    ) -> Result<plist::Dictionary, Error> {
        let spd = self.spd.as_ref().unwrap();
        let app_token = self.get_app_token(obf!("com.apple.gs.xcode.auth")).await?;
        let valid_anisette = self.get_anisette().await;

        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", HeaderValue::from_static("text/x-xml-plist"));
        headers.insert("Accept", HeaderValue::from_static("text/x-xml-plist"));
        headers.insert("Accept-Language", HeaderValue::from_static("en-us"));
        headers.insert("User-Agent", HeaderValue::from_str(obf!("Xcode")).unwrap());
        headers.insert(
            HeaderName::from_str(&obf!("X-Apple-I-Identity-Id")).unwrap(),
            HeaderValue::from_str(spd.get(obf!("adsid")).unwrap().as_string().unwrap()).unwrap(),
        );
        headers.insert(
            HeaderName::from_str(&obf!("X-Apple-GS-Token")).unwrap(),
            HeaderValue::from_str(&app_token.auth_token).unwrap(),
        );

        for (k, v) in valid_anisette.generate_headers(false, true, true) {
            headers.insert(
                HeaderName::from_bytes(k.as_bytes()).unwrap(),
                HeaderValue::from_str(&v).unwrap(),
            );
        }

        if let Ok(locale) = valid_anisette.get_header(obf!("x-apple-locale")) {
            headers.insert(
                HeaderName::from_str(&obf!("X-Apple-Locale")).unwrap(),
                HeaderValue::from_str(&locale).unwrap(),
            );
        }

        let response = if let Some(body) = body {
            let mut buf = Vec::new();
            plist::to_writer_xml(&mut buf, &body)?;
            self.client
                .post(url)
                .headers(headers)
                .body(buf)
                .send()
                .await?
        } else {
            self.client.get(url).headers(headers).send().await?
        };

        let response = response.text().await?;

        let response: plist::Dictionary = plist::from_bytes(response.as_bytes())?;
        Ok(response)
    }
}