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
pub mod client;
pub mod status;
pub mod xvc;

use reqwest::Method;

use crate::{read_response, read_structured_response, ApiResult};

use self::{
    client::{AuthClient, Device},
    xvc::XvcHasher,
};

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, Serialize)]
pub struct AccountForm<'a> {
    pub email: &'a str,
    pub password: &'a str,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Login {
    #[serde(rename = "userId")]
    pub user_id: u64,

    #[serde(rename = "countryIso")]
    pub country_iso: String,
    #[serde(rename = "countryCode")]
    pub country_code: String,

    #[serde(rename = "accountId")]
    pub account_id: u64,

    // pub server_time: u64,

    // #[serde(rename = "resetUserData")]
    // pub reset_user_data: bool,
    // pub story_url: Option<String>,
    pub access_token: String,
    pub refresh_token: String,
    pub token_type: String,

    #[serde(rename = "autoLoginAccountId")]
    pub auto_login_account_id: String,
    #[serde(rename = "displayAccountId")]
    pub display_account_id: String,

    #[serde(rename = "mainDeviceAgentName")]
    pub main_device_agent_name: String,
    #[serde(rename = "mainDeviceAppVersion")]
    pub main_device_app_version: String,
}

impl Login {
    pub async fn request_with_account(
        client: AuthClient<'_, impl XvcHasher>,
        account: AccountForm<'_>,
        forced: bool,
    ) -> ApiResult<Self> {
        #[derive(Serialize)]
        struct Form<'a> {
            #[serde(flatten)]
            device: Device<'a>,

            #[serde(flatten)]
            account: AccountForm<'a>,
            forced: bool,
        }

        let form = Form {
            device: client.device,
            account,
            forced,
        };

        read_structured_response(
            client
                .request(Method::POST, "account/login.json", account.email)?
                .form(&form),
        )
        .await
    }

    pub async fn request_with_token(
        client: AuthClient<'_, impl XvcHasher>,
        email: &str,
        token: &str,
        forced: bool,
        locked: bool,
    ) -> ApiResult<Self> {
        #[derive(Serialize)]
        struct Form<'a> {
            #[serde(flatten)]
            device: Device<'a>,

            email: &'a str,
            password: &'a str,
            auto_login: bool,
            autowithlock: bool,
            forced: bool,
        }

        let form = Form {
            device: client.device,
            email,
            password: token,
            auto_login: true,
            autowithlock: locked,
            forced,
        };

        read_structured_response(
            client
                .request(Method::POST, "account/login.json", email)?
                .form(&form),
        )
        .await
    }
}

pub async fn request_passcode(
    client: AuthClient<'_, impl XvcHasher>,
    account: AccountForm<'_>,
) -> ApiResult<()> {
    #[derive(Serialize)]
    struct Form<'a> {
        #[serde(flatten)]
        device: Device<'a>,

        #[serde(flatten)]
        account: AccountForm<'a>,
    }

    let form = Form {
        device: client.device,
        account,
    };

    read_response(
        client
            .request(Method::POST, "account/request_passcode.json", account.email)?
            .form(&form),
    )
    .await?;

    Ok(())
}

pub async fn register_device(
    client: AuthClient<'_, impl XvcHasher>,
    account: AccountForm<'_>,
    passcode: &str,
    permanent: bool,
) -> ApiResult<()> {
    #[derive(Serialize)]
    struct Form<'a> {
        #[serde(flatten)]
        device: Device<'a>,

        #[serde(flatten)]
        account: AccountForm<'a>,

        passcode: &'a str,
        permanent: bool,
    }

    let form = Form {
        device: client.device,
        account,
        passcode,
        permanent,
    };

    read_response(
        client
            .request(Method::POST, "account/register_device.json", account.email)?
            .form(&form),
    )
    .await?;

    Ok(())
}