about summary refs log tree commit diff
path: root/src/indieauth.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-05-12 01:37:59 +0300
committerVika <vika@fireburn.ru>2022-05-12 01:37:59 +0300
commit47285d5af803ed26d52b7f248a8f2de556e9057f (patch)
treef385c797742a4e98d1c78ba803396bd6ffe44b35 /src/indieauth.rs
parent34f1c6229bac92212c97cbacc77801d4a2921e4a (diff)
treewide: prepare for mf2 parsing and cleanup unused code
Diffstat (limited to 'src/indieauth.rs')
-rw-r--r--src/indieauth.rs75
1 files changed, 25 insertions, 50 deletions
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<serde_json::Error> for IndieAuthError {
     }
 }
 
-impl From<hyper::Error> for IndieAuthError {
-    fn from(err: hyper::Error) -> Self {
+impl From<reqwest::Error> 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<T>(token_endpoint: String, http: hyper::Client<T, hyper::Body>) -> impl Filter<Extract = (User,), Error = Rejection> + Clone
-where T: hyper::client::connect::Connect + Clone + Send + Sync + 'static {
+pub fn require_token(token_endpoint: String, http: reqwest::Client) -> impl Filter<Extract = (User,), Error = Rejection> + 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<T, hyper::Body>, 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<u8> = 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::<serde_json::Value>().await {
+                        Ok(json) => match serde_json::from_value::<User>(json.clone()) {
                             Ok(user) => Ok(user),
                             Err(err) => {
-                                if let Ok(json) = serde_json::from_slice::<serde_json::Value>(&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<u8> = 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::<TokenEndpointError>(&buf) {
+                        match res.json::<TokenEndpointError>().await {
                             Ok(err) => {
                                 if err.error == "unauthorized" {
                                     Err(IndieAuthError {
@@ -211,8 +186,8 @@ mod tests {
     }
 
     #[inline]
-    fn get_http_client() -> hyper::Client<impl hyper::client::connect::Connect + Clone + Send + Sync + 'static, hyper::Body> {
-        hyper::Client::new()
+    fn get_http_client() -> reqwest::Client {
+        reqwest::Client::new()
     }
     
     #[tokio::test]