From 47285d5af803ed26d52b7f248a8f2de556e9057f Mon Sep 17 00:00:00 2001 From: Vika Date: Thu, 12 May 2022 01:37:59 +0300 Subject: treewide: prepare for mf2 parsing and cleanup unused code --- src/indieauth.rs | 75 +++++++++++++++++++------------------------------------- 1 file changed, 25 insertions(+), 50 deletions(-) (limited to 'src/indieauth.rs') diff --git a/src/indieauth.rs b/src/indieauth.rs index 4dfe11d..57c0301 100644 --- a/src/indieauth.rs +++ b/src/indieauth.rs @@ -62,8 +62,8 @@ impl From for IndieAuthError { } } -impl From for IndieAuthError { - fn from(err: hyper::Error) -> Self { +impl From for IndieAuthError { + fn from(err: reqwest::Error) -> Self { Self { msg: format!("{}", err), source: Some(Box::new(err)), @@ -90,11 +90,10 @@ impl User { } } -pub fn require_token(token_endpoint: String, http: hyper::Client) -> impl Filter + Clone -where T: hyper::client::connect::Connect + Clone + Send + Sync + 'static { +pub fn require_token(token_endpoint: String, http: reqwest::Client) -> impl Filter + Clone { // It might be OK to panic here, because we're still inside the initialisation sequence for now. // Proper error handling on the top of this should be used though. - let token_endpoint_uri = hyper::Uri::try_from(&token_endpoint) + let token_endpoint_uri = url::Url::parse(&token_endpoint) .expect("Couldn't parse the token endpoint URI!"); warp::any() .map(move || token_endpoint_uri.clone()) @@ -110,60 +109,36 @@ where T: hyper::client::connect::Connect + Clone + Send + Sync + 'static { Err(err) } }).unify()) - .and_then(|token_endpoint, http: hyper::Client, token| async move { - let request = hyper::Request::builder() - .method(hyper::Method::GET) - .uri(token_endpoint) - .header("Authorization", token) - .header("Accept", "application/json") - .body(hyper::Body::from("")) - // TODO is it acceptable to panic here? - .unwrap(); - + .and_then(|token_endpoint, http: reqwest::Client, token| async move { use hyper::StatusCode; - match http.request(request).await { - Ok(mut res) => match res.status() { - StatusCode::OK => { - use hyper::body::HttpBody; - use bytes::BufMut; - let mut buf: Vec = Vec::default(); - while let Some(chunk) = res.body_mut().data().await { - if let Err(err) = chunk { - return Err(IndieAuthError::from(err).into()); - } - buf.put(chunk.unwrap()); - } - match serde_json::from_slice(&buf) { + match http + .get(token_endpoint) + .header("Authorization", token) + .header("Accept", "application/json") + .send() + .await + { + Ok(res) => match res.status() { + StatusCode::OK => match res.json::().await { + Ok(json) => match serde_json::from_value::(json.clone()) { Ok(user) => Ok(user), Err(err) => { - if let Ok(json) = serde_json::from_slice::(&buf) { - if Some(false) == json["active"].as_bool() { - Err(IndieAuthError { - source: None, - kind: ErrorKind::NotAuthorized, - msg: "The token endpoint deemed the token as not \"active\".".to_string() - }.into()) - } else { - Err(IndieAuthError::from(err).into()) - } + if let Some(false) = json["active"].as_bool() { + Err(IndieAuthError { + source: None, + kind: ErrorKind::NotAuthorized, + msg: "The token is not active for this user.".to_owned() + }.into()) } else { Err(IndieAuthError::from(err).into()) } } } + Err(err) => Err(IndieAuthError::from(err).into()) }, StatusCode::BAD_REQUEST => { - use hyper::body::HttpBody; - use bytes::BufMut; - let mut buf: Vec = Vec::default(); - while let Some(chunk) = res.body_mut().data().await { - if let Err(err) = chunk { - return Err(IndieAuthError::from(err).into()); - } - buf.put(chunk.unwrap()); - } - match serde_json::from_slice::(&buf) { + match res.json::().await { Ok(err) => { if err.error == "unauthorized" { Err(IndieAuthError { @@ -211,8 +186,8 @@ mod tests { } #[inline] - fn get_http_client() -> hyper::Client { - hyper::Client::new() + fn get_http_client() -> reqwest::Client { + reqwest::Client::new() } #[tokio::test] -- cgit 1.4.1