diff options
author | Vika <vika@fireburn.ru> | 2021-05-09 16:22:07 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2021-05-09 16:22:07 +0300 |
commit | a5a1e44022019995c2646e30be6fc055a61714fc (patch) | |
tree | b8472989478e0d8dba2503e12543001d267603c8 /src | |
parent | 8e4c7f215f11a3735bcf6f8944e13c1aa86d8101 (diff) | |
download | kittybox-a5a1e44022019995c2646e30be6fc055a61714fc.tar.zst |
Added a debug shim to IndieAuthMiddleware that makes it a no-op in staging (except unit tests)
Diffstat (limited to 'src')
-rw-r--r-- | src/indieauth.rs | 16 |
1 files changed, 15 insertions, 1 deletions
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<User>) { 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<retainer::Cache<String, User>>, monitor_task: Option<async_std::task::JoinHandle<()>> } @@ -72,6 +75,10 @@ impl IndieAuthMiddleware { let cache: Arc<retainer::Cache<String, User>> = 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<B> tide::Middleware<ApplicationState<B>> for IndieAuthMiddleware where B: database::Storage + Send + Sync + Clone { + + #[cfg(all(not(test), debug_assertions))] + async fn handle(&self, mut req: Request<ApplicationState<B>>, next: Next<'_, ApplicationState<B>>) -> 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<ApplicationState<B>>, next: Next<'_, ApplicationState<B>>) -> Result { let header = req.header("Authorization"); match header { |