Skip to content
Clone
main
48 lines 1.6 KB Rust
use ::session::{AuthedUser, Session};
use api::http::{HeaderMap, Json, State};
use api::rest::{AppResponseData, NoData};
use serde::Deserialize;
use utoipa::ToSchema;
use crate::AuthService;
use crate::errors::ApiError;
use crate::share::account_helpers;
#[derive(Debug, Deserialize, ToSchema)]
pub struct ChangePasswordCmd {
pub old_password: String,
pub new_password: String,
}
#[utoipa::path(
post,
path = "/auth/password/change",
tag = "auth",
description = "修改当前账号口令,成功后吊销除当前会话以外的全部会话。",
request_body = ChangePasswordCmd,
responses(
(status = 200, description = "password changed", body = AppResponseData<NoData>),
(status = 400, description = "request failed validation", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn change_password(
State(service): State<AuthService>,
session: Session,
AuthedUser(uid): AuthedUser,
headers: HeaderMap,
Json(cmd): Json<ChangePasswordCmd>,
) -> Result<AppResponseData<NoData>, ApiError> {
let keep = session.session_id().unwrap_or_default();
service
.change_password(
uid,
&account_helpers::digest(&cmd.old_password),
&account_helpers::digest(&cmd.new_password),
keep.as_str(),
&account_helpers::client_ip(&headers),
)
.await
.map_err(|error| ApiError::bad_request(error.to_string()))?;
Ok(AppResponseData::empty())
}