refactor: refactoring auth flow (#226)

This commit is contained in:
Medcl
2025-03-02 22:14:18 +08:00
committed by GitHub
parent 31aa8587ba
commit cf4a8ea58a
2 changed files with 9 additions and 6 deletions

View File

@@ -5,5 +5,5 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestAccessTokenResponse {
pub access_token: String,
pub expire_at: u32,
pub expire_in: u32,
}

View File

@@ -6,10 +6,11 @@ use crate::server::profile::get_user_profiles;
use crate::server::search::CocoSearchSource;
use crate::server::servers::{get_server_by_id, persist_servers, persist_servers_token, save_access_token, save_server};
use reqwest::{Client, StatusCode};
use std::collections::HashMap;
use tauri::{AppHandle, Manager, Runtime};
fn request_access_token_url(request_id: &str, code: &str) -> String {
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={}&token={}", request_id, code)
format!("/auth/request_access_token?request_id={}", request_id)
}
#[tauri::command]
@@ -25,10 +26,12 @@ pub async fn handle_sso_callback<R: Runtime>(
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, &code);
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, None)
let mut header = HashMap::new();
header.insert("Authorization".to_string(), format!("Bearer {}", code).to_string());
let response = HttpClient::advanced_post(&server_id, &path, Some(header), None, None)
.await
.map_err(|e| format!("Failed to send request to the server: {}", e))?;
@@ -45,7 +48,7 @@ pub async fn handle_sso_callback<R: Runtime>(
let access_token = ServerAccessToken::new(
server_id.clone(),
token.access_token.clone(),
token.expire_at,
token.expire_in,
);
// dbg!(&server_id, &request_id, &code, &token);
save_access_token(server_id.clone(), access_token);