From f345cb10ea62dd270ed11a5dcfb69063e1dba05b Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Mon, 2 Oct 2023 17:36:43 +0800 Subject: [PATCH] Use actual oauth clients + ship ids and secrets (#1415) * use actual oauth clients + ship ids and secrets * re-enable features * use prod as default api url --- apps/desktop/src-tauri/src/main.rs | 16 +++++++++++++++- apps/mobile/modules/sd-core/core/src/lib.rs | 14 +++++++++++++- apps/server/src/main.rs | 14 +++++++++++++- core/src/api/auth.rs | 11 ++++++++--- core/src/env.rs | 17 ++--------------- core/src/lib.rs | 9 +++++++-- 6 files changed, 58 insertions(+), 23 deletions(-) diff --git a/apps/desktop/src-tauri/src/main.rs b/apps/desktop/src-tauri/src/main.rs index c18c4097a..51c8782e6 100644 --- a/apps/desktop/src-tauri/src/main.rs +++ b/apps/desktop/src-tauri/src/main.rs @@ -71,6 +71,9 @@ macro_rules! tauri_handlers { }}; } +const CLIENT_ID: &str = "2abb241e-40b8-4517-a3e3-5594375c8fbb"; +const CLIENT_SECRET: &str = "eb4554cb-c08d-4e82-b4cc-0aa29c07e934"; + #[tokio::main] async fn main() -> tauri::Result<()> { #[cfg(debug_assertions)] @@ -88,7 +91,18 @@ async fn main() -> tauri::Result<()> { // The `_guard` must be assigned to variable for flushing remaining logs on main exit through Drop let (_guard, result) = match Node::init_logger(&data_dir) { - Ok(guard) => (Some(guard), Node::new(data_dir).await), + Ok(guard) => ( + Some(guard), + Node::new( + data_dir, + sd_core::Env { + api_url: "https://app.spacedrive.com".to_string(), + client_id: CLIENT_ID.to_string(), + client_secret: CLIENT_SECRET.to_string(), + }, + ) + .await, + ), Err(err) => (None, Err(NodeError::Logger(err))), }; diff --git a/apps/mobile/modules/sd-core/core/src/lib.rs b/apps/mobile/modules/sd-core/core/src/lib.rs index 16d263444..c9172ddc9 100644 --- a/apps/mobile/modules/sd-core/core/src/lib.rs +++ b/apps/mobile/modules/sd-core/core/src/lib.rs @@ -29,6 +29,9 @@ pub static SUBSCRIPTIONS: Lazy> = OnceCell::new(); +pub const CLIENT_ID: &str = "d068776a-05b6-4aaa-9001-4d01734e1944"; +pub const CLIENT_SECRET: &str = "961cdf5c-9eb1-43dc-b921-5b1dd8bbf6a5"; + pub struct MobileSender<'a> { resp: &'a mut Option, } @@ -70,7 +73,16 @@ pub fn handle_core_msg( let _guard = Node::init_logger(&data_dir); // TODO: probably don't unwrap - let new_node = Node::new(data_dir).await.unwrap(); + let new_node = Node::new( + data_dir, + sd_core::Env { + api_url: "https://app.spacedrive.com".to_string(), + client_id: CLIENT_ID.to_string(), + client_secret: CLIENT_SECRET.to_string(), + }, + ) + .await + .unwrap(); node.replace(new_node.clone()); new_node } diff --git a/apps/server/src/main.rs b/apps/server/src/main.rs index 1474e992d..46a245492 100644 --- a/apps/server/src/main.rs +++ b/apps/server/src/main.rs @@ -39,7 +39,19 @@ async fn main() { } }; - let (node, router) = match Node::new(data_dir).await { + let (node, router) = match Node::new( + data_dir, + sd_core::Env { + api_url: std::env::var("SD_API_URL") + .unwrap_or_else(|_| "https://app.spacedrive.com".to_string()), + client_id: std::env::var("SD_CLIENT_ID") + .unwrap_or_else(|_| "04701823-a498-406e-aef9-22081c1dae34".to_string()), + client_secret: std::env::var("SD_CLIENT_ID") + .unwrap_or_else(|_| "8c0e4f85-d1d3-4a0c-9445-65003ffc581d".to_string()), + }, + ) + .await + { Ok(d) => d, Err(e) => { panic!("{}", e.to_string()) diff --git a/core/src/api/auth.rs b/core/src/api/auth.rs index c2d09e6e4..8755a5249 100644 --- a/core/src/api/auth.rs +++ b/core/src/api/auth.rs @@ -57,11 +57,12 @@ pub(crate) fn mount() -> AlphaRouter { verification_uri_complete: String, } - let Ok(auth_response) = (match node.http + let Ok(auth_response) = (match node.http .post(&format!("{}/login/device/code", &node.env.api_url)) + .form(&[("client_id", &node.env.client_id)]) .send() .await { - Ok(resp) => resp.json::().await, + Ok(r) => r.json::().await, Err(e) => Err(e) }) else { yield Response::Error; @@ -79,7 +80,11 @@ pub(crate) fn mount() -> AlphaRouter { let Ok(token_resp) = node.http .post(&format!("{}/login/oauth/access_token", &node.env.api_url)) - .form(&[("grant_type", DEVICE_CODE_URN), ("device_code", &auth_response.device_code)]) + .form(&[ + ("grant_type", DEVICE_CODE_URN), + ("device_code", &auth_response.device_code), + ("client_id", &node.env.client_id) + ]) .send() .await else { break Response::Error; diff --git a/core/src/env.rs b/core/src/env.rs index 1bad174ab..0f999f78d 100644 --- a/core/src/env.rs +++ b/core/src/env.rs @@ -1,18 +1,5 @@ pub struct Env { pub api_url: String, -} - -impl Default for Env { - fn default() -> Self { - Self::new() - } -} - -impl Env { - pub fn new() -> Self { - Self { - api_url: std::env::var("SD_API_URL") - .unwrap_or_else(|_| "https://app.spacedrive.com".to_string()), - } - } + pub client_id: String, + pub client_secret: String, } diff --git a/core/src/lib.rs b/core/src/lib.rs index 3cd211db5..aa6dedc3a 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -49,6 +49,8 @@ pub(crate) mod preferences; pub mod util; pub(crate) mod volume; +pub use env::Env; + pub(crate) use sd_core_sync as sync; /// Represents a single running instance of the Spacedrive core. @@ -78,7 +80,10 @@ impl fmt::Debug for Node { } impl Node { - pub async fn new(data_dir: impl AsRef) -> Result<(Arc, Arc), NodeError> { + pub async fn new( + data_dir: impl AsRef, + env: env::Env, + ) -> Result<(Arc, Arc), NodeError> { let data_dir = data_dir.as_ref(); info!("Starting core with data directory '{}'", data_dir.display()); @@ -114,8 +119,8 @@ impl Node { ), libraries, files_over_p2p_flag: Arc::new(AtomicBool::new(false)), - env: Default::default(), http: reqwest::Client::new(), + env, }); // Restore backend feature flags