From a5a1e44022019995c2646e30be6fc055a61714fc Mon Sep 17 00:00:00 2001 From: Vika Date: Sun, 9 May 2021 16:22:07 +0300 Subject: Added a debug shim to IndieAuthMiddleware that makes it a no-op in staging (except unit tests) --- src/indieauth.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/indieauth.rs b/src/indieauth.rs index 4fac5e1..27a70a1 100644 --- a/src/indieauth.rs +++ b/src/indieauth.rs @@ -1,7 +1,9 @@ use async_trait::async_trait; +#[allow(unused_imports)] use log::{error,info}; use url::Url; use tide::prelude::*; +#[allow(unused_imports)] use tide::{Request, Response, Next, Result}; use std::sync::Arc; @@ -22,7 +24,6 @@ impl User { pub fn scopes(&self) -> std::str::SplitAsciiWhitespace<'_> { self.scope.split_ascii_whitespace() } - #[cfg(test)] pub fn new(me: &str, client_id: &str, scope: &str) -> Self { Self { me: Url::parse(me).unwrap(), @@ -32,6 +33,7 @@ impl User { } } +#[cfg(any(not(debug_assertions), test))] async fn get_token_data(token: String, token_endpoint: &http_types::Url, http_client: &surf::Client) -> (http_types::StatusCode, Option) { match http_client.get(token_endpoint).header("Authorization", token).header("Accept", "application/json").send().await { Ok(mut resp) => { @@ -59,6 +61,7 @@ async fn get_token_data(token: String, token_endpoint: &http_types::Url, http_cl } pub struct IndieAuthMiddleware { + #[allow(dead_code)] // it's not really dead since it's only dead in debug scope cache: Arc>, monitor_task: Option> } @@ -72,6 +75,10 @@ impl IndieAuthMiddleware { let cache: Arc> = Arc::new(retainer::Cache::new()); let cache_clone = cache.clone(); let task = async_std::task::spawn(async move { cache_clone.monitor(4, 0.1, std::time::Duration::from_secs(30)).await }); + + #[cfg(all(debug_assertions, not(test)))] + error!("ATTENTION: You are running in debug mode. NO REQUESTS TO TOKEN ENDPOINT WILL BE MADE. YOU WILL BE PROCEEDING WITH DEBUG USER CREDENTIALS. DO NOT RUN LIKE THIS IN PRODUCTION."); + Self { cache, monitor_task: Some(task) } } } @@ -101,6 +108,13 @@ impl Drop for IndieAuthMiddleware { impl tide::Middleware> for IndieAuthMiddleware where B: database::Storage + Send + Sync + Clone { + + #[cfg(all(not(test), debug_assertions))] + async fn handle(&self, mut req: Request>, next: Next<'_, ApplicationState>) -> Result { + req.set_ext(User::new("http://localhost:8080/", "https://curl.haxx.se/","create update delete undelete media")); + Ok(next.run(req).await) + } + #[cfg(any(not(debug_assertions), test))] async fn handle(&self, mut req: Request>, next: Next<'_, ApplicationState>) -> Result { let header = req.header("Authorization"); match header { -- cgit 1.4.1