-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathnc.rs
More file actions
142 lines (132 loc) · 4.64 KB
/
nc.rs
File metadata and controls
142 lines (132 loc) · 4.64 KB
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
/*
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
use crate::error::{AuthenticationError, NextCloudError};
use crate::{Result, UserId};
use reqwest::header::HeaderName;
use reqwest::{Certificate, Response, StatusCode, Url};
use std::fmt::Write;
use std::net::IpAddr;
static X_FORWARDED_FOR: HeaderName = HeaderName::from_static("x-forwarded-for");
pub struct Client {
http: reqwest::Client,
base_url: Url,
}
impl Client {
pub fn new(base_url: &str, allow_self_signed: bool) -> Result<Self, NextCloudError> {
let base_url = Url::parse(base_url)?;
let http = reqwest::Client::builder()
.tls_certs_merge(
webpki_root_certs::TLS_SERVER_ROOT_CERTS
.iter()
.map(|root| Certificate::from_der(root).unwrap()),
)
.tls_danger_accept_invalid_certs(allow_self_signed)
.build()?;
Ok(Client { http, base_url })
}
pub async fn verify_credentials(
&self,
username: &str,
password: &str,
forwarded_for: Vec<IpAddr>,
) -> Result<UserId, AuthenticationError> {
log::debug!("Verifying credentials for {username}");
let response = self.auth_request(username, password, forwarded_for).await?;
match response.status() {
StatusCode::OK => Ok(response
.text()
.await
.map_err(|_| AuthenticationError::InvalidMessage)?
.into()),
StatusCode::UNAUTHORIZED => Err(AuthenticationError::Invalid),
status if status.is_server_error() => Err(NextCloudError::Server(status).into()),
status if status.is_client_error() => Err(NextCloudError::Client(status).into()),
status => Err(NextCloudError::Other(status).into()),
}
}
async fn auth_request(
&self,
username: &str,
password: &str,
forwarded_for: Vec<IpAddr>,
) -> Result<Response, NextCloudError> {
self.http
.get(self.base_url.join("index.php/apps/notify_push/uid")?)
.basic_auth(username, Some(password))
.header(
&X_FORWARDED_FOR,
forwarded_for.iter().fold(
String::with_capacity(forwarded_for.len() * 16),
|mut joined, ip| {
if !joined.is_empty() {
write!(&mut joined, ", ").ok();
}
write!(&mut joined, "{ip}").ok();
joined
},
),
)
.send()
.await
.map_err(NextCloudError::NextcloudConnect)
}
pub async fn get_test_cookie(&self, token: &str) -> Result<u32, NextCloudError> {
let response = self
.http
.get(
self.base_url
.join("index.php/apps/notify_push/test/cookie")?,
)
.header(HeaderName::from_static("token"), token.to_string())
.send()
.await?;
let status = response.status();
let text = response.text().await?;
if status.is_client_error() {
if text.contains("admin-trusted-domains") {
Err(NextCloudError::NotATrustedDomain(
self.base_url.host_str().unwrap_or_default().into(),
))
} else {
Err(NextCloudError::Client(status))
}
} else {
Ok(text
.parse()
.map_err(NextCloudError::MalformedCookieResponse)?)
}
}
pub async fn test_set_remote(
&self,
addr: IpAddr,
token: &str,
) -> Result<IpAddr, NextCloudError> {
self.http
.get(
self.base_url
.join("index.php/apps/notify_push/test/remote")
.map_err(NextCloudError::from)?,
)
.header(&X_FORWARDED_FOR, addr.to_string())
.header(HeaderName::from_static("token"), token.to_string())
.send()
.await?
.text()
.await?
.parse()
.map_err(NextCloudError::MalformedRemote)
}
/// Ask the app to put it's version number into redis under 'notify_push_app_version'
pub async fn request_app_version(&self) -> Result<(), NextCloudError> {
self.http
.get(
self.base_url
.join("index.php/apps/notify_push/test/version")?,
)
.send()
.await?;
Ok(())
}
}