diff --git a/.github/workflows/deploy-to-production-on-merge-to-main.yaml b/.github/workflows/deploy-to-production-on-merge-to-main.yaml index 0a6c973..d47f717 100644 --- a/.github/workflows/deploy-to-production-on-merge-to-main.yaml +++ b/.github/workflows/deploy-to-production-on-merge-to-main.yaml @@ -13,7 +13,7 @@ jobs: - name: Checkout Repo uses: actions/checkout@v4 - name: Cache rust dependencies and build output - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: | ~/.cargo/ diff --git a/Cargo.lock b/Cargo.lock index ad86a85..6718bfa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -460,9 +460,9 @@ dependencies = [ [[package]] name = "bytemuck" -version = "1.14.0" +version = "1.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "374d28ec25809ee0e23827c2ab573d729e293f281dfe393500e7ad618baa61c6" +checksum = "ed2490600f404f2b94c167e31d3ed1d5f3c225a0f3b80230053b3e0b7b962bd9" [[package]] name = "byteorder" @@ -579,9 +579,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.32" +version = "0.4.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41daef31d7a747c5c847246f36de49ced6f7403b4cdabc807a97b5cc184cda7a" +checksum = "9f13690e35a5e4ace198e7beea2895d29f3a9cc55015fcebe6336bd2010af9eb" dependencies = [ "android-tzdata", "iana-time-zone", @@ -594,9 +594,9 @@ dependencies = [ [[package]] name = "ciborium" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "effd91f6c78e5a4ace8a5d3c0b6bfaec9e2baaef55f3efc00e45fb2e477ee926" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" dependencies = [ "ciborium-io", "ciborium-ll", @@ -605,18 +605,18 @@ dependencies = [ [[package]] name = "ciborium-io" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdf919175532b369853f5d5e20b26b43112613fd6fe7aee757e35f7a44642656" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" [[package]] name = "ciborium-ll" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "defaa24ecc093c77630e6c15e17c51f5e187bf35ee514f4e2d67baaa96dae22b" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" dependencies = [ "ciborium-io", - "half", + "half 2.3.1", ] [[package]] @@ -768,6 +768,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "crypto-bigint" version = "0.5.5" @@ -1348,6 +1354,16 @@ version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" +[[package]] +name = "half" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc52e53916c08643f1b56ec082790d1e86a32e58dc5268f897f313fbae7b4872" +dependencies = [ + "cfg-if", + "crunchy", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -1435,6 +1451,7 @@ dependencies = [ "tracing", "tracing-subscriber", "wasm-bindgen", + "wasm-bindgen-futures", ] [[package]] @@ -1781,9 +1798,9 @@ checksum = "71dd52191aae121e8611f1e8dc3e324dd0dd1dee1e6dd91d10ee07a3cfb4d9d8" [[package]] name = "inventory" -version = "0.3.14" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8573b2b1fb643a372c73b23f4da5f888677feef3305146d68a539250a9bccc7" +checksum = "f958d3d68f4167080a18141e10381e7634563984a537f2a49a30fd8e53ac5767" [[package]] name = "ipnet" @@ -1843,8 +1860,9 @@ checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" [[package]] name = "leptos" -version = "0.6.0-beta" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10b936412e6ff7b7d4033043f1382c687a9f1995a9871a68b7189f408133c2a0" dependencies = [ "cfg-if", "leptos_config", @@ -1862,8 +1880,9 @@ dependencies = [ [[package]] name = "leptos_axum" -version = "0.6.0-beta" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd9ee8367c3c55047a7270f071448943a771f30f34ee51d1cc0f760e9907aba" dependencies = [ "axum", "cfg-if", @@ -1885,8 +1904,9 @@ dependencies = [ [[package]] name = "leptos_config" -version = "0.6.0-beta" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a809362f6a14c740bfb38a9d8151ab06be7ccd4049a3354c1aab0f8d2bdf741" dependencies = [ "config", "regex", @@ -1897,8 +1917,9 @@ dependencies = [ [[package]] name = "leptos_dom" -version = "0.6.0-beta" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4e58dcb4c8504460eda3c57919ffa826c44a542253c6c93be75d21bfcd2b776" dependencies = [ "async-recursion", "cfg-if", @@ -1926,8 +1947,9 @@ dependencies = [ [[package]] name = "leptos_hot_reload" -version = "0.6.0-beta" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "273d7f2f1823a70944a72c8b47f925c489e38f6121808cd4c7f759b1b5efd5e6" dependencies = [ "anyhow", "camino", @@ -1943,8 +1965,9 @@ dependencies = [ [[package]] name = "leptos_integration_utils" -version = "0.6.0-beta" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "172169abef42e1d2c5c4e17d58e2521669ec600808590a03c58c6a87eabf76b0" dependencies = [ "futures", "leptos", @@ -1956,8 +1979,9 @@ dependencies = [ [[package]] name = "leptos_macro" -version = "0.6.0-beta" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "669b724389f52fa2b0c8eb55fc1cd652a6738360d0a76d966b62fb3368fc0bf7" dependencies = [ "attribute-derive", "cfg-if", @@ -1978,8 +2002,9 @@ dependencies = [ [[package]] name = "leptos_meta" -version = "0.6.0-beta" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6ae4f0934d77239f57dec8b438a2c7e8662a46c4704fed8d831f5329c6fcfb" dependencies = [ "cfg-if", "indexmap 2.1.0", @@ -1991,8 +2016,9 @@ dependencies = [ [[package]] name = "leptos_reactive" -version = "0.6.0-beta" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3624e8d4d2da4c79adc045c177be50050c70fb266d30d93892676f8100bb68aa" dependencies = [ "base64 0.21.7", "cfg-if", @@ -2018,8 +2044,9 @@ dependencies = [ [[package]] name = "leptos_router" -version = "0.6.0-beta" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0964eb6de904e102b71ed5707594edfbabe11789cc9bc097e5556574324ce7c" dependencies = [ "cached 0.45.1", "cfg-if", @@ -2049,8 +2076,9 @@ dependencies = [ [[package]] name = "leptos_server" -version = "0.6.0-beta" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b31b4f6e1afecb955011548516020feab2ce165c407508e6357ad2a29e91c18" dependencies = [ "inventory", "lazy_static", @@ -2425,18 +2453,18 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pin-project" -version = "1.1.3" +version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fda4ed1c6c173e3fc7a83629421152e01d7b1f9b7f65fb301e490e8cfc656422" +checksum = "0302c4a0442c456bd56f841aee5c3bfd17967563f6fadc9ceb9f9c23cf3807e0" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.1.3" +version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" +checksum = "266c042b60c9c76b8d53061e52b2e0d1116abc57cefc8c5cd671619a56ac3690" dependencies = [ "proc-macro2", "quote", @@ -2692,9 +2720,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.4" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b7fa1134405e2ec9353fd416b17f8dacd46c473d7d3fd1cf202706a14eb792a" +checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" dependencies = [ "aho-corasick", "memchr", @@ -2967,9 +2995,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.195" +version = "1.0.196" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" +checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32" dependencies = [ "serde_derive", ] @@ -3000,15 +3028,15 @@ version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5" dependencies = [ - "half", + "half 1.8.2", "serde", ] [[package]] name = "serde_derive" -version = "1.0.195" +version = "1.0.196" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" +checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67" dependencies = [ "proc-macro2", "quote", @@ -3017,9 +3045,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.111" +version = "1.0.112" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4" +checksum = "4d1bd37ce2324cf3bf85e5a25f96eb4baf0d5aa6eba43e7ae8958870c4ec48ed" dependencies = [ "itoa", "ryu", @@ -3090,9 +3118,9 @@ dependencies = [ [[package]] name = "serde_with" -version = "3.5.0" +version = "3.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f58c3a1b3e418f61c25b2aeb43fc6c95eaa252b8cecdda67f401943e9e08d33f" +checksum = "f5c9fdb6b00a489875b22efd4b78fe2b363b72265cc5f6eb2e2b9ee270e6140c" dependencies = [ "base64 0.21.7", "chrono", @@ -3107,9 +3135,9 @@ dependencies = [ [[package]] name = "serde_with_macros" -version = "3.5.0" +version = "3.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2068b437a31fc68f25dd7edc296b078f04b45145c199d8eed9866e45f1ff274" +checksum = "dbff351eb4b33600a2e138dfa0b10b65a238ea8ff8fb2387c422c5022a3e8298" dependencies = [ "darling 0.20.3", "proc-macro2", @@ -3119,8 +3147,9 @@ dependencies = [ [[package]] name = "server_fn" -version = "0.6.0-beta" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ede360796ba28c57625aa7cbbb25e7e87408f93cc662b68244d7dbfa1f589f" dependencies = [ "axum", "bytes", @@ -3153,8 +3182,9 @@ dependencies = [ [[package]] name = "server_fn_macro" -version = "0.6.0" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b69afff1278975e9f862661a1bc5d8944aa9b542215359a550d4054864423779" dependencies = [ "const_format", "convert_case", @@ -3166,8 +3196,9 @@ dependencies = [ [[package]] name = "server_fn_macro_default" -version = "0.6.0" -source = "git+https://github.com/leptos-rs/leptos.git?branch=main#fce2c727ab2562a181b171809c2003747420081d" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3b697f47a27c5e25361cb26838c05689aebc1659a561c4376e72e2e5165eb4c" dependencies = [ "server_fn_macro", "syn 2.0.48", @@ -3878,6 +3909,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1223296a201415c7fad14792dbefaace9bd52b62d33453ade1c5b5f07555406" dependencies = [ "cfg-if", + "serde", + "serde_json", "wasm-bindgen-macro", ] @@ -4144,9 +4177,9 @@ checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" [[package]] name = "winnow" -version = "0.5.34" +version = "0.5.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7cf47b659b318dccbd69cc4797a39ae128f533dce7902a1096044d1967b9c16" +checksum = "1931d78a9c73861da0134f453bb1f790ce49b2e30eba8410b4b79bac72b46a2d" dependencies = [ "memchr", ] diff --git a/Cargo.toml b/Cargo.toml index 0e8bfec..f945c69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,17 +19,17 @@ figment = { version = "0.10", optional = true, features = ["env", "toml"] } http = "1.0" ic-agent = { version = "0.32", optional = true } k256 = { version = "0.13", optional = true } -leptos = { version = "0.6.0-beta", features = ["nightly"], git = "https://github.com/leptos-rs/leptos.git", branch = "main" } -leptos_axum = { version = "0.6.0-beta", optional = true, git = "https://github.com/leptos-rs/leptos.git", branch = "main" } -leptos_meta = { version = "0.6.0-beta", features = ["nightly"], git = "https://github.com/leptos-rs/leptos.git", branch = "main" } -leptos_router = { version = "0.6.0-beta", features = ["nightly"], git = "https://github.com/leptos-rs/leptos.git", branch = "main" } +leptos = { version = "0.6", features = ["nightly"] } +leptos_axum = { version = "0.6", optional = true } +leptos_meta = { version = "0.6", features = ["nightly"] } +leptos_router = { version = "0.6", features = ["nightly"] } log = "0.4" oauth2 = "4.4" rand = { version = "0.8", optional = true } -reqwest = { version = "0.11", optional = true, default-features = false, features = ["json", "rustls"] } +reqwest = { version = "0.11", optional = true, default-features = false, features = ["json", "tokio-rustls"] } sec1 = { version = "0.7", optional = true } serde = { version = "1.0", features = ["derive"] } -serde_json = { version = "1.0", optional = true } +serde_json = { version = "1.0" } thiserror = "1.0" tiny-bip39 = { version = "1.0", optional = true } tokio = { version = "1.35", optional = true, features = ["rt-multi-thread", "macros"] } @@ -37,10 +37,15 @@ tower = { version = "0.4", optional = true } tower-http = { version = "0.5", optional = true, features = ["cors", "fs"] } tracing = { version = "0.1", optional = true } tracing-subscriber = { version = "0.3", optional = true } -wasm-bindgen = "0.2" +wasm-bindgen = { version = "0.2", features = ["serde-serialize"] } +wasm-bindgen-futures = { version = "0.4" } [features] -hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"] +hydrate = [ + "leptos/hydrate", + "leptos_meta/hydrate", + "leptos_router/hydrate", +] ssr = [ "dep:axum", "dep:bip32", @@ -53,7 +58,6 @@ ssr = [ "dep:rand", "dep:reqwest", "dep:sec1", - "dep:serde_json", "dep:tiny-bip39", "dep:tokio", "dep:tower", diff --git a/src/auth/agent_js.rs b/src/auth/agent_js.rs index 56dbc8c..d1302ac 100644 --- a/src/auth/agent_js.rs +++ b/src/auth/agent_js.rs @@ -9,29 +9,35 @@ struct PrincipalId { _is_principal: bool, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct DelegationIdentity { pub _inner: Vec>, pub _delegation: DelegationChain, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct DelegationChain { pub delegations: Vec, #[serde(rename = "publicKey")] pub public_key: Vec, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct SignedDelegation { pub delegation: Delegation, pub signature: Vec, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Delegation { pub pubkey: Vec, pub expiration: String, #[serde(skip_serializing_if = "Option::is_none")] pub targets: Option>, } + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct SessionResponse { + pub user_identity: String, + pub delegation_identity: DelegationIdentity, +} diff --git a/src/auth/identity.rs b/src/auth/identity.rs index 6f826ce..fba374f 100644 --- a/src/auth/identity.rs +++ b/src/auth/identity.rs @@ -17,13 +17,11 @@ use tokio::sync::RwLock; use tracing::log::info; #[server(endpoint = "generate_session")] -pub async fn generate_session() -> Result { +pub async fn generate_session() -> Result { let identity_keeper: IdentityKeeper = use_context::().unwrap(); let mut jar = - leptos_axum::extract_with_state::, IdentityKeeper, ServerFnErrorErr>( - &identity_keeper, - ) - .await?; + leptos_axum::extract_with_state::, IdentityKeeper>(&identity_keeper) + .await?; let user_identity: Option = match jar.get("user_identity") { Some(val) => Some(val.value().to_owned()), @@ -85,8 +83,11 @@ pub async fn generate_session() -> Result { Box::new(client_temp_identity.clone()), vec![signed_delegation.clone()], ); - info!("{}", client_identity.sender().unwrap()); - info!("{}", delegated_identity.sender().unwrap()); + info!("client_identity: {}", client_identity.sender().unwrap()); + info!( + "delegated_identity: {}", + delegated_identity.sender().unwrap() + ); // let sender_principal = delegated_identity.sender().unwrap().to_text(); let inner_pubkey = client_temp_identity.public_key().unwrap(); @@ -112,12 +113,12 @@ pub async fn generate_session() -> Result { public_key: signature_pubkey.clone(), }, }; - let session_response = SessionResponse { + let session_response = agent_js::SessionResponse { user_identity: user_key_pair.public_key.to_owned(), delegation_identity: shareable_delegated_identity, }; - info!("{}", user_key_pair.public_key); + info!("user_pubkey: {}", user_key_pair.public_key); let mut user_cookie = Cookie::new("user_identity", user_key_pair.public_key.to_owned()); user_cookie.set_domain("hot-or-not-web-leptos-ssr.fly.dev"); @@ -147,12 +148,6 @@ pub async fn generate_session() -> Result { // ) -> Json { // } -#[derive(Serialize, Deserialize)] -pub struct SessionResponse { - user_identity: String, - delegation_identity: agent_js::DelegationIdentity, -} - #[derive(Clone)] pub struct IdentityKeeper { pub leptos_options: LeptosOptions, @@ -160,6 +155,7 @@ pub struct IdentityKeeper { pub oauth_map: Arc>>, pub key: Key, pub oauth2_client: oauth2::basic::BasicClient, + pub reqwest_client: reqwest::Client, } impl FromRef for Key { diff --git a/src/auth/mod.rs b/src/auth/mod.rs index 38b7680..d40cbfe 100644 --- a/src/auth/mod.rs +++ b/src/auth/mod.rs @@ -1,6 +1,6 @@ cfg_if::cfg_if! { if #[cfg(feature = "ssr")] { -pub mod agent_js; pub mod generate; pub mod identity; }} +pub mod agent_js; diff --git a/src/main.rs b/src/main.rs index 506dc5c..586c35e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,6 @@ mod handlers { path: Path, request: Request, ) -> impl IntoResponse { - info!("{:?}", path); handle_server_fns_with_context( move || { provide_context(app_state.clone()); @@ -80,6 +79,7 @@ async fn main() { key: Key::from(auth_config.auth_sign_key.as_bytes()), routes: routes.clone(), oauth2_client, + reqwest_client: reqwest::Client::new(), }; let identity_keeper: identity::IdentityKeeper = identity_keeper; let service = ServiceBuilder::new().layer(CorsLayer::permissive()); diff --git a/src/page/login.rs b/src/page/login.rs index 72f0963..230cfa7 100644 --- a/src/page/login.rs +++ b/src/page/login.rs @@ -3,16 +3,8 @@ use leptos_router::A; #[component] pub fn LandingPage() -> impl IntoView { - // use crate::providers::google::GoogleAuthUrl; - - // let oauth2_url = Action::::server(); - // create_effect(move |_| { - // if let Some(Ok(redirect)) = oauth2_url.value().get() { - // // let navigate = leptos_router::use_navigate(); - // // navigate(&redirect, Default::default()); - // window().location().set_href(&redirect).unwrap(); - // } - // }); + // check if valid cookie with session + // if not show error message view! {
diff --git a/src/page/test.rs b/src/page/test.rs new file mode 100644 index 0000000..e776b67 --- /dev/null +++ b/src/page/test.rs @@ -0,0 +1,11 @@ +use leptos::*; + +#[component] +pub fn Test() -> impl IntoView { + create_effect(move |_| {}); + + view! { +
+
+ } +} diff --git a/src/providers/google.rs b/src/providers/google.rs index 1138ecf..216638f 100644 --- a/src/providers/google.rs +++ b/src/providers/google.rs @@ -1,3 +1,4 @@ +use crate::auth::agent_js::SessionResponse; use cfg_if::cfg_if; use leptos::SignalGet; use leptos::*; @@ -7,11 +8,11 @@ use oauth2::TokenResponse; cfg_if! { if #[cfg(feature="ssr")] { use axum::{http::header, response::IntoResponse}; -use axum_extra::extract::cookie::{Cookie, Key, PrivateCookieJar}; -use crate::auth::identity::IdentityKeeper; +use axum_extra::extract::cookie::{Cookie, Key, PrivateCookieJar, SignedCookieJar}; +use crate::auth::{identity::{IdentityKeeper, generate_session}}; use leptos_axum::ResponseOptions; -use oauth2::{reqwest::{http_client}, AuthorizationCode, CsrfToken, PkceCodeVerifier, PkceCodeChallenge, Scope}; -use tracing::log::info; +use oauth2::{reqwest::{async_http_client}, AuthorizationCode, CsrfToken, PkceCodeVerifier, PkceCodeChallenge, Scope}; +use tracing::log::{info, error}; } } @@ -19,11 +20,22 @@ use tracing::log::info; async fn google_auth_url() -> Result { let identity_keeper = use_context::().ok_or_else(|| ServerFnError::new("Context not found!"))?; + + // enable after integration + let signed_jar: SignedCookieJar = + leptos_axum::extract_with_state::, IdentityKeeper>(&identity_keeper) + .await?; + /* + let _user_identity = match signed_jar.get("user_identity") { + Some(val) => Some(val.value().to_owned()), + None => None, + } + .ok_or_else(|| ServerFnError::new("User Session not found."))?; + */ + let mut jar: PrivateCookieJar = - leptos_axum::extract_with_state::, IdentityKeeper, ServerFnErrorErr>( - &identity_keeper, - ) - .await?; + leptos_axum::extract_with_state::, IdentityKeeper>(&identity_keeper) + .await?; let client = identity_keeper.oauth2_client; // Generate a PKCE challenge. @@ -34,7 +46,6 @@ async fn google_auth_url() -> Result { .authorize_url(CsrfToken::new_random) // Set the desired scopes. .add_scope(Scope::new("openid".to_string())) - .add_scope(Scope::new("email".to_string())) // Set the PKCE code challenge. .set_pkce_challenge(pkce_challenge) .url(); @@ -46,18 +57,23 @@ async fn google_auth_url() -> Result { info!("b4 csrf sec: {}", csrf_token); let mut pkce_verifier = Cookie::new("pkce_verifier", pkce_verifier.to_owned()); - pkce_verifier.set_domain("hot-or-not-web-leptos-ssr.fly.dev"); + // pkce_verifier.set_domain("hot-or-not-web-leptos-ssr.fly.dev"); + pkce_verifier.set_domain("localhost"); pkce_verifier.set_http_only(true); + jar = jar.remove(Cookie::from("pkce_verifier")); jar = jar.add(pkce_verifier.clone()); let mut csrf_token = Cookie::new("csrf_token", csrf_token.to_owned()); - csrf_token.set_domain("hot-or-not-web-leptos-ssr.fly.dev"); + // csrf_token.set_domain("hot-or-not-web-leptos-ssr.fly.dev"); + csrf_token.set_domain("localhost"); csrf_token.set_http_only(true); + jar = jar.remove(Cookie::from("csrf_token")); jar = jar.add(csrf_token.clone()); let jar_into_response = jar.into_response(); let response = expect_context::(); for header_value in jar_into_response.headers().get_all(header::SET_COOKIE) { + info!("Adding cookie: {:?}", header_value); response.append_header(header::SET_COOKIE, header_value.clone()); } @@ -85,59 +101,59 @@ pub fn Login() -> impl IntoView { async fn google_verify_response( provided_csrf: String, code: String, -) -> Result<(String, u64), ServerFnError> { +) -> Result { let identity_keeper = use_context::().ok_or_else(|| ServerFnError::new("Context not found!"))?; - let mut jar: PrivateCookieJar = - leptos_axum::extract_with_state::, IdentityKeeper, ServerFnErrorErr>( - &identity_keeper, - ) - .await?; + let jar: PrivateCookieJar = + leptos_axum::extract_with_state::, IdentityKeeper>(&identity_keeper) + .await?; + let client = identity_keeper.oauth2_client; - let csrf_token: Option = match jar.get("csrf_token") { - Some(val) => Some(val.value().to_owned()), - None => None, - }; - match csrf_token.clone() { - Some(csrf) => { - if !csrf.eq(&provided_csrf) { - return Err(ServerFnError::new("Invalid CSRF token!")); - } - } - None => return Err(ServerFnError::new("No CSRF token!")), + let csrf_token = jar + .get("csrf_token") + .map(|cookie| cookie.value().to_owned()) + .ok_or_else(|| ServerFnError::new("No CSRF token found!"))?; + if !csrf_token.eq(&provided_csrf) { + return Err(ServerFnError::new("Invalid CSRF token!")); } - let pkce_verifier: Option = match jar.get("pkce_verifier") { - Some(val) => Some(val.value().to_owned()), - None => None, - }; - info!("aftr pkce sec: {}", pkce_verifier.clone().unwrap()); - info!("aftr csrf sec: {}", csrf_token.clone().unwrap()); + let pkce_verifier = jar + .get("pkce_verifier") + .map(|cookie| cookie.value().to_owned()) + .ok_or_else(|| ServerFnError::new("No Verifier found!"))?; + + info!("aftr pkce sec: {}", pkce_verifier); + info!("aftr csrf sec: {}", csrf_token); - let pkce_verifier = PkceCodeVerifier::new(pkce_verifier.unwrap()); + let pkce_verifier = PkceCodeVerifier::new(pkce_verifier); let token_result = client .exchange_code(AuthorizationCode::new(code.clone())) .set_pkce_verifier(pkce_verifier) - .request(http_client)?; + .request_async(async_http_client) + .await?; - info!("{:?}", &token_result); + info!("token_result: {:?}", &token_result); let access_token = token_result.access_token().secret(); let expires_in = token_result.expires_in().unwrap().as_secs(); - let refresh_secret = token_result.refresh_token().unwrap().secret(); + match token_result.refresh_token() { + Some(secret) => info!("secret: {:?}", secret), + None => {} + } let user_info_url = "https://www.googleapis.com/oauth2/v3/userinfo"; - let client = reqwest::Client::new(); - let response = client + let response = identity_keeper + .reqwest_client .get(user_info_url) .bearer_auth(access_token) .send() .await?; - let email = if response.status().is_success() { + let sub_openid = if response.status().is_success() { let response_json: serde_json::Value = response.json().await?; - leptos::logging::log!("{response_json:?}"); - response_json["email"] + info!("response_json: {response_json:?}"); + response_json["sub"] .as_str() - .expect("email to parse to string") + .expect("openid sub to parse to string") .to_string() } else { + error!("Response status failed: {:?}", response); return Err(ServerFnError::ServerError(format!( "Response from google has status of {}", response.status() @@ -146,29 +162,41 @@ async fn google_verify_response( let access_token = token_result.access_token().secret(); info!("aftr access_token: {:?}", access_token); + // TODO: add to user map for reference + let session_response = generate_session().await?; - Ok((email, expires_in as u64)) + Ok(session_response) +} + +#[wasm_bindgen::prelude::wasm_bindgen] +extern "C" { + #[wasm_bindgen::prelude::wasm_bindgen(js_namespace = ["window", "top"])] + pub fn postMessage(message: &str, target_origin: &str); } #[component] pub fn OAuth2Response() -> impl IntoView { - let handle_g_auth_redirect = Action::::server(); - let (email, set_email) = create_signal("".to_owned()); + let handle_oauth2_redirect = Action::::server(); let query = use_query::(); - let navigate = leptos_router::use_navigate(); + // let navigate = leptos_router::use_navigate(); create_effect(move |_| { - if let Some(Ok((email, expires_in))) = handle_g_auth_redirect.value().get() { - leptos::logging::log!("{}", email); - leptos::logging::log!("{}", expires_in); - set_email.set(email); + if let Some(Ok(session_response)) = handle_oauth2_redirect.value().get() { + leptos::logging::log!("session response: {:?}", session_response); + // TODO: targetOrigin to be updated from config + match serde_json::to_string(&session_response) { + Ok(session) => postMessage(session.as_str(), "*"), + Err(error) => { + postMessage(error.to_string().as_str(), "*"); + } + } // navigate("/", NavigateOptions::default()); } }); create_effect(move |_| { if let Ok(OAuthParams { code, state }) = query.get_untracked() { - handle_g_auth_redirect.dispatch(GoogleVerifyResponse { + handle_oauth2_redirect.dispatch(GoogleVerifyResponse { provided_csrf: state.unwrap(), code: code.unwrap(), }); @@ -178,7 +206,6 @@ pub fn OAuth2Response() -> impl IntoView { }); view! {
- "email: " {email.get()}
} }