Skip to content

Commit f13146e

Browse files
committed
apply authentication data to requests
1 parent aae34e9 commit f13146e

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

ntfy-daemon/src/models.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ impl Subscription {
157157
.append_pair("since", &since.to_string());
158158
Ok(url)
159159
}
160+
pub fn build_auth_url(server: &str, topic: &str) -> Result<url::Url, crate::Error> {
161+
let mut url = url::Url::parse(server)?;
162+
url.path_segments_mut()
163+
.map_err(|_| url::ParseError::RelativeUrlWithCannotBeABaseBase)?
164+
.push(topic)
165+
.push("auth");
166+
Ok(url)
167+
}
160168
pub fn validate(self) -> Result<Self, Vec<crate::Error>> {
161169
let mut errs = vec![];
162170
if let Err(e) = validate_topic(&self.topic) {

ntfy-daemon/src/system_client.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,7 @@ impl system_notifier::Server for SystemNotifier {
493493
let password = params.get()?.get_password()?.to_str()?;
494494

495495
info!("validating account");
496-
let url = models::Subscription::build_url(server, "stats", 0)
497-
.map_err(|e| capnp::Error::failed(e.to_string()))?;
496+
let url = models::Subscription::build_auth_url(server, "stats")?;
498497

499498
http.get(url)
500499
.basic_auth(username, Some(password))

ntfy-daemon/src/topic_listener.rs

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::ops::ControlFlow;
23
use std::sync::Arc;
34
use std::time::Duration;
@@ -61,16 +62,24 @@ pub fn build_client() -> anyhow::Result<reqwest::Client> {
6162
.build()?)
6263
}
6364

64-
fn topic_request(endpoint: &str, topic: &str, since: u64) -> anyhow::Result<reqwest::Request> {
65+
fn topic_request(
66+
client: &reqwest::Client,
67+
endpoint: &str,
68+
topic: &str,
69+
since: u64,
70+
username: Option<&str>,
71+
password: Option<&str>,
72+
) -> anyhow::Result<reqwest::Request> {
6573
let url = models::Subscription::build_url(endpoint, topic, since)?;
66-
let mut req = reqwest::Request::new(reqwest::Method::GET, url);
67-
let headers = req.headers_mut();
68-
headers.append(
69-
"Content-Type",
70-
HeaderValue::from_static("application/x-ndjson"),
71-
);
72-
headers.append("Transfer-Encoding", HeaderValue::from_static("chunked"));
73-
Ok(req)
74+
let mut req = client
75+
.get(url)
76+
.header("Content-Type", "application/x-ndjson")
77+
.header("Transfer-Encoding", "chunked");
78+
if let Some(username) = username {
79+
req = req.basic_auth(username, password);
80+
}
81+
82+
Ok(req.build()?)
7483
}
7584

7685
async fn response_lines(
@@ -161,8 +170,36 @@ impl TopicListener {
161170

162171
#[instrument(skip_all)]
163172
async fn recv_and_forward(&mut self) -> anyhow::Result<()> {
164-
let req = topic_request(&self.endpoint, &self.topic, self.since)?;
165-
let res = self.env.http.execute(req).await?;
173+
let (username, password) = {
174+
let attrs = HashMap::from([("type", "password"), ("server", &self.endpoint)]);
175+
let items = self
176+
.env
177+
.keyring
178+
.search_items(attrs)
179+
.await
180+
.map_err(|e| capnp::Error::failed(e.to_string()))?;
181+
182+
if let Some(item) = items.into_iter().next() {
183+
let attrs = item
184+
.attributes()
185+
.await
186+
.map_err(|e| capnp::Error::failed(e.to_string()))?;
187+
let password = item.secret().await?;
188+
let password = std::str::from_utf8(&*password)?;
189+
(attrs.get("username").cloned(), Some(password.to_string()))
190+
} else {
191+
(None, None)
192+
}
193+
};
194+
let req = topic_request(
195+
&self.env.http,
196+
&self.endpoint,
197+
&self.topic,
198+
self.since,
199+
username.as_deref(),
200+
password.as_deref(),
201+
);
202+
let res = self.env.http.execute(req?).await?;
166203
let reader = tokio_util::io::StreamReader::new(
167204
res.bytes_stream()
168205
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string())),

0 commit comments

Comments
 (0)