Compare commits

..
Author SHA1 Message Date
phoenix 53ed0060c8 Fix synxtax
soaricarus_auth Build / Rustfmt (pull_request) Failing after 2m44s
soaricarus_auth Build / Clippy (pull_request) Failing after 4m8s
soaricarus_auth Build / Check (pull_request) Failing after 4m10s
soaricarus_auth Build / Test Suite (pull_request) Failing after 4m21s
soaricarus_auth Build / build (pull_request) Failing after 4m20s
2026-09-20 19:32:26 -04:00
phoenix 9b4d0b7b32 Get back here later
soaricarus_auth Build / Rustfmt (pull_request) Failing after 44s
soaricarus_auth Build / build (pull_request) Failing after 50s
soaricarus_auth Build / Check (pull_request) Failing after 1m45s
soaricarus_auth Build / Test Suite (pull_request) Failing after 2m16s
soaricarus_auth Build / Clippy (pull_request) Failing after 2m17s
2026-09-19 18:14:01 -04:00
phoenix dd877369e3 bump: simodels
soaricarus_auth Build / Check (pull_request) Successful in 1m39s
soaricarus_auth Build / Rustfmt (pull_request) Successful in 41s
soaricarus_auth Build / build (pull_request) Successful in 1m12s
soaricarus_auth Build / Clippy (pull_request) Successful in 2m22s
soaricarus_auth Build / Test Suite (pull_request) Successful in 2m38s
2026-09-19 17:03:15 -04:00
5 changed files with 45 additions and 241 deletions
Generated
+1 -1
View File
@@ -1580,7 +1580,7 @@ dependencies = [
[[package]] [[package]]
name = "soaricarus_auth" name = "soaricarus_auth"
version = "0.8.3" version = "0.8.2"
dependencies = [ dependencies = [
"argon2", "argon2",
"axum", "axum",
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "soaricarus_auth" name = "soaricarus_auth"
version = "0.8.3" version = "0.8.2"
edition = "2024" edition = "2024"
rust-version = "1.95" rust-version = "1.95"
license = "MIT" license = "MIT"
+11 -41
View File
@@ -35,11 +35,7 @@ pub mod request {
impl Request { impl Request {
pub fn is_valid(&self) -> bool { pub fn is_valid(&self) -> bool {
!self.user_id.is_nil() !self.user_id.is_nil() || !self.username.is_empty() || !self.current_password.is_empty() || !self.updated_password.is_empty() || !self.confirmed_password.is_empty()
|| !self.username.is_empty()
|| !self.current_password.is_empty()
|| !self.updated_password.is_empty()
|| !self.confirmed_password.is_empty()
} }
} }
} }
@@ -305,8 +301,7 @@ pub mod endpoint {
), ),
responses( responses(
(status = 200, description = "Password updated successfully", body = super::response::update_password::Response), (status = 200, description = "Password updated successfully", body = super::response::update_password::Response),
(status = 400, description = "Invalid", body = super::response::update_password::Response), (status = 400, description = "Failure", body = super::response::update_password::Response)
(status = 500, description = "Failure", body = super::response::update_password::Response)
) )
)] )]
pub async fn update_password( pub async fn update_password(
@@ -320,52 +315,27 @@ pub mod endpoint {
if !payload.is_valid() { if !payload.is_valid() {
response.message = "Invalid request".to_string(); response.message = "Invalid request".to_string();
println!("Invalid request");
return (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)); return (axum::http::StatusCode::BAD_REQUEST, axum::Json(response));
} }
println!("Get User");
match repo::user::get(&pool, &payload.username).await { match repo::user::get(&pool, &payload.username).await {
Ok(user) => { Ok(user) => {
if hashing::verify_password(&payload.current_password, user.password.clone()) if hashing::verify_password(&payload.current_password, user.password.clone()).unwrap() {
.unwrap()
{
match repo::salt::get(&pool, &user.salt_id).await { match repo::salt::get(&pool, &user.salt_id).await {
Ok(salt) => { Ok(salt) => {
// Left off here
let updated_salt_string = hashing::generate_salt().unwrap(); let updated_salt_string = hashing::generate_salt().unwrap();
let updated_salt = simodels::user::salt::Salt { let mut updated_salt = simodels::user::salt::Salt::default();
salt: updated_salt_string.to_string(), updated_salt.salt = updated_salt_string.to_string();
id: salt.id, updated_salt.id = salt.id.clone();
}; let updated_hashed_password = hashing::hash_password(&payload.updated_password, &updated_salt_string).unwrap();
let updated_hashed_password = hashing::hash_password(
&payload.updated_password,
&updated_salt_string,
)
.unwrap();
match repo::salt::update_salt(&pool, &salt, &updated_salt.salt).await { match repo::user::update_password(&pool, &user, &updated_hashed_password).await {
Ok(_) => {
match repo::user::update_password(
&pool,
&user,
&updated_hashed_password,
)
.await
{
Ok(_) => { Ok(_) => {
response.message = "Successful".to_string(); response.message = "Successful".to_string();
response.data.push(user.id); response.data.push(user.id);
(axum::http::StatusCode::OK, axum::Json(response)) (axum::http::StatusCode::OK, axum::Json(response))
} }
Err(err) => {
response.message = err.to_string();
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(response),
)
}
}
}
Err(err) => { Err(err) => {
response.message = err.to_string(); response.message = err.to_string();
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) (axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
@@ -373,12 +343,12 @@ pub mod endpoint {
} }
} }
Err(err) => { Err(err) => {
response.message = err.to_string();
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) (axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
} }
} }
} else { } else {
response.message = "Invalid".to_string(); // invalid
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) (axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
} }
} }
+23 -159
View File
@@ -21,7 +21,7 @@ async fn main() {
mod init { mod init {
use axum::{ use axum::{
Router, Router,
routing::{get, patch, post}, routing::{get, post},
}; };
use utoipa::OpenApi; use utoipa::OpenApi;
@@ -38,11 +38,11 @@ mod init {
paths( paths(
common_callers::endpoint::db_ping, common_callers::endpoint::root, common_callers::endpoint::db_ping, common_callers::endpoint::root,
register_caller::register_user, register_caller::register_user,
login_endpoints::login, login_endpoints::update_password, login_endpoints::service_login, login_endpoints::refresh_token login_endpoints::login, login_endpoints::service_login, login_endpoints::refresh_token
), ),
components(schemas(common_callers::response::TestResult, components(schemas(common_callers::response::TestResult,
register_responses::Response, register_responses::Response,
login_responses::Response, login_responses::service_login::Response, login_responses::update_password::Response, login_responses::refresh_token::Response)), login_responses::Response, login_responses::service_login::Response, login_responses::refresh_token::Response)),
tags( tags(
(name = "soaricarus Auth API", description = "Auth API for soaricarus API") (name = "soaricarus Auth API", description = "Auth API for soaricarus API")
) )
@@ -51,20 +51,20 @@ mod init {
mod cors { mod cors {
pub async fn configure_cors() -> tower_http::cors::CorsLayer { pub async fn configure_cors() -> tower_http::cors::CorsLayer {
// Start building the CORS layer with common settings
let cors = tower_http::cors::CorsLayer::new() let cors = tower_http::cors::CorsLayer::new()
.allow_methods([ .allow_methods([
axum::http::Method::GET, axum::http::Method::GET,
axum::http::Method::PATCH,
axum::http::Method::POST, axum::http::Method::POST,
axum::http::Method::PUT, axum::http::Method::PUT,
axum::http::Method::DELETE, axum::http::Method::DELETE,
]) ]) // Specify allowed methods:cite[2]
.allow_headers([ .allow_headers([
axum::http::header::CONTENT_TYPE, axum::http::header::CONTENT_TYPE,
axum::http::header::AUTHORIZATION, axum::http::header::AUTHORIZATION,
]) ]) // Specify allowed headers:cite[2]
.allow_credentials(true) .allow_credentials(true) // If you need to send cookies or authentication headers:cite[2]
.max_age(std::time::Duration::from_secs(3600)); .max_age(std::time::Duration::from_secs(3600)); // Cache the preflight response for 1 hour:cite[2]
// Dynamically set the allowed origin based on the environment // Dynamically set the allowed origin based on the environment
match std::env::var(sienvy::keys::APP_ENV).as_deref() { match std::env::var(sienvy::keys::APP_ENV).as_deref() {
@@ -116,10 +116,6 @@ mod init {
callers::endpoints::LOGIN, callers::endpoints::LOGIN,
post(callers::login::endpoint::login), post(callers::login::endpoint::login),
) )
.route(
callers::endpoints::UPDATE_PASSWORD,
patch(callers::login::endpoint::update_password),
)
.route( .route(
callers::endpoints::SERVICE_LOGIN, callers::endpoints::SERVICE_LOGIN,
post(callers::login::endpoint::service_login), post(callers::login::endpoint::service_login),
@@ -251,27 +247,6 @@ mod tests {
}) })
} }
fn get_test_login_payload(usr: &callers::login::request::Request) -> serde_json::Value {
json!({
"username": &usr.username,
"password": &usr.password,
})
}
pub mod test_data {
use serde_json::json;
pub fn get_updated_password(user_id: &uuid::Uuid) -> serde_json::Value {
json!({
"username": "somethingsss",
"user_id": user_id,
"current_password": "Raindown!",
"updated_password": "SpeakWithUrChest22!",
"confirmed_password": "SpeakWithUrChest22!"
})
}
}
pub mod requests { pub mod requests {
use tower::ServiceExt; // for `call`, `oneshot`, and `ready` use tower::ServiceExt; // for `call`, `oneshot`, and `ready`
@@ -289,36 +264,6 @@ mod tests {
app.clone().oneshot(req).await app.clone().oneshot(req).await
} }
pub async fn login(
app: &axum::Router,
usr: &super::callers::login::request::Request,
) -> Result<axum::response::Response, std::convert::Infallible> {
let payload = super::get_test_login_payload(&usr);
let req = axum::http::Request::builder()
.method(axum::http::Method::POST)
.uri(crate::callers::endpoints::LOGIN)
.header(axum::http::header::CONTENT_TYPE, "application/json")
.body(axum::body::Body::from(payload.to_string()))
.unwrap();
app.clone().oneshot(req).await
}
pub async fn update_password(
app: &axum::Router,
user_id: &uuid::Uuid,
) -> Result<axum::response::Response, std::convert::Infallible> {
let payload = super::test_data::get_updated_password(user_id);
let req = axum::http::Request::builder()
.method(axum::http::Method::PATCH)
.uri(crate::callers::endpoints::UPDATE_PASSWORD)
.header(axum::http::header::CONTENT_TYPE, "application/json")
.body(axum::body::Body::from(payload.to_string()))
.unwrap();
app.clone().oneshot(req).await
}
} }
#[tokio::test] #[tokio::test]
@@ -449,11 +394,22 @@ mod tests {
); );
assert!(returned_usr.date_created.is_some(), "Date Created is empty"); assert!(returned_usr.date_created.is_some(), "Date Created is empty");
let mut n_usr = callers::login::request::Request::default(); let login_payload = json!({
n_usr.username = usr.username.clone(); "username": &usr.username,
n_usr.password = usr.password.clone(); "password": &usr.password,
});
match requests::login(&app, &n_usr).await { match app
.oneshot(
Request::builder()
.method(axum::http::Method::POST)
.uri(callers::endpoints::LOGIN)
.header(axum::http::header::CONTENT_TYPE, "application/json")
.body(Body::from(login_payload.to_string()))
.unwrap(),
)
.await
{
Ok(resp) => { Ok(resp) => {
assert_eq!(StatusCode::OK, resp.status(), "Status is not right"); assert_eq!(StatusCode::OK, resp.status(), "Status is not right");
let body = axum::body::to_bytes(resp.into_body(), usize::MAX) let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
@@ -597,96 +553,4 @@ mod tests {
let _ = db_mgr::drop_database(&tm_pool, &db_name).await; let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
} }
#[tokio::test]
async fn test_update_password() {
let tm_pool = db_mgr::get_pool().await.unwrap();
let db_name = db_mgr::generate_db_name().await;
match db_mgr::create_database(&tm_pool, &db_name).await {
Ok(_) => {
println!("Success");
}
Err(e) => {
assert!(false, "Error: {:?}", e.to_string());
}
}
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
db::init::migrations(&pool).await;
let app = init::routes().await.layer(axum::Extension(pool));
let usr = get_test_register_request();
match requests::register(&app, &usr).await {
Ok(resp) => {
assert_eq!(
resp.status(),
StatusCode::CREATED,
"Message: {:?} {:?}",
resp,
usr.username
);
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let parsed_body: callers::register::response::Response =
serde_json::from_slice(&body).unwrap();
let returned_usr = &parsed_body.data[0];
assert_eq!(false, returned_usr.id.is_nil(), "Id is not populated");
assert_eq!(
usr.username, returned_usr.username,
"Usernames do not match"
);
assert!(returned_usr.date_created.is_some(), "Date Created is empty");
let mut n_usr = callers::login::request::Request::default();
n_usr.username = usr.username.clone();
n_usr.password = usr.password.clone();
match requests::login(&app, &n_usr).await {
Ok(resp) => {
assert_eq!(StatusCode::OK, resp.status(), "Status is not right");
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let parsed_body: callers::login::response::Response =
serde_json::from_slice(&body).unwrap();
let login_result = &parsed_body.data[0];
assert!(!login_result.id.is_nil(), "Id is nil");
match requests::update_password(&app, &login_result.id).await {
Ok(resp) => {
assert_eq!(StatusCode::OK, resp.status(), "Status is not right");
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let parsed_body: callers::login::response::update_password::Response =
serde_json::from_slice(&body).unwrap();
let id = parsed_body.data[0];
assert_eq!(id, login_result.id, "Ids do not match");
}
Err(err) => {
assert!(false, "Error: {err:?}");
}
}
}
Err(err) => {
assert!(false, "Error: {:?}", err.to_string());
}
}
}
Err(err) => {
assert!(false, "Error: {err:?}");
}
}
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
}
} }
+6 -36
View File
@@ -44,29 +44,25 @@ pub mod user {
} }
} }
pub async fn update_password( pub async fn update_password(pool: &sqlx::PgPool, user: &simodels::user::User, password: &str) -> Result<bool, sqlx::Error> {
pool: &sqlx::PgPool,
user: &simodels::user::User,
password: &str,
) -> Result<bool, sqlx::Error> {
match sqlx::query( match sqlx::query(
r#" r#"
UPDATE "user" SET password = $1 WHERE id = $2 UPDATE "user" SET password = $1 WHERE id = $2
"#, "#
) )
.bind(password) .bind(password)
.bind(user.id) .bind(user.id)
.execute(pool) .execute(pool)
.await // .fetch_optional(pool)
{ .await {
Ok(r) => { Ok(r) => {
if r.rows_affected() > 0 { return if r.rows_affected() > 0 {
Ok(true) Ok(true)
} else { } else {
Ok(false) Ok(false)
} }
} }
Err(err) => Err(err), Err(err) => Err(err)
} }
} }
@@ -222,30 +218,4 @@ pub mod salt {
Err(sqlx::Error::RowNotFound) Err(sqlx::Error::RowNotFound)
} }
} }
pub async fn update_salt(
pool: &sqlx::PgPool,
salt: &simodels::user::salt::Salt,
updated_salt: &str,
) -> Result<bool, sqlx::Error> {
match sqlx::query(
r#"
UPDATE "salt" SET salt = $1 WHERE id = $2
"#,
)
.bind(updated_salt)
.bind(salt.id)
.execute(pool)
.await
{
Ok(row) => {
if row.rows_affected() > 0 {
Ok(true)
} else {
Ok(false)
}
}
Err(err) => Err(err),
}
}
} }