1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-20 16:35:17 +00:00

Can authenticate admin with reset token

This commit is contained in:
2021-05-11 16:52:57 +02:00
parent 25830fe6d1
commit d8ec093786
9 changed files with 146 additions and 6 deletions

View File

@ -3,10 +3,12 @@
//! @author Pierre Hubert
use crate::api_data::admin::admin_auth_options::AdminAuthOptions;
use crate::api_data::admin::admin_auth_success::AdminAuthSuccess;
use crate::data::base_request_handler::BaseRequestHandler;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::helpers::admin_account_helper;
use crate::helpers::{admin_access_token_helper, admin_account_helper};
use crate::routes::RequestResult;
use crate::utils::date_utils::time;
/// Get admin auth options
pub fn get_auth_options(r: &mut HttpRequestHandler) -> RequestResult {
@ -14,4 +16,23 @@ pub fn get_auth_options(r: &mut HttpRequestHandler) -> RequestResult {
let admin = admin_account_helper::find_admin_by_email(&mail)?;
r.set_response(AdminAuthOptions::new(&admin))
}
/// Login admin using a reset token
pub fn auth_with_reset_token(r: &mut HttpRequestHandler) -> RequestResult {
let reset_token = r.post_string("token")?;
let admin = admin_account_helper::find_admin_by_email(&r.post_email("mail")?)?;
let token = r.some_or_internal_error(
admin.reset_token,
"Specified user has not valid reset token for now!",
)?;
if !token.token.eq(&reset_token) || time() > token.expire {
return r.forbidden("Specified reset token is invalid!".to_string());
}
let token = admin_access_token_helper::create(admin.id)?;
r.set_response(AdminAuthSuccess::new(token))
}