use serde::de::DeserializeOwned;
use serde_json::Value;
use crate::AuthError;
pub fn url_encode(text: &str) -> String {
const HEX: &[u8; 16] = b"0123456789ABCDEF";
let mut out = String::with_capacity(text.len());
for b in text.bytes() {
if b.is_ascii_alphanumeric() || b == b'-' || b == b'_' || b == b'.' || b == b'~' {
out.push(b as char);
} else {
out.push('%');
out.push(HEX[usize::from(b >> 4)] as char);
out.push(HEX[usize::from(b & 15)] as char);
}
}
out
}
pub fn url_decode(text: &str) -> Option<String> {
let bytes = text.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
match bytes[i] {
b'%' => {
let hi = (*bytes.get(i + 1)? as char).to_digit(16)?;
let lo = (*bytes.get(i + 2)? as char).to_digit(16)?;
out.push((hi * 16 + lo) as u8);
i += 3;
}
b'+' => {
out.push(b' ');
i += 1;
}
b => {
out.push(b);
i += 1;
}
}
}
String::from_utf8(out).ok()
}
pub fn form_encode(fields: &[(&str, &str)]) -> String {
fields
.iter()
.map(|(k, v)| format!("{}={}", url_encode(k), url_encode(v)))
.collect::<Vec<_>>()
.join("&")
}
pub fn form_decode(text: &str) -> Vec<(String, String)> {
text.split('&')
.filter(|part| !part.is_empty())
.filter_map(|part| {
let (k, v) = part.split_once('=').unwrap_or((part, ""));
Some((url_decode(k)?, url_decode(v)?))
})
.collect()
}
pub fn query_of(url: &str) -> Vec<(String, String)> {
let after = url.split_once('?').map_or("", |(_, q)| q);
let query = after.split('#').next().unwrap_or("");
form_decode(query)
}
pub fn with_query(url: &str, fields: &[(&str, &str)]) -> String {
let sep = if url.contains('?') { '&' } else { '?' };
format!("{url}{sep}{}", form_encode(fields))
}
fn provider_error(status: u16, body: &[u8]) -> AuthError {
let parsed: Option<Value> = serde_json::from_slice(body).ok();
let error = parsed
.as_ref()
.and_then(|v| v.get("error"))
.and_then(Value::as_str)
.map_or_else(|| format!("http_{status}"), str::to_string);
let description = parsed
.as_ref()
.and_then(|v| v.get("error_description"))
.and_then(Value::as_str)
.map(str::to_string)
.or_else(|| {
let text = String::from_utf8_lossy(body).trim().to_string();
(parsed.is_none() && !text.is_empty() && text.len() < 400).then_some(text)
});
AuthError::Provider { error, description }
}
async fn send(request: day_part_http::Request) -> Result<day_part_http::Response, AuthError> {
crate::urls::endpoint(request.url())?;
let client = day_part_http::Client::builder()
.redirects(day_part_http::Redirects::Never)
.timeout_total(std::time::Duration::from_secs(30))
.build();
let response = client
.fetch_limited_future(request, 1024 * 1024)
.await
.map_err(|e| AuthError::Network(e.to_string()))?;
if (300..400).contains(&response.status) {
return Err(AuthError::Network(
"authentication endpoint redirected; use its final URL".into(),
));
}
if response.status >= 400 {
return Err(provider_error(response.status, &response.body));
}
Ok(response)
}
fn parse<T: DeserializeOwned>(response: &day_part_http::Response) -> Result<T, AuthError> {
serde_json::from_slice(&response.body)
.map_err(|e| AuthError::InvalidToken(format!("response body: {e}")))
}
pub async fn get_json<T: DeserializeOwned>(
url: &str,
bearer: Option<&str>,
) -> Result<T, AuthError> {
let mut request = day_part_http::Request::get(url).header("Accept", "application/json");
if let Some(token) = bearer {
request = request.bearer(token);
}
let response = send(request).await?;
parse(&response)
}
pub async fn post_form_json<T: DeserializeOwned>(
url: &str,
fields: &[(&str, &str)],
basic: Option<(&str, &str)>,
) -> Result<T, AuthError> {
let mut request = day_part_http::Request::post(url, form_encode(fields).into_bytes())
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Accept", "application/json");
if let Some((user, password)) = basic {
request = request.basic_auth(user, password);
}
let response = send(request).await?;
parse(&response)
}
pub async fn post_form(
url: &str,
fields: &[(&str, &str)],
basic: Option<(&str, &str)>,
) -> Result<(), AuthError> {
let mut request = day_part_http::Request::post(url, form_encode(fields).into_bytes())
.header("Content-Type", "application/x-www-form-urlencoded");
if let Some((user, password)) = basic {
request = request.basic_auth(user, password);
}
send(request).await.map(|_| ())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encoding_round_trips_and_reserves_what_rfc3986_reserves() {
assert_eq!(url_encode("a b&c=d/é~"), "a%20b%26c%3Dd%2F%C3%A9~");
assert_eq!(url_decode("a%20b+c%26"), Some("a b c&".into()));
assert_eq!(url_decode("%zz"), None);
let fields = [
("redirect_uri", "myapp://auth?x=1"),
("scope", "openid email"),
];
let encoded = form_encode(&fields);
assert_eq!(
encoded,
"redirect_uri=myapp%3A%2F%2Fauth%3Fx%3D1&scope=openid%20email"
);
assert_eq!(
form_decode(&encoded),
vec![
("redirect_uri".to_string(), "myapp://auth?x=1".to_string()),
("scope".to_string(), "openid email".to_string())
]
);
assert_eq!(with_query("https://a/b", &[("c", "d")]), "https://a/b?c=d");
assert_eq!(
with_query("https://a/b?x=1", &[("c", "d")]),
"https://a/b?x=1&c=d"
);
assert_eq!(
query_of("myapp://cb?code=1&state=s#frag"),
vec![
("code".to_string(), "1".to_string()),
("state".to_string(), "s".to_string())
]
);
assert_eq!(
query_of("myapp://cb"),
[] as [(std::string::String, std::string::String); 0]
);
}
#[test]
fn provider_errors_are_read_from_the_body() {
let e = provider_error(
400,
br#"{"error":"invalid_grant","error_description":"expired"}"#,
);
assert_eq!(
e,
AuthError::Provider {
error: "invalid_grant".into(),
description: Some("expired".into())
}
);
let e = provider_error(503, b"down");
assert_eq!(
e,
AuthError::Provider {
error: "http_503".into(),
description: Some("down".into())
}
);
let e = provider_error(401, br#"{"error":"invalid_token"}"#);
assert_eq!(
e,
AuthError::Provider {
error: "invalid_token".into(),
description: None
}
);
}
}