improv: replace eprintln with logger (#18) + fix mod documentation

This commit is contained in:
landrigun 2022-11-07 10:47:45 +00:00
parent 45c9112af2
commit 17a098ef89
7 changed files with 22 additions and 21 deletions

View File

@ -34,7 +34,10 @@ impl TryFrom<Ini> for Config {
match u64::from_str(&exp_time) { match u64::from_str(&exp_time) {
Ok(v) => v, Ok(v) => v,
Err(e) => { 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 0
} }
} }
@ -59,27 +62,27 @@ impl Config {
/// validates config ini file /// validates config ini file
fn validate(&self) -> bool { fn validate(&self) -> bool {
if self.jwt_exp_time <= 0 { 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; return false;
} }
if self.jwt_issuer == "" { if self.jwt_issuer == "" {
eprintln!("invalid config parameter: JWT issuer is empty"); log::error!("invalid config parameter: JWT issuer is empty");
return false; return false;
} }
if self.jwt_pub_key == "" { 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; return false;
} }
if self.jwt_priv_key == "" { 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; return false;
} }
if self.filestore_path == "" { if self.filestore_path == "" {
eprintln!("invalid config parameter: filestore path is empty"); log::error!("invalid config parameter: filestore path is empty");
return false; return false;
} }

View File

@ -1,3 +1,4 @@
//! provides `Config` struct to load and validate `.ini` file
mod config; mod config;
pub use config::Config; pub use config::Config;

View File

@ -115,7 +115,7 @@ async fn test_route() {
let config: Config = Config::default(); let config: Config = Config::default();
let request_str = "POST /get/ HTTP/1.1\r\n\r\n"; 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!( assert_eq!(
HTTPStatusCode::Http400, HTTPStatusCode::Http400,
response.status_line.get_status_code() response.status_line.get_status_code()

View File

@ -1,5 +1,3 @@
//! simple module to read `.pem` files and sign the token
use crate::config::Config; use crate::config::Config;
use jwt_simple::common::VerificationOptions; use jwt_simple::common::VerificationOptions;
use jwt_simple::prelude::*; use jwt_simple::prelude::*;
@ -28,7 +26,7 @@ impl JWTSigner {
jwt_signer.private_key = c; jwt_signer.private_key = c;
} }
Err(e) => { 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; jwt_signer.public_key = c;
} }
Err(e) => { 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) { match RS384KeyPair::from_pem(&self.private_key) {
Ok(k) => k, Ok(k) => k,
Err(e) => { 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) { match jwt_key.sign(claims) {
Ok(token) => Ok(token), Ok(token) => Ok(token),
Err(e) => { 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) = if let Err(e) =
key.verify_token::<NoCustomClaims>(token, Some(verification_options)) key.verify_token::<NoCustomClaims>(token, Some(verification_options))
{ {
return Err(format!("token validation failed err={}", e)); return Err(format!("token validation failed details={}", e));
} }
Ok(()) Ok(())
} }
Err(e) => Err(format!( 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 e
)), )),
} }

View File

@ -1,3 +1,4 @@
//! simple module to read `.pem` files and sign the token
mod jwt; mod jwt;
pub use jwt::JWTSigner; pub use jwt::JWTSigner;

View File

@ -41,10 +41,7 @@ impl FileStore {
} }
} }
Err(e) => { Err(e) => {
eprintln!( log::error!("while reading store file: {}, details={:?}", self.path, e);
"error occurred while reading store file: {}, err={:?}",
self.path, e
);
} }
} }
self.credentials = credentials; self.credentials = credentials;
@ -69,13 +66,13 @@ impl Store for FileStore {
async fn is_auth(&mut self, data: &json::JsonValue) -> bool { async fn is_auth(&mut self, data: &json::JsonValue) -> bool {
// ensure that the store file already exists even after its instanciation // ensure that the store file already exists even after its instanciation
if !Path::new(&self.path).is_file() { 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; return false;
} }
let credentials = Credentials::from(data); let credentials = Credentials::from(data);
if credentials.is_empty() { 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; return false;
} }

View File

@ -1,3 +1,4 @@
//! includes utility function, that's all !
mod utils; mod utils;
pub use utils::extract_json_value; pub use utils::extract_json_value;