Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
53ed0060c8
|
||
|
|
9b4d0b7b32
|
||
|
|
dd877369e3
|
Generated
+2
-2
@@ -1551,8 +1551,8 @@ checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "simodels"
|
name = "simodels"
|
||||||
version = "0.11.4"
|
version = "0.11.6"
|
||||||
source = "git+ssh://git@git.kundeng.us/phoenix/simodels.git?tag=v0.11.4#d6d719741945ef7719ab1db3e0ef3c617b5bb909"
|
source = "git+ssh://git@git.kundeng.us/phoenix/simodels.git?tag=v0.11.6#e6ab0bcd83fd2ab3ab47298022c885132eed9945"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"josekit",
|
"josekit",
|
||||||
"rand",
|
"rand",
|
||||||
|
|||||||
+1
-1
@@ -22,7 +22,7 @@ time = { version = "0.3.55", features = ["macros", "serde"] }
|
|||||||
josekit = { version = "0.10.3" }
|
josekit = { version = "0.10.3" }
|
||||||
utoipa = { version = "5.5.0", features = ["axum_extras"] }
|
utoipa = { version = "5.5.0", features = ["axum_extras"] }
|
||||||
utoipa-swagger-ui = { version = "9.0.2", features = ["axum"] }
|
utoipa-swagger-ui = { version = "9.0.2", features = ["axum"] }
|
||||||
simodels = { git = "ssh://git@git.kundeng.us/phoenix/simodels.git", tag = "v0.11.4" }
|
simodels = { git = "ssh://git@git.kundeng.us/phoenix/simodels.git", tag = "v0.11.6" }
|
||||||
sienvy = { git = "ssh://git@git.kundeng.us/phoenix/sienvy.git", tag = "v0.8.1" }
|
sienvy = { git = "ssh://git@git.kundeng.us/phoenix/sienvy.git", tag = "v0.8.1" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|||||||
@@ -20,6 +20,25 @@ pub mod request {
|
|||||||
pub access_token: String,
|
pub access_token: String,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod update_password {
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
|
pub struct Request {
|
||||||
|
pub user_id: uuid::Uuid,
|
||||||
|
pub username: String,
|
||||||
|
pub current_password: String,
|
||||||
|
pub updated_password: String,
|
||||||
|
pub confirmed_password: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Request {
|
||||||
|
pub fn is_valid(&self) -> bool {
|
||||||
|
!self.user_id.is_nil() || !self.username.is_empty() || !self.current_password.is_empty() || !self.updated_password.is_empty() || !self.confirmed_password.is_empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod response {
|
pub mod response {
|
||||||
@@ -46,6 +65,16 @@ pub mod response {
|
|||||||
pub data: Vec<simodels::login_result::LoginResult>,
|
pub data: Vec<simodels::login_result::LoginResult>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod update_password {
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
|
pub struct Response {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<uuid::Uuid>,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Module for login endpoints
|
/// Module for login endpoints
|
||||||
@@ -261,4 +290,72 @@ pub mod endpoint {
|
|||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = super::super::endpoints::UPDATE_PASSWORD,
|
||||||
|
request_body(
|
||||||
|
content = super::request::update_password::Request,
|
||||||
|
description = "Update user password",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Password updated successfully", body = super::response::update_password::Response),
|
||||||
|
(status = 400, description = "Failure", body = super::response::update_password::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
pub async fn update_password(
|
||||||
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::Json(payload): axum::Json<super::request::update_password::Request>,
|
||||||
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<super::response::update_password::Response>,
|
||||||
|
) {
|
||||||
|
let mut response = super::response::update_password::Response::default();
|
||||||
|
|
||||||
|
if !payload.is_valid() {
|
||||||
|
response.message = "Invalid request".to_string();
|
||||||
|
return (axum::http::StatusCode::BAD_REQUEST, axum::Json(response));
|
||||||
|
}
|
||||||
|
|
||||||
|
match repo::user::get(&pool, &payload.username).await {
|
||||||
|
Ok(user) => {
|
||||||
|
if hashing::verify_password(&payload.current_password, user.password.clone()).unwrap() {
|
||||||
|
match repo::salt::get(&pool, &user.salt_id).await {
|
||||||
|
Ok(salt) => {
|
||||||
|
// Left off here
|
||||||
|
let updated_salt_string = hashing::generate_salt().unwrap();
|
||||||
|
let mut updated_salt = simodels::user::salt::Salt::default();
|
||||||
|
updated_salt.salt = updated_salt_string.to_string();
|
||||||
|
updated_salt.id = salt.id.clone();
|
||||||
|
let updated_hashed_password = hashing::hash_password(&payload.updated_password, &updated_salt_string).unwrap();
|
||||||
|
|
||||||
|
match repo::user::update_password(&pool, &user, &updated_hashed_password).await {
|
||||||
|
Ok(_) => {
|
||||||
|
response.message = "Successful".to_string();
|
||||||
|
response.data.push(user.id);
|
||||||
|
(axum::http::StatusCode::OK, axum::Json(response))
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// invalid
|
||||||
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ pub mod endpoints {
|
|||||||
pub const REGISTER: &str = "/api/v2/register";
|
pub const REGISTER: &str = "/api/v2/register";
|
||||||
pub const DBTEST: &str = "/api/v2/test/db";
|
pub const DBTEST: &str = "/api/v2/test/db";
|
||||||
pub const LOGIN: &str = "/api/v2/login";
|
pub const LOGIN: &str = "/api/v2/login";
|
||||||
|
pub const UPDATE_PASSWORD: &str = "/api/v2/user/password";
|
||||||
pub const SERVICE_LOGIN: &str = "/api/v2/service/login";
|
pub const SERVICE_LOGIN: &str = "/api/v2/service/login";
|
||||||
pub const REFRESH_TOKEN: &str = "/api/v2/token/refresh";
|
pub const REFRESH_TOKEN: &str = "/api/v2/token/refresh";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,28 @@ pub mod user {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn update_password(pool: &sqlx::PgPool, user: &simodels::user::User, password: &str) -> Result<bool, sqlx::Error> {
|
||||||
|
match sqlx::query(
|
||||||
|
r#"
|
||||||
|
UPDATE "user" SET password = $1 WHERE id = $2
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
.bind(password)
|
||||||
|
.bind(user.id)
|
||||||
|
.execute(pool)
|
||||||
|
// .fetch_optional(pool)
|
||||||
|
.await {
|
||||||
|
Ok(r) => {
|
||||||
|
return if r.rows_affected() > 0 {
|
||||||
|
Ok(true)
|
||||||
|
} else {
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn update_last_login(
|
pub async fn update_last_login(
|
||||||
pool: &sqlx::PgPool,
|
pool: &sqlx::PgPool,
|
||||||
user: &simodels::user::User,
|
user: &simodels::user::User,
|
||||||
|
|||||||
Reference in New Issue
Block a user