diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-11-25 12:35:35 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-11-25 12:35:35 -0500 |
commit | 9d191019757eae3bba45e8e1a021fefdc69a9fae (patch) | |
tree | dcb6d7d6e7a3aa726650e401b92e8484e049ff32 /server | |
parent | f4713b1ccf508c0ec1192ce8d800f21111e655e1 (diff) |
Add fake authenticator
Diffstat (limited to 'server')
-rw-r--r-- | server/src/fake_auth.rs | 33 | ||||
-rw-r--r-- | server/src/main.rs | 23 |
2 files changed, 55 insertions, 1 deletions
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<String, error::BridgeError> { + Err(BridgeError::NotLoggedIn) + } + + async fn authenticate( + &self, + pool: &sqlx::PgPool, + session_id: SessionId, + auth_params: HashMap<String, String>, + ) -> Result<AuthenticatedSession, error::BridgeError> { + 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<dyn Authenticator + Send + Sync> { + Box::new(OauthAuthenticator::from_env(db_pool.clone()).await) +} + +#[cfg(debug_assertions)] +async fn create_authenticator(db_pool: &PgPool) -> Box<dyn Authenticator + Send + Sync> { + 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<dyn Authenticator + Send + Sync> { + 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, }); |