From 9d191019757eae3bba45e8e1a021fefdc69a9fae Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 25 Nov 2022 12:35:35 -0500 Subject: Add fake authenticator --- server/src/fake_auth.rs | 33 +++++++++++++++++++++++++++++++++ server/src/main.rs | 23 ++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 server/src/fake_auth.rs (limited to 'server') diff --git a/server/src/fake_auth.rs b/server/src/fake_auth.rs new file mode 100644 index 0000000..5ef9cc8 --- /dev/null +++ b/server/src/fake_auth.rs @@ -0,0 +1,33 @@ +use std::collections::HashMap; + +use async_trait::async_trait; + +use crate::{ + auth::{AuthenticatedSession, Authenticator, SessionId}, + error::{self, BridgeError}, +}; + +pub struct FakeAuthenticator {} + +#[async_trait] +impl Authenticator for FakeAuthenticator { + async fn user_info( + &self, + session: &mut AuthenticatedSession, + ) -> Result { + Err(BridgeError::NotLoggedIn) + } + + async fn authenticate( + &self, + pool: &sqlx::PgPool, + session_id: SessionId, + auth_params: HashMap, + ) -> Result { + Err(BridgeError::Internal("not implemented".to_string())) + } + + async fn get_login_url(&self) -> (SessionId, reqwest::Url) { + todo!() + } +} diff --git a/server/src/main.rs b/server/src/main.rs index 2363110..39425d6 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -19,6 +19,7 @@ mod auth; mod error; mod play; mod server; +mod fake_auth; use crate::{ auth::{OauthAuthenticator, SessionId}, server::ServerContext, @@ -28,6 +29,26 @@ use crate::{ play::{DbJournal, Journal}, }; use sqlx::{postgres::PgPoolOptions, PgPool}; +use auth::Authenticator; + +async fn create_default_authenticator(db_pool: &PgPool) -> Box { + Box::new(OauthAuthenticator::from_env(db_pool.clone()).await) +} + +#[cfg(debug_assertions)] +async fn create_authenticator(db_pool: &PgPool) -> Box { + const FAKE_AUTHENTICATOR: &str = "fake"; + if std::env::var("AUTHENTICATOR").unwrap_or("".to_string()) == FAKE_AUTHENTICATOR { + Box::new(fake_auth::FakeAuthenticator {}) + } else { + create_default_authenticator(db_pool).await + } +} + +#[cfg(not(debug_assertions))] +async fn create_authenticator(db_pool: &PgPool) -> Box { + create_default_authenticator(db_pool) +} #[tokio::main] async fn main() { @@ -66,7 +87,7 @@ async fn main() { let state = Arc::new(ServerContext { app_url, - authenticator: Box::new(OauthAuthenticator::from_env(db_pool.clone()).await), + authenticator: create_authenticator(&db_pool).await, db: db_pool, }); -- cgit v1.2.3