summaryrefslogtreecommitdiff
path: root/src/user/routes/password/post.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/user/routes/password/post.rs')
-rw-r--r--src/user/routes/password/post.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/user/routes/password/post.rs b/src/user/routes/password/post.rs
new file mode 100644
index 0000000..296f6cd
--- /dev/null
+++ b/src/user/routes/password/post.rs
@@ -0,0 +1,54 @@
+use axum::{
+ extract::{Json, State},
+ http::StatusCode,
+ response::{IntoResponse, Response},
+};
+
+use crate::{
+ app::App,
+ clock::RequestedAt,
+ error::Internal,
+ token::{
+ app,
+ extract::{Identity, IdentityCookie},
+ },
+ user::{Password, User},
+};
+
+pub async fn handler(
+ State(app): State<App>,
+ RequestedAt(now): RequestedAt,
+ identity: Identity,
+ cookie: IdentityCookie,
+ Json(request): Json<Request>,
+) -> Result<(IdentityCookie, Json<User>), Error> {
+ let (login, secret) = app
+ .tokens()
+ .change_password(&identity.user, &request.password, &request.to, &now)
+ .await
+ .map_err(Error)?;
+ let cookie = cookie.set(secret);
+ Ok((cookie, Json(login)))
+}
+
+#[derive(serde::Deserialize)]
+pub struct Request {
+ pub password: Password,
+ pub to: Password,
+}
+
+#[derive(Debug, thiserror::Error)]
+#[error(transparent)]
+pub struct Error(#[from] pub app::LoginError);
+
+impl IntoResponse for Error {
+ fn into_response(self) -> Response {
+ let Self(error) = self;
+ match error {
+ app::LoginError::Rejected => {
+ (StatusCode::BAD_REQUEST, "invalid name or password").into_response()
+ }
+ other => Internal::from(other).into_response(),
+ }
+ }
+}