mirror of
https://github.com/infinilabs/coco-app.git
synced 2025-12-16 11:37:47 +01:00
* feat: impl Coco server related APIs * chore: remove unused method * fix: invoke Rust interfaces in tauri::run() * chore: add invoke * feat: add add_coco_server * fix: trim the tailing forward slash * feat: interface get_user_profiles * chore: add * fix: store the servers in add interface * chore: ass * fix: skip non-publich servers with no token * feat: add * feat: get datasources and connectors * fix: invoke interfaces in tauri::run() * chore: add SidebarRef * refactor: refactoring coco-app * refactor: refactoring coco app * refactor: refactoring project layout * refactor: refactoring server management * chore: cleanup code * chore: display error when connect failed * refactor: refactoring refresh server's info * refactor: refactoring how to connect the coco serverg * chore: rename to cloud * refactor: refactoring remove coco server * fix: refresh current selected server * fix: reset server selection * chore: update login status * feat: add error message tips * fix: fix login and logout * refactor: refactoring http client * fix: fix the datasources * chore: minor fix * refactor: refactoring code * fix: fix search api * chore: optimize part of icons * chore: fix build * refactor: search list icon * refactor: search list icon * chore: lib * feat: add plugin-os --------- Co-authored-by: rain <15911122312@163.com> Co-authored-by: medcl <m@medcl.net>
92 lines
4.0 KiB
Rust
92 lines
4.0 KiB
Rust
use std::fmt::format;
|
|
use reqwest::StatusCode;
|
|
use tauri::{AppHandle, Runtime};
|
|
use crate::common::auth::RequestAccessTokenResponse;
|
|
use crate::common::server::{Server, ServerAccessToken};
|
|
use crate::server::http_client::HttpClient;
|
|
use crate::server::profile::get_user_profiles;
|
|
use crate::server::servers::{get_server_by_id, persist_servers, persist_servers_token, save_access_token, save_server};
|
|
use crate::util;
|
|
fn request_access_token_url(request_id: &str) -> String {
|
|
// Remove the endpoint part and keep just the path for the request
|
|
format!("/auth/request_access_token?request_id={}", request_id)
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn handle_sso_callback<R: Runtime>(
|
|
app_handle: AppHandle<R>,
|
|
server_id: String,
|
|
request_id: String,
|
|
code: String,
|
|
) -> Result<(), String> {
|
|
// Retrieve the server details using the server ID
|
|
let server = get_server_by_id(&server_id);
|
|
|
|
if let Some(mut server) = server {
|
|
// Prepare the URL for requesting the access token (endpoint is base URL, path is relative)
|
|
save_access_token(server_id.clone(), ServerAccessToken::new(server_id.clone(), code.clone(), 60*15));
|
|
let path = request_access_token_url( &request_id);
|
|
|
|
// Send the request for the access token using the util::http::HttpClient::get method
|
|
let response = HttpClient::get(&server_id, &path)
|
|
.await
|
|
.map_err(|e| format!("Failed to send request to the server: {}", e))?;
|
|
|
|
if response.status() == StatusCode::OK {
|
|
// Check if the response has a valid content length
|
|
if let Some(content_length) = response.content_length() {
|
|
if content_length > 0 {
|
|
// Deserialize the response body to get the access token
|
|
let token_result: Result<RequestAccessTokenResponse, _> = response.json().await;
|
|
|
|
match token_result {
|
|
Ok(token) => {
|
|
// Save the access token for the server
|
|
let access_token = ServerAccessToken::new(
|
|
server_id.clone(),
|
|
token.access_token.clone(),
|
|
token.expire_at,
|
|
);
|
|
dbg!(&server_id, &request_id, &code, &token);
|
|
save_access_token(server_id.clone(), access_token);
|
|
persist_servers_token(&app_handle)?;
|
|
|
|
// Update the server's profile using the util::http::HttpClient::get method
|
|
let profile = get_user_profiles(app_handle.clone(), server_id.clone()).await;
|
|
dbg!(&profile);
|
|
|
|
match profile {
|
|
Ok(p) => {
|
|
server.profile = Some(p);
|
|
server.available = true;
|
|
save_server(&server);
|
|
persist_servers(&app_handle)?;
|
|
Ok(())
|
|
}
|
|
Err(e) => Err(format!("Failed to get user profile: {}", e)),
|
|
}
|
|
}
|
|
Err(e) => Err(format!("Failed to deserialize the token response: {}", e)),
|
|
}
|
|
} else {
|
|
Err("Received empty response body.".to_string())
|
|
}
|
|
} else {
|
|
Err("Could not determine the content length.".to_string())
|
|
}
|
|
} else {
|
|
Err(format!(
|
|
"Request failed with status: {}, URL: {}, Code: {}, Response: {:?}",
|
|
response.status(),
|
|
path,
|
|
code,
|
|
response
|
|
))
|
|
}
|
|
} else {
|
|
Err(format!(
|
|
"Server not found for ID: {}, Request ID: {}, Code: {}",
|
|
server_id, request_id, code
|
|
))
|
|
}
|
|
} |