use actix_web::{HttpMessage, HttpRequest, HttpResponse, web};
use serde::Deserialize;
use serde_json::json;
use authr_core::{context::AuthrContext, database::AuthDatabase};
use authr_operations::password::{
ChangePasswordInput, SetPasswordInput, VerifyPasswordInput,
change_password as change_password_operation, set_password as set_password_operation,
verify_password as verify_password_operation,
};
use crate::{error::ServerError, middleware::session::AuthenticatedSession};
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct VerifyPasswordRequest {
pub password: String,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ChangePasswordRequest {
pub new_password: String,
pub current_password: String,
#[serde(default)]
pub revoke_other_sessions: bool,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SetPasswordRequest {
pub new_password: String,
}
fn get_authenticated_session(req: &HttpRequest) -> Result<AuthenticatedSession, ServerError> {
req.extensions()
.get::<AuthenticatedSession>()
.cloned()
.ok_or(ServerError::InvalidCredentials)
}
#[tracing::instrument(name = "auth.verify_password", skip_all)]
pub async fn verify_password<D: AuthDatabase + 'static>(
req: HttpRequest,
ctx: web::Data<AuthrContext<D>>,
body: web::Json<VerifyPasswordRequest>,
) -> Result<HttpResponse, ServerError> {
let authenticated = get_authenticated_session(&req)?;
let body = body.into_inner();
verify_password_operation(
ctx.get_ref(),
VerifyPasswordInput {
user_id: authenticated.user.id.to_string(),
password: body.password,
},
)
.await?;
tracing::debug!(
user_id = %authenticated.user.id,
"Password verified successfully"
);
Ok(HttpResponse::Ok().json(json!({
"status": true
})))
}
#[tracing::instrument(name = "auth.change_password", skip_all)]
pub async fn change_password<D: AuthDatabase + 'static>(
req: HttpRequest,
ctx: web::Data<AuthrContext<D>>,
body: web::Json<ChangePasswordRequest>,
) -> Result<HttpResponse, ServerError> {
let authenticated = get_authenticated_session(&req)?;
let body = body.into_inner();
change_password_operation(
ctx.get_ref(),
ChangePasswordInput {
user_id: authenticated.user.id.to_string(),
current_password: body.current_password,
new_password: body.new_password,
revoke_other_sessions: body.revoke_other_sessions,
current_session_id: authenticated.session.id.to_string(),
},
)
.await?;
tracing::info!(
user_id = %authenticated.user.id,
revoke_other_sessions = body.revoke_other_sessions,
"Password changed"
);
Ok(HttpResponse::Ok().json(json!({
"token": null,
"user": authenticated.user
})))
}
#[tracing::instrument(name = "auth.set_password", skip_all)]
pub async fn set_password<D: AuthDatabase + 'static>(
req: HttpRequest,
ctx: web::Data<AuthrContext<D>>,
body: web::Json<SetPasswordRequest>,
) -> Result<HttpResponse, ServerError> {
let authenticated = get_authenticated_session(&req)?;
let body = body.into_inner();
set_password_operation(
ctx.get_ref(),
SetPasswordInput {
user_id: authenticated.user.id.to_string(),
new_password: body.new_password,
},
)
.await?;
tracing::info!(
user_id = %authenticated.user.id,
"Credential password set"
);
Ok(HttpResponse::Ok().json(json!({
"status": true
})))
}