diff --git a/CHANGELOG.md b/CHANGELOG.md index 573b57365..e3ef9ebfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog ## vNext + +## v0.5.0 + * refactor auth middleware for supporting bearer, cookie and query. [https://github.com/loco-rs/loco/pull/560](https://github.com/loco-rs/loco/pull/560) * SeaORM upgraded: `rc1` -> `rc4`. [https://github.com/loco-rs/loco/pull/585](https://github.com/loco-rs/loco/pull/585) * Adding Cache to app content. [https://github.com/loco-rs/loco/pull/570](https://github.com/loco-rs/loco/pull/570) diff --git a/Cargo.toml b/Cargo.toml index cb39e83b2..c13a6d331 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ license = "Apache-2.0" [package] name = "loco-rs" -version = "0.4.0" +version = "0.5.0" description = "The one-person framework for Rust" homepage = "https://loco.rs/" documentation = "https://docs.rs/loco-rs" diff --git a/loco-extras/Cargo.toml b/loco-extras/Cargo.toml index 31172c44c..6eeb1b0de 100644 --- a/loco-extras/Cargo.toml +++ b/loco-extras/Cargo.toml @@ -20,18 +20,26 @@ tower = { workspace = true, optional = true } tower-http = { workspace = true, optional = true, features = [ "normalize-path", ] } -opentelemetry = { version = "0.22" , optional = true} -opentelemetry-otlp = { version = "0.15", optional = true, features = ["grpc-tonic"] } +opentelemetry = { version = "0.22", optional = true } +opentelemetry-otlp = { version = "0.15", optional = true, features = [ + "grpc-tonic", +] } axum-tracing-opentelemetry = { version = "0.18", optional = true } -init-tracing-opentelemetry = { version = "0.18", optional = true , features = ["otlp", "tracing_subscriber_ext"] } +init-tracing-opentelemetry = { version = "0.18", optional = true, features = [ + "otlp", + "tracing_subscriber_ext", +] } tracing-opentelemetry-instrumentation-sdk = { version = "0.18", optional = true } -tracing-subscriber = { version = "0.3.18", optional = true , features = ["env-filter", "json"] } +tracing-subscriber = { version = "0.3.18", optional = true, features = [ + "env-filter", + "json", +] } tracing = { version = "0.1.40", optional = true } mongodb = { version = "2.8.0", optional = true } [dependencies.loco-rs] path = "../" -version = "0.4.0" +version = "0.5.0" default-features = true features = ["with-db", "auth_jwt"] @@ -50,6 +58,13 @@ initializer-prometheus = ["dep:axum-prometheus"] initializer-extra-db = [] initializer-multi-db = ["dep:serde_json"] initializer-normalize-path = ["dep:tower", "dep:tower-http"] -initializer-opentelemetry = ["dep:opentelemetry","dep:opentelemetry-otlp","dep:axum-tracing-opentelemetry", - "dep:init-tracing-opentelemetry", "dep:tracing-opentelemetry-instrumentation-sdk","dep:tracing-subscriber","dep:tracing"] +initializer-opentelemetry = [ + "dep:opentelemetry", + "dep:opentelemetry-otlp", + "dep:axum-tracing-opentelemetry", + "dep:init-tracing-opentelemetry", + "dep:tracing-opentelemetry-instrumentation-sdk", + "dep:tracing-subscriber", + "dep:tracing", +] initializer-mongodb = ["dep:mongodb", "dep:serde", "dep:serde_json"] diff --git a/src/boot.rs b/src/boot.rs index e1289c692..9205571ba 100644 --- a/src/boot.rs +++ b/src/boot.rs @@ -241,8 +241,8 @@ pub async fn create_app( ) -> Result { let app_context = create_context::(environment).await?; - if let Some(pool) = &app_context.redis { - redis::converge(pool, &app_context.config.redis).await?; + if let Some(pool) = &app_context.queue { + redis::converge(pool, &app_context.config.queue).await?; } run_app::(&mode, app_context).await diff --git a/starters/lightweight-service/Cargo.lock b/starters/lightweight-service/Cargo.lock index 377cfd388..ce57e0bb7 100644 --- a/starters/lightweight-service/Cargo.lock +++ b/starters/lightweight-service/Cargo.lock @@ -1477,9 +1477,7 @@ dependencies = [ [[package]] name = "loco-rs" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9cb93effcc77153e538b4ba3bba72b0c6ffe6b862c47e986fc2aa2684586844" +version = "0.5.0" dependencies = [ "argon2", "async-trait", diff --git a/starters/lightweight-service/Cargo.toml b/starters/lightweight-service/Cargo.toml index 18303e69e..6606a3bdb 100644 --- a/starters/lightweight-service/Cargo.toml +++ b/starters/lightweight-service/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" [dependencies] -loco-rs = { version = "0.4.0", default-features = false, features = ["cli"] } +loco-rs = { version = "0.5.0", default-features = false, features = ["cli"] } serde = "*" serde_json = "*" eyre = "*" @@ -28,7 +28,7 @@ required-features = [] [dev-dependencies] serial_test = "*" rstest = "*" -loco-rs = { version = "0.4.0", default-features = false, features = [ +loco-rs = { version = "0.5.0", default-features = false, features = [ "testing", "cli", ] } diff --git a/starters/rest-api/Cargo.lock b/starters/rest-api/Cargo.lock index 3063012bf..1075a78d2 100644 --- a/starters/rest-api/Cargo.lock +++ b/starters/rest-api/Cargo.lock @@ -3391,9 +3391,9 @@ dependencies = [ [[package]] name = "sea-orm" -version = "1.0.0-rc.3" +version = "1.0.0-rc.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d5a485429878b8f6eb9ec1c99e91bf4e9055d445be71218faed5847c2e40dba" +checksum = "b467731e7b3e5c7581291c030a0574edbb346b5f3fba9297130416a7178b2385" dependencies = [ "async-stream", "async-trait", @@ -3419,9 +3419,9 @@ dependencies = [ [[package]] name = "sea-orm-cli" -version = "1.0.0-rc.3" +version = "1.0.0-rc.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7fc1c75b5075c7bc2aa41518731e93408f3a158dfb8714e747430734934a901" +checksum = "e4d9173b3768a8cd39e4c6ddb50f3d0bca7907aad0f7c9c8b9db4dad70740535" dependencies = [ "chrono", "clap", @@ -3436,9 +3436,9 @@ dependencies = [ [[package]] name = "sea-orm-macros" -version = "1.0.0-rc.3" +version = "1.0.0-rc.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f303a2b29c6d92a669652fa7776e6590df7118e05d4f6bf31d48127b06cb21a" +checksum = "a55f66b1b0caf9bb64f01ceca74126a34842396c7678b0d178ad0acfa8c12412" dependencies = [ "heck 0.4.1", "proc-macro2", @@ -3450,9 +3450,9 @@ dependencies = [ [[package]] name = "sea-orm-migration" -version = "1.0.0-rc.3" +version = "1.0.0-rc.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e8b13ba1f4308423946530237d5b46f8bef85aec2463686c4eb1e45d87ce9ef" +checksum = "9ce5df1fc1cb6e6d0aeaef6d306ffa4642f9b8dbb25cfed45f844d501d697460" dependencies = [ "async-trait", "clap", diff --git a/starters/rest-api/Cargo.toml b/starters/rest-api/Cargo.toml index 31dd0e7f1..2297bcd94 100644 --- a/starters/rest-api/Cargo.toml +++ b/starters/rest-api/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" [dependencies] -loco-rs = { version = "0.4.0" } +loco-rs = { version = "0.5.0" } migration = { path = "migration" } serde = { version = "1", features = ["derive"] } @@ -40,5 +40,5 @@ required-features = [] [dev-dependencies] serial_test = "2.0.0" rstest = "0.18.2" -loco-rs = { version = "0.4.0", features = ["testing"] } +loco-rs = { version = "0.5.0", features = ["testing"] } insta = { version = "1.34.0", features = ["redactions", "yaml", "filters"] } diff --git a/starters/rest-api/config/development.yaml b/starters/rest-api/config/development.yaml index 06ae9ffb6..8e5e37b6b 100644 --- a/starters/rest-api/config/development.yaml +++ b/starters/rest-api/config/development.yaml @@ -111,8 +111,8 @@ database: # Recreating schema when application loaded. This is a dangerous operation, make sure that you using this flag only on dev environments or test mode dangerously_recreate: false -# Redis Configuration -redis: +# Queue Configuration +queue: # Redis connection URI uri: {{ get_env(name="REDIS_URL", default="redis://127.0.0.1") }} # Dangerously flush all data in Redis on startup. dangerous operation, make sure that you using this flag only on dev environments or test mode diff --git a/starters/rest-api/config/test.yaml b/starters/rest-api/config/test.yaml index b391cd0f5..21be0d166 100644 --- a/starters/rest-api/config/test.yaml +++ b/starters/rest-api/config/test.yaml @@ -108,8 +108,8 @@ database: # Recreating schema when application loaded. This is a dangerous operation, make sure that you using this flag only on dev environments or test mode dangerously_recreate: false -# Redis Configuration -redis: +# Queue Configuration +queue: # Redis connection URI uri: {{get_env(name="REDIS_URL", default="redis://127.0.0.1")}} # Dangerously flush all data in Redis on startup. dangerous operation, make sure that you using this flag only on dev environments or test mode diff --git a/starters/rest-api/migration/Cargo.toml b/starters/rest-api/migration/Cargo.toml index 23e6c9b54..40593328f 100644 --- a/starters/rest-api/migration/Cargo.toml +++ b/starters/rest-api/migration/Cargo.toml @@ -10,7 +10,7 @@ path = "src/lib.rs" [dependencies] async-std = { version = "1", features = ["attributes", "tokio1"] } -loco-rs = { version = "0.4.0" } +loco-rs = { version = "0.5.0" } [dependencies.sea-orm-migration] version = "1.0.0-rc.4" diff --git a/starters/rest-api/src/models/users.rs b/starters/rest-api/src/models/users.rs index b9c2ec62b..fb5785201 100644 --- a/starters/rest-api/src/models/users.rs +++ b/starters/rest-api/src/models/users.rs @@ -58,7 +58,11 @@ impl ActiveModelBehavior for super::_entities::users::ActiveModel { impl Authenticable for super::_entities::users::Model { async fn find_by_api_key(db: &DatabaseConnection, api_key: &str) -> ModelResult { let user = users::Entity::find() - .filter(users::Column::ApiKey.eq(api_key)) + .filter( + model::query::condition() + .eq(users::Column::ApiKey, api_key) + .build(), + ) .one(db) .await?; user.ok_or_else(|| ModelError::EntityNotFound) @@ -77,7 +81,11 @@ impl super::_entities::users::Model { /// When could not find user by the given token or DB query error pub async fn find_by_email(db: &DatabaseConnection, email: &str) -> ModelResult { let user = users::Entity::find() - .filter(users::Column::Email.eq(email)) + .filter( + model::query::condition() + .eq(users::Column::Email, email) + .build(), + ) .one(db) .await?; user.ok_or_else(|| ModelError::EntityNotFound) @@ -93,7 +101,11 @@ impl super::_entities::users::Model { token: &str, ) -> ModelResult { let user = users::Entity::find() - .filter(users::Column::EmailVerificationToken.eq(token)) + .filter( + model::query::condition() + .eq(users::Column::EmailVerificationToken, token) + .build(), + ) .one(db) .await?; user.ok_or_else(|| ModelError::EntityNotFound) @@ -106,7 +118,11 @@ impl super::_entities::users::Model { /// When could not find user by the given token or DB query error pub async fn find_by_reset_token(db: &DatabaseConnection, token: &str) -> ModelResult { let user = users::Entity::find() - .filter(users::Column::ResetToken.eq(token)) + .filter( + model::query::condition() + .eq(users::Column::ResetToken, token) + .build(), + ) .one(db) .await?; user.ok_or_else(|| ModelError::EntityNotFound) @@ -120,7 +136,11 @@ impl super::_entities::users::Model { pub async fn find_by_pid(db: &DatabaseConnection, pid: &str) -> ModelResult { let parse_uuid = Uuid::parse_str(pid).map_err(|e| ModelError::Any(e.into()))?; let user = users::Entity::find() - .filter(users::Column::Pid.eq(parse_uuid)) + .filter( + model::query::condition() + .eq(users::Column::Pid, parse_uuid) + .build(), + ) .one(db) .await?; user.ok_or_else(|| ModelError::EntityNotFound) @@ -133,7 +153,11 @@ impl super::_entities::users::Model { /// When could not find user by the given token or DB query error pub async fn find_by_api_key(db: &DatabaseConnection, api_key: &str) -> ModelResult { let user = users::Entity::find() - .filter(users::Column::ApiKey.eq(api_key)) + .filter( + model::query::condition() + .eq(users::Column::ApiKey, api_key) + .build(), + ) .one(db) .await?; user.ok_or_else(|| ModelError::EntityNotFound) @@ -162,7 +186,11 @@ impl super::_entities::users::Model { let txn = db.begin().await?; if users::Entity::find() - .filter(users::Column::Email.eq(¶ms.email)) + .filter( + model::query::condition() + .eq(users::Column::Email, ¶ms.email) + .build(), + ) .one(&txn) .await? .is_some() @@ -192,7 +220,7 @@ impl super::_entities::users::Model { /// /// when could not convert user claims to jwt token pub fn generate_jwt(&self, secret: &str, expiration: &u64) -> ModelResult { - Ok(jwt::JWT::new(secret).generate_token(expiration, self.pid.to_string())?) + Ok(jwt::JWT::new(secret).generate_token(expiration, self.pid.to_string(), None)?) } } diff --git a/starters/saas/Cargo.lock b/starters/saas/Cargo.lock index edd4a2fc5..14b5ed658 100644 --- a/starters/saas/Cargo.lock +++ b/starters/saas/Cargo.lock @@ -3534,9 +3534,9 @@ dependencies = [ [[package]] name = "sea-orm" -version = "1.0.0-rc.3" +version = "1.0.0-rc.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d5a485429878b8f6eb9ec1c99e91bf4e9055d445be71218faed5847c2e40dba" +checksum = "b467731e7b3e5c7581291c030a0574edbb346b5f3fba9297130416a7178b2385" dependencies = [ "async-stream", "async-trait", @@ -3562,9 +3562,9 @@ dependencies = [ [[package]] name = "sea-orm-cli" -version = "1.0.0-rc.3" +version = "1.0.0-rc.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7fc1c75b5075c7bc2aa41518731e93408f3a158dfb8714e747430734934a901" +checksum = "e4d9173b3768a8cd39e4c6ddb50f3d0bca7907aad0f7c9c8b9db4dad70740535" dependencies = [ "chrono", "clap", @@ -3579,9 +3579,9 @@ dependencies = [ [[package]] name = "sea-orm-macros" -version = "1.0.0-rc.3" +version = "1.0.0-rc.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f303a2b29c6d92a669652fa7776e6590df7118e05d4f6bf31d48127b06cb21a" +checksum = "a55f66b1b0caf9bb64f01ceca74126a34842396c7678b0d178ad0acfa8c12412" dependencies = [ "heck 0.4.1", "proc-macro2", @@ -3593,9 +3593,9 @@ dependencies = [ [[package]] name = "sea-orm-migration" -version = "1.0.0-rc.3" +version = "1.0.0-rc.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e8b13ba1f4308423946530237d5b46f8bef85aec2463686c4eb1e45d87ce9ef" +checksum = "9ce5df1fc1cb6e6d0aeaef6d306ffa4642f9b8dbb25cfed45f844d501d697460" dependencies = [ "async-trait", "clap", diff --git a/starters/saas/Cargo.toml b/starters/saas/Cargo.toml index 864a9bfd6..383d5b1b4 100644 --- a/starters/saas/Cargo.toml +++ b/starters/saas/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" [dependencies] -loco-rs = { version = "0.4.0" } +loco-rs = { version = "0.5.0" } migration = { path = "migration" } serde = { version = "1", features = ["derive"] } @@ -45,5 +45,5 @@ required-features = [] [dev-dependencies] serial_test = "2.0.0" rstest = "0.18.2" -loco-rs = { version = "0.4.0", features = ["testing"] } +loco-rs = { version = "0.5.0", features = ["testing"] } insta = { version = "1.34.0", features = ["redactions", "yaml", "filters"] } diff --git a/starters/saas/config/development.yaml b/starters/saas/config/development.yaml index 33524970f..f80565d97 100644 --- a/starters/saas/config/development.yaml +++ b/starters/saas/config/development.yaml @@ -133,8 +133,8 @@ database: # Recreating schema when application loaded. This is a dangerous operation, make sure that you using this flag only on dev environments or test mode dangerously_recreate: false -# Redis Configuration -redis: +# Queue Configuration +queue: # Redis connection URI uri: {{get_env(name="REDIS_URL", default="redis://127.0.0.1")}} # Dangerously flush all data in Redis on startup. dangerous operation, make sure that you using this flag only on dev environments or test mode diff --git a/starters/saas/config/test.yaml b/starters/saas/config/test.yaml index 8a56d170a..f7be6bc18 100644 --- a/starters/saas/config/test.yaml +++ b/starters/saas/config/test.yaml @@ -114,8 +114,8 @@ database: # Recreating schema when application loaded. This is a dangerous operation, make sure that you using this flag only on dev environments or test mode dangerously_recreate: false -# Redis Configuration -redis: +# Queue Configuration +queue: # Redis connection URI uri: {{get_env(name="REDIS_URL", default="redis://127.0.0.1")}} # Dangerously flush all data in Redis on startup. dangerous operation, make sure that you using this flag only on dev environments or test mode diff --git a/starters/saas/migration/Cargo.toml b/starters/saas/migration/Cargo.toml index 23e6c9b54..40593328f 100644 --- a/starters/saas/migration/Cargo.toml +++ b/starters/saas/migration/Cargo.toml @@ -10,7 +10,7 @@ path = "src/lib.rs" [dependencies] async-std = { version = "1", features = ["attributes", "tokio1"] } -loco-rs = { version = "0.4.0" } +loco-rs = { version = "0.5.0" } [dependencies.sea-orm-migration] version = "1.0.0-rc.4" diff --git a/starters/saas/src/models/users.rs b/starters/saas/src/models/users.rs index b9c2ec62b..fb5785201 100644 --- a/starters/saas/src/models/users.rs +++ b/starters/saas/src/models/users.rs @@ -58,7 +58,11 @@ impl ActiveModelBehavior for super::_entities::users::ActiveModel { impl Authenticable for super::_entities::users::Model { async fn find_by_api_key(db: &DatabaseConnection, api_key: &str) -> ModelResult { let user = users::Entity::find() - .filter(users::Column::ApiKey.eq(api_key)) + .filter( + model::query::condition() + .eq(users::Column::ApiKey, api_key) + .build(), + ) .one(db) .await?; user.ok_or_else(|| ModelError::EntityNotFound) @@ -77,7 +81,11 @@ impl super::_entities::users::Model { /// When could not find user by the given token or DB query error pub async fn find_by_email(db: &DatabaseConnection, email: &str) -> ModelResult { let user = users::Entity::find() - .filter(users::Column::Email.eq(email)) + .filter( + model::query::condition() + .eq(users::Column::Email, email) + .build(), + ) .one(db) .await?; user.ok_or_else(|| ModelError::EntityNotFound) @@ -93,7 +101,11 @@ impl super::_entities::users::Model { token: &str, ) -> ModelResult { let user = users::Entity::find() - .filter(users::Column::EmailVerificationToken.eq(token)) + .filter( + model::query::condition() + .eq(users::Column::EmailVerificationToken, token) + .build(), + ) .one(db) .await?; user.ok_or_else(|| ModelError::EntityNotFound) @@ -106,7 +118,11 @@ impl super::_entities::users::Model { /// When could not find user by the given token or DB query error pub async fn find_by_reset_token(db: &DatabaseConnection, token: &str) -> ModelResult { let user = users::Entity::find() - .filter(users::Column::ResetToken.eq(token)) + .filter( + model::query::condition() + .eq(users::Column::ResetToken, token) + .build(), + ) .one(db) .await?; user.ok_or_else(|| ModelError::EntityNotFound) @@ -120,7 +136,11 @@ impl super::_entities::users::Model { pub async fn find_by_pid(db: &DatabaseConnection, pid: &str) -> ModelResult { let parse_uuid = Uuid::parse_str(pid).map_err(|e| ModelError::Any(e.into()))?; let user = users::Entity::find() - .filter(users::Column::Pid.eq(parse_uuid)) + .filter( + model::query::condition() + .eq(users::Column::Pid, parse_uuid) + .build(), + ) .one(db) .await?; user.ok_or_else(|| ModelError::EntityNotFound) @@ -133,7 +153,11 @@ impl super::_entities::users::Model { /// When could not find user by the given token or DB query error pub async fn find_by_api_key(db: &DatabaseConnection, api_key: &str) -> ModelResult { let user = users::Entity::find() - .filter(users::Column::ApiKey.eq(api_key)) + .filter( + model::query::condition() + .eq(users::Column::ApiKey, api_key) + .build(), + ) .one(db) .await?; user.ok_or_else(|| ModelError::EntityNotFound) @@ -162,7 +186,11 @@ impl super::_entities::users::Model { let txn = db.begin().await?; if users::Entity::find() - .filter(users::Column::Email.eq(¶ms.email)) + .filter( + model::query::condition() + .eq(users::Column::Email, ¶ms.email) + .build(), + ) .one(&txn) .await? .is_some() @@ -192,7 +220,7 @@ impl super::_entities::users::Model { /// /// when could not convert user claims to jwt token pub fn generate_jwt(&self, secret: &str, expiration: &u64) -> ModelResult { - Ok(jwt::JWT::new(secret).generate_token(expiration, self.pid.to_string())?) + Ok(jwt::JWT::new(secret).generate_token(expiration, self.pid.to_string(), None)?) } }