refactor: replace convoluted token redaction code with a simpler one
This commit is contained in:
parent
fba59560d1
commit
632426672c
12
Cargo.lock
generated
12
Cargo.lock
generated
@ -614,6 +614,7 @@ dependencies = [
|
|||||||
"scc",
|
"scc",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"serde_qs",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tower-http",
|
"tower-http",
|
||||||
"tracing",
|
"tracing",
|
||||||
@ -884,6 +885,17 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_qs"
|
||||||
|
version = "0.12.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0431a35568651e363364210c91983c1da5eb29404d9f0928b67d4ebcfa7d330c"
|
||||||
|
dependencies = [
|
||||||
|
"percent-encoding",
|
||||||
|
"serde",
|
||||||
|
"thiserror",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_urlencoded"
|
name = "serde_urlencoded"
|
||||||
version = "0.7.1"
|
version = "0.7.1"
|
||||||
|
@ -21,3 +21,4 @@ rust-argon2 = "1.0"
|
|||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
scc = "1"
|
scc = "1"
|
||||||
base64 = "0.21"
|
base64 = "0.21"
|
||||||
|
serde_qs = "0.12"
|
@ -1,4 +1,4 @@
|
|||||||
use std::fmt::Display;
|
use std::{collections::HashMap, fmt::Display};
|
||||||
|
|
||||||
use super::AppError;
|
use super::AppError;
|
||||||
use async_tungstenite::{
|
use async_tungstenite::{
|
||||||
@ -47,69 +47,37 @@ fn extract_password_from_basic_auth(auth: &str) -> Result<String, AppError> {
|
|||||||
Ok(auth.trim_start_matches("default:").to_string())
|
Ok(auth.trim_start_matches("default:").to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ComponentDisplay<Left, Right> {
|
struct QueryDisplay<'a, 'b> {
|
||||||
left: Left,
|
map: HashMap<&'a str, &'b str>,
|
||||||
right: Right,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> ComponentDisplay<&'a str, &'b str> {
|
impl<'a, 'b> Display for QueryDisplay<'a, 'b> {
|
||||||
fn is_empty(&self) -> bool {
|
|
||||||
self.left.is_empty() && self.right.is_empty()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Left, Right> Display for ComponentDisplay<Left, Right>
|
|
||||||
where
|
|
||||||
Left: Display,
|
|
||||||
Right: Display,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "{}{}", self.left, self.right)
|
let length = self.map.len();
|
||||||
|
for (index, (k, v)) in self.map.iter().enumerate() {
|
||||||
|
write!(f, "{k}={v}")?;
|
||||||
|
if index != length {
|
||||||
|
write!(f, "&")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_span_trace<B>(req: &Request<B>) -> Span {
|
fn make_span_trace<B>(req: &Request<B>) -> Span {
|
||||||
let uri_query_filtered = req
|
let query = req.uri().query();
|
||||||
.uri()
|
let mut query_map = query
|
||||||
.query()
|
.and_then(|v| serde_qs::from_str::<HashMap<&str, &str>>(v).ok())
|
||||||
.map(|q| {
|
.unwrap_or_else(HashMap::new);
|
||||||
let token_start = q.find("&token=");
|
if query_map.contains_key("token") {
|
||||||
if let Some(pos) = token_start {
|
query_map.insert("token", "<redacted>");
|
||||||
let (left, right) = q.split_at(pos);
|
}
|
||||||
let (_, right) = right.split_at(pos + 6 + 30);
|
let query_display = QueryDisplay { map: query_map };
|
||||||
return ComponentDisplay { left, right };
|
|
||||||
}
|
|
||||||
let token_start = q.find("token=");
|
|
||||||
if let Some(_) = token_start {
|
|
||||||
let (_, right) = q.split_at(6 + 30);
|
|
||||||
return ComponentDisplay { left: "", right };
|
|
||||||
}
|
|
||||||
ComponentDisplay { left: q, right: "" }
|
|
||||||
})
|
|
||||||
.unwrap_or(ComponentDisplay {
|
|
||||||
left: "",
|
|
||||||
right: "",
|
|
||||||
});
|
|
||||||
let uri_path = ComponentDisplay {
|
|
||||||
left: {
|
|
||||||
if !uri_query_filtered.is_empty() {
|
|
||||||
ComponentDisplay {
|
|
||||||
left: req.uri().path(),
|
|
||||||
right: "?",
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ComponentDisplay {
|
|
||||||
left: req.uri().path(),
|
|
||||||
right: "",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
right: uri_query_filtered,
|
|
||||||
};
|
|
||||||
tracing::debug_span!(
|
tracing::debug_span!(
|
||||||
"request",
|
"request",
|
||||||
method = %req.method(),
|
method = %req.method(),
|
||||||
uri = %uri_path,
|
path = %req.uri().path(),
|
||||||
|
query = %query_display,
|
||||||
version = ?req.version(),
|
version = ?req.version(),
|
||||||
headers = ?req.headers(),
|
headers = ?req.headers(),
|
||||||
)
|
)
|
||||||
@ -209,9 +177,11 @@ async fn get_scoped_music(
|
|||||||
.expect("cant fail"),
|
.expect("cant fail"),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
// add cache header
|
if resp.status().is_success() {
|
||||||
resp.headers_mut()
|
// add cache header
|
||||||
.insert(CACHE_CONTROL, AUDIO_CACHE_HEADER.clone());
|
resp.headers_mut()
|
||||||
|
.insert(CACHE_CONTROL, AUDIO_CACHE_HEADER.clone());
|
||||||
|
}
|
||||||
Ok(resp)
|
Ok(resp)
|
||||||
} else {
|
} else {
|
||||||
Ok(Response::builder()
|
Ok(Response::builder()
|
||||||
@ -227,9 +197,11 @@ async fn get_music(
|
|||||||
req: Request<Body>,
|
req: Request<Body>,
|
||||||
) -> Result<Response<Body>, AppError> {
|
) -> Result<Response<Body>, AppError> {
|
||||||
http(State(app), Query(query), req).await.map(|mut resp| {
|
http(State(app), Query(query), req).await.map(|mut resp| {
|
||||||
// add cache header
|
if resp.status().is_success() {
|
||||||
resp.headers_mut()
|
// add cache header
|
||||||
.insert(CACHE_CONTROL, AUDIO_CACHE_HEADER.clone());
|
resp.headers_mut()
|
||||||
|
.insert(CACHE_CONTROL, AUDIO_CACHE_HEADER.clone());
|
||||||
|
}
|
||||||
resp
|
resp
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user