login flow

This commit is contained in:
Brandon Thomas committed 2025-04-07 17:50:48 -04:00
1 parent 448c760339
commit bf035f9048
2 files changed
+69 -15

No files matched your search

@@ -2,12 +2,7 @@ use log::{error, info};
use once_cell::sync::Lazy;
use tauri::{AppHandle, Manager, Url, WebviewUrl, WebviewWindowBuilder, WindowBuilder};
use errors::AnyhowResult;
const LOGIN_URL_STR : &str = "https://auth.openai.com/log-in";
static LOGIN_URL: Lazy<Url> = Lazy::new(|| {
Url::parse("https://auth.openai.com/log-in").expect("URL should parse")
});
use crate::threads::login_thread::{LOGIN_WINDOW_NAME, SORA_LOGIN_URL};
#[tauri::command]
pub async fn open_login_command(
@@ -28,10 +23,9 @@ pub async fn open_login_command(
async fn do_open_login(app: &AppHandle) -> AnyhowResult<()> {
info!("Building login window...");
let url = WebviewUrl::External(LOGIN_URL.clone());
let url = WebviewUrl::External(SORA_LOGIN_URL.clone());
//let window = WindowBuilder::new(app, "login")
let window = WebviewWindowBuilder::new(app, "login", url)
let _window = WebviewWindowBuilder::new(app, LOGIN_WINDOW_NAME, url)
.always_on_top(true)
.title("Login to OpenAI")
.center()
@@ -39,6 +33,7 @@ async fn do_open_login(app: &AppHandle) -> AnyhowResult<()> {
.visible(true)
.closable(true)
.focused(true)
.devtools(true)
.build()?;
//info!("Iterating webviews...");
@@ -1,19 +1,78 @@
use anyhow::anyhow;
use log::{error, info};
use once_cell::sync::Lazy;
use reqwest::Url;
use tauri::{AppHandle, Manager, Webview};
use errors::AnyhowResult;
pub const LOGIN_WINDOW_NAME: &str = "login_window";
pub const SORA_LOGIN_URL_STR: &str = "https://sora.com/auth/login?callback_path=/";
pub static SORA_LOGIN_URL: Lazy<Url> = Lazy::new(|| {
Url::parse(SORA_LOGIN_URL_STR).expect("URL should parse")
});
pub async fn login_thread(app: AppHandle) -> ! {
loop {
/*for (w, webview) in app.webviews() {
info!("Sending webview {}", w);
if let Err(err) = random_behavior(&webview) {
error!("Error in webview : {:?}", err);
for (window_name, webview) in app.webviews() {
if window_name == LOGIN_WINDOW_NAME {
let result = check_login_window(&webview).await;
if let Err(err) = result {
error!("Error checking login window: {:?}", err);
}
break;
}
}*/
tokio::time::sleep(std::time::Duration::from_millis(5_000)).await;
}
tokio::time::sleep(std::time::Duration::from_millis(2_000)).await;
}
}
async fn check_login_window(webview: &Webview) -> AnyhowResult<()> {
clear_browsing_data_on_test_domain(webview)?;
keep_on_task(webview)?;
// TODO: Other maintenance tasks ...
Ok(())
}
fn keep_on_task(webview: &Webview) -> AnyhowResult<()> {
let url = webview.url()?;
let hostname= url.host()
.ok_or(anyhow!("no host in url"))?
.to_string();
match hostname.as_str() {
"auth.openai.com" => {},
"openai.com" => {},
"sora.com" => {},
// Third party SSO
"accounts.google.com" => {},
"accounts.youtube.com" => {},
"login.live.com" => {},
"appleid.apple.com" => {},
_ => {
info!("Non login hostname: {}", hostname);
webview.navigate(SORA_LOGIN_URL.clone())?;
}
}
Ok(())
}
/// This is just so we have a way to clear browsing data.
fn clear_browsing_data_on_test_domain(webview: &Webview) -> AnyhowResult<()> {
let url = webview.url()?;
let hostname= url.host()
.ok_or(anyhow!("no host in url"))?
.to_string();
match hostname.as_str() {
"storyteller.ai" => {
info!("Clearing all browsing data...");
webview.clear_all_browsing_data()?;
}
_ => {}
}
Ok(())
}
pub fn random_behavior(webview: &Webview) -> AnyhowResult<()> {
let url = webview.url();
//webview.navigate(url)?;