Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e82a62dec4 | ||
|
|
14259c6c38 |
No files matched your search
Generated
+29
-28
@@ -188,9 +188,9 @@ checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.10.0"
|
||||
version = "1.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "86e055fa6e3e40ad6187ba25b6855eeb635d5271f8e0f0e1e9a5826b8d75bea0"
|
||||
checksum = "375989decec1a0fa424442bed520fa12ede43a48475ce8c7417d2c69b32b5f6b"
|
||||
dependencies = [
|
||||
"find-msvc-tools",
|
||||
"shlex",
|
||||
@@ -297,9 +297,9 @@ checksum = "217698eaf96b4a3f0bc4f3662aaa55bdf913cd54d7204591faa790070c6d0853"
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.5.3"
|
||||
version = "1.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ba106e844045ddc02c20768ac4b0b7cb45f2cc2ecbd833ac9f5dacb183c2055"
|
||||
checksum = "5f97d3416d5337d15c4b0d78c8c91df922ca143f57ec20e2d1c188333047ef04"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
@@ -321,9 +321,9 @@ checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6"
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.7"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
|
||||
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"typenum",
|
||||
@@ -340,9 +340,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ctutils"
|
||||
version = "0.4.7"
|
||||
version = "0.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2376ffd6f96d8a9db59875b4b6b0237d6900b5baf64279714e05493ce64828a"
|
||||
checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f"
|
||||
dependencies = [
|
||||
"cmov",
|
||||
]
|
||||
@@ -363,7 +363,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
|
||||
dependencies = [
|
||||
"block-buffer 0.10.4",
|
||||
"crypto-common 0.1.7",
|
||||
"crypto-common 0.1.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -576,9 +576,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.7"
|
||||
version = "0.14.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
|
||||
checksum = "4bb6743198531e02858aeaea5398fcc883e71851fcbcb5a2f773e2fb6cb1edf2"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
"version_check",
|
||||
@@ -962,9 +962,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.34"
|
||||
version = "0.4.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6"
|
||||
checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c"
|
||||
|
||||
[[package]]
|
||||
name = "matchit"
|
||||
@@ -990,9 +990,9 @@ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||
|
||||
[[package]]
|
||||
name = "mime"
|
||||
version = "0.3.17"
|
||||
version = "0.3.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
||||
checksum = "d5848f5a9e8117cf94f2f90faac24a87ff81600ff8628c1e3deaab572e76bfa1"
|
||||
|
||||
[[package]]
|
||||
name = "mime_guess"
|
||||
@@ -1086,9 +1086,9 @@ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
||||
|
||||
[[package]]
|
||||
name = "openssl"
|
||||
version = "0.10.81"
|
||||
version = "0.10.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77823a27f0babb03091cb9ed9ef80af3b39dbc82f97e8fa530374b7dafd87a45"
|
||||
checksum = "4de8111e94a56adca7ebfe6c365b6d42f99deb69b74b6becacd4204e2f193aa2"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cfg-if",
|
||||
@@ -1126,9 +1126,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "openssl-sys"
|
||||
version = "0.9.117"
|
||||
version = "0.9.118"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b47e7e6bb2c38cd930d25a23b40fa52e068c10e85f3e03a7f5ba5aaca5713695"
|
||||
checksum = "19c413d6ec9ebaf5e1c829d355cb3e9f783778d700f135be0921d52fce3c60ae"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
@@ -1482,8 +1482,8 @@ checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
||||
|
||||
[[package]]
|
||||
name = "sienvy"
|
||||
version = "0.8.2"
|
||||
source = "git+ssh://git@git.kundeng.us/phoenix/sienvy.git?tag=v0.8.2#492c3e04d1b3f49fcdfe4af71ce0686fa4649526"
|
||||
version = "0.8.3"
|
||||
source = "git+ssh://git@git.kundeng.us/phoenix/sienvy.git?tag=v0.8.3#71544fd2e8d217eac5fc51b14bb062c302465718"
|
||||
dependencies = [
|
||||
"const_format",
|
||||
"dotenvy",
|
||||
@@ -1497,8 +1497,8 @@ checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea"
|
||||
|
||||
[[package]]
|
||||
name = "simodels"
|
||||
version = "0.11.7"
|
||||
source = "git+ssh://git@git.kundeng.us/phoenix/simodels.git?tag=v0.11.7#6022347cb762b413bb59b975619577a45d26383f"
|
||||
version = "0.11.9"
|
||||
source = "git+ssh://git@git.kundeng.us/phoenix/simodels.git?tag=v0.11.9#8e91af657e2e1f124bbba9547ee7e6936f6bb249"
|
||||
dependencies = [
|
||||
"josekit",
|
||||
"rand",
|
||||
@@ -1526,7 +1526,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "soaricarus_auth"
|
||||
version = "0.8.7"
|
||||
version = "0.8.9"
|
||||
dependencies = [
|
||||
"argon2",
|
||||
"axum",
|
||||
@@ -2089,9 +2089,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "unicode-properties"
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7df058c713841ad818f1dc5d3fd88063241cc61f49f5fbea4b951e8cf5a8d71d"
|
||||
checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
@@ -2354,14 +2354,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "zip"
|
||||
version = "9.1.3"
|
||||
version = "9.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1156b39d142a34b999c2310a50455e0cb3760bf7efad0122f796d813bee26ccb"
|
||||
checksum = "3cec733c9820347a592f6375c3c0089b479ba0a0b1cbf3dd1958b02959b7c033"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"flate2",
|
||||
"getrandom",
|
||||
"indexmap",
|
||||
"libc",
|
||||
"memchr",
|
||||
"nix",
|
||||
"typed-path",
|
||||
|
||||
+4
-4
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "soaricarus_auth"
|
||||
version = "0.8.7"
|
||||
version = "0.8.9"
|
||||
edition = "2024"
|
||||
rust-version = "1.95"
|
||||
license = "MIT"
|
||||
@@ -8,7 +8,7 @@ description = "Auth API for soaricarus"
|
||||
|
||||
[dependencies]
|
||||
axum = { version = "0.8.9" }
|
||||
openssl = { version = "0.10.81", features = ["vendored"] }
|
||||
openssl = { version = "0.10.82", features = ["vendored"] }
|
||||
serde = { version = "1.0.229", features = ["derive"] }
|
||||
serde_json = { version = "1.0.154" }
|
||||
tokio = { version = "1.54.1", features = ["rt-multi-thread"] }
|
||||
@@ -23,8 +23,8 @@ josekit = { version = "0.10.3" }
|
||||
utoipa = { version = "6.0.0", features = ["axum_extras", "uuid", "time"] }
|
||||
utoipa-swagger-ui = { version = "10.0.2", features = ["axum"] }
|
||||
rand = { version = "0.10.3" }
|
||||
simodels = { git = "ssh://git@git.kundeng.us/phoenix/simodels.git", tag = "v0.11.7" }
|
||||
sienvy = { git = "ssh://git@git.kundeng.us/phoenix/sienvy.git", tag = "v0.8.2" }
|
||||
simodels = { git = "ssh://git@git.kundeng.us/phoenix/simodels.git", tag = "v0.11.9", features = ["auth"] }
|
||||
sienvy = { git = "ssh://git@git.kundeng.us/phoenix/sienvy.git", tag = "v0.8.3" }
|
||||
|
||||
[dev-dependencies]
|
||||
http-body-util = { version = "0.1.5" }
|
||||
|
||||
+108
-3
@@ -43,6 +43,23 @@ pub mod request {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod update_user_profile {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, utoipa::ToSchema)]
|
||||
pub struct UpdateUserProfileRequest {
|
||||
pub user_id: uuid::Uuid,
|
||||
pub firstname: String,
|
||||
pub lastname: String,
|
||||
}
|
||||
|
||||
impl UpdateUserProfileRequest {
|
||||
pub fn is_valid(&self) -> bool {
|
||||
!self.user_id.is_nil() && !self.firstname.is_empty() && !self.lastname.is_empty()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
@@ -89,6 +106,16 @@ pub mod response {
|
||||
pub data: Vec<simodels::user::UserProfile>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod update_user_profile {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||
pub struct UpdateUserProfileResponse {
|
||||
pub message: String,
|
||||
pub data: Vec<simodels::user::UserProfile>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Module for login endpoints
|
||||
@@ -151,7 +178,7 @@ pub mod endpoint {
|
||||
id: user.id,
|
||||
username: user.username.clone(),
|
||||
token: token_literal,
|
||||
token_type: String::from(simodels::token::TOKEN_TYPE),
|
||||
token_type: simodels::constants::token::TOKEN_TYPE.to_string(),
|
||||
expiration: duration,
|
||||
}],
|
||||
}),
|
||||
@@ -203,7 +230,7 @@ pub mod endpoint {
|
||||
id,
|
||||
username,
|
||||
token: token_literal,
|
||||
token_type: String::from(simodels::token::TOKEN_TYPE),
|
||||
token_type: simodels::constants::token::TOKEN_TYPE.to_string(),
|
||||
expiration: duration,
|
||||
};
|
||||
|
||||
@@ -262,7 +289,8 @@ pub mod endpoint {
|
||||
id,
|
||||
token: access_token,
|
||||
expiration: exp_dur,
|
||||
token_type: String::from(simodels::token::TOKEN_TYPE),
|
||||
token_type: simodels::constants::token::TOKEN_TYPE
|
||||
.to_string(),
|
||||
username,
|
||||
};
|
||||
response.message = String::from("Successful");
|
||||
@@ -445,4 +473,81 @@ pub mod endpoint {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
put,
|
||||
path = super::super::endpoints::UPDATE_USER_PROFILE,
|
||||
request_body(
|
||||
content = super::request::update_user_profile::UpdateUserProfileRequest,
|
||||
description = "Update user profile",
|
||||
content_type = "application/json"
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "User profile updated successfully", body = super::response::update_user_profile::UpdateUserProfileResponse),
|
||||
(status = 400, description = "Invalid", body = super::response::update_user_profile::UpdateUserProfileResponse),
|
||||
(status = 500, description = "Failure", body = super::response::update_user_profile::UpdateUserProfileResponse)
|
||||
)
|
||||
)]
|
||||
pub async fn update_user_profile(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::Json(payload): axum::Json<
|
||||
super::request::update_user_profile::UpdateUserProfileRequest,
|
||||
>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<super::response::update_user_profile::UpdateUserProfileResponse>,
|
||||
) {
|
||||
let mut response =
|
||||
super::response::update_user_profile::UpdateUserProfileResponse::default();
|
||||
if !payload.is_valid() {
|
||||
response.message = "Invalid".to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
} else {
|
||||
let user_id = payload.user_id;
|
||||
match repo::user::get_with_id(&pool, &user_id).await {
|
||||
Ok(user) => {
|
||||
let firstname = payload.firstname;
|
||||
let lastname = payload.lastname;
|
||||
|
||||
if firstname == user.firstname && lastname == user.lastname {
|
||||
(axum::http::StatusCode::NOT_MODIFIED, axum::Json(response))
|
||||
} else {
|
||||
match repo::user::update_user_profile(
|
||||
&pool, &user_id, &firstname, &lastname,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {
|
||||
let user_profile = simodels::user::UserProfile {
|
||||
firstname,
|
||||
lastname,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
};
|
||||
response.message = "SUCCESSFUL".to_string();
|
||||
response.data.push(user_profile);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = "ERROR - Updating user profile".to_string();
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
response.message = "ERROR".to_string();
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,4 +11,5 @@ pub mod endpoints {
|
||||
pub const SERVICE_LOGIN: &str = "/api/v2/service/login";
|
||||
pub const REFRESH_TOKEN: &str = "/api/v2/token/refresh";
|
||||
pub const GET_USER_PROFILE: &str = "/api/v2/user/profile/{id}";
|
||||
pub const UPDATE_USER_PROFILE: &str = "/api/v2/user/profile/update";
|
||||
}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
pub mod callers;
|
||||
pub mod config;
|
||||
pub mod db;
|
||||
pub mod hashing;
|
||||
pub mod repo;
|
||||
pub mod token_stuff;
|
||||
|
||||
pub mod init {
|
||||
use axum::{
|
||||
Router,
|
||||
routing::{get, patch, post, put},
|
||||
};
|
||||
use utoipa::OpenApi;
|
||||
|
||||
use super::callers;
|
||||
use callers::common as common_callers;
|
||||
use callers::login as login_caller;
|
||||
use callers::register as register_caller;
|
||||
use login_caller::endpoint as login_endpoints;
|
||||
use login_caller::response as login_responses;
|
||||
use register_caller::response as register_responses;
|
||||
|
||||
#[derive(utoipa::OpenApi)]
|
||||
#[openapi(
|
||||
paths(
|
||||
common_callers::endpoint::db_ping, common_callers::endpoint::root,
|
||||
register_caller::register_user,
|
||||
login_endpoints::login, login_endpoints::update_password, login_endpoints::service_login, login_endpoints::refresh_token, login_endpoints::get_user_profile,
|
||||
),
|
||||
components(schemas(common_callers::response::TestResult,
|
||||
register_responses::Response,
|
||||
login_responses::Response, login_responses::service_login::Response, login_responses::update_password::Response, login_responses::refresh_token::Response, login_responses::get_user_profile::GetUserProfileResponse)),
|
||||
tags(
|
||||
(name = "soaricarus Auth API", description = "Auth API for soaricarus API")
|
||||
)
|
||||
)]
|
||||
struct ApiDoc;
|
||||
|
||||
mod cors {
|
||||
pub async fn configure_cors() -> tower_http::cors::CorsLayer {
|
||||
let cors = tower_http::cors::CorsLayer::new()
|
||||
.allow_methods([
|
||||
axum::http::Method::GET,
|
||||
axum::http::Method::PATCH,
|
||||
axum::http::Method::POST,
|
||||
axum::http::Method::PUT,
|
||||
axum::http::Method::DELETE,
|
||||
])
|
||||
.allow_headers([
|
||||
axum::http::header::CONTENT_TYPE,
|
||||
axum::http::header::AUTHORIZATION,
|
||||
])
|
||||
.allow_credentials(true)
|
||||
.max_age(std::time::Duration::from_secs(3600));
|
||||
|
||||
// Dynamically set the allowed origin based on the environment
|
||||
match std::env::var(sienvy::keys::APP_ENV).as_deref() {
|
||||
Ok("production") => {
|
||||
let allowed_origins_env = sienvy::environment::get_allowed_origins();
|
||||
match sienvy::utility::delimitize(&allowed_origins_env) {
|
||||
Ok(alwd) => {
|
||||
let allowed_origins: Vec<axum::http::HeaderValue> = alwd
|
||||
.into_iter()
|
||||
.map(|s| s.parse::<axum::http::HeaderValue>().unwrap())
|
||||
.collect();
|
||||
cors.allow_origin(allowed_origins)
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!(
|
||||
"Could not parse out allowed origins from env: Error: {err:?}"
|
||||
);
|
||||
std::process::exit(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// Development (default): Allow localhost origins
|
||||
cors.allow_origin(vec![
|
||||
"http://localhost:4200".parse().unwrap(),
|
||||
"http://127.0.0.1:4200".parse().unwrap(),
|
||||
])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn routes() -> Router {
|
||||
// build our application with a route
|
||||
Router::new()
|
||||
.route(
|
||||
callers::endpoints::DBTEST,
|
||||
get(callers::common::endpoint::db_ping),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::ROOT,
|
||||
get(callers::common::endpoint::root),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::REGISTER,
|
||||
post(callers::register::register_user),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::LOGIN,
|
||||
post(callers::login::endpoint::login),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::UPDATE_PASSWORD,
|
||||
patch(callers::login::endpoint::update_password),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::SERVICE_LOGIN,
|
||||
post(callers::login::endpoint::service_login),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::REFRESH_TOKEN,
|
||||
post(callers::login::endpoint::refresh_token),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::GET_USER_PROFILE,
|
||||
get(callers::login::endpoint::get_user_profile),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::UPDATE_USER_PROFILE,
|
||||
put(callers::login::endpoint::update_user_profile),
|
||||
)
|
||||
.layer(cors::configure_cors().await)
|
||||
}
|
||||
|
||||
pub async fn app() -> Router {
|
||||
let pool = super::db::init::create_pool()
|
||||
.await
|
||||
.expect("Failed to create pool");
|
||||
|
||||
super::db::init::migrations(&pool).await;
|
||||
|
||||
routes()
|
||||
.await
|
||||
.merge(
|
||||
utoipa_swagger_ui::SwaggerUi::new("/swagger-ui")
|
||||
.url("/api-docs/openapi.json", ApiDoc::openapi()),
|
||||
)
|
||||
.layer(axum::Extension(pool))
|
||||
}
|
||||
}
|
||||
+141
-146
@@ -1,9 +1,4 @@
|
||||
pub mod callers;
|
||||
pub mod config;
|
||||
pub mod db;
|
||||
pub mod hashing;
|
||||
pub mod repo;
|
||||
pub mod token_stuff;
|
||||
use soaricarus_auth::{config, init};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -18,143 +13,11 @@ async fn main() {
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
mod init {
|
||||
use axum::{
|
||||
Router,
|
||||
routing::{get, patch, post},
|
||||
};
|
||||
use utoipa::OpenApi;
|
||||
|
||||
use super::callers;
|
||||
use callers::common as common_callers;
|
||||
use callers::login as login_caller;
|
||||
use callers::register as register_caller;
|
||||
use login_caller::endpoint as login_endpoints;
|
||||
use login_caller::response as login_responses;
|
||||
use register_caller::response as register_responses;
|
||||
|
||||
#[derive(utoipa::OpenApi)]
|
||||
#[openapi(
|
||||
paths(
|
||||
common_callers::endpoint::db_ping, common_callers::endpoint::root,
|
||||
register_caller::register_user,
|
||||
login_endpoints::login, login_endpoints::update_password, login_endpoints::service_login, login_endpoints::refresh_token, login_endpoints::get_user_profile,
|
||||
),
|
||||
components(schemas(common_callers::response::TestResult,
|
||||
register_responses::Response,
|
||||
login_responses::Response, login_responses::service_login::Response, login_responses::update_password::Response, login_responses::refresh_token::Response, login_responses::get_user_profile::GetUserProfileResponse)),
|
||||
tags(
|
||||
(name = "soaricarus Auth API", description = "Auth API for soaricarus API")
|
||||
)
|
||||
)]
|
||||
struct ApiDoc;
|
||||
|
||||
mod cors {
|
||||
pub async fn configure_cors() -> tower_http::cors::CorsLayer {
|
||||
let cors = tower_http::cors::CorsLayer::new()
|
||||
.allow_methods([
|
||||
axum::http::Method::GET,
|
||||
axum::http::Method::PATCH,
|
||||
axum::http::Method::POST,
|
||||
axum::http::Method::PUT,
|
||||
axum::http::Method::DELETE,
|
||||
])
|
||||
.allow_headers([
|
||||
axum::http::header::CONTENT_TYPE,
|
||||
axum::http::header::AUTHORIZATION,
|
||||
])
|
||||
.allow_credentials(true)
|
||||
.max_age(std::time::Duration::from_secs(3600));
|
||||
|
||||
// Dynamically set the allowed origin based on the environment
|
||||
match std::env::var(sienvy::keys::APP_ENV).as_deref() {
|
||||
Ok("production") => {
|
||||
let allowed_origins_env = sienvy::environment::get_allowed_origins();
|
||||
match sienvy::utility::delimitize(&allowed_origins_env) {
|
||||
Ok(alwd) => {
|
||||
let allowed_origins: Vec<axum::http::HeaderValue> = alwd
|
||||
.into_iter()
|
||||
.map(|s| s.parse::<axum::http::HeaderValue>().unwrap())
|
||||
.collect();
|
||||
cors.allow_origin(allowed_origins)
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!(
|
||||
"Could not parse out allowed origins from env: Error: {err:?}"
|
||||
);
|
||||
std::process::exit(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// Development (default): Allow localhost origins
|
||||
cors.allow_origin(vec![
|
||||
"http://localhost:4200".parse().unwrap(),
|
||||
"http://127.0.0.1:4200".parse().unwrap(),
|
||||
])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn routes() -> Router {
|
||||
// build our application with a route
|
||||
Router::new()
|
||||
.route(
|
||||
callers::endpoints::DBTEST,
|
||||
get(callers::common::endpoint::db_ping),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::ROOT,
|
||||
get(callers::common::endpoint::root),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::REGISTER,
|
||||
post(callers::register::register_user),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::LOGIN,
|
||||
post(callers::login::endpoint::login),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::UPDATE_PASSWORD,
|
||||
patch(callers::login::endpoint::update_password),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::SERVICE_LOGIN,
|
||||
post(callers::login::endpoint::service_login),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::REFRESH_TOKEN,
|
||||
post(callers::login::endpoint::refresh_token),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::GET_USER_PROFILE,
|
||||
get(callers::login::endpoint::get_user_profile),
|
||||
)
|
||||
.layer(cors::configure_cors().await)
|
||||
}
|
||||
|
||||
pub async fn app() -> Router {
|
||||
let pool = super::db::init::create_pool()
|
||||
.await
|
||||
.expect("Failed to create pool");
|
||||
|
||||
super::db::init::migrations(&pool).await;
|
||||
|
||||
routes()
|
||||
.await
|
||||
.merge(
|
||||
utoipa_swagger_ui::SwaggerUi::new("/swagger-ui")
|
||||
.url("/api-docs/openapi.json", ApiDoc::openapi()),
|
||||
)
|
||||
.layer(axum::Extension(pool))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use soaricarus_auth;
|
||||
use soaricarus_auth::{callers, db};
|
||||
|
||||
use axum::{
|
||||
body::Body,
|
||||
@@ -265,6 +128,9 @@ mod tests {
|
||||
pub mod test_data {
|
||||
use serde_json::json;
|
||||
|
||||
pub const UPDATED_FIRSTNAME: &str = "Bolt";
|
||||
pub const UPDATED_LASTNAME: &str = "Thunder";
|
||||
|
||||
pub fn get_updated_password(user_id: &uuid::Uuid) -> serde_json::Value {
|
||||
json!({
|
||||
"username": "somethingsss",
|
||||
@@ -274,9 +140,22 @@ mod tests {
|
||||
"confirmed_password": "SpeakWithUrChest22!"
|
||||
})
|
||||
}
|
||||
|
||||
pub fn update_user_profile(
|
||||
user_id: &uuid::Uuid,
|
||||
firstname: &str,
|
||||
lastname: &str,
|
||||
) -> serde_json::Value {
|
||||
json!({
|
||||
"firstname": firstname,
|
||||
"lastname": lastname,
|
||||
"user_id": user_id
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub mod requests {
|
||||
use soaricarus_auth::callers;
|
||||
use tower::ServiceExt; // for `call`, `oneshot`, and `ready`
|
||||
|
||||
pub async fn register(
|
||||
@@ -286,7 +165,7 @@ mod tests {
|
||||
let payload = super::get_test_register_payload(&usr);
|
||||
let req = axum::http::Request::builder()
|
||||
.method(axum::http::Method::POST)
|
||||
.uri(crate::callers::endpoints::REGISTER)
|
||||
.uri(callers::endpoints::REGISTER)
|
||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||
.body(axum::body::Body::from(payload.to_string()))
|
||||
.unwrap();
|
||||
@@ -301,7 +180,7 @@ mod tests {
|
||||
let payload = super::get_test_login_payload(&usr);
|
||||
let req = axum::http::Request::builder()
|
||||
.method(axum::http::Method::POST)
|
||||
.uri(crate::callers::endpoints::LOGIN)
|
||||
.uri(callers::endpoints::LOGIN)
|
||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||
.body(axum::body::Body::from(payload.to_string()))
|
||||
.unwrap();
|
||||
@@ -316,7 +195,7 @@ mod tests {
|
||||
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)
|
||||
.uri(callers::endpoints::UPDATE_PASSWORD)
|
||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||
.body(axum::body::Body::from(payload.to_string()))
|
||||
.unwrap();
|
||||
@@ -328,7 +207,7 @@ mod tests {
|
||||
app: &axum::Router,
|
||||
user_id: &uuid::Uuid,
|
||||
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||
let raw_uri = String::from(crate::callers::endpoints::GET_USER_PROFILE);
|
||||
let raw_uri = String::from(callers::endpoints::GET_USER_PROFILE);
|
||||
let end_index = raw_uri.len() - 4;
|
||||
|
||||
let uri = format!("{}{user_id}", (&raw_uri[..end_index]).to_string());
|
||||
@@ -340,6 +219,28 @@ mod tests {
|
||||
.unwrap();
|
||||
app.clone().oneshot(req).await
|
||||
}
|
||||
|
||||
pub async fn update_user_profile(
|
||||
app: &axum::Router,
|
||||
user_id: &uuid::Uuid,
|
||||
firstname: &str,
|
||||
lastname: &str,
|
||||
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||
let payload = super::test_data::update_user_profile(user_id, firstname, lastname);
|
||||
let req = axum::http::Request::builder()
|
||||
.method(axum::http::Method::PUT)
|
||||
.uri(callers::endpoints::UPDATE_USER_PROFILE)
|
||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||
.body(axum::body::Body::from(payload.to_string()))
|
||||
.unwrap();
|
||||
|
||||
app.clone().oneshot(req).await
|
||||
}
|
||||
}
|
||||
|
||||
pub mod responses {
|
||||
pub use soaricarus_auth::callers::login::response as login_responses;
|
||||
pub use soaricarus_auth::callers::register::response as register_responses;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -575,7 +476,7 @@ mod tests {
|
||||
let id = uuid::Uuid::parse_str("22f9c775-cce9-457a-a147-9dafbb801f61").unwrap();
|
||||
let key = sienvy::environment::get_secret_key().value;
|
||||
|
||||
match token_stuff::create_service_token(&key, &id) {
|
||||
match soaricarus_auth::token_stuff::create_service_token(&key, &id) {
|
||||
Ok((token, _expire)) => {
|
||||
let payload = serde_json::json!({
|
||||
"access_token": token
|
||||
@@ -771,7 +672,8 @@ mod tests {
|
||||
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
|
||||
.await
|
||||
.unwrap();
|
||||
let parsed_body: callers::login::response::get_user_profile::GetUserProfileResponse =
|
||||
|
||||
let parsed_body: responses::login_responses::get_user_profile::GetUserProfileResponse =
|
||||
serde_json::from_slice(&body).unwrap();
|
||||
let user_profile = &parsed_body.data[0];
|
||||
assert!(
|
||||
@@ -791,4 +693,97 @@ mod tests {
|
||||
|
||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_update_user_profile() {
|
||||
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();
|
||||
|
||||
let response = requests::register(&app, &usr).await;
|
||||
|
||||
match response {
|
||||
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: responses::register_responses::Response =
|
||||
serde_json::from_slice(&body).unwrap();
|
||||
let returned_usr = &parsed_body.data[0];
|
||||
|
||||
let user_id = returned_usr.id;
|
||||
assert_eq!(false, user_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 updated_firstname = test_data::UPDATED_FIRSTNAME;
|
||||
let updated_lastname = test_data::UPDATED_LASTNAME;
|
||||
|
||||
match requests::update_user_profile(
|
||||
&app,
|
||||
&user_id,
|
||||
&updated_firstname,
|
||||
&updated_lastname,
|
||||
)
|
||||
.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: responses::login_responses::update_user_profile::UpdateUserProfileResponse =
|
||||
serde_json::from_slice(&body).unwrap();
|
||||
let user_profile = &parsed_body.data[0];
|
||||
|
||||
assert_eq!(
|
||||
updated_firstname, user_profile.firstname,
|
||||
"Firstname has not been updated"
|
||||
);
|
||||
assert_eq!(
|
||||
updated_lastname, user_profile.lastname,
|
||||
"Lastname has not been updated"
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err.to_string());
|
||||
}
|
||||
};
|
||||
|
||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||
}
|
||||
}
|
||||
@@ -132,6 +132,34 @@ pub mod user {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_user_profile(
|
||||
pool: &sqlx::PgPool,
|
||||
id: &uuid::Uuid,
|
||||
firstname: &str,
|
||||
lastname: &str,
|
||||
) -> Result<bool, sqlx::Error> {
|
||||
match sqlx::query(
|
||||
r#"
|
||||
UPDATE "user" SET firstname = $1, lastname = $2 WHERE id = $3
|
||||
"#,
|
||||
)
|
||||
.bind(firstname)
|
||||
.bind(lastname)
|
||||
.bind(id)
|
||||
.execute(pool)
|
||||
.await
|
||||
{
|
||||
Ok(r) => {
|
||||
if r.rows_affected() > 0 {
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_last_login(
|
||||
pool: &sqlx::PgPool,
|
||||
user: &simodels::user::User,
|
||||
|
||||
Reference in new issue
Block a user