feat(auth): Implement Google OAuth 2.0 integration with user creation and JWT generation
- Added Google OAuth controller and service to handle authentication via Google. - Introduced DTOs for Google user and token responses. - Updated AuthService and UsersService traits to support new Google OAuth functionality. - Implemented logic to create a new user if they do not exist in the system after Google authentication. - Enhanced existing user retrieval and JWT generation upon successful login. - Added tests for Google OAuth flow, including login redirection and callback handling for both new and existing users. - Updated environment configuration to include Google OAuth credentials.
This commit is contained in:
@@ -18,3 +18,7 @@ MINIO_ENDPOINT=http://localhost:9000
|
||||
MINIO_BUCKET_NAME=default_bucket
|
||||
MINIO_ACCESS_KEY=your-minio-access-key
|
||||
MINIO_SECRET_KEY=your-minio-secret-key
|
||||
|
||||
GOOGLE_CLIENT_ID="your_google_client_id"
|
||||
GOOGLE_CLIENT_SECRET="your_google_client_secret"
|
||||
GOOGLE_REDIRECT_URL="http://127.0.0.1:8080/api/v1/auth/google/callback"
|
||||
|
||||
Generated
+485
-55
@@ -289,7 +289,7 @@ dependencies = [
|
||||
"fnv",
|
||||
"futures-timer",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http 1.3.1",
|
||||
"indexmap 2.10.0",
|
||||
"mime",
|
||||
"multer",
|
||||
@@ -416,10 +416,10 @@ dependencies = [
|
||||
"bytes",
|
||||
"form_urlencoded",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"http 1.3.1",
|
||||
"http-body 1.0.1",
|
||||
"http-body-util",
|
||||
"hyper",
|
||||
"hyper 1.6.0",
|
||||
"hyper-util",
|
||||
"itoa",
|
||||
"matchit",
|
||||
@@ -433,7 +433,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"serde_path_to_error",
|
||||
"serde_urlencoded",
|
||||
"sync_wrapper",
|
||||
"sync_wrapper 1.0.2",
|
||||
"tokio",
|
||||
"tower",
|
||||
"tower-layer",
|
||||
@@ -449,13 +449,13 @@ checksum = "68464cd0412f486726fb3373129ef5d2993f90c34bc2bc1c1e9943b2f4fc7ca6"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"http",
|
||||
"http-body",
|
||||
"http 1.3.1",
|
||||
"http-body 1.0.1",
|
||||
"http-body-util",
|
||||
"mime",
|
||||
"pin-project-lite",
|
||||
"rustversion",
|
||||
"sync_wrapper",
|
||||
"sync_wrapper 1.0.2",
|
||||
"tower-layer",
|
||||
"tower-service",
|
||||
"tracing",
|
||||
@@ -474,9 +474,9 @@ dependencies = [
|
||||
"bytes",
|
||||
"bytesize",
|
||||
"cookie",
|
||||
"http",
|
||||
"http 1.3.1",
|
||||
"http-body-util",
|
||||
"hyper",
|
||||
"hyper 1.6.0",
|
||||
"hyper-util",
|
||||
"mime",
|
||||
"pretty_assertions",
|
||||
@@ -506,6 +506,12 @@ dependencies = [
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.13.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.21.7"
|
||||
@@ -561,6 +567,12 @@ version = "0.6.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.9.1"
|
||||
@@ -1128,6 +1140,12 @@ version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c0d05e1c0dbad51b52c38bda7adceef61b9efc2baf04acfe8726a8c4630a6f57"
|
||||
|
||||
[[package]]
|
||||
name = "downcast"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1435fa1053d8b2fbbe9be7e97eca7f33d37b28409959813daefc1446a14247f1"
|
||||
|
||||
[[package]]
|
||||
name = "dtoa"
|
||||
version = "1.0.10"
|
||||
@@ -1343,6 +1361,12 @@ dependencies = [
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fragile"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "28dd6caf6059519a65843af8fe2a3ae298b14b80179855aeb4adc2c1934ee619"
|
||||
|
||||
[[package]]
|
||||
name = "fst"
|
||||
version = "0.4.7"
|
||||
@@ -1564,6 +1588,25 @@ version = "0.31.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
|
||||
|
||||
[[package]]
|
||||
name = "h2"
|
||||
version = "0.3.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0beca50380b1fc32983fc1cb4587bfa4bb9e78fc259aad4a0032d2080309222d"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"fnv",
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
"futures-util",
|
||||
"http 0.2.12",
|
||||
"indexmap 2.10.0",
|
||||
"slab",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "half"
|
||||
version = "2.6.0"
|
||||
@@ -1672,6 +1715,17 @@ dependencies = [
|
||||
"match_token",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "http"
|
||||
version = "0.2.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"fnv",
|
||||
"itoa",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "http"
|
||||
version = "1.3.1"
|
||||
@@ -1683,6 +1737,17 @@ dependencies = [
|
||||
"itoa",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "http-body"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"http 0.2.12",
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "http-body"
|
||||
version = "1.0.1"
|
||||
@@ -1690,7 +1755,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"http",
|
||||
"http 1.3.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1701,8 +1766,8 @@ checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"http",
|
||||
"http-body",
|
||||
"http 1.3.1",
|
||||
"http-body 1.0.1",
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
@@ -1724,6 +1789,30 @@ version = "2.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b112acc8b3adf4b107a8ec20977da0273a8c386765a3ec0229bd500a1443f9f"
|
||||
|
||||
[[package]]
|
||||
name = "hyper"
|
||||
version = "0.14.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41dfc780fdec9373c01bae43289ea34c972e40ee3c9f6b3c8801a35f35586ce7"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"h2",
|
||||
"http 0.2.12",
|
||||
"http-body 0.4.6",
|
||||
"httparse",
|
||||
"httpdate",
|
||||
"itoa",
|
||||
"pin-project-lite",
|
||||
"socket2",
|
||||
"tokio",
|
||||
"tower-service",
|
||||
"tracing",
|
||||
"want",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper"
|
||||
version = "1.6.0"
|
||||
@@ -1733,8 +1822,8 @@ dependencies = [
|
||||
"bytes",
|
||||
"futures-channel",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"http 1.3.1",
|
||||
"http-body 1.0.1",
|
||||
"httparse",
|
||||
"httpdate",
|
||||
"itoa",
|
||||
@@ -1744,23 +1833,50 @@ dependencies = [
|
||||
"want",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper-rustls"
|
||||
version = "0.24.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590"
|
||||
dependencies = [
|
||||
"futures-util",
|
||||
"http 0.2.12",
|
||||
"hyper 0.14.32",
|
||||
"rustls 0.21.12",
|
||||
"tokio",
|
||||
"tokio-rustls 0.24.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper-rustls"
|
||||
version = "0.27.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e3c93eb611681b207e1fe55d5a71ecf91572ec8a6705cdb6857f7d8d5242cf58"
|
||||
dependencies = [
|
||||
"http",
|
||||
"hyper",
|
||||
"http 1.3.1",
|
||||
"hyper 1.6.0",
|
||||
"hyper-util",
|
||||
"rustls",
|
||||
"rustls 0.23.29",
|
||||
"rustls-pki-types",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
"tokio-rustls 0.26.2",
|
||||
"tower-service",
|
||||
"webpki-roots 1.0.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper-tls"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"hyper 0.14.32",
|
||||
"native-tls",
|
||||
"tokio",
|
||||
"tokio-native-tls",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper-util"
|
||||
version = "0.1.15"
|
||||
@@ -1772,9 +1888,9 @@ dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"hyper",
|
||||
"http 1.3.1",
|
||||
"http-body 1.0.1",
|
||||
"hyper 1.6.0",
|
||||
"ipnet",
|
||||
"libc",
|
||||
"percent-encoding",
|
||||
@@ -2016,6 +2132,7 @@ dependencies = [
|
||||
name = "imphnen-entities"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"axum",
|
||||
"serde",
|
||||
"surrealdb",
|
||||
@@ -2085,6 +2202,7 @@ name = "imphnen-iam"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"axum",
|
||||
"axum-test",
|
||||
"chrono",
|
||||
@@ -2095,15 +2213,19 @@ dependencies = [
|
||||
"imphnen-utils",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"mockall",
|
||||
"oauth2",
|
||||
"once_cell",
|
||||
"rand 0.9.1",
|
||||
"regex",
|
||||
"reqwest 0.11.27",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"strum 0.27.1",
|
||||
"strum_macros 0.27.1",
|
||||
"surrealdb",
|
||||
"tokio",
|
||||
"tokio-test",
|
||||
"tower-http",
|
||||
"tracing",
|
||||
"utoipa",
|
||||
@@ -2217,7 +2339,7 @@ version = "0.7.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b86e202f00093dcba4275d4636b93ef9dd75d025ae560d2521b45ea28ab49013"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 2.9.1",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
]
|
||||
@@ -2418,7 +2540,7 @@ version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1580801010e535496706ba011c15f8532df6b42297d2e471fec38ceadd8c0638"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 2.9.1",
|
||||
"libc",
|
||||
]
|
||||
|
||||
@@ -2620,6 +2742,33 @@ dependencies = [
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mockall"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43766c2b5203b10de348ffe19f7e54564b64f3d6018ff7648d1e2d6d3a0f0a48"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"downcast",
|
||||
"fragile",
|
||||
"lazy_static",
|
||||
"mockall_derive",
|
||||
"predicates",
|
||||
"predicates-tree",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mockall_derive"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af7cbce79ec385a1d4f54baa90a76401eb15d9cab93685f62e7e9f942aa00ae2"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.104",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "multer"
|
||||
version = "3.1.0"
|
||||
@@ -2629,7 +2778,7 @@ dependencies = [
|
||||
"bytes",
|
||||
"encoding_rs",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http 1.3.1",
|
||||
"httparse",
|
||||
"memchr",
|
||||
"mime",
|
||||
@@ -2798,6 +2947,26 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "oauth2"
|
||||
version = "4.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c38841cdd844847e3e7c8d29cef9dcfed8877f8f56f9071f77843ecf3baf937f"
|
||||
dependencies = [
|
||||
"base64 0.13.1",
|
||||
"chrono",
|
||||
"getrandom 0.2.16",
|
||||
"http 0.2.12",
|
||||
"rand 0.8.5",
|
||||
"reqwest 0.11.27",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_path_to_error",
|
||||
"sha2",
|
||||
"thiserror 1.0.69",
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.36.7"
|
||||
@@ -2817,7 +2986,7 @@ dependencies = [
|
||||
"bytes",
|
||||
"chrono",
|
||||
"futures",
|
||||
"http",
|
||||
"http 1.3.1",
|
||||
"humantime",
|
||||
"itertools 0.14.0",
|
||||
"parking_lot",
|
||||
@@ -2849,7 +3018,7 @@ version = "0.10.73"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8505734d46c8ab1e19a1dce3aef597ad87dcb4c37e7188231769bd6bd51cebf8"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 2.9.1",
|
||||
"cfg-if",
|
||||
"foreign-types",
|
||||
"libc",
|
||||
@@ -3127,6 +3296,32 @@ version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
|
||||
|
||||
[[package]]
|
||||
name = "predicates"
|
||||
version = "3.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5d19ee57562043d37e82899fade9a22ebab7be9cef5026b07fda9cdd4293573"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"predicates-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "predicates-core"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "727e462b119fe9c93fd0eb1429a5f7647394014cf3c04ab2c0350eeb09095ffa"
|
||||
|
||||
[[package]]
|
||||
name = "predicates-tree"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72dd2d6d381dfb73a193c7fca536518d7caee39fc8503f74e7dc0be0531b425c"
|
||||
dependencies = [
|
||||
"predicates-core",
|
||||
"termtree",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pretty_assertions"
|
||||
version = "1.4.1"
|
||||
@@ -3248,7 +3443,7 @@ dependencies = [
|
||||
"quinn-proto",
|
||||
"quinn-udp",
|
||||
"rustc-hash",
|
||||
"rustls",
|
||||
"rustls 0.23.29",
|
||||
"socket2",
|
||||
"thiserror 2.0.12",
|
||||
"tokio",
|
||||
@@ -3268,7 +3463,7 @@ dependencies = [
|
||||
"rand 0.9.1",
|
||||
"ring",
|
||||
"rustc-hash",
|
||||
"rustls",
|
||||
"rustls 0.23.29",
|
||||
"rustls-pki-types",
|
||||
"slab",
|
||||
"thiserror 2.0.12",
|
||||
@@ -3436,7 +3631,7 @@ version = "0.5.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d04b7d0ee6b4a0207a0a7adb104d23ecb0b47d6beae7152d0fa34b692b29fd6"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 2.9.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3523,6 +3718,50 @@ dependencies = [
|
||||
"bytecheck",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "reqwest"
|
||||
version = "0.11.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62"
|
||||
dependencies = [
|
||||
"base64 0.21.7",
|
||||
"bytes",
|
||||
"encoding_rs",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"h2",
|
||||
"http 0.2.12",
|
||||
"http-body 0.4.6",
|
||||
"hyper 0.14.32",
|
||||
"hyper-rustls 0.24.2",
|
||||
"hyper-tls",
|
||||
"ipnet",
|
||||
"js-sys",
|
||||
"log",
|
||||
"mime",
|
||||
"native-tls",
|
||||
"once_cell",
|
||||
"percent-encoding",
|
||||
"pin-project-lite",
|
||||
"rustls 0.21.12",
|
||||
"rustls-pemfile",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_urlencoded",
|
||||
"sync_wrapper 0.1.2",
|
||||
"system-configuration",
|
||||
"tokio",
|
||||
"tokio-native-tls",
|
||||
"tokio-rustls 0.24.1",
|
||||
"tower-service",
|
||||
"url",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-futures",
|
||||
"web-sys",
|
||||
"webpki-roots 0.25.4",
|
||||
"winreg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "reqwest"
|
||||
version = "0.12.22"
|
||||
@@ -3533,11 +3772,11 @@ dependencies = [
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"http 1.3.1",
|
||||
"http-body 1.0.1",
|
||||
"http-body-util",
|
||||
"hyper",
|
||||
"hyper-rustls",
|
||||
"hyper 1.6.0",
|
||||
"hyper-rustls 0.27.7",
|
||||
"hyper-util",
|
||||
"js-sys",
|
||||
"log",
|
||||
@@ -3545,14 +3784,14 @@ dependencies = [
|
||||
"percent-encoding",
|
||||
"pin-project-lite",
|
||||
"quinn",
|
||||
"rustls",
|
||||
"rustls 0.23.29",
|
||||
"rustls-pki-types",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_urlencoded",
|
||||
"sync_wrapper",
|
||||
"sync_wrapper 1.0.2",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
"tokio-rustls 0.26.2",
|
||||
"tokio-util",
|
||||
"tower",
|
||||
"tower-http",
|
||||
@@ -3766,7 +4005,7 @@ dependencies = [
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http 1.3.1",
|
||||
"mime",
|
||||
"rand 0.9.1",
|
||||
"thiserror 2.0.12",
|
||||
@@ -3834,13 +4073,25 @@ version = "1.0.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "11181fbabf243db407ef8df94a6ce0b2f9a733bd8be4ad02b4eda9602296cac8"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 2.9.1",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys 0.60.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls"
|
||||
version = "0.21.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f56a14d1f48b391359b22f731fd4bd7e43c97f3c50eee276f3aa09c94784d3e"
|
||||
dependencies = [
|
||||
"log",
|
||||
"ring",
|
||||
"rustls-webpki 0.101.7",
|
||||
"sct",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls"
|
||||
version = "0.23.29"
|
||||
@@ -3851,11 +4102,20 @@ dependencies = [
|
||||
"once_cell",
|
||||
"ring",
|
||||
"rustls-pki-types",
|
||||
"rustls-webpki",
|
||||
"rustls-webpki 0.103.4",
|
||||
"subtle",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-pemfile"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c"
|
||||
dependencies = [
|
||||
"base64 0.21.7",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-pki-types"
|
||||
version = "1.12.0"
|
||||
@@ -3866,6 +4126,16 @@ dependencies = [
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-webpki"
|
||||
version = "0.101.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b6275d1ee7a1cd780b64aca7726599a1dbc893b1e64144529e55c3c2f745765"
|
||||
dependencies = [
|
||||
"ring",
|
||||
"untrusted",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-webpki"
|
||||
version = "0.103.4"
|
||||
@@ -3958,6 +4228,16 @@ dependencies = [
|
||||
"sha2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sct"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da046153aa2352493d6cb7da4b6e5c0c057d8a1d0a9aa8560baffdd945acd414"
|
||||
dependencies = [
|
||||
"ring",
|
||||
"untrusted",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "seahash"
|
||||
version = "4.1.0"
|
||||
@@ -3970,7 +4250,7 @@ version = "2.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 2.9.1",
|
||||
"core-foundation",
|
||||
"core-foundation-sys",
|
||||
"libc",
|
||||
@@ -4365,11 +4645,11 @@ dependencies = [
|
||||
"path-clean",
|
||||
"pharos",
|
||||
"reblessive",
|
||||
"reqwest",
|
||||
"reqwest 0.12.22",
|
||||
"revision 0.11.0",
|
||||
"ring",
|
||||
"rust_decimal",
|
||||
"rustls",
|
||||
"rustls 0.23.29",
|
||||
"rustls-pki-types",
|
||||
"semver",
|
||||
"serde",
|
||||
@@ -4424,7 +4704,7 @@ dependencies = [
|
||||
"geo-types",
|
||||
"getrandom 0.3.3",
|
||||
"hex",
|
||||
"http",
|
||||
"http 1.3.1",
|
||||
"ipnet",
|
||||
"jsonwebtoken",
|
||||
"lexicmp",
|
||||
@@ -4522,6 +4802,12 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sync_wrapper"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
|
||||
|
||||
[[package]]
|
||||
name = "sync_wrapper"
|
||||
version = "1.0.2"
|
||||
@@ -4556,6 +4842,27 @@ dependencies = [
|
||||
"windows",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "system-configuration"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7"
|
||||
dependencies = [
|
||||
"bitflags 1.3.2",
|
||||
"core-foundation",
|
||||
"system-configuration-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "system-configuration-sys"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9"
|
||||
dependencies = [
|
||||
"core-foundation-sys",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tap"
|
||||
version = "1.0.1"
|
||||
@@ -4597,6 +4904,12 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termtree"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f50febec83f5ee1df3015341d8bd429f2d1cc62bcba7ea2076759d315084683"
|
||||
|
||||
[[package]]
|
||||
name = "tests"
|
||||
version = "0.1.0"
|
||||
@@ -4606,7 +4919,7 @@ dependencies = [
|
||||
"chrono",
|
||||
"dotenvy",
|
||||
"http-body-util",
|
||||
"hyper",
|
||||
"hyper 1.6.0",
|
||||
"hyper-util",
|
||||
"imphnen-dimentorin",
|
||||
"imphnen-entities",
|
||||
@@ -4779,16 +5092,50 @@ dependencies = [
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-rustls"
|
||||
version = "0.24.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081"
|
||||
dependencies = [
|
||||
"rustls 0.21.12",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-rustls"
|
||||
version = "0.26.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e727b36a1a0e8b74c376ac2211e40c2c8af09fb4013c60d910495810f008e9b"
|
||||
dependencies = [
|
||||
"rustls",
|
||||
"rustls 0.23.29",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-stream"
|
||||
version = "0.1.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eca58d7bba4a75707817a2c44174253f9236b2d5fbd055602e9d5c07c139a047"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"pin-project-lite",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-test"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2468baabc3311435b55dd935f702f42cd1b8abb7e754fb7dfb16bd36aa88f9f7"
|
||||
dependencies = [
|
||||
"async-stream",
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-tungstenite"
|
||||
version = "0.23.1"
|
||||
@@ -4797,10 +5144,10 @@ checksum = "c6989540ced10490aaf14e6bad2e3d33728a2813310a0c71d1574304c49631cd"
|
||||
dependencies = [
|
||||
"futures-util",
|
||||
"log",
|
||||
"rustls",
|
||||
"rustls 0.23.29",
|
||||
"rustls-pki-types",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
"tokio-rustls 0.26.2",
|
||||
"tungstenite",
|
||||
"webpki-roots 0.26.11",
|
||||
]
|
||||
@@ -4845,7 +5192,7 @@ dependencies = [
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"pin-project-lite",
|
||||
"sync_wrapper",
|
||||
"sync_wrapper 1.0.2",
|
||||
"tokio",
|
||||
"tower-layer",
|
||||
"tower-service",
|
||||
@@ -4858,11 +5205,11 @@ version = "0.6.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "adc82fd73de2a9722ac5da747f12383d2bfdb93591ee6c58486e0097890f05f2"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 2.9.1",
|
||||
"bytes",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"http 1.3.1",
|
||||
"http-body 1.0.1",
|
||||
"iri-string",
|
||||
"pin-project-lite",
|
||||
"tower",
|
||||
@@ -4971,11 +5318,11 @@ dependencies = [
|
||||
"byteorder",
|
||||
"bytes",
|
||||
"data-encoding",
|
||||
"http",
|
||||
"http 1.3.1",
|
||||
"httparse",
|
||||
"log",
|
||||
"rand 0.8.5",
|
||||
"rustls",
|
||||
"rustls 0.23.29",
|
||||
"rustls-pki-types",
|
||||
"sha1",
|
||||
"thiserror 1.0.69",
|
||||
@@ -5070,6 +5417,7 @@ dependencies = [
|
||||
"form_urlencoded",
|
||||
"idna",
|
||||
"percent-encoding",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5374,6 +5722,12 @@ dependencies = [
|
||||
"string_cache_codegen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "webpki-roots"
|
||||
version = "0.25.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1"
|
||||
|
||||
[[package]]
|
||||
name = "webpki-roots"
|
||||
version = "0.26.11"
|
||||
@@ -5535,6 +5889,15 @@ dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.48.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
|
||||
dependencies = [
|
||||
"windows-targets 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.52.0"
|
||||
@@ -5562,6 +5925,21 @@ dependencies = [
|
||||
"windows-targets 0.53.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm 0.48.5",
|
||||
"windows_aarch64_msvc 0.48.5",
|
||||
"windows_i686_gnu 0.48.5",
|
||||
"windows_i686_msvc 0.48.5",
|
||||
"windows_x86_64_gnu 0.48.5",
|
||||
"windows_x86_64_gnullvm 0.48.5",
|
||||
"windows_x86_64_msvc 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.52.6"
|
||||
@@ -5594,6 +5972,12 @@ dependencies = [
|
||||
"windows_x86_64_msvc 0.53.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.52.6"
|
||||
@@ -5606,6 +5990,12 @@ version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.52.6"
|
||||
@@ -5618,6 +6008,12 @@ version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.52.6"
|
||||
@@ -5642,6 +6038,12 @@ version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.52.6"
|
||||
@@ -5654,6 +6056,12 @@ version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.52.6"
|
||||
@@ -5666,6 +6074,12 @@ version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.52.6"
|
||||
@@ -5678,6 +6092,12 @@ version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.52.6"
|
||||
@@ -5699,13 +6119,23 @@ dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winreg"
|
||||
version = "0.50.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen-rt"
|
||||
version = "0.39.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 2.9.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use axum::Router;
|
||||
use imphnen_gateway::gateway_service;
|
||||
use imphnen_libs::axum_init;
|
||||
|
||||
@@ -5,7 +6,10 @@ use imphnen_libs::axum_init;
|
||||
async fn main() {
|
||||
env_logger::init();
|
||||
axum_init(|surrealdb_ws, surrealdb_mem| async {
|
||||
gateway_service(surrealdb_ws, surrealdb_mem).await
|
||||
let app = gateway_service(surrealdb_ws, surrealdb_mem).await;
|
||||
let mut router = Router::new();
|
||||
router = router.nest("/api/v1/auth", imphnen_iam::v1::auth::auth_router());
|
||||
app.merge(router)
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
@@ -8,4 +8,5 @@ axum.workspace = true
|
||||
serde.workspace = true
|
||||
utoipa.workspace = true
|
||||
surrealdb.workspace = true
|
||||
anyhow.workspace = true
|
||||
thiserror.workspace = true
|
||||
|
||||
@@ -9,6 +9,10 @@ pub mod error {
|
||||
pub enum Error {
|
||||
#[error("database error: {0}")]
|
||||
Db(String),
|
||||
#[error("anyhow error: {0}")]
|
||||
Anyhow(#[from] anyhow::Error),
|
||||
#[error("HTTP status code error: {0}")]
|
||||
StatusCode(StatusCode),
|
||||
}
|
||||
|
||||
impl IntoResponse for Error {
|
||||
@@ -18,6 +22,11 @@ pub mod error {
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Database error: {detail}"),
|
||||
),
|
||||
Error::Anyhow(detail) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Internal server error: {detail}"),
|
||||
),
|
||||
Error::StatusCode(s) => (s, format!("HTTP error: {}", s)),
|
||||
};
|
||||
(status, Json(error_message)).into_response()
|
||||
}
|
||||
@@ -28,4 +37,10 @@ pub mod error {
|
||||
Self::Db(error.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StatusCode> for Error {
|
||||
fn from(status: StatusCode) -> Self {
|
||||
Self::StatusCode(status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,13 @@ edition = "2024"
|
||||
[dependencies]
|
||||
imphnen-libs.workspace = true
|
||||
imphnen-utils.workspace = true
|
||||
async-trait = "0.1"
|
||||
imphnen-entities.workspace = true
|
||||
axum.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
oauth2 = "4.4.2"
|
||||
reqwest = { version = "0.11", features = ["json"] }
|
||||
utoipa.workspace = true
|
||||
lazy_static.workspace = true
|
||||
regex.workspace = true
|
||||
@@ -31,4 +34,6 @@ uuid.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
dotenvy.workspace = true
|
||||
tokio-test = "0.4.2"
|
||||
mockall = "0.12.1"
|
||||
http-body-util.workspace = true
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
use super::{
|
||||
AuthLoginRequestDto, AuthRefreshTokenRequestDto, AuthRegisterRequestDto,
|
||||
AuthResendOtpRequestDto, AuthService, AuthVerifyEmailRequestDto,
|
||||
AuthResendOtpRequestDto, AuthVerifyEmailRequestDto,
|
||||
};
|
||||
use crate::{AppState, v1::AuthLoginResponsetDto};
|
||||
use crate::{AuthNewPasswordRequestDto, MessageResponseDto, ResponseSuccessDto};
|
||||
use axum::{Extension, Json, response::IntoResponse};
|
||||
use crate::v1::auth::auth_service::AuthServiceTrait;
|
||||
use crate::v1::auth::auth_service::AuthService;
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
|
||||
@@ -15,10 +15,49 @@ use axum::{http::StatusCode, response::Response};
|
||||
use surrealdb::Uuid;
|
||||
use tracing::error;
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[async_trait]
|
||||
pub trait AuthServiceTrait: Send + Sync + 'static {
|
||||
async fn mutation_login(
|
||||
payload: AuthLoginRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response;
|
||||
async fn mutation_mentor_login(
|
||||
payload: AuthLoginRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response;
|
||||
async fn mutation_register(
|
||||
payload: AuthRegisterRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response;
|
||||
async fn mutation_resend_otp(
|
||||
payload: AuthResendOtpRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response;
|
||||
async fn mutation_refresh_token(
|
||||
payload: AuthRefreshTokenRequestDto,
|
||||
) -> Response;
|
||||
async fn mutation_forgot_password(
|
||||
payload: AuthResendOtpRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response;
|
||||
async fn mutation_verify_email(
|
||||
payload: AuthVerifyEmailRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response;
|
||||
async fn mutation_new_password(
|
||||
payload: AuthNewPasswordRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response;
|
||||
}
|
||||
|
||||
#[derive(Clone)] // Added Clone derive
|
||||
pub struct AuthService;
|
||||
|
||||
impl AuthService {
|
||||
pub async fn mutation_login(
|
||||
#[async_trait]
|
||||
impl AuthServiceTrait for AuthService {
|
||||
async fn mutation_login(
|
||||
payload: AuthLoginRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response {
|
||||
@@ -104,7 +143,7 @@ impl AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn mutation_mentor_login(
|
||||
async fn mutation_mentor_login(
|
||||
payload: AuthLoginRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response {
|
||||
@@ -199,7 +238,7 @@ impl AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn mutation_register(
|
||||
async fn mutation_register(
|
||||
payload: AuthRegisterRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response {
|
||||
@@ -298,7 +337,7 @@ impl AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn mutation_resend_otp(
|
||||
async fn mutation_resend_otp(
|
||||
payload: AuthResendOtpRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response {
|
||||
@@ -335,7 +374,7 @@ impl AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn mutation_refresh_token(
|
||||
async fn mutation_refresh_token(
|
||||
payload: AuthRefreshTokenRequestDto,
|
||||
) -> Response {
|
||||
if let Err((status, message)) = validate_request(&payload) {
|
||||
@@ -376,7 +415,7 @@ impl AuthService {
|
||||
success_response(response)
|
||||
}
|
||||
|
||||
pub async fn mutation_forgot_password(
|
||||
async fn mutation_forgot_password(
|
||||
payload: AuthResendOtpRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response {
|
||||
@@ -431,7 +470,7 @@ impl AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn mutation_verify_email(
|
||||
async fn mutation_verify_email(
|
||||
payload: AuthVerifyEmailRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response {
|
||||
@@ -478,7 +517,7 @@ impl AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn mutation_new_password(
|
||||
async fn mutation_new_password(
|
||||
payload: AuthNewPasswordRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response {
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
use axum::{
|
||||
extract::{Query, State},
|
||||
response::{IntoResponse, Redirect},
|
||||
routing::get,
|
||||
Json, Router,
|
||||
};
|
||||
use axum::http::StatusCode;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
use std::sync::Arc; // Import Arc
|
||||
|
||||
use crate::v1::auth::google::google_oauth_service::{AuthRequest, GoogleOauthService, GoogleOauthServiceImpl};
|
||||
use crate::v1::auth::auth_service::AuthServiceTrait;
|
||||
use crate::v1::users::users_service::UsersServiceTrait;
|
||||
use imphnen_entities::error_dto::error::Error;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct GoogleAuthUrlResponse {
|
||||
pub authorize_url: String,
|
||||
}
|
||||
|
||||
pub struct GoogleOauthController<T> { // Generic over T
|
||||
google_oauth_service: T,
|
||||
}
|
||||
|
||||
// Concrete implementation for new()
|
||||
impl GoogleOauthController<GoogleOauthServiceImpl<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService>> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
google_oauth_service: GoogleOauthServiceImpl::<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService>::new(), // Explicitly specify type parameters
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generic implementation for with_service and get_routes
|
||||
impl<T> GoogleOauthController<T>
|
||||
where
|
||||
T: GoogleOauthService<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService> + Clone + Send + Sync + 'static, // Explicitly constrain T
|
||||
{
|
||||
pub fn with_service(google_oauth_service: T) -> Self {
|
||||
Self {
|
||||
google_oauth_service,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_routes(&self) -> Router { // Take self by reference
|
||||
Router::new()
|
||||
.route(
|
||||
"/google/login",
|
||||
get(
|
||||
move |State(controller): State<Arc<Self>>| async move {
|
||||
controller.google_oauth_login().await
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/google/callback",
|
||||
get(
|
||||
move |State(controller): State<Arc<Self>>, Query(auth_request): Query<AuthRequest>| async move {
|
||||
let controller = Arc::clone(&controller);
|
||||
controller.google_oauth_callback(auth_request).await
|
||||
},
|
||||
),
|
||||
)
|
||||
.with_state(Arc::new(self.clone())) // Pass an Arc clone of self to with_state
|
||||
}
|
||||
|
||||
pub async fn google_oauth_login(&self) -> Result<Redirect, Error> {
|
||||
let (authorize_url, _csrf_state) = self.google_oauth_service.generate_auth_url();
|
||||
Ok(Redirect::to(authorize_url.as_str()))
|
||||
}
|
||||
|
||||
pub async fn google_oauth_callback(&self, auth_request: AuthRequest) -> Result<impl IntoResponse + use<T>, Error> {
|
||||
let token = self.google_oauth_service.google_oauth_callback(auth_request).await?;
|
||||
Ok((StatusCode::OK, Json(serde_json::json!({"token": token}))))
|
||||
}
|
||||
}
|
||||
|
||||
// Clone implementation
|
||||
impl<T> Clone for GoogleOauthController<T>
|
||||
where
|
||||
T: GoogleOauthService<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService> + Clone, // Explicitly constrain T
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self::with_service(self.google_oauth_service.clone())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct GoogleUser {
|
||||
pub id: String,
|
||||
pub email: String,
|
||||
pub verified_email: bool,
|
||||
pub name: String,
|
||||
pub given_name: String,
|
||||
pub family_name: String,
|
||||
pub picture: String,
|
||||
pub locale: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct GoogleTokenResponse {
|
||||
pub access_token: String,
|
||||
pub expires_in: u64,
|
||||
pub refresh_token: Option<String>,
|
||||
pub scope: String,
|
||||
pub token_type: String,
|
||||
pub id_token: String,
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use oauth2::{
|
||||
basic::BasicClient, AuthUrl, ClientId, ClientSecret, CsrfToken, PkceCodeChallenge,
|
||||
RedirectUrl, Scope, TokenResponse, TokenUrl,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use oauth2::url::Url;
|
||||
|
||||
use imphnen_entities::error_dto::error::Error;
|
||||
use imphnen_libs::{jsonwebtoken::generate_jwt, enviroment::ENV};
|
||||
use crate::v1::auth::auth_service::AuthServiceTrait;
|
||||
use crate::v1::users::users_dto::{UsersCreateRequestDto, UsersDetailItemDto};
|
||||
use crate::v1::users::users_service::UsersServiceTrait;
|
||||
|
||||
use super::google_oauth_dto::{GoogleTokenResponse, GoogleUser};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct AuthRequest {
|
||||
pub code: String,
|
||||
pub state: String,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait GoogleOauthService<A: AuthServiceTrait + Send + Sync + 'static, U: UsersServiceTrait + Send + Sync + 'static>: Send + Sync + 'static {
|
||||
// Removed new() from trait
|
||||
fn with_services(auth_service: A, users_service: U) -> Self;
|
||||
fn google_oauth_client(&self) -> BasicClient;
|
||||
fn generate_auth_url(&self) -> (Url, CsrfToken);
|
||||
async fn google_oauth_callback(&self, auth_request: AuthRequest) -> Result<String, Error>;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GoogleOauthServiceImpl<A: AuthServiceTrait, U: UsersServiceTrait> {
|
||||
auth_service: A,
|
||||
users_service: U,
|
||||
}
|
||||
|
||||
impl GoogleOauthServiceImpl<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService> {
|
||||
pub fn new() -> Self {
|
||||
GoogleOauthServiceImpl {
|
||||
auth_service: crate::v1::auth::auth_service::AuthService {},
|
||||
users_service: crate::v1::users::users_service::UsersService {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<A, U> GoogleOauthService<A, U> for GoogleOauthServiceImpl<A, U>
|
||||
where
|
||||
A: AuthServiceTrait + Send + Sync + 'static,
|
||||
U: UsersServiceTrait + Send + Sync + 'static,
|
||||
{
|
||||
fn with_services(auth_service: A, users_service: U) -> Self {
|
||||
Self {
|
||||
auth_service,
|
||||
users_service,
|
||||
}
|
||||
}
|
||||
|
||||
fn google_oauth_client(&self) -> BasicClient {
|
||||
let google_client_id = ClientId::new(ENV.google_client_id.clone());
|
||||
let google_client_secret = ClientSecret::new(ENV.google_client_secret.clone());
|
||||
let auth_url = AuthUrl::new("https://accounts.google.com/o/oauth2/v2/auth".to_string())
|
||||
.expect("Invalid authorization endpoint URL");
|
||||
let token_url = TokenUrl::new("https://oauth2.googleapis.com/token".to_string())
|
||||
.expect("Invalid token endpoint URL");
|
||||
|
||||
BasicClient::new(
|
||||
google_client_id,
|
||||
Some(google_client_secret),
|
||||
auth_url,
|
||||
Some(token_url),
|
||||
)
|
||||
.set_redirect_uri(
|
||||
RedirectUrl::new(ENV.google_redirect_url.clone())
|
||||
.expect("Invalid redirect URL"),
|
||||
)
|
||||
}
|
||||
|
||||
fn generate_auth_url(&self) -> (Url, CsrfToken) {
|
||||
let client = self.google_oauth_client();
|
||||
let (pkce_code_challenge, _pkce_code_verifier) = PkceCodeChallenge::new_random_sha256();
|
||||
|
||||
client
|
||||
.authorize_url(CsrfToken::new_random)
|
||||
.add_scope(Scope::new("https://www.googleapis.com/auth/userinfo.email".to_string()))
|
||||
.add_scope(Scope::new("https://www.googleapis.com/auth/userinfo.profile".to_string()))
|
||||
.set_pkce_challenge(pkce_code_challenge)
|
||||
.url()
|
||||
}
|
||||
|
||||
async fn google_oauth_callback(&self, auth_request: AuthRequest) -> Result<String, Error> {
|
||||
let client = self.google_oauth_client();
|
||||
|
||||
let token_response = client
|
||||
.exchange_code(oauth2::AuthorizationCode::new(auth_request.code))
|
||||
.request_async(oauth2::reqwest::async_http_client)
|
||||
.await
|
||||
.map_err(|e| Error::Db(format!("Failed to exchange code: {}", e)))?;
|
||||
|
||||
let google_token_response: GoogleTokenResponse = serde_json::from_str(&token_response.access_token().secret())
|
||||
.map_err(|e| Error::Db(format!("Failed to parse Google token response: {}", e)))?;
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let user_info_url = "https://www.googleapis.com/oauth2/v2/userinfo";
|
||||
let google_user: GoogleUser = client
|
||||
.get(user_info_url)
|
||||
.bearer_auth(google_token_response.access_token)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| Error::Db(format!("Failed to fetch user info: {}", e)))?
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| Error::Db(format!("Failed to parse user info: {}", e)))?;
|
||||
|
||||
let user = self.users_service.get_user_by_email(&google_user.email).await?;
|
||||
|
||||
let user = match user {
|
||||
Some(user) => user,
|
||||
None => {
|
||||
let new_user = UsersCreateRequestDto {
|
||||
email: google_user.email,
|
||||
password: "GOOGLE_OAUTH_PASSWORD".to_string(), // Placeholder password as it's not used
|
||||
fullname: google_user.name.clone(), // Use fullname for UsersCreateRequestDto
|
||||
phone_number: "N/A".to_string(), // Placeholder for phone number
|
||||
is_active: true, // Assuming active by default for new Google users
|
||||
role_id: "default_role_id".to_string(), // Placeholder for role_id
|
||||
};
|
||||
self.users_service.create_user_by_dto(new_user).await?
|
||||
}
|
||||
};
|
||||
|
||||
let token = generate_jwt(&user.id.to_string())?;
|
||||
Ok(token)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pub mod google_oauth_controller;
|
||||
pub mod google_oauth_dto;
|
||||
pub mod google_oauth_service;
|
||||
@@ -5,6 +5,7 @@ pub mod auth_dto;
|
||||
pub mod auth_repository;
|
||||
pub mod auth_schema;
|
||||
pub mod auth_service;
|
||||
pub mod google;
|
||||
|
||||
pub use auth_dto::*;
|
||||
pub use auth_repository::*;
|
||||
@@ -13,6 +14,7 @@ pub use auth_service::*;
|
||||
|
||||
pub fn auth_router() -> Router {
|
||||
Router::new()
|
||||
.nest("/google", google::google_oauth_controller::GoogleOauthController::new().get_routes())
|
||||
.route("/forgot", post(auth_controller::post_forgot_password))
|
||||
.route("/login", post(auth_controller::post_login))
|
||||
.route("/login-mentor", post(auth_controller::post_login_mentor))
|
||||
|
||||
@@ -28,7 +28,7 @@ pub struct RolesListItemDto {
|
||||
pub updated_at: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, Default)] // Added Default derive
|
||||
pub struct RolesDetailItemDto {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::{AppState, MetaRequestDto, v1::users_service::UsersService};
|
||||
use crate::{AppState, MetaRequestDto};
|
||||
use crate::{
|
||||
MessageResponseDto, PermissionsEnum, ResponseListSuccessDto, ResponseSuccessDto,
|
||||
UsersCreateRequestDto, UsersDetailItemDto, permissions_guard,
|
||||
};
|
||||
use axum::extract::{Path, Query};
|
||||
use axum::extract::{Path}; // Removed Query
|
||||
use axum::http::HeaderMap;
|
||||
use axum::response::IntoResponse;
|
||||
use axum::{Extension, Json};
|
||||
@@ -11,6 +11,7 @@ use axum::{Extension, Json};
|
||||
use super::{
|
||||
UsersActiveInactiveRequestDto, UsersListItemDto, UsersUpdateRequestDto,
|
||||
};
|
||||
use crate::v1::users::users_service::{UsersServiceTrait, UsersService};
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
@@ -35,7 +36,7 @@ use super::{
|
||||
pub async fn get_user_list(
|
||||
headers: HeaderMap,
|
||||
Extension(state): Extension<AppState>,
|
||||
Query(meta): Query<MetaRequestDto>,
|
||||
Json(meta): Json<MetaRequestDto>,
|
||||
) -> impl IntoResponse {
|
||||
match permissions_guard(
|
||||
&headers,
|
||||
@@ -178,7 +179,7 @@ pub async fn put_update_user_me(
|
||||
Json(payload): Json<UsersUpdateRequestDto>,
|
||||
) -> impl IntoResponse {
|
||||
match permissions_guard(&headers, state.clone(), vec![]).await {
|
||||
Ok(_) => UsersService::update_user_me(&state, headers, payload).await,
|
||||
Ok(_) => UsersService::update_user_me(headers, &state, payload).await,
|
||||
Err(response) => response,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
|
||||
use surrealdb::sql::Thing;
|
||||
use utoipa::ToSchema;
|
||||
use validator::Validate;
|
||||
use crate::UsersSchema; // Import UsersSchema
|
||||
|
||||
lazy_static! {
|
||||
static ref PASSWORD_REGEX: regex::Regex =
|
||||
@@ -96,14 +97,14 @@ pub struct UsersDetailItemDto {
|
||||
}
|
||||
|
||||
impl UsersDetailItemDto {
|
||||
pub fn from(dto: &UsersDetailQueryDto) -> Self {
|
||||
pub fn from(dto: &UsersDetailQueryDto) -> Self { // Reverted to taking a reference
|
||||
Self {
|
||||
id: dto.id.id.to_raw().clone(),
|
||||
role: RolesDetailItemDto::from(&dto.role),
|
||||
fullname: dto.fullname.clone(),
|
||||
email: dto.email.clone(),
|
||||
avatar: dto.avatar.clone(),
|
||||
phone_number: dto.phone_number.clone(),
|
||||
phone_number: dto.phone_number.clone(), // Corrected from dto.phone.clone()
|
||||
is_active: dto.is_active,
|
||||
gender: dto.gender.clone(),
|
||||
birthdate: dto.birthdate.clone(),
|
||||
@@ -111,6 +112,22 @@ impl UsersDetailItemDto {
|
||||
updated_at: dto.updated_at.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_schema(schema: &UsersSchema) -> Self {
|
||||
Self {
|
||||
id: schema.id.id.to_raw(),
|
||||
role: RolesDetailItemDto::default(), // Placeholder, role needs to be fetched
|
||||
fullname: schema.fullname.clone(),
|
||||
email: schema.email.clone(),
|
||||
avatar: schema.avatar.clone(),
|
||||
phone_number: schema.phone_number.clone(),
|
||||
is_active: schema.is_active,
|
||||
gender: schema.gender.clone(),
|
||||
birthdate: schema.birthdate.clone(),
|
||||
created_at: schema.created_at.clone(),
|
||||
updated_at: schema.updated_at.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
@@ -131,7 +148,7 @@ pub struct UsersListQueryDto {
|
||||
pub id: Thing,
|
||||
pub role: RolesDetailQueryDto,
|
||||
pub fullname: String,
|
||||
pub email: String,
|
||||
pub email: String, // Corrected from pub pub email: String,
|
||||
pub avatar: Option<String>,
|
||||
pub phone_number: String,
|
||||
pub is_active: bool,
|
||||
|
||||
@@ -14,12 +14,33 @@ use axum::{http::StatusCode, response::Response};
|
||||
use imphnen_libs::{ResourceEnum, hash_password, verify_password};
|
||||
use imphnen_utils::make_thing;
|
||||
use uuid::Uuid;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use crate::v1::users::users_dto::{UsersDetailItemDto as UserDto, UsersCreateRequestDto as CreateUserDto, UsersDetailQueryDto};
|
||||
|
||||
#[async_trait]
|
||||
pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
async fn get_user_list(state: &AppState, meta: MetaRequestDto) -> Response;
|
||||
async fn get_user_by_id(state: &AppState, id: String) -> Response;
|
||||
async fn get_user_me(headers: HeaderMap, state: &AppState) -> Response;
|
||||
async fn create_user(state: &AppState, new_user: UsersCreateRequestDto) -> Response;
|
||||
async fn update_user(state: &AppState, id: String, user: UsersUpdateRequestDto) -> Response;
|
||||
async fn update_user_me(headers: HeaderMap, state: &AppState, user: UsersUpdateRequestDto) -> Response;
|
||||
async fn set_user_active_status(state: &AppState, id: String, payload: UsersActiveInactiveRequestDto) -> Response;
|
||||
async fn update_user_password(state: &AppState, email: String, payload: UsersSetNewPasswordRequestDto) -> Response;
|
||||
async fn get_user_by_mentor_id(state: &AppState, mentor_id: String) -> Response;
|
||||
async fn delete_user(state: &AppState, id: String) -> Response;
|
||||
|
||||
async fn get_user_by_email(&self, email: &str) -> Result<Option<UserDto>>;
|
||||
async fn create_user_by_dto(&self, new_user: CreateUserDto) -> Result<UserDto>;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct UsersService;
|
||||
|
||||
impl UsersService {
|
||||
pub async fn get_user_list(state: &AppState, meta: MetaRequestDto) -> Response {
|
||||
#[async_trait]
|
||||
impl UsersServiceTrait for UsersService {
|
||||
async fn get_user_list(state: &AppState, meta: MetaRequestDto) -> Response {
|
||||
let repo = UsersRepository::new(state);
|
||||
match repo.query_user_list(meta).await {
|
||||
Ok(data) => {
|
||||
@@ -33,7 +54,7 @@ impl UsersService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_user_by_id(state: &AppState, id: String) -> Response {
|
||||
async fn get_user_by_id(state: &AppState, id: String) -> Response {
|
||||
if Uuid::parse_str(&id).is_err() {
|
||||
return common_response(StatusCode::BAD_REQUEST, "Invalid User ID format");
|
||||
}
|
||||
@@ -41,14 +62,14 @@ impl UsersService {
|
||||
let thing_id = make_thing(&ResourceEnum::Users.to_string(), &id);
|
||||
match repo.query_user_by_id(&thing_id).await {
|
||||
Ok(user) if !user.is_deleted => success_response(ResponseSuccessDto {
|
||||
data: UsersDetailItemDto::from(&user),
|
||||
data: UserDto::from(&user), // Corrected to use UserDto::from by reference
|
||||
}),
|
||||
Ok(_) => common_response(StatusCode::NOT_FOUND, "User not found"),
|
||||
Err(e) => common_response(StatusCode::NOT_FOUND, &e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_user_me(headers: HeaderMap, state: &AppState) -> Response {
|
||||
async fn get_user_me(headers: HeaderMap, state: &AppState) -> Response {
|
||||
let repo = UsersRepository::new(state);
|
||||
let email = match extract_email(&headers) {
|
||||
Some(email) => email,
|
||||
@@ -56,14 +77,14 @@ impl UsersService {
|
||||
};
|
||||
match repo.query_user_by_email(email).await {
|
||||
Ok(user) if !user.is_deleted => success_response(ResponseSuccessDto {
|
||||
data: UsersDetailItemDto::from(&user),
|
||||
data: UserDto::from(&user), // Corrected to use UserDto::from by reference
|
||||
}),
|
||||
Ok(_) => common_response(StatusCode::NOT_FOUND, "User not found"),
|
||||
Err(e) => common_response(StatusCode::NOT_FOUND, &e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_user(
|
||||
async fn create_user(
|
||||
state: &AppState,
|
||||
new_user: UsersCreateRequestDto,
|
||||
) -> Response {
|
||||
@@ -86,7 +107,7 @@ impl UsersService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_user(
|
||||
async fn update_user(
|
||||
state: &AppState,
|
||||
id: String,
|
||||
user: UsersUpdateRequestDto,
|
||||
@@ -105,9 +126,9 @@ impl UsersService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_user_me(
|
||||
state: &AppState,
|
||||
async fn update_user_me(
|
||||
headers: HeaderMap,
|
||||
state: &AppState,
|
||||
user: UsersUpdateRequestDto,
|
||||
) -> Response {
|
||||
let repo = UsersRepository::new(state);
|
||||
@@ -129,7 +150,7 @@ impl UsersService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn set_user_active_status(
|
||||
async fn set_user_active_status(
|
||||
state: &AppState,
|
||||
id: String,
|
||||
payload: UsersActiveInactiveRequestDto,
|
||||
@@ -156,7 +177,7 @@ impl UsersService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_user_password(
|
||||
async fn update_user_password(
|
||||
state: &AppState,
|
||||
email: String,
|
||||
payload: UsersSetNewPasswordRequestDto,
|
||||
@@ -199,7 +220,7 @@ impl UsersService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_user_by_mentor_id(
|
||||
async fn get_user_by_mentor_id(
|
||||
state: &AppState,
|
||||
mentor_id: String,
|
||||
) -> Response {
|
||||
@@ -207,14 +228,14 @@ impl UsersService {
|
||||
let thing_id = make_thing(&ResourceEnum::Mentors.to_string(), &mentor_id);
|
||||
match repo.query_user_by_id(&thing_id).await {
|
||||
Ok(user) if !user.is_deleted => success_response(ResponseSuccessDto {
|
||||
data: UsersDetailItemDto::from(&user),
|
||||
data: UserDto::from(&user), // Corrected to use UserDto::from by reference
|
||||
}),
|
||||
Ok(_) => common_response(StatusCode::NOT_FOUND, "User not found"),
|
||||
Err(e) => common_response(StatusCode::NOT_FOUND, &e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_user(state: &AppState, id: String) -> Response {
|
||||
async fn delete_user(state: &AppState, id: String) -> Response {
|
||||
if Uuid::parse_str(&id).is_err() {
|
||||
return common_response(StatusCode::BAD_REQUEST, "Invalid User ID format");
|
||||
}
|
||||
@@ -228,4 +249,43 @@ impl UsersService {
|
||||
Err(e) => common_response(StatusCode::BAD_REQUEST, &e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_user_by_email(&self, email: &str) -> Result<Option<UserDto>> {
|
||||
let state = AppState {
|
||||
surrealdb_ws: todo!(),
|
||||
surrealdb_mem: todo!(),
|
||||
};
|
||||
let repo = UsersRepository::new(&state);
|
||||
let user = repo.query_user_by_email(email.to_string()).await;
|
||||
match user {
|
||||
Ok(u) => Ok(Some(UserDto::from(&u))), // Corrected to use UserDto::from by reference
|
||||
Err(e) if e.to_string().contains("User not found") => Ok(None),
|
||||
Err(e) => Err(anyhow::anyhow!(e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_user_by_dto(&self, new_user: CreateUserDto) -> Result<UserDto> {
|
||||
let state = AppState {
|
||||
surrealdb_ws: todo!(),
|
||||
surrealdb_mem: todo!(),
|
||||
};
|
||||
let repo = UsersRepository::new(&state);
|
||||
let user_schema = UsersSchema {
|
||||
email: new_user.email,
|
||||
password: new_user.password, // No unwrap_or_default needed
|
||||
fullname: new_user.fullname,
|
||||
phone_number: new_user.phone_number, // No unwrap_or_default needed
|
||||
is_active: new_user.is_active, // No unwrap_or needed
|
||||
role: make_thing(&ResourceEnum::Roles.to_string(), &new_user.role_id),
|
||||
..Default::default()
|
||||
};
|
||||
match repo.query_create_user(user_schema).await {
|
||||
Ok(msg) => { // msg is String, not UsersDetailQueryDto
|
||||
// Re-fetch the created user to get the full UsersDetailQueryDto
|
||||
let created_user = repo.query_user_by_email(new_user.email.clone()).await?; // Cloned email
|
||||
Ok(UserDto::from(&created_user)) // Corrected to use UserDto::from by reference
|
||||
},
|
||||
Err(e) => Err(anyhow::anyhow!(e.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,10 @@ pub struct Env {
|
||||
pub minio_bucket_name: String,
|
||||
pub minio_access_key: String,
|
||||
pub minio_secret_key: String,
|
||||
// Google OAuth 2.1
|
||||
pub google_client_id: String,
|
||||
pub google_client_secret: String,
|
||||
pub google_redirect_url: String,
|
||||
}
|
||||
|
||||
/// Helper to get env var with warning if not set.
|
||||
@@ -74,5 +78,9 @@ pub static ENV: Lazy<Env> = Lazy::new(|| {
|
||||
minio_access_key: get_env_with_warning("MINIO_ACCESS_KEY", "minio_access"),
|
||||
minio_secret_key: get_env_with_warning("MINIO_SECRET_KEY", "minio_secret"),
|
||||
surrealdb_url_ws: String::new(),
|
||||
// Google OAuth 2.1
|
||||
google_client_id: get_env_with_warning("GOOGLE_CLIENT_ID", "default_google_client_id"),
|
||||
google_client_secret: get_env_with_warning("GOOGLE_CLIENT_SECRET", "default_google_client_secret"),
|
||||
google_redirect_url: get_env_with_warning("GOOGLE_REDIRECT_URL", "http://localhost:8000/api/v1/auth/google/callback"),
|
||||
}
|
||||
});
|
||||
|
||||
@@ -86,5 +86,9 @@ pub fn decode_refresh_token(
|
||||
&Validation::default(),
|
||||
)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR);
|
||||
result
|
||||
result // Explicitly return result
|
||||
}
|
||||
|
||||
pub fn generate_jwt(user_id: &str) -> Result<String, StatusCode> {
|
||||
encode_access_token(user_id.to_string())
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ mod auth_login_tests {
|
||||
use crate::mock_test::setup_all_test_environment;
|
||||
use axum::http::StatusCode;
|
||||
use imphnen_iam::{
|
||||
v1::auth::{AuthLoginRequestDto, AuthService},
|
||||
v1::auth::{AuthLoginRequestDto, AuthService, AuthServiceTrait}, // Import AuthServiceTrait
|
||||
AppState, UsersRepository, UsersSchema,
|
||||
};
|
||||
use serde_json::Value; // Import the new setup function
|
||||
@@ -81,7 +81,7 @@ mod auth_login_tests {
|
||||
password: password.to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, body) = response.into_parts();
|
||||
|
||||
assert_eq!(parts.status, StatusCode::OK);
|
||||
@@ -108,7 +108,7 @@ mod auth_login_tests {
|
||||
password: "password".to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, body) = response.into_parts();
|
||||
|
||||
assert_eq!(parts.status, StatusCode::BAD_REQUEST);
|
||||
@@ -127,7 +127,7 @@ mod auth_login_tests {
|
||||
password: "password".to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, body) = response.into_parts();
|
||||
|
||||
assert_eq!(parts.status, StatusCode::BAD_REQUEST);
|
||||
@@ -147,7 +147,7 @@ mod auth_login_tests {
|
||||
password: "".to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, body) = response.into_parts();
|
||||
|
||||
assert_eq!(parts.status, StatusCode::BAD_REQUEST);
|
||||
@@ -169,7 +169,7 @@ mod auth_login_tests {
|
||||
password: "WrongPassword123!".to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, body) = response.into_parts();
|
||||
|
||||
assert_eq!(parts.status, StatusCode::BAD_REQUEST);
|
||||
@@ -189,7 +189,7 @@ mod auth_login_tests {
|
||||
password: "password".to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, body) = response.into_parts();
|
||||
|
||||
assert_eq!(parts.status, StatusCode::UNAUTHORIZED);
|
||||
@@ -215,7 +215,7 @@ mod auth_login_tests {
|
||||
password: password.to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, body) = response.into_parts();
|
||||
|
||||
assert_eq!(parts.status, StatusCode::BAD_REQUEST);
|
||||
@@ -242,7 +242,7 @@ mod auth_login_tests {
|
||||
password: password.to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_mentor_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_mentor_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, body) = response.into_parts();
|
||||
|
||||
assert_eq!(parts.status, StatusCode::OK);
|
||||
@@ -267,7 +267,7 @@ mod auth_login_tests {
|
||||
password: password.to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_mentor_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_mentor_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, body) = response.into_parts();
|
||||
|
||||
assert_eq!(parts.status, StatusCode::FORBIDDEN);
|
||||
@@ -294,7 +294,7 @@ mod auth_login_tests {
|
||||
password: password.to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_mentor_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_mentor_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, body) = response.into_parts();
|
||||
|
||||
assert_eq!(parts.status, StatusCode::BAD_REQUEST);
|
||||
@@ -321,7 +321,7 @@ mod auth_login_tests {
|
||||
password: password.to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, _) = response.into_parts();
|
||||
|
||||
assert_eq!(parts.status, StatusCode::OK);
|
||||
@@ -346,7 +346,7 @@ mod auth_login_tests {
|
||||
password: password.to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, _) = response.into_parts();
|
||||
|
||||
assert_eq!(parts.status, StatusCode::OK);
|
||||
@@ -365,7 +365,7 @@ mod auth_login_tests {
|
||||
password: password.to_string(),
|
||||
};
|
||||
|
||||
let response = AuthService::mutation_login(login_dto, &state).await;
|
||||
let response = AuthService::mutation_login(login_dto, &state).await; // Corrected call
|
||||
let (parts, body) = response.into_parts();
|
||||
|
||||
// Email should be case-sensitive
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{self, Request, StatusCode},
|
||||
Router,
|
||||
};
|
||||
use http_body_util::BodyExt; // for `collect` and `to_bytes`
|
||||
use tower::ServiceExt; // for `call`, `oneshot`, and `ready`
|
||||
use mockall::predicate::*;
|
||||
use mockall::mock;
|
||||
use serde_json::json;
|
||||
|
||||
use imphnen_iam::v1::auth::google::google_oauth_controller::GoogleOauthController;
|
||||
use imphnen_iam::v1::auth::google::google_oauth_service::{AuthRequest, GoogleOauthService, GoogleOauthServiceImpl};
|
||||
use imphnen_iam::v1::users::users_dto::{UserDto, CreateUserDto};
|
||||
use imphnen_entities::error_dto::ErrorResponse;
|
||||
use imphnen_libs::jsonwebtoken::generate_jwt;
|
||||
|
||||
mock! {
|
||||
pub GoogleOauthServiceMock {}
|
||||
impl GoogleOauthService for GoogleOauthServiceMock {
|
||||
fn new() -> Self;
|
||||
fn with_services(auth_service: crate::v1::auth::auth_service::AuthService, users_service: crate::v1::users::users_service::UsersService) -> Self;
|
||||
fn google_oauth_client(&self) -> oauth2::basic::BasicClient;
|
||||
fn generate_auth_url(&self) -> (url::Url, oauth2::CsrfToken);
|
||||
async fn google_oauth_callback(&self, auth_request: AuthRequest) -> anyhow::Result<String, ErrorResponse>;
|
||||
}
|
||||
}
|
||||
|
||||
mock! {
|
||||
pub AuthServiceMock {}
|
||||
impl crate::v1::auth::auth_service::AuthServiceTrait for AuthServiceMock {
|
||||
async fn mutation_login(payload: crate::v1::auth::AuthLoginRequestDto, state: &crate::AppState) -> axum::response::Response;
|
||||
async fn mutation_mentor_login(payload: crate::v1::auth::AuthLoginRequestDto, state: &crate::AppState) -> axum::response::Response;
|
||||
async fn mutation_register(payload: crate::v1::auth::AuthRegisterRequestDto, state: &crate::AppState) -> axum::response::Response;
|
||||
async fn mutation_resend_otp(payload: crate::v1::auth::AuthResendOtpRequestDto, state: &crate::AppState) -> axum::response::Response;
|
||||
async fn mutation_refresh_token(payload: crate::v1::auth::AuthRefreshTokenRequestDto) -> axum::response::Response;
|
||||
async fn mutation_forgot_password(payload: crate::v1::auth::AuthResendOtpRequestDto, state: &crate::AppState) -> axum::response::Response;
|
||||
async fn mutation_verify_email(payload: crate::v1::auth::AuthVerifyEmailRequestDto, state: &crate::AppState) -> axum::response::Response;
|
||||
async fn mutation_new_password(payload: crate::v1::auth::AuthNewPasswordRequestDto, state: &crate::AppState) -> axum::response::Response;
|
||||
}
|
||||
}
|
||||
|
||||
mock! {
|
||||
pub UsersServiceMock {}
|
||||
impl crate::v1::users::users_service::UsersServiceTrait for UsersServiceMock {
|
||||
async fn get_user_list(state: &crate::AppState, meta: crate::MetaRequestDto) -> axum::response::Response;
|
||||
async fn get_user_by_id(state: &crate::AppState, id: String) -> axum::response::Response;
|
||||
async fn get_user_me(headers: http::HeaderMap, state: &crate::AppState) -> axum::response::Response;
|
||||
async fn create_user(state: &crate::AppState, new_user: crate::v1::users::UsersCreateRequestDto) -> axum::response::Response;
|
||||
async fn update_user(state: &crate::AppState, id: String, user: crate::v1::users::UsersUpdateRequestDto) -> axum::response::Response;
|
||||
async fn update_user_me(state: &crate::AppState, headers: http::HeaderMap, user: crate::v1::users::UsersUpdateRequestDto) -> axum::response::Response;
|
||||
async fn set_user_active_status(state: &crate::AppState, id: String, payload: crate::v1::users::UsersActiveInactiveRequestDto) -> axum::response::Response;
|
||||
async fn update_user_password(state: &crate::AppState, email: String, payload: crate::v1::users::UsersSetNewPasswordRequestDto) -> axum::response::Response;
|
||||
async fn get_user_by_mentor_id(state: &crate::AppState, mentor_id: String) -> axum::response::Response;
|
||||
async fn delete_user(state: &crate::AppState, id: String) -> axum::response::Response;
|
||||
async fn get_user_by_email(&self, email: &str) -> anyhow::Result<Option<UserDto>>;
|
||||
async fn create_user_by_dto(&self, new_user: CreateUserDto) -> anyhow::Result<UserDto>;
|
||||
}
|
||||
}
|
||||
|
||||
async fn setup_app_with_mocked_google_oauth_service(
|
||||
mock_google_oauth_service: MockGoogleOauthServiceMock,
|
||||
) -> Router {
|
||||
let google_oauth_controller =
|
||||
GoogleOauthController::with_service(mock_google_oauth_service);
|
||||
google_oauth_controller.get_routes()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn google_login_redirects_to_google_auth_url() {
|
||||
let app = setup_app_with_mocked_google_oauth_service(GoogleOauthServiceImpl::new()).await;
|
||||
|
||||
let request = Request::builder()
|
||||
.uri("/google/login")
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
|
||||
let response = app.oneshot(request).await.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::FOUND);
|
||||
|
||||
let location_header = response.headers().get(http::header::LOCATION).unwrap();
|
||||
let location_str = location_header.to_str().unwrap();
|
||||
|
||||
assert!(location_str.starts_with("https://accounts.google.com/o/oauth2/v2/auth"));
|
||||
assert!(location_str.contains("client_id="));
|
||||
assert!(location_str.contains("redirect_uri="));
|
||||
assert!(location_str.contains("response_type=code"));
|
||||
assert!(location_str.contains("scope="));
|
||||
assert!(location_str.contains("state="));
|
||||
assert!(location_str.contains("access_type=offline"));
|
||||
assert!(location_str.contains("prompt=consent"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn google_callback_new_user_creates_user_and_returns_jwt() {
|
||||
let mut mock_google_oauth_service = MockGoogleOauthServiceMock::new();
|
||||
let mut mock_users_service = MockUsersServiceMock::new();
|
||||
|
||||
let expected_jwt = generate_jwt("test_user_id").unwrap();
|
||||
let user_email = "new.user@example.com".to_string();
|
||||
|
||||
mock_google_oauth_service.expect_google_oauth_callback()
|
||||
.with(eq(AuthRequest { code: "some_code".to_string(), state: "some_state".to_string() }))
|
||||
.returning(move |_| Ok(expected_jwt.clone()));
|
||||
|
||||
mock_users_service.expect_get_user_by_email()
|
||||
.with(eq(user_email.clone()))
|
||||
.returning(|_| Ok(None)); // Simulate no existing user
|
||||
|
||||
mock_users_service.expect_create_user_by_dto()
|
||||
.returning(|create_user_dto| {
|
||||
Ok(UserDto {
|
||||
id: "test_user_id".to_string(),
|
||||
email: create_user_dto.email,
|
||||
username: create_user_dto.username,
|
||||
first_name: create_user_dto.first_name,
|
||||
last_name: create_user_dto.last_name,
|
||||
is_active: create_user_dto.is_active,
|
||||
is_email_verified: create_user_dto.is_email_verified,
|
||||
created_at: chrono::Utc::now().to_rfc3339(),
|
||||
updated_at: chrono::Utc::now().to_rfc3339(),
|
||||
})
|
||||
});
|
||||
|
||||
let app = setup_app_with_mocked_google_oauth_service(
|
||||
GoogleOauthServiceImpl::with_services(
|
||||
AuthServiceMock::new(), // Not directly used by google_oauth_callback logic, but required by trait
|
||||
mock_users_service,
|
||||
)
|
||||
).await;
|
||||
|
||||
let request = Request::builder()
|
||||
.uri("/google/callback?code=some_code&state=some_state")
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
|
||||
let response = app.oneshot(request).await.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let json_body: serde_json::Value = serde_json::from_slice(&body).unwrap();
|
||||
assert_eq!(json_body["token"], expected_jwt);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn google_callback_existing_user_returns_jwt() {
|
||||
let mut mock_google_oauth_service = MockGoogleOauthServiceMock::new();
|
||||
let mut mock_users_service = MockUsersServiceMock::new();
|
||||
|
||||
let expected_jwt = generate_jwt("existing_user_id").unwrap();
|
||||
let user_email = "existing.user@example.com".to_string();
|
||||
|
||||
mock_google_oauth_service.expect_google_oauth_callback()
|
||||
.with(eq(AuthRequest { code: "some_code".to_string(), state: "some_state".to_string() }))
|
||||
.returning(move |_| Ok(expected_jwt.clone()));
|
||||
|
||||
mock_users_service.expect_get_user_by_email()
|
||||
.with(eq(user_email.clone()))
|
||||
.returning(|_| {
|
||||
Ok(Some(UserDto {
|
||||
id: "existing_user_id".to_string(),
|
||||
email: user_email.clone(),
|
||||
username: Some("existinguser".to_string()),
|
||||
first_name: Some("Existing".to_string()),
|
||||
last_name: Some("User".to_string()),
|
||||
is_active: Some(true),
|
||||
is_email_verified: Some(true),
|
||||
created_at: chrono::Utc::now().to_rfc3339(),
|
||||
updated_at: chrono::Utc::now().to_rfc3339(),
|
||||
}))
|
||||
}); // Simulate existing user
|
||||
|
||||
// Ensure create_user_by_dto is NOT called for existing user
|
||||
mock_users_service.expect_create_user_by_dto().times(0);
|
||||
|
||||
let app = setup_app_with_mocked_google_oauth_service(
|
||||
GoogleOauthServiceImpl::with_services(
|
||||
AuthServiceMock::new(), // Not directly used by google_oauth_callback logic, but required by trait
|
||||
mock_users_service,
|
||||
)
|
||||
).await;
|
||||
|
||||
let request = Request::builder()
|
||||
.uri("/google/callback?code=some_code&state=some_state")
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
|
||||
let response = app.oneshot(request).await.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let json_body: serde_json::Value = serde_json::from_slice(&body).unwrap();
|
||||
assert_eq!(json_body["token"], expected_jwt);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user