Vastly improved error handling
Vastly improved error handling in some functions. Rest will be done later. Code cleanup and fixed typos. Removed actix_htttp crate as dependency
This commit is contained in:
parent
8d8cdfe267
commit
5618d043bc
1
.env
1
.env
@ -1,2 +1,3 @@
|
|||||||
DATABASE_URL=test.db
|
DATABASE_URL=test.db
|
||||||
BIND_ADDRESS=127.0.0.1:7800
|
BIND_ADDRESS=127.0.0.1:7800
|
||||||
|
HASH_COST=8
|
||||||
|
1087
Cargo.lock
generated
1087
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
15
Cargo.toml
15
Cargo.toml
@ -23,7 +23,6 @@ validator_derive = '0.10'
|
|||||||
jsonwebtoken = '7'
|
jsonwebtoken = '7'
|
||||||
actix-identity = '0.2.1'
|
actix-identity = '0.2.1'
|
||||||
actix-web-httpauth = '0.4.1'
|
actix-web-httpauth = '0.4.1'
|
||||||
actix-http = '1.0.1'
|
|
||||||
rand = '0.7.3'
|
rand = '0.7.3'
|
||||||
nanoid = '0.3.0'
|
nanoid = '0.3.0'
|
||||||
bcrypt = '0.7'
|
bcrypt = '0.7'
|
||||||
@ -41,9 +40,9 @@ actix-threadpool = '0.3.1'
|
|||||||
version = '1.0.106'
|
version = '1.0.106'
|
||||||
features = ['derive']
|
features = ['derive']
|
||||||
|
|
||||||
[dependencies.yarte]
|
# [dependencies.yarte]
|
||||||
version = '0.9.0'
|
# version = '0.9.0'
|
||||||
features = ['html-min']
|
# features = ['html-min']
|
||||||
|
|
||||||
[dependencies.diesel]
|
[dependencies.diesel]
|
||||||
version = '1.4.4'
|
version = '1.4.4'
|
||||||
@ -67,7 +66,7 @@ features = ['bundled']
|
|||||||
[dependencies.chrono]
|
[dependencies.chrono]
|
||||||
version = '0.4.11'
|
version = '0.4.11'
|
||||||
features = ['serde']
|
features = ['serde']
|
||||||
[build-dependencies.yarte_helpers]
|
# [build-dependencies.yarte_helpers]
|
||||||
version = '0.9.0'
|
# version = '0.9.0'
|
||||||
default-features = false
|
# default-features = false
|
||||||
features = ['config']
|
# features = ['config']
|
||||||
|
@ -24,7 +24,7 @@ use std::convert::From;
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
custom_error! { #[derive(new)] pub DomainError
|
custom_error! { #[derive(new)] pub DomainError
|
||||||
PwdHashError {source: BcryptError} = "Failed to has password",
|
PwdHashError {source: BcryptError} = "Failed to hash password",
|
||||||
DbError {source: diesel::result::Error} = "Database error",
|
DbError {source: diesel::result::Error} = "Database error",
|
||||||
DbPoolError {source: r2d2::Error} = "Failed to get connection from pool",
|
DbPoolError {source: r2d2::Error} = "Failed to get connection from pool",
|
||||||
PasswordError {cause: String} = "Failed to validate password - {cause}",
|
PasswordError {cause: String} = "Failed to validate password - {cause}",
|
||||||
@ -53,10 +53,10 @@ impl ResponseError for DomainError {
|
|||||||
reason: format!("{} {}", err.to_string(), source).as_str(),
|
reason: format!("{} {}", err.to_string(), source).as_str(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
DomainError::PasswordError { cause } => HttpResponse::BadRequest()
|
DomainError::PasswordError { cause: _ } => HttpResponse::BadRequest()
|
||||||
.json(ErrorModel {
|
.json(ErrorModel {
|
||||||
error_code: 400,
|
error_code: 400,
|
||||||
reason: format!("{} {}, ", err.to_string(), cause.clone())
|
reason: format!("{}", err.to_string())
|
||||||
.as_str(),
|
.as_str(),
|
||||||
}),
|
}),
|
||||||
DomainError::GenericError { cause } => HttpResponse::BadRequest()
|
DomainError::GenericError { cause } => HttpResponse::BadRequest()
|
||||||
|
@ -7,11 +7,10 @@ extern crate custom_error;
|
|||||||
extern crate regex;
|
extern crate regex;
|
||||||
extern crate validator;
|
extern crate validator;
|
||||||
|
|
||||||
use actix_web::{middleware, web, App, HttpServer};
|
use actix_web::{middleware, web, App, HttpServer, cookie::SameSite};
|
||||||
|
|
||||||
use actix_web_httpauth::middleware::HttpAuthentication;
|
use actix_web_httpauth::middleware::HttpAuthentication;
|
||||||
|
|
||||||
use actix_http::cookie::SameSite;
|
|
||||||
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
@ -80,7 +79,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
CookieIdentityPolicy::new(&private_key)
|
CookieIdentityPolicy::new(&private_key)
|
||||||
.name("my-app-auth")
|
.name("my-app-auth")
|
||||||
.secure(false)
|
.secure(false)
|
||||||
.same_site(SameSite::Lax),
|
.same_site(SameSite::Lax)
|
||||||
))
|
))
|
||||||
.wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
.service(
|
.service(
|
||||||
|
@ -4,7 +4,6 @@ use crate::schema::users;
|
|||||||
use crate::utils::regexs;
|
use crate::utils::regexs;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
use validator_derive::*;
|
use validator_derive::*;
|
||||||
use yarte::Template;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Queryable, Identifiable, Deserialize)]
|
#[derive(Debug, Clone, Queryable, Identifiable, Deserialize)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
@ -28,10 +27,3 @@ pub struct UserDTO {
|
|||||||
pub registration_date: chrono::NaiveDateTime,
|
pub registration_date: chrono::NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Template)]
|
|
||||||
#[template(path = "hello.hbs")]
|
|
||||||
pub struct CardTemplate<'a> {
|
|
||||||
pub title: &'a str,
|
|
||||||
pub body: String,
|
|
||||||
pub num: u32,
|
|
||||||
}
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use actix_web::{web, ResponseError};
|
use actix_web::web;
|
||||||
use actix_web_httpauth::extractors::basic::BasicAuth;
|
use actix_web_httpauth::extractors::basic::BasicAuth;
|
||||||
|
|
||||||
use crate::actions::users;
|
use crate::actions::users;
|
||||||
@ -11,7 +11,7 @@ pub async fn login(
|
|||||||
id: Identity,
|
id: Identity,
|
||||||
credentials: BasicAuth,
|
credentials: BasicAuth,
|
||||||
config: web::Data<AppConfig>,
|
config: web::Data<AppConfig>,
|
||||||
) -> Result<HttpResponse, impl ResponseError> {
|
) -> Result<HttpResponse, Error> {
|
||||||
let maybe_identity = id.identity();
|
let maybe_identity = id.identity();
|
||||||
let response = if let Some(identity) = maybe_identity {
|
let response = if let Some(identity) = maybe_identity {
|
||||||
Ok(HttpResponse::Found()
|
Ok(HttpResponse::Found()
|
||||||
@ -20,30 +20,21 @@ pub async fn login(
|
|||||||
.json(format!("Already logged in as {}", identity)))
|
.json(format!("Already logged in as {}", identity)))
|
||||||
} else {
|
} else {
|
||||||
let credentials2 = credentials.clone();
|
let credentials2 = credentials.clone();
|
||||||
web::block(move || validate_basic_auth(credentials2, &config))
|
let valid =
|
||||||
.await
|
web::block(move || validate_basic_auth(credentials2, &config))
|
||||||
.and_then(|valid| {
|
.await?;
|
||||||
if valid {
|
if valid {
|
||||||
id.remember(credentials.user_id().to_string());
|
id.remember(credentials.user_id().to_string());
|
||||||
Ok(HttpResponse::Found().header("location", "/").finish())
|
Ok(HttpResponse::Found().header("location", "/").finish())
|
||||||
} else {
|
} else {
|
||||||
// Err(BlockingError::Error(
|
Ok(HttpResponse::BadRequest().json(
|
||||||
// errors::DomainError::new_password_error(
|
crate::models::errors::ErrorModel::new(
|
||||||
// "Wrong password or account does not exist"
|
20,
|
||||||
// .to_string(),
|
"Wrong password or account does not exist",
|
||||||
// ),
|
),
|
||||||
// ))
|
))
|
||||||
Ok(HttpResponse::BadRequest().json(
|
}
|
||||||
crate::models::errors::ErrorModel::new(
|
|
||||||
20,
|
|
||||||
"Wrong password or account does not exist",
|
|
||||||
),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
};
|
};
|
||||||
// println!("{}", credentials.user_id());
|
|
||||||
// println!("{:?}", credentials.password());
|
|
||||||
response
|
response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
use actix_web_httpauth::extractors::basic::BasicAuth;
|
use actix_web_httpauth::extractors::basic::BasicAuth;
|
||||||
|
|
||||||
use crate::AppConfig;
|
use crate::AppConfig;
|
||||||
// use actix_identity::Identity;
|
|
||||||
use crate::routes::validate_basic_auth;
|
use crate::routes::validate_basic_auth;
|
||||||
use actix_threadpool::BlockingError;
|
|
||||||
|
|
||||||
use actix_web::{dev::ServiceRequest, web, Error};
|
use actix_web::{dev::ServiceRequest, web, Error};
|
||||||
|
|
||||||
// use Response;
|
|
||||||
|
|
||||||
pub async fn validator(
|
pub async fn validator(
|
||||||
req: ServiceRequest,
|
req: ServiceRequest,
|
||||||
@ -16,33 +13,16 @@ pub async fn validator(
|
|||||||
println!("{}", credentials.user_id());
|
println!("{}", credentials.user_id());
|
||||||
println!("{:?}", credentials.password());
|
println!("{:?}", credentials.password());
|
||||||
// verify credentials from db
|
// verify credentials from db
|
||||||
let credentials2 = credentials.clone();
|
let config = req.app_data::<AppConfig>().expect("Error getting config");
|
||||||
// let pool = req.app_data();
|
|
||||||
let config = req.app_data::<AppConfig>().expect("Error getting db");
|
|
||||||
// .get_ref()
|
|
||||||
// .clone();
|
|
||||||
// let _config = req
|
|
||||||
// .app_data::<Config>()
|
|
||||||
// .map(|data| data.get_ref().clone())
|
|
||||||
// .unwrap_or_else(Default::default);
|
|
||||||
|
|
||||||
let res = web::block(move || validate_basic_auth(credentials2, &config))
|
let valid =
|
||||||
.await
|
web::block(move || validate_basic_auth(credentials, &config)).await?;
|
||||||
.and_then(|valid| {
|
if valid {
|
||||||
if valid {
|
debug!("Success");
|
||||||
debug!("Success");
|
Ok(req)
|
||||||
Ok(req)
|
} else {
|
||||||
} else {
|
Err(crate::errors::DomainError::new_password_error(
|
||||||
debug!("Failure");
|
"Wrong password or account does not exist".to_string(),
|
||||||
Err(BlockingError::Error(
|
).into())
|
||||||
crate::errors::DomainError::new_password_error(
|
}
|
||||||
"Wrong password or account does not exist".to_string(),
|
|
||||||
),
|
|
||||||
))
|
|
||||||
// Err(AuthenticationError::from(config))
|
|
||||||
// Ok(req)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
let res2: Result<ServiceRequest, Error> = res.map_err(|e| e.into());
|
|
||||||
res2
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user