-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogin_client.rs
493 lines (459 loc) · 18.6 KB
/
login_client.rs
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
use super::{
WpApiDetails,
url_discovery::{
self, API_ROOT_LINK_HEADER, ApiRootUrl, ApplicationPasswordsNotSupportedReason,
AutoDiscoveryAttempt, AutoDiscoveryAttemptFailure, AutoDiscoveryAttemptResult,
AutoDiscoveryAttemptSuccess, AutoDiscoveryResult, FetchAndParseApiRootFailure,
FindApiRootFailure, ParseHomepageResult, XmlrpcDisabledReason, XmlrpcDiscoveryError,
extract_rsd_url, is_xmlrpc_response, parse_rsd_for_xmlrpc,
},
};
use crate::{
ParsedUrl, RequestExecutionError, WpError,
middleware::{PerformsRequests, WpApiMiddlewarePipeline},
request::{
RequestExecutor, RequestMethod, ResponseBodyType, WpNetworkHeaderMap, WpNetworkRequest,
WpNetworkRequestBody, WpNetworkResponse,
endpoint::{WP_JSON_PATH_SEGMENTS, WpEndpointUrl},
},
};
use itertools::Itertools;
use std::sync::Arc;
use uuid::Uuid;
#[derive(Debug, uniffi::Object)]
struct UniffiWpLoginClient {
inner: Arc<WpLoginClient>,
}
#[uniffi::export]
impl UniffiWpLoginClient {
#[uniffi::constructor]
fn new(
request_executor: Arc<dyn RequestExecutor>,
middleware_pipeline: Arc<WpApiMiddlewarePipeline>,
) -> Self {
Self {
inner: WpLoginClient::new(request_executor, middleware_pipeline).into(),
}
}
async fn api_discovery(
&self,
site_url: String,
) -> Result<AutoDiscoveryAttemptSuccess, AutoDiscoveryAttemptFailure> {
self.inner
.api_discovery(site_url)
.await
.combined_result()
.cloned()
.map_err(|e| e.clone())
}
}
#[derive(Debug)]
pub struct WpLoginClient {
request_executor: Arc<dyn RequestExecutor>,
middleware_pipeline: Arc<WpApiMiddlewarePipeline>,
}
impl WpLoginClient {
pub fn new(
request_executor: Arc<dyn RequestExecutor>,
middleware_pipeline: Arc<WpApiMiddlewarePipeline>,
) -> Self {
Self {
request_executor,
middleware_pipeline,
}
}
pub async fn api_discovery(&self, site_url: String) -> AutoDiscoveryResult {
let attempts = futures::future::join_all(
url_discovery::construct_attempts(site_url)
.into_iter()
.map(|attempt| async { self.attempt_api_discovery(attempt).await }),
)
.await;
AutoDiscoveryResult {
attempts: attempts.into_iter().map(|r| (r.attempt_type, r)).collect(),
}
}
async fn attempt_api_discovery(
&self,
attempt: AutoDiscoveryAttempt,
) -> AutoDiscoveryAttemptResult {
let parsed_site_url: Arc<ParsedUrl> = match ParsedUrl::parse(&attempt.attempt_site_url) {
Ok(u) => u,
Err(e) => {
return AutoDiscoveryAttemptResult::from_parse_site_url_error(attempt, e);
}
}
.into();
match self.find_api_root_url(Arc::clone(&parsed_site_url)).await {
Ok(api_root_url) => AutoDiscoveryAttemptResult {
attempt_type: attempt.attempt_type,
attempt_site_url: attempt.attempt_site_url,
api_discovery_result: self
.fetch_and_parse_api_root(Arc::clone(&parsed_site_url), &api_root_url)
.await
.map_err(|fetch_and_parse_api_root_failure| {
AutoDiscoveryAttemptFailure::from_fetch_and_parse_api_root_failure(
parsed_site_url,
api_root_url.0,
fetch_and_parse_api_root_failure,
)
}),
},
Err(find_api_root_failure) => {
let root_wp_json_url: Arc<ParsedUrl> =
match Self::root_wp_json_url((*parsed_site_url).clone()) {
Some(u) => u,
None => {
return AutoDiscoveryAttemptResult {
attempt_type: attempt.attempt_type,
attempt_site_url: attempt.attempt_site_url,
api_discovery_result: Err(
AutoDiscoveryAttemptFailure::from_find_api_root_failure(
parsed_site_url,
find_api_root_failure,
),
),
};
}
}
.into();
// If we can't find the api root, we try using the root `/wp-json` as a last resort
match self
.fetch_and_parse_api_root(
Arc::clone(&parsed_site_url),
&ApiRootUrl(Arc::clone(&root_wp_json_url)),
)
.await
{
Ok(api_discovery_success) => AutoDiscoveryAttemptResult {
attempt_type: attempt.attempt_type,
attempt_site_url: attempt.attempt_site_url,
api_discovery_result: Ok(api_discovery_success),
},
Err(fetch_and_parse_api_root_failure) => match fetch_and_parse_api_root_failure
{
FetchAndParseApiRootFailure::FetchApiRoot { .. }
| FetchAndParseApiRootFailure::ParseApiRoot { .. } => {
// If we fail to fetch or parse root `/wp-json`, we return the original
// find API root url failure
AutoDiscoveryAttemptResult {
attempt_type: attempt.attempt_type,
attempt_site_url: attempt.attempt_site_url,
api_discovery_result: Err(
AutoDiscoveryAttemptFailure::from_find_api_root_failure(
parsed_site_url,
find_api_root_failure,
),
),
}
}
_ => {
// If we successfully fetch the root `/wp-json`, but had another
// failure afterwards, we return that failure, because the API
// discovery has progressed further than the original find API root url
// failure
let err = Err(
AutoDiscoveryAttemptFailure::from_fetch_and_parse_api_root_failure(
parsed_site_url,
root_wp_json_url,
fetch_and_parse_api_root_failure,
),
);
AutoDiscoveryAttemptResult {
attempt_type: attempt.attempt_type,
attempt_site_url: attempt.attempt_site_url,
api_discovery_result: err,
}
}
},
}
}
}
}
async fn fetch_and_parse_api_root(
&self,
parsed_site_url: Arc<ParsedUrl>,
api_root_url: &ApiRootUrl,
) -> Result<AutoDiscoveryAttemptSuccess, FetchAndParseApiRootFailure> {
let fetch_api_details_response = match self.fetch_api_root(api_root_url).await {
Ok(r) => r,
Err(error) => return Err(FetchAndParseApiRootFailure::FetchApiRoot { error }),
};
let api_details = Self::parse_api_root(&fetch_api_details_response)?;
if !api_details.has_application_passwords_authentication_url() {
let reason = if api_details.has_application_password_blocking_plugin() {
let plugins = api_details.application_password_blocking_plugins();
if plugins.len() == 1 {
// If there's only one candidate, we can show more information in the error message
Some(ApplicationPasswordsNotSupportedReason::ApplicationPasswordBlockedByPlugin {
plugin: plugins.first().expect("Already verified there is one plugin").clone(),
})
} else {
// If there's more than one, for now we'll just give a generic error
Some(ApplicationPasswordsNotSupportedReason::ApplicationPasswordBlockedByMultiplePlugins)
}
} else if !api_details.uses_https() {
// Application Passwords are disabled for non-HTTPS sites by default
if api_details.site_url_is_local_development_environment() {
Some(ApplicationPasswordsNotSupportedReason::SiteIsLocalDevelopmentEnvironment)
} else {
Some(ApplicationPasswordsNotSupportedReason::ApplicationPasswordsDisabledForHttpSite)
}
} else {
None
};
Err(
FetchAndParseApiRootFailure::ApplicationPasswordsNotSupported {
api_details: api_details.into(),
reason,
},
)
} else {
Ok(AutoDiscoveryAttemptSuccess {
parsed_site_url,
api_root_url: Arc::clone(&api_root_url.0),
api_details: Arc::new(api_details),
})
}
}
async fn find_api_root_url(
&self,
parsed_site_url: Arc<ParsedUrl>,
) -> Result<ApiRootUrl, FindApiRootFailure> {
let response = self
.fetch_homepage(Arc::clone(&parsed_site_url))
.await
.map_err(|error| FindApiRootFailure::FetchHomepage { error })?;
// First check if we can find and parse the api root from the link header
if let Some(api_root_url) = self.parse_response_link_header_to_find_api_root(&response) {
return Ok(ApiRootUrl(api_root_url.into()));
}
// If we can't find the api root in the link header, we parse the HTML page to look for it
// in the link tags
let parse_html_result = ParseHomepageResult::parse_response(&response.body_as_string());
if let Some(api_root_url) = parse_html_result.api_root_url_from_link_tag {
return Ok(ApiRootUrl(api_root_url));
}
if parse_html_result.does_look_like_a_wp_site() {
Err(FindApiRootFailure::RestApiDisabled)
} else {
Err(FindApiRootFailure::ProbablyNotAWordPressSite)
}
}
fn parse_response_link_header_to_find_api_root(
&self,
response: &WpNetworkResponse,
) -> Option<ParsedUrl> {
response
.get_link_header(API_ROOT_LINK_HEADER)
.into_iter()
.nth(0)
.map(ParsedUrl::new)
}
async fn fetch_api_root(
&self,
api_root_url: &ApiRootUrl,
) -> Result<WpNetworkResponse, RequestExecutionError> {
self.perform(
WpNetworkRequest {
uuid: Uuid::new_v4().into(),
retry_count: 0,
method: RequestMethod::GET,
url: WpEndpointUrl(api_root_url.0.url()),
header_map: WpNetworkHeaderMap::default().into(),
body: None,
}
.into(),
)
.await
}
fn root_wp_json_url(parsed_site_url: ParsedUrl) -> Option<ParsedUrl> {
let mut root_wp_json_url = parsed_site_url.inner;
root_wp_json_url
.path_segments_mut()
.ok()?
.extend(WP_JSON_PATH_SEGMENTS);
Some(root_wp_json_url.into())
}
async fn fetch_homepage(
&self,
parsed_site_url: Arc<ParsedUrl>,
) -> Result<WpNetworkResponse, RequestExecutionError> {
self.perform(
WpNetworkRequest {
uuid: Uuid::new_v4().into(),
retry_count: 0,
method: RequestMethod::GET,
url: WpEndpointUrl(parsed_site_url.url()),
header_map: WpNetworkHeaderMap::default().into(),
body: None,
}
.into(),
)
.await
}
fn parse_api_root(
fetch_api_details_response: &WpNetworkResponse,
) -> Result<WpApiDetails, FetchAndParseApiRootFailure> {
WpApiDetails::try_from(fetch_api_details_response.body.as_slice()).map_err(|error| {
if let Some(wp_error) = WpError::try_parse(&fetch_api_details_response.body) {
FetchAndParseApiRootFailure::WpError {
error_code: wp_error.code,
error_message: wp_error.message,
status_code: fetch_api_details_response.status_code,
}
} else {
let response_body = fetch_api_details_response.body_as_string();
let response_body_type = ResponseBodyType::new(&response_body);
FetchAndParseApiRootFailure::ParseApiRoot {
parsing_error_message: error.to_string(),
response_body,
response_body_type,
}
}
})
}
pub async fn xmlrpc_discovery(
&self,
details: AutoDiscoveryAttemptSuccess,
) -> Result<ParsedUrl, XmlrpcDiscoveryError> {
let mut candidates: Vec<ParsedUrl> = vec![];
// Prioritize discovered XML-RPC URL if it's available from the site.
if let Ok(url) = self.xmlrpc_from_rsd(&details.parsed_site_url).await {
candidates.push(url);
}
// Fallback to the default XML-RPC URL.
candidates.push(
details
.parsed_site_url
.by_extending_and_splitting_by_forward_slash(["xmlrpc.php"])
.into(),
);
candidates.dedup();
let mut failures: Vec<XmlrpcDiscoveryError> = vec![];
for candidate in candidates {
match self
.validate_xmlrpc_url(&candidate, &details.api_details)
.await
{
Ok(_) => return Ok(candidate),
Err(error) => {
failures.push(error);
}
}
}
Err(failures
.into_iter()
.sorted_by(|a, b| b.importance().cmp(&a.importance()))
.next()
.expect("There is at least one failure"))
}
async fn validate_xmlrpc_url(
&self,
url: &ParsedUrl,
api_details: &WpApiDetails,
) -> Result<(), XmlrpcDiscoveryError> {
let response = self.perform(
WpNetworkRequest {
uuid: Uuid::new_v4().into(),
retry_count: 0,
method: RequestMethod::POST,
url: WpEndpointUrl(url.url()),
header_map: WpNetworkHeaderMap::default().into(),
body: Some(Arc::new(WpNetworkRequestBody::new(r#"<?xml version="1.0"?><methodCall><methodName>system.listMethods</methodName></methodCall>"#.as_bytes().to_vec()))),
}
.into(),
)
.await
// It's very likely xml-rpc is blocked by the hosting provider (the request has not reached to WordPress),
// if the site does not send any valid HTTP response.
.map_err(|_| XmlrpcDiscoveryError::Disabled { reason: XmlrpcDisabledReason::ByHost })?;
// 200 status code and a valid XML-RPC response indicates that XML-RPC is enabled.
// All other responses indicate that XML-RPC is disabled.
if response.status_code == 200 && is_xmlrpc_response(&response.body_as_string()) {
return Ok(());
}
let mut plugins = api_details.xmlrpc_blocking_plugins();
let reason = match plugins.len() {
0 => XmlrpcDisabledReason::ByHost,
1 => XmlrpcDisabledReason::ByPlugin {
plugin: plugins.pop().expect("Already verified there is one plugin"),
},
_ => XmlrpcDisabledReason::ByMultiplePlugins,
};
Err(XmlrpcDiscoveryError::Disabled { reason })
}
async fn xmlrpc_from_rsd(
&self,
parsed_site_url: &ParsedUrl,
) -> Result<ParsedUrl, XmlrpcDiscoveryError> {
let response = self
.perform(
WpNetworkRequest {
uuid: Uuid::new_v4().into(),
retry_count: 0,
method: RequestMethod::GET,
url: WpEndpointUrl(parsed_site_url.url()),
header_map: WpNetworkHeaderMap::default().into(),
body: None,
}
.into(),
)
.await
.map_err(|error| XmlrpcDiscoveryError::FetchHomepage { error })?;
let rsd_url = extract_rsd_url(&response.body_as_string())
.ok_or(XmlrpcDiscoveryError::EndpointNotFound)?;
let rsd_response = self
.perform(
WpNetworkRequest {
uuid: Uuid::new_v4().into(),
retry_count: 0,
method: RequestMethod::GET,
url: WpEndpointUrl(rsd_url),
header_map: WpNetworkHeaderMap::default().into(),
body: None,
}
.into(),
)
.await
.map_err(|_| XmlrpcDiscoveryError::Disabled {
reason: XmlrpcDisabledReason::ByHost,
})?;
parse_rsd_for_xmlrpc(&rsd_response.body_as_string())
.ok_or(XmlrpcDiscoveryError::EndpointNotFound)
}
}
impl PerformsRequests for WpLoginClient {
fn get_middleware_pipeline(&self) -> Arc<WpApiMiddlewarePipeline> {
self.middleware_pipeline.clone()
}
fn get_request_executor(&self) -> Arc<dyn RequestExecutor> {
self.request_executor.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{WpErrorCode, unit_test_common::wp_network_response_from_json};
#[test]
fn test_parse_api_details_wp_error_rest_forbidden() {
let json = r#"{
"code": "rest_forbidden",
"message": "REST API access is restricted."
}"#;
let response = wp_network_response_from_json(json, 403);
let result = WpLoginClient::parse_api_root(&response);
assert!(
matches!(
result,
Err(FetchAndParseApiRootFailure::WpError {
error_code: WpErrorCode::Forbidden,
status_code: 403,
..
})
),
"{:#?}",
result
);
}
}