2023-06-07 17:45:13 +10:00
|
|
|
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
|
|
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
|
|
|
|
2023-11-29 05:15:04 -05:00
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
2023-06-19 10:16:45 +10:00
|
|
|
use std::io::Read;
|
|
|
|
|
|
|
|
use anyhow::Result;
|
2023-07-13 19:05:56 -07:00
|
|
|
use oauth2::TokenResponse;
|
|
|
|
use tauri::{InvokeError, Manager};
|
2023-09-19 14:08:26 -04:00
|
|
|
const DEFAULT_HOST: &str = "https://api.kittycad.io";
|
2023-06-19 10:16:45 +10:00
|
|
|
|
|
|
|
/// This command returns the a json string parse from a toml file at the path.
|
|
|
|
#[tauri::command]
|
|
|
|
fn read_toml(path: &str) -> Result<String, InvokeError> {
|
2023-07-13 19:05:56 -07:00
|
|
|
let mut file = std::fs::File::open(path).map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
|
|
|
let mut contents = String::new();
|
|
|
|
file.read_to_string(&mut contents)
|
|
|
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
|
|
|
let value =
|
|
|
|
toml::from_str::<toml::Value>(&contents).map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
|
|
|
let value = serde_json::to_string(&value).map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
|
|
|
Ok(value)
|
2023-06-19 10:16:45 +10:00
|
|
|
}
|
|
|
|
|
2023-07-13 19:05:56 -07:00
|
|
|
/// This command returns a string that is the contents of a file at the path.
|
2023-06-19 10:16:45 +10:00
|
|
|
#[tauri::command]
|
|
|
|
fn read_txt_file(path: &str) -> Result<String, InvokeError> {
|
2023-07-13 19:05:56 -07:00
|
|
|
let mut file = std::fs::File::open(path).map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
|
|
|
let mut contents = String::new();
|
|
|
|
file.read_to_string(&mut contents)
|
|
|
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
|
|
|
Ok(contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This command instantiates a new window with auth.
|
|
|
|
/// The string returned from this method is the access token.
|
|
|
|
#[tauri::command]
|
2023-08-08 09:06:14 +10:00
|
|
|
async fn login(app: tauri::AppHandle, host: &str) -> Result<String, InvokeError> {
|
2023-07-13 19:05:56 -07:00
|
|
|
println!("Logging in...");
|
|
|
|
// Do an OAuth 2.0 Device Authorization Grant dance to get a token.
|
2023-08-08 09:06:14 +10:00
|
|
|
let device_auth_url = oauth2::DeviceAuthorizationUrl::new(format!("{host}/oauth2/device/auth"))
|
2023-07-13 19:05:56 -07:00
|
|
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
|
|
|
// We can hardcode the client ID.
|
|
|
|
// This value is safe to be embedded in version control.
|
|
|
|
// This is the client ID of the KittyCAD app.
|
|
|
|
let client_id = "2af127fb-e14e-400a-9c57-a9ed08d1a5b7".to_string();
|
|
|
|
let auth_client = oauth2::basic::BasicClient::new(
|
|
|
|
oauth2::ClientId::new(client_id),
|
|
|
|
None,
|
2023-08-08 09:06:14 +10:00
|
|
|
oauth2::AuthUrl::new(format!("{host}/authorize"))
|
2023-07-13 19:05:56 -07:00
|
|
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?,
|
|
|
|
Some(
|
2023-08-08 09:06:14 +10:00
|
|
|
oauth2::TokenUrl::new(format!("{host}/oauth2/device/token"))
|
2023-07-13 19:05:56 -07:00
|
|
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.set_auth_type(oauth2::AuthType::RequestBody)
|
|
|
|
.set_device_authorization_url(device_auth_url);
|
|
|
|
|
|
|
|
let details: oauth2::devicecode::StandardDeviceAuthorizationResponse = auth_client
|
|
|
|
.exchange_device_code()
|
|
|
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?
|
|
|
|
.request_async(oauth2::reqwest::async_http_client)
|
|
|
|
.await
|
|
|
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
|
|
|
|
|
|
|
let Some(auth_uri) = details.verification_uri_complete() else {
|
|
|
|
return Err(InvokeError::from("getting the verification uri failed"));
|
|
|
|
};
|
|
|
|
|
|
|
|
// Open the system browser with the auth_uri.
|
2023-11-01 17:34:54 -05:00
|
|
|
// We do this in the browser and not a separate window because we want 1password and
|
2023-07-13 19:05:56 -07:00
|
|
|
// other crap to work well.
|
2023-11-29 05:15:04 -05:00
|
|
|
// TODO: find a better way to share this value with tauri e2e tests
|
|
|
|
// Here we're using an env var to enable the /tmp file (windows not supported for now)
|
|
|
|
// and bypass the shell::open call as it fails on GitHub Actions.
|
|
|
|
let e2e_tauri_enabled = env::var("E2E_TAURI_ENABLED").is_ok();
|
|
|
|
if (e2e_tauri_enabled) {
|
|
|
|
println!(
|
|
|
|
"E2E_TAURI_ENABLED is set, won't open {} externally",
|
|
|
|
auth_uri.secret()
|
|
|
|
);
|
|
|
|
fs::write(
|
|
|
|
"/tmp/kittycad_user_code",
|
|
|
|
details.user_code().secret().to_string(),
|
|
|
|
)
|
|
|
|
.expect("Unable to write /tmp/kittycad_user_code file");
|
|
|
|
} else {
|
|
|
|
tauri::api::shell::open(&app.shell_scope(), auth_uri.secret(), None)
|
|
|
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
|
|
|
}
|
2023-07-13 19:05:56 -07:00
|
|
|
|
|
|
|
// Wait for the user to login.
|
|
|
|
let token = auth_client
|
|
|
|
.exchange_device_access_token(&details)
|
|
|
|
.request_async(oauth2::reqwest::async_http_client, tokio::time::sleep, None)
|
|
|
|
.await
|
|
|
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?
|
|
|
|
.access_token()
|
|
|
|
.secret()
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
Ok(token)
|
2023-06-19 10:16:45 +10:00
|
|
|
}
|
|
|
|
|
2023-09-14 19:31:16 -04:00
|
|
|
///This command returns the KittyCAD user info given a token.
|
|
|
|
/// The string returned from this method is the user info as a json string.
|
|
|
|
#[tauri::command]
|
2023-09-19 14:08:26 -04:00
|
|
|
async fn get_user(
|
|
|
|
token: Option<String>,
|
|
|
|
hostname: &str,
|
|
|
|
) -> Result<kittycad::types::User, InvokeError> {
|
|
|
|
// Use the host passed in if it's set.
|
|
|
|
// Otherwise, use the default host.
|
|
|
|
let host = if hostname.is_empty() {
|
|
|
|
DEFAULT_HOST.to_string()
|
|
|
|
} else {
|
|
|
|
hostname.to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Change the baseURL to the one we want.
|
|
|
|
let mut baseurl = host.to_string();
|
|
|
|
if !host.starts_with("http://") && !host.starts_with("https://") {
|
|
|
|
baseurl = format!("https://{host}");
|
|
|
|
if host.starts_with("localhost") {
|
|
|
|
baseurl = format!("http://{host}")
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 19:31:16 -04:00
|
|
|
println!("Getting user info...");
|
|
|
|
|
|
|
|
// use kittycad library to fetch the user info from /user/me
|
2023-09-19 14:08:26 -04:00
|
|
|
let mut client = kittycad::Client::new(token.unwrap());
|
|
|
|
|
|
|
|
if baseurl != DEFAULT_HOST {
|
|
|
|
client.set_base_url(&baseurl);
|
|
|
|
}
|
2023-09-14 19:31:16 -04:00
|
|
|
|
|
|
|
let user_info: kittycad::types::User = client
|
|
|
|
.users()
|
|
|
|
.get_self()
|
|
|
|
.await
|
|
|
|
.map_err(|e| InvokeError::from_anyhow(e.into()))?;
|
|
|
|
|
|
|
|
Ok(user_info)
|
|
|
|
}
|
|
|
|
|
2023-06-07 17:45:13 +10:00
|
|
|
fn main() {
|
2023-07-13 19:05:56 -07:00
|
|
|
tauri::Builder::default()
|
2023-11-01 07:39:31 -04:00
|
|
|
.setup(|_app| {
|
2023-07-13 19:05:56 -07:00
|
|
|
#[cfg(debug_assertions)] // only include this code on debug builds
|
|
|
|
{
|
2023-11-01 07:39:31 -04:00
|
|
|
let window = _app.get_window("main").unwrap();
|
2023-07-13 19:05:56 -07:00
|
|
|
// comment out the below if you don't devtools to open everytime.
|
|
|
|
// it's useful because otherwise devtools shuts everytime rust code changes.
|
|
|
|
window.open_devtools();
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
2023-09-14 19:31:16 -04:00
|
|
|
.invoke_handler(tauri::generate_handler![
|
|
|
|
get_user,
|
|
|
|
login,
|
|
|
|
read_toml,
|
|
|
|
read_txt_file
|
|
|
|
])
|
2023-08-15 21:56:24 -04:00
|
|
|
.plugin(tauri_plugin_fs_extra::init())
|
2023-07-13 19:05:56 -07:00
|
|
|
.run(tauri::generate_context!())
|
|
|
|
.expect("error while running tauri application");
|
2023-06-07 17:45:13 +10:00
|
|
|
}
|