Update password endpoint (#8)
Rust Build / Rustfmt (push) Successful in 40s
Rust Build / Clippy (push) Successful in 1m12s
Rust Build / Check (push) Successful in 2m3s
Rust Build / Test Suite (push) Successful in 2m10s
Rust Build / build (push) Successful in 1m48s
Rust Build / Rustfmt (pull_request) Successful in 37s
Rust Build / Test Suite (pull_request) Successful in 1m10s
Rust Build / Check (pull_request) Successful in 1m30s
Rust Build / Clippy (pull_request) Successful in 1m36s
Rust Build / build (pull_request) Successful in 3m29s

Reviewed-on: phoenix/textsender-auth#8
This commit was merged in pull request #8.
This commit is contained in:
2026-06-12 13:08:34 -04:00
parent 8e0b8b737b
commit e3ee24c131
9 changed files with 450 additions and 4 deletions
+206
View File
@@ -37,6 +37,28 @@ pub mod request {
pub struct RefreshTokenRequest {
pub access_token: String,
}
#[derive(Deserialize, utoipa::ToSchema)]
pub struct UpdatePasswordRequest {
pub user_id: uuid::Uuid,
pub current_password: String,
pub updated_password: String,
pub confirmed_password: String,
}
impl UpdatePasswordRequest {
pub fn is_valid(&self) -> bool {
if self.user_id.is_nil()
|| self.current_password.is_empty()
|| self.updated_password.is_empty()
|| self.confirmed_password.is_empty()
{
false
} else {
self.updated_password == self.confirmed_password
}
}
}
}
pub mod response {
@@ -68,6 +90,12 @@ pub mod response {
pub message: String,
pub data: Vec<textsender_models::token::LoginResult>,
}
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
pub struct UpdatePasswordResponse {
pub message: String,
pub data: Vec<uuid::Uuid>,
}
}
/// Endpoint for a user login
@@ -509,3 +537,181 @@ pub async fn refresh_token(
}
}
}
/// Endpoint for a updating password
#[utoipa::path(
patch,
path = super::endpoints::UPDATE_PASSWORD,
request_body(
content = request::UpdatePasswordRequest,
description = "Data required to update password",
content_type = "application/json"
),
responses(
(status = 200, description = "User login successful", body = response::UpdatePasswordResponse),
(status = 400, description = "Bad data", body = response::UpdatePasswordResponse),
(status = 500, description = "Something went wrong", body = response::UpdatePasswordResponse)
)
)]
pub async fn update_password(
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::Json(payload): axum::Json<request::UpdatePasswordRequest>,
) -> (
axum::http::StatusCode,
axum::Json<response::UpdatePasswordResponse>,
) {
if !payload.is_valid() {
(
axum::http::StatusCode::BAD_REQUEST,
axum::Json(response::UpdatePasswordResponse {
message: String::from("Invalid passwords"),
data: Vec::new(),
}),
)
} else {
let verify_password = |current_password, hashed_password| -> Result<bool, std::io::Error> {
match hashing::verify_password(current_password, hashed_password) {
Ok(matches) => Ok(matches),
Err(err) => Err(std::io::Error::other(err.to_string())),
}
};
match repo::user::get_with_id(&pool, &payload.user_id).await {
Ok(user) => {
let hashed_password = user.password.clone();
match verify_password(&payload.current_password, hashed_password) {
Ok(matches) => {
if matches {
let (generate_salt, mut salt) = super::register::generate_the_salt();
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
let updated_hashed_password = match hashing::hash_password(
&payload.updated_password,
&generate_salt,
) {
Ok(hashed) => hashed,
Err(err) => {
eprintln!("Error: {err:?}");
String::new()
}
};
match repo::user::update_password(
&pool,
&user,
&updated_hashed_password,
)
.await
{
Ok(()) => (
axum::http::StatusCode::OK,
axum::Json(response::UpdatePasswordResponse {
message: String::from(super::messages::SUCCESSFUL_MESSAGE),
data: vec![user.id],
}),
),
Err(err) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::UpdatePasswordResponse {
message: err.to_string(),
data: Vec::new(),
}),
),
}
} else {
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::UpdatePasswordResponse {
message: String::from("Issue updating password"),
data: Vec::new(),
}),
)
}
}
Err(err) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::UpdatePasswordResponse {
message: err.to_string(),
data: Vec::new(),
}),
),
}
}
Err(err) => {
println!("No User found, trying Service User: {err:?}");
// Try service user
match repo::service::get(&pool, &payload.user_id).await {
Ok(service_user) => {
let hashed_password = service_user.passphrase.clone();
match verify_password(&payload.current_password, hashed_password) {
Ok(matches) => {
if matches {
let (generate_salt, mut salt) =
super::register::generate_the_salt();
salt.id = repo::salt::insert(&pool, &salt).await.unwrap();
let updated_hashed_password = match hashing::hash_password(
&payload.updated_password,
&generate_salt,
) {
Ok(hashed) => hashed,
Err(err) => {
eprintln!("Error: {err:?}");
String::new()
}
};
match repo::service::update_passphrase(
&pool,
&service_user,
&updated_hashed_password,
)
.await
{
Ok(()) => (
axum::http::StatusCode::OK,
axum::Json(response::UpdatePasswordResponse {
message: String::from(
super::messages::SUCCESSFUL_MESSAGE,
),
data: vec![service_user.id],
}),
),
Err(err) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::UpdatePasswordResponse {
message: err.to_string(),
data: Vec::new(),
}),
),
}
} else {
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::UpdatePasswordResponse {
message: String::from("Issue updating password"),
data: Vec::new(),
}),
)
}
}
Err(err) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::UpdatePasswordResponse {
message: err.to_string(),
data: Vec::new(),
}),
),
}
}
Err(err) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response::UpdatePasswordResponse {
message: err.to_string(),
data: Vec::new(),
}),
),
}
}
}
}
}
+2
View File
@@ -15,4 +15,6 @@ pub mod endpoints {
pub const LOGIN_SERVICE_USER: &str = "/api/v1/service/login";
/// Endpoint constant for refresh token
pub const REFRESH_TOKEN: &str = "/api/v1/token/refresh";
/// Endpoint constant for updating password
pub const UPDATE_PASSWORD: &str = "/api/v1/user/password/update";
}
+1 -1
View File
@@ -59,7 +59,7 @@ pub mod response {
}
}
fn generate_the_salt() -> (
pub fn generate_the_salt() -> (
argon2::password_hash::SaltString,
textsender_models::user::Salt,
) {
+5 -1
View File
@@ -8,7 +8,7 @@ pub mod token_stuff;
pub mod init {
use axum::{
Router,
routing::{get, post},
routing::{get, patch, post},
};
use utoipa::OpenApi;
@@ -110,6 +110,10 @@ pub mod init {
callers::endpoints::REFRESH_TOKEN,
post(callers::login::refresh_token),
)
.route(
callers::endpoints::UPDATE_PASSWORD,
patch(callers::login::update_password),
)
.layer(cors::configure_cors().await)
}
+26
View File
@@ -104,6 +104,32 @@ pub mod user {
}
}
pub async fn update_password(
pool: &sqlx::PgPool,
user: &textsender_models::user::User,
updated_hashed_password: &String,
) -> Result<(), sqlx::Error> {
match sqlx::query(
r#"
UPDATE "user" SET password = $1 WHERE id = $2
"#,
)
.bind(updated_hashed_password)
.bind(user.id)
.execute(pool)
.await
{
Ok(row) => {
if row.rows_affected() > 0 {
Ok(())
} else {
Err(sqlx::Error::RowNotFound)
}
}
Err(err) => Err(err),
}
}
pub async fn exists(pool: &sqlx::PgPool, username: &String) -> Result<bool, sqlx::Error> {
let result = sqlx::query(
r#"
+26
View File
@@ -154,6 +154,32 @@ pub async fn update_last_login(
}
}
pub async fn update_passphrase(
pool: &sqlx::PgPool,
user: &textsender_models::user::ServiceUser,
updated_hashed_passphrase: &String,
) -> Result<(), sqlx::Error> {
match sqlx::query(
r#"
UPDATE "service_user" SET passphrase = $1 WHERE id = $2
"#,
)
.bind(updated_hashed_passphrase)
.bind(user.id)
.execute(pool)
.await
{
Ok(row) => {
if row.rows_affected() > 0 {
Ok(())
} else {
Err(sqlx::Error::RowNotFound)
}
}
Err(err) => Err(err),
}
}
pub async fn insert(
pool: &sqlx::PgPool,
service_user: &textsender_models::user::ServiceUser,