diff options
| author | KunoiSayami <[email protected]> | 2021-11-11 00:27:25 +0800 |
|---|---|---|
| committer | KunoiSayami <[email protected]> | 2021-11-11 00:28:01 +0800 |
| commit | ff968cf5c39652d9153aa3dcf5563e6d83ea0e6c (patch) | |
| tree | 4bf3b31affa531886aed2f73a2950b9e76dc00db | |
| parent | c64151e929cea76be5553cdeeeaf90de2ca75d89 (diff) | |
fmt(core): Address cargo fmtv4.0.0-alpha.1
* feat(core): Address cargo clippy
* feat(test): Support change PAM provide from environments
Signed-off-by: KunoiSayami <[email protected]>
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/datastructures.rs | 55 | ||||
| -rw-r--r-- | src/main.rs | 7 | ||||
| -rw-r--r-- | src/test.rs | 8 |
5 files changed, 38 insertions, 36 deletions
@@ -346,7 +346,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cgit-simple-authentication" -version = "4.0.0-alpha" +version = "4.0.0-alpha.1" dependencies = [ "anyhow", "argon2", @@ -1,6 +1,6 @@ [package] name = "cgit-simple-authentication" -version = "4.0.0-alpha" +version = "4.0.0-alpha.1" authors = ["KunoiSayami <[email protected]>"] edition = "2018" diff --git a/src/datastructures.rs b/src/datastructures.rs index 7df1ed1..f581653 100644 --- a/src/datastructures.rs +++ b/src/datastructures.rs @@ -26,12 +26,12 @@ use argon2::{ use rand::Rng; use rand_core::OsRng; use serde::{Deserialize, Serialize}; +use sqlx::ConnectOptions; use std::borrow::{BorrowMut, Cow}; use std::fmt::Formatter; use std::fs::read_to_string; use std::path::{Path, PathBuf}; use std::str::FromStr; -use sqlx::ConnectOptions; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use url::form_urlencoded; @@ -83,10 +83,10 @@ pub(crate) struct PAMConfig { impl From<&str> for PAMConfig { fn from(s: &str) -> Self { - let use_pam = ! s.to_lowercase().eq("false"); + let use_pam = !s.to_lowercase().eq("false"); Self { use_pam, - provider: s.to_string() + provider: s.to_string(), } } } @@ -105,7 +105,7 @@ impl Default for PAMConfig { fn default() -> Self { Self { use_pam: false, - provider: "system-auth".to_string() + provider: "system-auth".to_string(), } } } @@ -361,7 +361,7 @@ impl ProtectSettings { Self::load_repos_from_context(white_list_mode, &context) } - fn load_repos_from_context(white_list_mode: bool, s: &String) -> Vec<String> { + fn load_repos_from_context(white_list_mode: bool, s: &str) -> Vec<String> { let mut repos: Vec<String> = Default::default(); let mut last_insert_repo = ""; @@ -611,21 +611,21 @@ impl std::fmt::Display for Cookie { #[derive(Debug, Clone)] pub enum AuthorizerType { PAM, - PASSWORD, + Password, } #[async_trait::async_trait] pub trait Authorizer { fn method(&self) -> AuthorizerType { - AuthorizerType::PASSWORD + AuthorizerType::Password } async fn verify(&self, name: &str, password: &str) -> anyhow::Result<bool>; } #[async_trait::async_trait] -impl<F: ?Sized> Authorizer for Box<F> - where - F: Authorizer + Sync + Send, +impl<F: ?Sized + Sync + Send> Authorizer for Box<F> +where + F: Authorizer + Sync + Send, { fn method(&self) -> AuthorizerType { (**self).method() @@ -636,7 +636,6 @@ impl<F: ?Sized> Authorizer for Box<F> } } - pub struct WrapConfigure { config: Config, authorizer: Box<dyn Authorizer>, @@ -651,7 +650,7 @@ impl From<Config> for WrapConfigure { }; Self { config: cfg, - authorizer + authorizer, } } } @@ -664,7 +663,7 @@ struct PAMAuthorizer { impl From<&PAMConfig> for PAMAuthorizer { fn from(cfg: &PAMConfig) -> Self { Self { - provider: cfg.get_provider().clone() + provider: cfg.get_provider().clone(), } } } @@ -679,7 +678,9 @@ impl Authorizer for PAMAuthorizer { let service = self.provider.as_str(); let mut auth = pam::Authenticator::with_password(service).unwrap(); - auth.get_handler().borrow_mut().set_credentials(user, password); + auth.get_handler() + .borrow_mut() + .set_credentials(user, password); Ok(auth.authenticate().is_ok() && auth.open_session().is_ok()) } } @@ -692,7 +693,11 @@ struct SQLAuthorizer { impl From<&Config> for SQLAuthorizer { fn from(cfg: &Config) -> Self { Self { - database_location: cfg.get_copied_database_location().to_str().unwrap().to_string() + database_location: cfg + .get_copied_database_location() + .to_str() + .unwrap() + .to_string(), } } } @@ -702,7 +707,8 @@ impl WrapConfigure { let cfg = &self.config; if !cfg.get_test_status() { let last_copied = cfg.get_last_copy_timestamp().await.unwrap_or(0); - if last_copied == 0 || cfg.get_last_commit_timestamp().await.unwrap_or(0) != last_copied { + if last_copied == 0 || cfg.get_last_commit_timestamp().await.unwrap_or(0) != last_copied + { std::fs::copy( cfg.get_database_location(), cfg.get_copied_database_location(), @@ -722,17 +728,15 @@ impl WrapConfigure { } } - #[async_trait::async_trait] impl Authorizer for SQLAuthorizer { async fn verify(&self, user: &str, password: &str) -> anyhow::Result<bool> { - let mut conn = sqlx::sqlite::SqliteConnectOptions::from_str( - self.database_location.as_str() - )? - .journal_mode(sqlx::sqlite::SqliteJournalMode::Off) - .log_statements(log::LevelFilter::Trace) - .connect() - .await?; + let mut conn = + sqlx::sqlite::SqliteConnectOptions::from_str(self.database_location.as_str())? + .journal_mode(sqlx::sqlite::SqliteJournalMode::Off) + .log_statements(log::LevelFilter::Trace) + .connect() + .await?; let (passwd_hash,) = sqlx::query_as::<_, (String,)>(r#"SELECT "password" FROM "accounts" WHERE "user" = ?"#) @@ -743,8 +747,7 @@ impl Authorizer for SQLAuthorizer { let parsed_hash = PasswordHash::new(passwd_hash.as_str()).unwrap(); let argon2_alg = Argon2::default(); - Ok( - argon2_alg + Ok(argon2_alg .verify_password(password.as_bytes(), &parsed_hash) .is_ok()) } diff --git a/src/main.rs b/src/main.rs index 91c4c01..217255e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -211,11 +211,8 @@ async fn cmd_init(cfg: Config) -> Result<()> { } async fn verify_login(cfg: &WrapConfigure, data: &FormData) -> Result<bool> { - match cfg.get_authorizer().method() { - AuthorizerType::PASSWORD => { - cfg.hook().await?; - } - _ => {} + if let AuthorizerType::Password = cfg.get_authorizer().method() { + cfg.hook().await?; } data.authorize(cfg.get_authorizer()).await } diff --git a/src/test.rs b/src/test.rs index ee9bc03..bdc66dc 100644 --- a/src/test.rs +++ b/src/test.rs @@ -20,7 +20,6 @@ #[cfg(test)] mod core { - use std::borrow::BorrowMut; use crate::datastructures::{rand_str, Config, TestSuite}; use crate::{cmd_add_user, cmd_authenticate_cookie, cmd_init, cmd_repo_user_control}; use crate::{get_arg_matches, IOModule}; @@ -29,6 +28,7 @@ mod core { Argon2, }; use redis::AsyncCommands; + use std::borrow::BorrowMut; use std::io::{Read, Write}; use std::path::Path; use std::path::PathBuf; @@ -370,12 +370,14 @@ mod core { #[test] fn test_pam() { - let service = "system-login"; + let service = option_env!("pam_service").unwrap_or("system-auth"); let user = option_env!("pam_user").unwrap_or("user"); let password = option_env!("pam_password").unwrap_or("password"); let mut auth = pam::Authenticator::with_password(service).unwrap(); - auth.get_handler().borrow_mut().set_credentials(user, password); + auth.get_handler() + .borrow_mut() + .set_credentials(user, password); assert!(auth.authenticate().is_ok() && auth.open_session().is_ok()) } } |
