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
use base64::{Engine, prelude::BASE64_STANDARD};
use reqwest::{Response, header::HeaderValue};
use serde::Deserialize;
use crate::{
ApiError, Error,
api::auth,
challenge::{
CHALLENGE_ID_HEADER, CHALLENGE_METADATA_HEADER, CHALLENGE_TYPE_HEADER, Challenge,
ChallengeMetadata, ChallengeType, ChefChallengeMetadata,
},
client::Client,
};
const TOKEN_HEADER: &str = "x-csrf-token";
#[derive(Debug, Deserialize)]
pub struct ErrorJson {
code: u8,
message: String,
}
#[derive(Debug, Deserialize)]
pub struct ErrorsJson {
errors: Vec<ErrorJson>,
}
#[derive(Debug, Deserialize)]
pub struct DataErrorJson {
#[serde(rename = "isValid")]
is_valid: bool,
data: Option<String>, // maybe, i always got null,
#[serde(rename = "error")]
message: String,
}
impl Client {
fn set_token(&mut self, token: &str) {
self.requestor
.default_headers
.insert(TOKEN_HEADER, HeaderValue::from_str(token).unwrap());
}
// NOTE: this doesn't work on all apis, since some apis expect a custom token,
// you'll know which ones are affected based on the `TokenValidation` error
pub async fn ensure_token(&mut self) -> Result<(), Error> {
let result = self
.requestor
.client
.post(format!("{}//", auth::URL))
.headers(self.requestor.default_headers.clone())
.send()
.await;
let result = self.validate_response(result).await;
if let Err(Error::ApiError(ApiError::TokenValidation)) = result {
return Ok(());
}
if result.is_err() {
return Err(result.err().unwrap());
}
Ok(())
}
/// TODO: test if account is terminated
/// TODO: add reactivate account function
//pub async fn test_account_status() {}
pub(crate) async fn validate_response(
&mut self,
result: Result<Response, reqwest::Error>,
) -> Result<Response, Error> {
// remove all challenge headers after validation
self.remove_challenge();
match result {
Ok(response) => {
let code = response.status().as_u16();
let token = response.headers().get(TOKEN_HEADER);
if let Some(token) = token {
// EVERYTHING must be mutable to do this, perhaps there's another datatype we can use
self.set_token(&String::from_utf8_lossy(token.as_bytes()).to_string());
}
// TODO: some apis like the data api can return an error even with status_code 200
if code == 200 {
return Ok(response);
}
// TODO: move this block into the challenge required case
let challenge = {
let challenge_id = response.headers().get(CHALLENGE_ID_HEADER);
let challenge_type = response.headers().get(CHALLENGE_TYPE_HEADER);
let challenge_metadata_b64 = response.headers().get(CHALLENGE_METADATA_HEADER);
if let (Some(id), Some(kind), Some(metadata_b64)) =
(challenge_id, challenge_type, challenge_metadata_b64)
{
let kind = ChallengeType::from(kind.to_str().unwrap());
match kind {
ChallengeType::Chef => {
let _metadata: ChefChallengeMetadata = serde_json::from_slice(
BASE64_STANDARD
.decode(metadata_b64.to_str().unwrap())
.unwrap()
.as_slice(),
)
.unwrap();
todo!("Unsupported chef challenge");
}
_ => {
let metadata: ChallengeMetadata = serde_json::from_slice(
BASE64_STANDARD
.decode(metadata_b64.to_str().unwrap())
.unwrap()
.as_slice(),
)
.unwrap();
Some(Challenge {
id: id.to_str().unwrap().to_string(),
kind,
metadata,
})
}
}
} else {
None
}
};
let bytes = response.bytes().await.unwrap().to_owned();
let errors = if let Ok(errors) = serde_json::from_slice::<ErrorsJson>(&bytes) {
errors
} else if let Ok(error) = serde_json::from_slice::<ErrorJson>(&bytes) {
ErrorsJson {
errors: vec![error],
}
} else if let Ok(error) = serde_json::from_slice::<DataErrorJson>(&bytes) {
ErrorsJson {
errors: vec![ErrorJson {
code: 0,
message: error.message,
}],
}
} else {
ErrorsJson {
errors: vec![ErrorJson {
code: 0,
message: String::from_utf8_lossy(&bytes).to_string(),
}],
}
};
match code {
400 => {
let errors: Vec<ApiError> = errors
.errors
.iter()
.map(|x| match x.message.as_str() {
"Invalid challenge ID." => ApiError::InvalidChallengeId,
"User not found." => ApiError::UserNotFound,
"The user ID is invalid." => ApiError::InvalidUserId,
"The gender provided is invalid." => ApiError::InvalidGender,
"The two step verification challenge code is invalid." => {
ApiError::InvalidTwoStepVerificationCode
}
"Invalid display name." => ApiError::InvalidDisplayName,
"Request must contain a birthdate" => {
ApiError::RequestMissingArgument("Birthdate".to_string())
}
_ => ApiError::Unknown(code),
})
.collect();
if errors.len() == 1 {
Err(Error::ApiError(errors.first().unwrap().clone()))
} else {
Err(Error::ApiError(ApiError::Multiple(errors)))
}
}
401 => Err(Error::ApiError(ApiError::Unauthorized)),
403 => {
let errors: Vec<ApiError> = errors
.errors
.iter()
.map(|x| match x.message.as_str() {
"Token Validation Failed"
| "XSRF token invalid"
| "XSRF Token Validation Failed"
| "\"XSRF Token Validation Failed\"" => ApiError::TokenValidation,
"PIN is locked." => ApiError::PinIsLocked,
"Invalid birthdate change." => ApiError::InvalidBirthdate,
"Challenge is required to authorize the request" => {
ApiError::ChallengeRequired(challenge.clone().unwrap())
}
"Challenge failed to authorize request" => {
ApiError::ChallengeFailed
}
"You do not have permission to view the owners of this asset." => {
ApiError::PermissionError
}
"an internal error occurred" => ApiError::Internal,
// TODO: add missing challenge duplicate code
_ => ApiError::Unknown(code),
})
.collect();
if errors.len() == 1 {
Err(Error::ApiError(errors.first().unwrap().clone()))
} else {
Err(Error::ApiError(ApiError::Multiple(errors)))
}
}
429 => Err(Error::ApiError(ApiError::Ratelimited)),
500 => Err(Error::ApiError(ApiError::Internal)),
_ => Err(Error::ApiError(ApiError::Unknown(code))),
}
}
Err(error) => Err(Error::ReqwestError(error)),
}
}
}