From 17a098ef89a720087b4d7bcbb9788c84c2e3b4aa Mon Sep 17 00:00:00 2001 From: landrigun Date: Mon, 7 Nov 2022 10:47:45 +0000 Subject: [PATCH] improv: replace eprintln with logger (#18) + fix mod documentation --- src/config/config.rs | 15 +++++++++------ src/config/mod.rs | 1 + src/http/router.rs | 2 +- src/jwt/jwt.rs | 14 ++++++-------- src/jwt/mod.rs | 1 + src/stores/file.rs | 9 +++------ src/utils/mod.rs | 1 + 7 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/config/config.rs b/src/config/config.rs index 1f77698..d70a3d5 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -34,7 +34,10 @@ impl TryFrom for Config { match u64::from_str(&exp_time) { Ok(v) => v, Err(e) => { - eprintln!("unable to convert JWT expiration time into u64 err={}", e); + log::error!( + "unable to convert JWT expiration time into u64 details={}", + e + ); 0 } } @@ -59,27 +62,27 @@ impl Config { /// validates config ini file fn validate(&self) -> bool { if self.jwt_exp_time <= 0 { - eprintln!("invalid config parameter: JWT expiration time is negative or equals to 0"); + log::error!("invalid config parameter: JWT expiration time is negative or equals to 0"); return false; } if self.jwt_issuer == "" { - eprintln!("invalid config parameter: JWT issuer is empty"); + log::error!("invalid config parameter: JWT issuer is empty"); return false; } if self.jwt_pub_key == "" { - eprintln!("invalid config parameter: JWT public key file path is empty"); + log::error!("invalid config parameter: JWT public key file path is empty"); return false; } if self.jwt_priv_key == "" { - eprintln!("invalid config parameter: JWT private key file path is empty"); + log::error!("invalid config parameter: JWT private key file path is empty"); return false; } if self.filestore_path == "" { - eprintln!("invalid config parameter: filestore path is empty"); + log::error!("invalid config parameter: filestore path is empty"); return false; } diff --git a/src/config/mod.rs b/src/config/mod.rs index 43fe76c..b82b271 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,3 +1,4 @@ +//! provides `Config` struct to load and validate `.ini` file mod config; pub use config::Config; diff --git a/src/http/router.rs b/src/http/router.rs index 36a9f68..1c41b40 100644 --- a/src/http/router.rs +++ b/src/http/router.rs @@ -115,7 +115,7 @@ async fn test_route() { let config: Config = Config::default(); let request_str = "POST /get/ HTTP/1.1\r\n\r\n"; - let response: HTTPResponse = router.route(request_str, config).await; + let response: HTTPResponse = router.route(request_str, "".to_string(), config).await; assert_eq!( HTTPStatusCode::Http400, response.status_line.get_status_code() diff --git a/src/jwt/jwt.rs b/src/jwt/jwt.rs index df9f932..ddcba72 100644 --- a/src/jwt/jwt.rs +++ b/src/jwt/jwt.rs @@ -1,5 +1,3 @@ -//! simple module to read `.pem` files and sign the token - use crate::config::Config; use jwt_simple::common::VerificationOptions; use jwt_simple::prelude::*; @@ -28,7 +26,7 @@ impl JWTSigner { jwt_signer.private_key = c; } Err(e) => { - return Err(format!("unable to read the private key err={}", e)); + return Err(format!("unable to read the private key details={}", e)); } } @@ -37,7 +35,7 @@ impl JWTSigner { jwt_signer.public_key = c; } Err(e) => { - return Err(format!("unable to read the public key err={}", e)); + return Err(format!("unable to read the public key details={}", e)); } } @@ -60,7 +58,7 @@ impl JWTSigner { match RS384KeyPair::from_pem(&self.private_key) { Ok(k) => k, Err(e) => { - return Err(format!("unable to load the private key err={}", e)); + return Err(format!("unable to load the private key details={}", e)); } } }; @@ -70,7 +68,7 @@ impl JWTSigner { match jwt_key.sign(claims) { Ok(token) => Ok(token), Err(e) => { - return Err(format!("unable to sign the token err={}", e)); + return Err(format!("unable to sign the token details={}", e)); } } } @@ -82,12 +80,12 @@ impl JWTSigner { if let Err(e) = key.verify_token::(token, Some(verification_options)) { - return Err(format!("token validation failed err={}", e)); + return Err(format!("token validation failed details={}", e)); } Ok(()) } Err(e) => Err(format!( - "token validation failed can't read the public key err={}", + "token validation failed, can't read the public key details={}", e )), } diff --git a/src/jwt/mod.rs b/src/jwt/mod.rs index 051f27f..9b43d55 100644 --- a/src/jwt/mod.rs +++ b/src/jwt/mod.rs @@ -1,3 +1,4 @@ +//! simple module to read `.pem` files and sign the token mod jwt; pub use jwt::JWTSigner; diff --git a/src/stores/file.rs b/src/stores/file.rs index fda7f36..d5ab489 100644 --- a/src/stores/file.rs +++ b/src/stores/file.rs @@ -41,10 +41,7 @@ impl FileStore { } } Err(e) => { - eprintln!( - "error occurred while reading store file: {}, err={:?}", - self.path, e - ); + log::error!("while reading store file: {}, details={:?}", self.path, e); } } self.credentials = credentials; @@ -69,13 +66,13 @@ impl Store for FileStore { async fn is_auth(&mut self, data: &json::JsonValue) -> bool { // ensure that the store file already exists even after its instanciation if !Path::new(&self.path).is_file() { - eprintln!("{} path referencing file store does not exist", self.path); + log::error!("{} path referencing file store does not exist", self.path); return false; } let credentials = Credentials::from(data); if credentials.is_empty() { - eprintln!("unable to parse the credentials correctly from the incoming request"); + log::error!("unable to parse the credentials correctly from the incoming request"); return false; } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index de6e774..4815b85 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,3 +1,4 @@ +//! includes utility function, that's all ! mod utils; pub use utils::extract_json_value;