1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-02-20 16:02:39 +00:00
comunicapiv3/src/helpers/account_helper.rs

66 lines
2.1 KiB
Rust
Raw Normal View History

2020-05-23 19:17:48 +02:00
use crate::data::api_client::APIClient;
2020-05-24 16:35:54 +02:00
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::user::UserID;
use crate::data::user_token::UserAccessToken;
use crate::database_structure::USER_ACCESS_TOKENS_TABLE;
use crate::helpers::{database, user_helper};
use crate::helpers::database::{QueryInfo, InsertQuery};
use crate::utils::crypt_utils::{crypt_pass, rand_str};
2020-05-23 19:17:48 +02:00
/// Account helper
///
/// @author Pierre Hubert
/// Attempt to sign-in user
///
/// In this version of the api, we consider that there is only one login token required
/// This is why I returns just a simple string, the token created for the user in case of success
pub fn login_user(email: &str, password: &str, client: &APIClient) -> ResultBoxError<String> {
let user = user_helper::find_user_by_email(email)?;
2020-05-24 16:35:54 +02:00
// Validate user password
let password = crypt_pass(password)?;
if !user.password.eq(&password) {
return Err(ExecError::boxed_new("The user gave an invalid password!"));
}
2020-05-23 19:17:48 +02:00
2020-05-24 16:35:54 +02:00
// Check if we already have a login token for this user
if let Ok(token) = get_client_tokens(user.id, client) {
return Ok(token.token);
}
2020-05-23 19:17:48 +02:00
2020-05-24 16:35:54 +02:00
// Create new login tokens
let new_token = UserAccessToken {
user_id: user.id,
client_id: client.id,
token: rand_str(150)
};
// Save it
database::insert(
InsertQuery::new(USER_ACCESS_TOKENS_TABLE)
.add_i64("user_id", new_token.user_id)
.add_u32("service_id", client.id)
.add_str("token1", &new_token.token)
.add_str("token2", "dummy_data")
)?;
Ok(new_token.token)
}
/// Get user login tokens
fn get_client_tokens(user_id: UserID, client: &APIClient) -> ResultBoxError<UserAccessToken> {
database::query_row(
QueryInfo::new(USER_ACCESS_TOKENS_TABLE)
2020-05-24 16:39:48 +02:00
.cond_i64("user_id", user_id)
.cond_u32("service_id", client.id),
2020-05-24 16:35:54 +02:00
|res| {
Ok(UserAccessToken {
user_id: res.get_int64("user_id")?,
client_id: res.get_int64("service_id")? as u32,
token: res.get_str("token1")?,
})
},
)
2020-05-23 19:17:48 +02:00
}