mirror of
https://github.com/danbulant/Portfolio
synced 2026-07-06 11:30:44 +00:00
refactor: application service
This commit is contained in:
parent
9e1ba16ef6
commit
97b087f4c2
6 changed files with 118 additions and 83 deletions
|
|
@ -2,7 +2,7 @@ use std::net::SocketAddr;
|
||||||
|
|
||||||
use portfolio_core::{
|
use portfolio_core::{
|
||||||
crypto::random_8_char_string,
|
crypto::random_8_char_string,
|
||||||
services::{admin_service::AdminService, candidate_service::CandidateService},
|
services::{admin_service::AdminService, candidate_service::CandidateService, application_service::ApplicationService},
|
||||||
};
|
};
|
||||||
use requests::{AdminLoginRequest, RegisterRequest};
|
use requests::{AdminLoginRequest, RegisterRequest};
|
||||||
use rocket::http::{Cookie, Status, CookieJar};
|
use rocket::http::{Cookie, Status, CookieJar};
|
||||||
|
|
@ -75,7 +75,7 @@ pub async fn create_candidate(
|
||||||
|
|
||||||
let plain_text_password = random_8_char_string();
|
let plain_text_password = random_8_char_string();
|
||||||
|
|
||||||
CandidateService::create(
|
ApplicationService::create_candidate_with_parent(
|
||||||
db,
|
db,
|
||||||
form.application_id,
|
form.application_id,
|
||||||
&plain_text_password,
|
&plain_text_password,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
use portfolio_core::candidate_details::CandidateDetails;
|
use portfolio_core::candidate_details::CandidateDetails;
|
||||||
|
use portfolio_core::services::application_service::ApplicationService;
|
||||||
use portfolio_core::services::candidate_service::{CandidateService};
|
use portfolio_core::services::candidate_service::{CandidateService};
|
||||||
use requests::LoginRequest;
|
use requests::LoginRequest;
|
||||||
use rocket::http::{Cookie, CookieJar, Status};
|
use rocket::http::{Cookie, CookieJar, Status};
|
||||||
|
|
@ -65,13 +66,13 @@ pub async fn fill_details(
|
||||||
) -> Result<String, Custom<String>> {
|
) -> Result<String, Custom<String>> {
|
||||||
let db = conn.into_inner();
|
let db = conn.into_inner();
|
||||||
let form = details.into_inner();
|
let form = details.into_inner();
|
||||||
let candidate: entity::candidate::Model = session.into();
|
let candidate: entity::candidate::Model = session.into(); // TODO: don't return candidate from session
|
||||||
|
|
||||||
let candidate = CandidateService::add_candidate_details(db, candidate, form).await;
|
let candidate_parent = ApplicationService::add_all_details(db, candidate.application, form).await;
|
||||||
|
|
||||||
if candidate.is_err() {
|
if candidate_parent.is_err() {
|
||||||
// TODO cleanup
|
// TODO cleanup
|
||||||
let e = candidate.err().unwrap();
|
let e = candidate_parent.err().unwrap();
|
||||||
return Err(Custom(
|
return Err(Custom(
|
||||||
Status::from_code(e.code()).unwrap_or_default(),
|
Status::from_code(e.code()).unwrap_or_default(),
|
||||||
e.message(),
|
e.message(),
|
||||||
|
|
@ -92,7 +93,7 @@ pub async fn get_details(
|
||||||
let password = password_form.password.clone();
|
let password = password_form.password.clone();
|
||||||
|
|
||||||
// let handle = tokio::spawn(async move {
|
// let handle = tokio::spawn(async move {
|
||||||
let details = CandidateService::decrypt_details(db, candidate.application, password)
|
let details = ApplicationService::decrypt_all_details(db, candidate.application, password)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Custom(Status::from_code(e.code()).unwrap_or_default(), e.message()));
|
.map_err(|e| Custom(Status::from_code(e.code()).unwrap_or_default(), e.message()));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,89 @@
|
||||||
/* pub struct ApplicationService;
|
use entity::{candidate, parent};
|
||||||
|
use sea_orm::DbConn;
|
||||||
|
|
||||||
|
use crate::{error::ServiceError, candidate_details::{CandidateDetails, EncryptedCandidateDetails}, Query, crypto};
|
||||||
|
|
||||||
|
use super::{parent_service::ParentService, candidate_service::CandidateService};
|
||||||
|
|
||||||
|
pub struct ApplicationService;
|
||||||
|
|
||||||
impl ApplicationService {
|
impl ApplicationService {
|
||||||
pub fn create
|
pub async fn create_candidate_with_parent( // uchazeč s maminkou 👩🍼
|
||||||
} */
|
db: &DbConn,
|
||||||
|
application_id: i32,
|
||||||
|
plain_text_password: &String,
|
||||||
|
personal_id_number: String,
|
||||||
|
) -> Result<(candidate::Model, parent::Model), ServiceError> {
|
||||||
|
Ok(
|
||||||
|
(
|
||||||
|
CandidateService::create(db, application_id, plain_text_password, personal_id_number).await?,
|
||||||
|
ParentService::create(db, application_id).await?
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn add_all_details(
|
||||||
|
db: &DbConn,
|
||||||
|
application: i32,
|
||||||
|
form: CandidateDetails,
|
||||||
|
) -> Result<(candidate::Model, parent::Model), ServiceError> {
|
||||||
|
let candidate = Query::find_candidate_by_id(db, application)
|
||||||
|
.await
|
||||||
|
.map_err(|_| ServiceError::DbError)?
|
||||||
|
.ok_or(ServiceError::CandidateNotFound)?;
|
||||||
|
|
||||||
|
let parent = Query::find_parent_by_id(db, application)
|
||||||
|
.await
|
||||||
|
.map_err(|_| ServiceError::DbError)?
|
||||||
|
.ok_or(ServiceError::ParentNotFound)?;
|
||||||
|
|
||||||
|
let Ok(admin_public_keys) = Query::get_all_admin_public_keys(db).await else {
|
||||||
|
return Err(ServiceError::DbError);
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut admin_public_keys_refrence: Vec<&str> =
|
||||||
|
admin_public_keys.iter().map(|s| &**s).collect();
|
||||||
|
|
||||||
|
let mut recipients = vec![&*candidate.public_key];
|
||||||
|
recipients.append(&mut admin_public_keys_refrence);
|
||||||
|
|
||||||
|
let enc_details = EncryptedCandidateDetails::new(form, recipients).await?;
|
||||||
|
|
||||||
|
Ok(
|
||||||
|
(
|
||||||
|
CandidateService::add_candidate_details(db, candidate, enc_details.clone()).await?,
|
||||||
|
ParentService::add_parent_details(db, parent, enc_details.clone()).await?
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn decrypt_all_details(
|
||||||
|
db: &DbConn,
|
||||||
|
application_id: i32,
|
||||||
|
password: String,
|
||||||
|
) -> Result<CandidateDetails, ServiceError> {
|
||||||
|
let candidate = match Query::find_candidate_by_id(db, application_id).await {
|
||||||
|
Ok(candidate) => candidate.unwrap(),
|
||||||
|
Err(_) => return Err(ServiceError::DbError), // TODO: logging
|
||||||
|
};
|
||||||
|
let parent = Query::find_parent_by_id(db, application_id).await.unwrap().unwrap();
|
||||||
|
|
||||||
|
match crypto::verify_password((&password).to_string(), candidate.code.clone()).await {
|
||||||
|
Ok(valid) => {
|
||||||
|
if !valid {
|
||||||
|
return Err(ServiceError::InvalidCredentials);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => return Err(ServiceError::InvalidCredentials),
|
||||||
|
}
|
||||||
|
|
||||||
|
let dec_priv_key = crypto::decrypt_password(candidate.private_key.clone(), password)
|
||||||
|
.await
|
||||||
|
.ok()
|
||||||
|
.unwrap();
|
||||||
|
let enc_details = EncryptedCandidateDetails::try_from((candidate, parent))?;
|
||||||
|
|
||||||
|
enc_details.decrypt(dec_priv_key).await
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -19,7 +19,7 @@ impl CandidateService {
|
||||||
/// Hashed password
|
/// Hashed password
|
||||||
/// Encrypted private key
|
/// Encrypted private key
|
||||||
/// Public key
|
/// Public key
|
||||||
pub async fn create(
|
pub(in crate::services) async fn create(
|
||||||
db: &DbConn,
|
db: &DbConn,
|
||||||
application_id: i32,
|
application_id: i32,
|
||||||
plain_text_password: &String,
|
plain_text_password: &String,
|
||||||
|
|
@ -53,9 +53,6 @@ impl CandidateService {
|
||||||
return Err(ServiceError::CryptoHashFailed);
|
return Err(ServiceError::CryptoHashFailed);
|
||||||
};
|
};
|
||||||
|
|
||||||
ParentService::create_parent(db, application_id)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Mutation::create_candidate(
|
Mutation::create_candidate(
|
||||||
db,
|
db,
|
||||||
application_id,
|
application_id,
|
||||||
|
|
@ -68,59 +65,16 @@ impl CandidateService {
|
||||||
.map_err(|_| ServiceError::DbError)
|
.map_err(|_| ServiceError::DbError)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn add_candidate_details(
|
pub(in crate::services) async fn add_candidate_details(
|
||||||
db: &DbConn,
|
db: &DbConn,
|
||||||
candidate: candidate::Model,
|
candidate: candidate::Model,
|
||||||
form: CandidateDetails,
|
enc_details: EncryptedCandidateDetails,
|
||||||
) -> Result<entity::candidate::Model, ServiceError> {
|
) -> Result<entity::candidate::Model, ServiceError> {
|
||||||
let Ok(admin_public_keys) = Query::get_all_admin_public_keys(db).await else {
|
|
||||||
return Err(ServiceError::DbError);
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut admin_public_keys_refrence: Vec<&str> =
|
|
||||||
admin_public_keys.iter().map(|s| &**s).collect();
|
|
||||||
|
|
||||||
let mut recipients = vec![&*candidate.public_key];
|
|
||||||
|
|
||||||
recipients.append(&mut admin_public_keys_refrence);
|
|
||||||
|
|
||||||
let enc_details = EncryptedCandidateDetails::new(form, recipients).await?;
|
|
||||||
|
|
||||||
ParentService::add_parent_details(db, candidate.application, enc_details.clone()).await?;
|
|
||||||
Mutation::add_candidate_details(db, candidate, enc_details.clone())
|
Mutation::add_candidate_details(db, candidate, enc_details.clone())
|
||||||
.await
|
.await
|
||||||
.map_err(|_| ServiceError::DbError)
|
.map_err(|_| ServiceError::DbError)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn decrypt_details(
|
|
||||||
db: &DbConn,
|
|
||||||
application_id: i32,
|
|
||||||
password: String,
|
|
||||||
) -> Result<CandidateDetails, ServiceError> {
|
|
||||||
let candidate = match Query::find_candidate_by_id(db, application_id).await {
|
|
||||||
Ok(candidate) => candidate.unwrap(),
|
|
||||||
Err(_) => return Err(ServiceError::DbError), // TODO: logging
|
|
||||||
};
|
|
||||||
let parent = Query::find_parent_by_id(db, application_id).await.unwrap().unwrap();
|
|
||||||
|
|
||||||
match crypto::verify_password((&password).to_string(), candidate.code.clone()).await {
|
|
||||||
Ok(valid) => {
|
|
||||||
if !valid {
|
|
||||||
return Err(ServiceError::InvalidCredentials);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => return Err(ServiceError::InvalidCredentials),
|
|
||||||
}
|
|
||||||
|
|
||||||
let dec_priv_key = crypto::decrypt_password(candidate.private_key.clone(), password)
|
|
||||||
.await
|
|
||||||
.ok()
|
|
||||||
.unwrap();
|
|
||||||
let enc_details = EncryptedCandidateDetails::try_from((candidate, parent))?;
|
|
||||||
|
|
||||||
enc_details.decrypt(dec_priv_key).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_set_up(candidate: &candidate::Model) -> bool {
|
pub fn is_set_up(candidate: &candidate::Model) -> bool {
|
||||||
candidate.name.is_some() &&
|
candidate.name.is_some() &&
|
||||||
candidate.surname.is_some() &&
|
candidate.surname.is_some() &&
|
||||||
|
|
@ -218,7 +172,6 @@ impl CandidateService {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use entity::candidate::Model;
|
|
||||||
use sea_orm::{Database, DbConn};
|
use sea_orm::{Database, DbConn};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
|
@ -227,6 +180,10 @@ mod tests {
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::EncryptedCandidateDetails;
|
use super::EncryptedCandidateDetails;
|
||||||
|
use chrono::NaiveDate;
|
||||||
|
use entity::{parent, candidate};
|
||||||
|
|
||||||
|
use crate::services::application_service::ApplicationService;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_application_id_validation() {
|
async fn test_application_id_validation() {
|
||||||
|
|
@ -300,11 +257,9 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
async fn put_user_data(db: &DbConn) -> Model {
|
async fn put_user_data(db: &DbConn) -> (candidate::Model, parent::Model) {
|
||||||
use chrono::NaiveDate;
|
|
||||||
|
|
||||||
let plain_text_password = "test".to_string();
|
let plain_text_password = "test".to_string();
|
||||||
let candidate = CandidateService::create(&db, 103151, &plain_text_password, "".to_string())
|
let (candidate, parent) = ApplicationService::create_candidate_with_parent(&db, 103151, &plain_text_password, "".to_string())
|
||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
@ -327,7 +282,7 @@ mod tests {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CandidateService::add_candidate_details(&db, candidate, form)
|
ApplicationService::add_all_details(&db, candidate.application, form)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
@ -335,16 +290,16 @@ mod tests {
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_put_user_data() {
|
async fn test_put_user_data() {
|
||||||
let db = get_memory_sqlite_connection().await;
|
let db = get_memory_sqlite_connection().await;
|
||||||
let candidate = put_user_data(&db).await;
|
let (candidate, parent) = put_user_data(&db).await;
|
||||||
assert!(candidate.name.is_some());
|
assert!(candidate.name.is_some());
|
||||||
|
assert!(parent.name.is_some());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_encrypt_decrypt_user_data() {
|
async fn test_encrypt_decrypt_user_data() {
|
||||||
let password = "test".to_string();
|
let password = "test".to_string();
|
||||||
let db = get_memory_sqlite_connection().await;
|
let db = get_memory_sqlite_connection().await;
|
||||||
let enc_candidate = put_user_data(&db).await;
|
let (enc_candidate, enc_parent) = put_user_data(&db).await;
|
||||||
let enc_parent = Query::find_parent_by_id(&db, enc_candidate.application).await.unwrap().unwrap();
|
|
||||||
|
|
||||||
let dec_priv_key = crypto::decrypt_password(enc_candidate.private_key.clone(), password)
|
let dec_priv_key = crypto::decrypt_password(enc_candidate.private_key.clone(), password)
|
||||||
.await
|
.await
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
use entity::{parent};
|
use entity::{parent};
|
||||||
use sea_orm::DbConn;
|
use sea_orm::DbConn;
|
||||||
|
|
||||||
use crate::{error::ServiceError, Mutation, candidate_details::EncryptedCandidateDetails, Query};
|
use crate::{error::ServiceError, Mutation, candidate_details::EncryptedCandidateDetails};
|
||||||
|
|
||||||
pub struct ParentService;
|
pub struct ParentService;
|
||||||
|
|
||||||
impl ParentService {
|
impl ParentService {
|
||||||
pub async fn create_parent(
|
pub async fn create(
|
||||||
db: &DbConn,
|
db: &DbConn,
|
||||||
application_id: i32,
|
application_id: i32,
|
||||||
) -> Result<parent::Model, ServiceError> {
|
) -> Result<parent::Model, ServiceError> {
|
||||||
|
|
@ -19,14 +19,9 @@ impl ParentService {
|
||||||
|
|
||||||
pub async fn add_parent_details(
|
pub async fn add_parent_details(
|
||||||
db: &DbConn,
|
db: &DbConn,
|
||||||
application_id: i32,
|
parent: parent::Model,
|
||||||
enc_details: EncryptedCandidateDetails,
|
enc_details: EncryptedCandidateDetails,
|
||||||
) -> Result<parent::Model, ServiceError> {
|
) -> Result<parent::Model, ServiceError> {
|
||||||
let parent = Query::find_parent_by_id(db, application_id)
|
|
||||||
.await
|
|
||||||
.map_err(|_| ServiceError::DbError)?
|
|
||||||
.ok_or(ServiceError::ParentNotFound)?;
|
|
||||||
|
|
||||||
let parent = Mutation::add_parent_details(db, parent, enc_details)
|
let parent = Mutation::add_parent_details(db, parent, enc_details)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| ServiceError::DbError)?;
|
.map_err(|_| ServiceError::DbError)?;
|
||||||
|
|
|
||||||
|
|
@ -171,7 +171,7 @@ mod tests {
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
crypto,
|
crypto,
|
||||||
services::{candidate_service::CandidateService, session_service::SessionService},
|
services::{session_service::SessionService, application_service::ApplicationService},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
@ -205,10 +205,10 @@ mod tests {
|
||||||
|
|
||||||
let db = get_memory_sqlite_connection().await;
|
let db = get_memory_sqlite_connection().await;
|
||||||
|
|
||||||
let candidate = CandidateService::create(&db, 103151, &SECRET.to_string(), "".to_string())
|
let candidate = ApplicationService::create_candidate_with_parent(&db, 103151, &SECRET.to_string(), "".to_string())
|
||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
.unwrap();
|
.unwrap().0;
|
||||||
|
|
||||||
assert_eq!(candidate.application, 103151);
|
assert_eq!(candidate.application, 103151);
|
||||||
assert_ne!(candidate.code, SECRET.to_string());
|
assert_ne!(candidate.code, SECRET.to_string());
|
||||||
|
|
@ -222,9 +222,9 @@ mod tests {
|
||||||
async fn test_candidate_session_correct_password() {
|
async fn test_candidate_session_correct_password() {
|
||||||
let db = &get_memory_sqlite_connection().await;
|
let db = &get_memory_sqlite_connection().await;
|
||||||
|
|
||||||
CandidateService::create(db, 103151, &"Tajny_kod".to_string(), "".to_string())
|
ApplicationService::create_candidate_with_parent(db, 103151, &"Tajny_kod".to_string(), "".to_string())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap().0;
|
||||||
|
|
||||||
// correct password
|
// correct password
|
||||||
let session = SessionService::new_session(
|
let session = SessionService::new_session(
|
||||||
|
|
@ -249,9 +249,9 @@ mod tests {
|
||||||
let db = &get_memory_sqlite_connection().await;
|
let db = &get_memory_sqlite_connection().await;
|
||||||
|
|
||||||
let candidate_form =
|
let candidate_form =
|
||||||
CandidateService::create(&db, 103151, &"Tajny_kod".to_string(), "".to_string())
|
ApplicationService::create_candidate_with_parent(&db, 103151, &"Tajny_kod".to_string(), "".to_string())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap().0;
|
||||||
|
|
||||||
// incorrect password
|
// incorrect password
|
||||||
assert!(SessionService::new_session(
|
assert!(SessionService::new_session(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue