diff --git a/crates/desktop/artcraft/src/services/midjourney/windows/extract_midjourney_webview_cookies.rs b/crates/desktop/artcraft/src/services/midjourney/windows/extract_midjourney_webview_cookies.rs new file mode 100644 index 0000000000..69eccf1c01 --- /dev/null +++ b/crates/desktop/artcraft/src/services/midjourney/windows/extract_midjourney_webview_cookies.rs @@ -0,0 +1,28 @@ +use errors::AnyhowResult; +use once_cell::sync::Lazy; +use reqwest::Url; +use tauri::webview::Cookie; +use tauri::WebviewWindow; + +const ROOT_COOKIE_URL_STR: &str = "https://midjourney.com"; + +static ROOT_COOKIE_URL: Lazy = Lazy::new(|| { + Url::parse(ROOT_COOKIE_URL_STR).expect("URL should parse") +}); + +pub fn extract_midjourney_webview_cookies(webview: &WebviewWindow) -> AnyhowResult { + // NB(bt): Cookies were originally on sora.com, but now they're located on + // the sora.chatgpt.com / *.chatgpt.com domains. + let cookies = get_all_midjourney_cookies(webview)?; + let cookie_string = cookies + .iter() + .map(|cookie| format!("{}={}", cookie.name(), cookie.value())) + .collect::>() + .join("; "); + Ok(cookie_string) +} + +fn get_all_midjourney_cookies(webview: &WebviewWindow) -> AnyhowResult> { + let cookies = webview.cookies_for_url(ROOT_COOKIE_URL.clone())?; + Ok(cookies) +} diff --git a/crates/desktop/artcraft/src/services/midjourney/windows/midjourney_login_window_thread.rs b/crates/desktop/artcraft/src/services/midjourney/windows/midjourney_login_window_thread.rs index dc0cfb5dd2..0c26182886 100644 --- a/crates/desktop/artcraft/src/services/midjourney/windows/midjourney_login_window_thread.rs +++ b/crates/desktop/artcraft/src/services/midjourney/windows/midjourney_login_window_thread.rs @@ -1,6 +1,6 @@ use crate::core::events::sendable_event_trait::SendableEvent; use crate::core::state::data_dir::app_data_root::AppDataRoot; -use crate::services::midjourney::windows::open_midjourney_login_window::LOGIN_WINDOW_NAME; +use crate::services::midjourney::windows::open_midjourney_login_window::MIDJOURNEY_LOGIN_WINDOW_NAME; use crate::services::sora::events::sora_login_success_event::SoraLoginSuccessEvent; use crate::services::sora::state::sora_credential_manager::SoraCredentialManager; use crate::services::sora::windows::sora_login_window::extract_sora_webview_cookies::extract_sora_webview_cookies; @@ -11,14 +11,15 @@ use openai_sora_client::creds::sora_credential_set::SoraCredentialSet; use openai_sora_client::recipes::maybe_upgrade_or_renew_session::maybe_upgrade_or_renew_session; use openai_sora_client::utils::has_session_cookie::{has_session_cookie, SessionCookiePresence}; use tauri::{AppHandle, Manager, WebviewWindow}; +use crate::services::midjourney::windows::extract_midjourney_webview_cookies::extract_midjourney_webview_cookies; -pub async fn midjourney_login_thread( +pub async fn midjourney_login_window_thread( app: AppHandle, app_data_root: AppDataRoot, sora_creds_manager: SoraCredentialManager ) { loop { - let login_webview_window = match app.get_webview_window(LOGIN_WINDOW_NAME) { + let login_webview_window = match app.get_webview_window(MIDJOURNEY_LOGIN_WINDOW_NAME) { Some(webview) => webview, None => { info!("Exit midjourney login thread."); @@ -61,11 +62,10 @@ async fn check_login_window( /* Login flow looks like this: - 1. Start: https://sora.chatgpt.com/ - 2. Login Start: https://chatgpt.com/auth/login?next=%2Fsora%2F [this is where we start] - 3. Login Continue: https://auth.openai.com/log-in - 4. SSO / Google Login (etc): https://accounts.google.com/v3/signin/challenge/pwd?[query] - 5. Done / Landing: https://sora.chatgpt.com/explore + 1. Start: https://www.midjourney.com/auth/signin + 2. Login Continue: https://auth.openai.com/log-in + 3. SSO / Google Login (etc): https://accounts.google.com/v3/signin/challenge/pwd?[query] + 4. Done / Landing: https://sora.chatgpt.com/explore */ /* @@ -97,7 +97,7 @@ async fn check_login_window( _ => {}, // We just don't know... } - let webview_cookies = extract_sora_webview_cookies(webview_window)?.trim().to_string(); + let webview_cookies = extract_midjourney_webview_cookies(webview_window)?.trim().to_string(); let session_cookie_presence = has_session_cookie(&webview_cookies) .unwrap_or_else(|err| { @@ -105,7 +105,7 @@ async fn check_login_window( SessionCookiePresence::MaybePresent }); - info!("Sora login webview is at hostname `{}`; cookie status: {:?}.", hostname, session_cookie_presence); + info!("Midjourney login webview is at hostname `{}`; cookie status: {:?}.", hostname, session_cookie_presence); match session_cookie_presence { SessionCookiePresence::Absent => { @@ -115,6 +115,10 @@ async fn check_login_window( _ => {}, } + if true { + return Ok(false); + } + let mut new_credentials = SoraCredentialSet::initialize_with_just_cookies_str(&webview_cookies); diff --git a/crates/desktop/artcraft/src/services/midjourney/windows/mod.rs b/crates/desktop/artcraft/src/services/midjourney/windows/mod.rs index 7865b5753a..b5417787da 100644 --- a/crates/desktop/artcraft/src/services/midjourney/windows/mod.rs +++ b/crates/desktop/artcraft/src/services/midjourney/windows/mod.rs @@ -1,2 +1,3 @@ -pub mod open_midjourney_login_window; -pub mod midjourney_login_window_thread; \ No newline at end of file +pub mod extract_midjourney_webview_cookies; +pub mod midjourney_login_window_thread; +pub mod open_midjourney_login_window; \ No newline at end of file diff --git a/crates/desktop/artcraft/src/services/midjourney/windows/open_midjourney_login_window.rs b/crates/desktop/artcraft/src/services/midjourney/windows/open_midjourney_login_window.rs index 1b3e7ec395..f0bc4678c0 100644 --- a/crates/desktop/artcraft/src/services/midjourney/windows/open_midjourney_login_window.rs +++ b/crates/desktop/artcraft/src/services/midjourney/windows/open_midjourney_login_window.rs @@ -1,7 +1,7 @@ use crate::core::state::data_dir::app_data_root::AppDataRoot; use crate::core::utils::clear_all_webview_cookies::clear_all_webview_cookies; +use crate::services::midjourney::windows::midjourney_login_window_thread::midjourney_login_window_thread; use crate::services::sora::state::sora_credential_manager::SoraCredentialManager; -use crate::services::sora::windows::sora_login_window::sora_login_thread::sora_login_thread; use anyhow::anyhow; use errors::AnyhowResult; use once_cell::sync::Lazy; @@ -10,12 +10,12 @@ use std::time::Duration; use tauri::{AppHandle, Manager, WebviewUrl, WebviewWindowBuilder}; /// Name of the window -pub (super) const LOGIN_WINDOW_NAME: &str = "midjourney_login_window"; +pub (super) const MIDJOURNEY_LOGIN_WINDOW_NAME: &str = "midjourney_login_window"; -pub (super) const START_URL_STR: &str = "https://google.com"; +pub (super) const MIDJOURNEY_START_URL_STR: &str = "https://google.com"; pub (super) static START_URL: Lazy = Lazy::new(|| { - Url::parse(START_URL_STR).expect("URL should parse") + Url::parse(MIDJOURNEY_START_URL_STR).expect("URL should parse") }); pub (super) const MIDJOURNEY_HOMEPAGE_URL_STR: &str = "https://www.midjourney.com/"; @@ -35,13 +35,13 @@ pub async fn open_midjourney_login_window( app_data_root: &AppDataRoot, sora_creds_manager: &SoraCredentialManager, ) -> AnyhowResult<()> { - if app.get_window(LOGIN_WINDOW_NAME).is_some() { + if app.get_window(MIDJOURNEY_LOGIN_WINDOW_NAME).is_some() { return Err(anyhow!("Login window already open")); } let url = WebviewUrl::External(START_URL.clone()); - let window = WebviewWindowBuilder::new(app, LOGIN_WINDOW_NAME, url) + let window = WebviewWindowBuilder::new(app, MIDJOURNEY_LOGIN_WINDOW_NAME, url) //.user_agent(openai_sora_client::credentials::USER_AGENT) .always_on_top(false) .title("Login to Midjourney") @@ -54,7 +54,7 @@ pub async fn open_midjourney_login_window( .devtools(true) .build()?; - let webview = window.get_webview(LOGIN_WINDOW_NAME) + let webview = window.get_webview(MIDJOURNEY_LOGIN_WINDOW_NAME) .ok_or_else(|| anyhow!("no webview found"))?; clear_all_webview_cookies(&webview)?; @@ -71,7 +71,7 @@ pub async fn open_midjourney_login_window( let sora_creds_manager = sora_creds_manager.clone(); let _ = tauri::async_runtime::spawn(async move { - sora_login_thread(app_handle, app_data_root, sora_creds_manager).await; + midjourney_login_window_thread(app_handle, app_data_root, sora_creds_manager).await; }); Ok(())