diff --git a/.env.docker.sample b/.env.docker.sample index 2b82918..3d5a382 100644 --- a/.env.docker.sample +++ b/.env.docker.sample @@ -7,4 +7,5 @@ DB_AUTH_PORT=5432 DB_AUTH_SSLMODE=disable DATABASE_URL=postgres://${DB_AUTH_USER}:${DB_AUTH_PASSWORD}@${DB_AUTH_HOST}:${DB_AUTH_PORT}/${DB_AUTH_NAME} ENABLE_REGISTRATION=true -ALLOWED_ORIGINS="http://textsender.com" +ALLOWED_ORIGINS="http://textsender.com,http://localhost:5173,http://localhost:9080" +APP_ENV=production diff --git a/.env.local.sample b/.env.local.sample index c76597b..eb17ac6 100644 --- a/.env.local.sample +++ b/.env.local.sample @@ -7,4 +7,5 @@ DB_AUTH_PORT=5432 DB_AUTH_SSLMODE=disable DATABASE_URL=postgres://${DB_AUTH_USER}:${DB_AUTH_PASSWORD}@${DB_AUTH_HOST}:${DB_AUTH_PORT}/${DB_AUTH_NAME} ENABLE_REGISTRATION=true -ALLOWED_ORIGINS="http://textsender.com" +ALLOWED_ORIGINS="http://textsender.com,http://localhost:5173,http://localhost:9080" +APP_ENV=production diff --git a/.gitea/workflows/pr.yml b/.gitea/workflows/pr.yml index 267d91e..db11117 100644 --- a/.gitea/workflows/pr.yml +++ b/.gitea/workflows/pr.yml @@ -14,10 +14,10 @@ jobs: name: Check runs-on: ubuntu-24.04 steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 - uses: actions-rust-lang/setup-rust-toolchain@v1 with: - toolchain: 1.96 + toolchain: 1.96.1 - uses: Swatinem/rust-cache@v2 - run: | mkdir -p ~/.ssh @@ -35,10 +35,10 @@ jobs: name: Rustfmt runs-on: ubuntu-24.04 steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 - uses: actions-rust-lang/setup-rust-toolchain@v1 with: - toolchain: 1.96 + toolchain: 1.96.1 - uses: Swatinem/rust-cache@v2 - run: rustup component add rustfmt - run: | @@ -55,10 +55,10 @@ jobs: name: Clippy runs-on: ubuntu-24.04 steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 - uses: actions-rust-lang/setup-rust-toolchain@v1 with: - toolchain: 1.96 + toolchain: 1.96.1 - uses: Swatinem/rust-cache@v2 - run: rustup component add clippy - run: | diff --git a/.gitea/workflows/workflow.yml b/.gitea/workflows/workflow.yml index ab0a22f..638fc4a 100644 --- a/.gitea/workflows/workflow.yml +++ b/.gitea/workflows/workflow.yml @@ -14,10 +14,10 @@ jobs: name: Check runs-on: ubuntu-24.04 steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 - uses: actions-rust-lang/setup-rust-toolchain@v1 with: - toolchain: 1.96 + toolchain: 1.96.1 - uses: Swatinem/rust-cache@v2 - run: | mkdir -p ~/.ssh @@ -51,10 +51,10 @@ jobs: --health-retries 5 steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 - uses: actions-rust-lang/setup-rust-toolchain@v1 with: - toolchain: 1.96 + toolchain: 1.96.1 - uses: Swatinem/rust-cache@v2 # --- Add this step for explicit verification --- - name: Verify Docker Environment @@ -94,10 +94,10 @@ jobs: name: Rustfmt runs-on: ubuntu-24.04 steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 - uses: actions-rust-lang/setup-rust-toolchain@v1 with: - toolchain: 1.96 + toolchain: 1.96.1 - uses: Swatinem/rust-cache@v2 - run: rustup component add rustfmt - run: | @@ -114,10 +114,10 @@ jobs: name: Clippy runs-on: ubuntu-24.04 steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 - uses: actions-rust-lang/setup-rust-toolchain@v1 with: - toolchain: 1.96 + toolchain: 1.96.1 - uses: Swatinem/rust-cache@v2 - run: rustup component add clippy - run: | @@ -134,10 +134,10 @@ jobs: name: build runs-on: ubuntu-24.04 steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 - uses: actions-rust-lang/setup-rust-toolchain@v1 with: - toolchain: 1.96 + toolchain: 1.96.1 - uses: Swatinem/rust-cache@v2 - run: | mkdir -p ~/.ssh diff --git a/Cargo.lock b/Cargo.lock index 7dfd4e0..0cc4c35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,9 +191,9 @@ checksum = "8ae3f5d315924270530207e2a68396c3cc547f6dca3fbdca317cfb1a51edb593" [[package]] name = "cc" -version = "1.2.65" +version = "1.2.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e228eec9be7c17ccb640b59b36a5cd805ea2a564a4c5e162c2f659fea30d3b96" +checksum = "f5d6cac793997bd970000024b2934968efe83b382de4fdcf4fcb46b6ee4ad996" dependencies = [ "find-msvc-tools", "shlex", @@ -312,18 +312,18 @@ dependencies = [ [[package]] name = "crossbeam-queue" -version = "0.3.12" +version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115" +checksum = "803d13fb3b09d88be9f4dbc29062c66b19bf7170867ceb746d2a8689bf6c7a26" dependencies = [ "crossbeam-utils", ] [[package]] name = "crossbeam-utils" -version = "0.8.21" +version = "0.8.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +checksum = "61803da095bee82a81bb1a452ecc25d3b2f1416d1897eb86430c6159ef717c17" [[package]] name = "crypto-common" @@ -1833,8 +1833,8 @@ dependencies = [ [[package]] name = "textsender_models" -version = "0.5.0" -source = "git+ssh://git@git.kundeng.us/phoenix/textsender_models.git?tag=v0.5.0#137398405fbc61acd352530f8cbfd4d98e648e95" +version = "0.5.1" +source = "git+ssh://git@git.kundeng.us/phoenix/textsender_models.git?tag=v0.5.1#151eeaacf64e255bde56a48df03f4c46ec77c235" dependencies = [ "const_format", "dotenvy", diff --git a/Cargo.toml b/Cargo.toml index e93c034..7de7b76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "textsender_auth" version = "0.2.2" edition = "2024" -rust-version = "1.96" +rust-version = "1.96.1" [dependencies] axum = { version = "0.8.9" } @@ -13,12 +13,12 @@ tracing-subscriber = { version = "0.3.23" } tower-http = { version = "0.7.0", features = ["cors"] } sqlx = { version = "0.9.0", features = ["runtime-tokio", "tls-native-tls", "postgres", "time", "uuid"] } uuid = { version = "1.23.3", features = ["v4", "serde"] } -argon2 = { version = "0.5.3", features = ["std"] } # Use the latest 0.5.x version +argon2 = { version = "0.5.3", features = ["std"] } time = { version = "0.3.53", features = ["macros", "serde"] } josekit = { version = "0.10.3" } utoipa = { version = "5.5.0", features = ["axum_extras"] } utoipa-swagger-ui = { version = "9.0.2", features = ["axum"] } -textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender_models.git", tag = "v0.5.0", features = ["config", "user"] } +textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender_models.git", tag = "v0.5.1", features = ["config", "user"] } [dev-dependencies] tower = { version = "0.5.3", features = ["full"] } diff --git a/Dockerfile b/Dockerfile index 5683cce..f9f29dd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # Stage 1: Build the application # Use a specific Rust version for reproducibility. Choose one that matches your development environment. # Using slim variant for smaller base image -FROM rust:1.96 as builder +FROM rust:1.96.1 as builder # Set the working directory inside the container WORKDIR /usr/src/app diff --git a/src/callers/login.rs b/src/callers/login.rs index 17b5065..fcf80e6 100644 --- a/src/callers/login.rs +++ b/src/callers/login.rs @@ -96,11 +96,19 @@ pub mod response { pub async fn extract( response: axum::response::Response, ) -> Result { - let body = axum::body::to_bytes(response.into_body(), usize::MAX) - .await - .unwrap(); - let _parsed_body: LoginResponse = serde_json::from_slice(&body).unwrap(); - todo!("Add code to convert axum::Response to this type"); + let body = match axum::body::to_bytes(response.into_body(), usize::MAX).await { + Ok(body) => body, + Err(err) => { + return Err(std::io::Error::other(err)); + } + }; + let parsed_body: LoginResponse = match serde_json::from_slice(&body) { + Ok(body) => body, + Err(err) => { + return Err(std::io::Error::other(err)); + } + }; + Ok(parsed_body) } #[derive(Deserialize, Serialize, utoipa::ToSchema)] @@ -194,7 +202,18 @@ pub async fn user_login( // Create token let key = textsender_models::envy::environment::get_secret_key().value; - let cst = token_stuff::create_token(&key, &user.id).unwrap(); + let cst = match token_stuff::create_token(&key, &user.id) { + Ok(token) => token, + Err(_err) => { + return ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response::LoginResponse { + message: String::from("Error creating token"), + data: Vec::new(), + }), + ); + } + }; if token_stuff::verify_token(&key, &cst.access_token) { let current_time = time::OffsetDateTime::now_utc(); diff --git a/src/callers/register.rs b/src/callers/register.rs index c8ce07a..b57127b 100644 --- a/src/callers/register.rs +++ b/src/callers/register.rs @@ -80,7 +80,7 @@ pub fn generate_the_salt() -> ( responses( (status = 201, description = "User created", body = response::Response), (status = 404, description = "User already exists", body = response::Response), - (status = 400, description = "Issue creating user", body = response::Response) + (status = 500, description = "Issue creating user", body = response::Response) ) )] pub async fn register_user( @@ -105,9 +105,7 @@ pub async fn register_user( let mut user = textsender_models::user::User { username: payload.username.clone(), password: payload.password.clone(), - // email: payload.email.clone(), phone_number: payload.phone_number.clone(), - // email_verified: true, ..Default::default() }; diff --git a/src/lib.rs b/src/lib.rs index 536abf1..ec1f3bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,7 +38,6 @@ pub mod init { mod cors { pub async fn configure_cors() -> tower_http::cors::CorsLayer { - // Start building the CORS layer with common settings let cors = tower_http::cors::CorsLayer::new() .allow_methods([ axum::http::Method::GET, @@ -46,11 +45,11 @@ pub mod init { axum::http::Method::POST, axum::http::Method::PUT, axum::http::Method::DELETE, - ]) // Specify allowed methods:cite[2] + ]) .allow_headers([ axum::http::header::CONTENT_TYPE, axum::http::header::AUTHORIZATION, - ]) // Specify allowed headers:cite[2] + ]) .allow_credentials(true) // If you need to send cookies or authentication headers:cite[2] .max_age(std::time::Duration::from_secs(3600)); // Cache the preflight response for 1 hour:cite[2] @@ -93,7 +92,6 @@ pub mod init { } pub async fn routes() -> Router { - // build our application with a route Router::new() .route( callers::endpoints::DBTEST, diff --git a/tests/tests.rs b/tests/tests.rs index ba88e04..118013e 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -9,9 +9,7 @@ mod db_mgr { pub const LIMIT: usize = 6; pub async fn get_pool() -> Result { - let tm_db_url = textsender_models::envy::environment::get_db_url() - .await - .value; + let tm_db_url = textsender_models::envy::environment::get_db_url().value; let tm_options = sqlx::postgres::PgConnectOptions::from_str(&tm_db_url).unwrap(); sqlx::PgPool::connect_with(tm_options).await } @@ -23,9 +21,7 @@ mod db_mgr { } pub async fn connect_to_db(db_name: &str) -> Result { - let db_url = textsender_models::envy::environment::get_db_url() - .await - .value; + let db_url = textsender_models::envy::environment::get_db_url().value; let options = sqlx::postgres::PgConnectOptions::from_str(&db_url)?.database(db_name); sqlx::PgPool::connect_with(options).await } @@ -35,7 +31,10 @@ mod db_mgr { db_name: &str, ) -> Result<(), sqlx::Error> { let create_query = format!("CREATE DATABASE {}", db_name); - match sqlx::query(&create_query).execute(template_pool).await { + match sqlx::query(sqlx::AssertSqlSafe(create_query)) + .execute(template_pool) + .await + { Ok(_) => Ok(()), Err(e) => Err(e), } @@ -47,14 +46,14 @@ mod db_mgr { db_name: &str, ) -> Result<(), sqlx::Error> { let drop_query = format!("DROP DATABASE IF EXISTS {} WITH (FORCE)", db_name); - sqlx::query(&drop_query).execute(template_pool).await?; + sqlx::query(sqlx::AssertSqlSafe(drop_query)) + .execute(template_pool) + .await?; Ok(()) } pub async fn get_database_name() -> Result> { - let database_url = textsender_models::envy::environment::get_db_url() - .await - .value; + let database_url = textsender_models::envy::environment::get_db_url().value; let parsed_url = url::Url::parse(&database_url)?; if parsed_url.scheme() == "postgres" || parsed_url.scheme() == "postgresql" {