Skip to content

Commit

Permalink
✨ feat(onedrive): finish onedrive func
Browse files Browse the repository at this point in the history
  • Loading branch information
CakeAL committed Dec 8, 2024
1 parent 3eaf267 commit 36eb258
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 92 deletions.
3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ build-android:
clippy:
cd src-tauri && cargo clippy

clean:
cd src-tauri && cargo clean

change_version:
python3 change_version.py
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ustb-wifi-tools",
"private": true,
"version": "0.9.0",
"version": "0.9.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
99 changes: 40 additions & 59 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ustb-wifi-tools"
version = "0.9.0"
version = "0.9.1"
description = "A Tauri App that can be used to get information of the USTB Wifi"
authors = ["CakeAL"]
edition = "2021"
Expand Down
3 changes: 1 addition & 2 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::RwLock;
use crate::commands::*;
use crate::entities::AppState;
use crate::setting::Setting;
use onedrive::{code_to_access_token, open_microsoft_login};
use onedrive::open_microsoft_login;
use tauri::Manager;


Expand Down Expand Up @@ -59,7 +59,6 @@ pub fn run() {
set_mac_custom_name,
collapse,
open_microsoft_login,
code_to_access_token
])
.setup(|app| {
#[cfg(not(any(target_os = "android", target_os = "linux")))]
Expand Down
37 changes: 21 additions & 16 deletions src-tauri/src/onedrive.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::{entities::AppState, setting::Setting};
use base64::{engine::general_purpose::URL_SAFE, Engine as _};
use rand::Rng;
use reqwest::Client;
Expand All @@ -6,14 +7,9 @@ use sha2::{Digest, Sha256};
use tauri::{Manager, WebviewWindow};
use tauri_plugin_dialog::DialogExt;

use crate::{entities::AppState, setting::Setting};

#[tauri::command(async)]
pub async fn open_microsoft_login(app_handle: tauri::AppHandle) -> Result<(), String> {
#[allow(unused_variables)]
let url = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=6c2e411f-bea9-4598-8cb9-bebadac59bdc&scope=Files.ReadWrite%20offline_access&response_type=code&redirect_uri=https%3A%2F%2Ftauri.localhost%2F";
#[cfg(debug_assertions)]
let url = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=6c2e411f-bea9-4598-8cb9-bebadac59bdc&scope=Files.ReadWrite%20offline_access&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A1420%2F";
let url: &str = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=6c2e411f-bea9-4598-8cb9-bebadac59bdc&scope=Files.ReadWrite%20offline_access&response_type=code";

let code_verifier = generate_random_string(128);
let code_challenge = sha256_base64url(&code_verifier);
Expand All @@ -24,17 +20,29 @@ pub async fn open_microsoft_login(app_handle: tauri::AppHandle) -> Result<(), St
.unwrap() = Some(code_verifier);

let url = format!(
"{}&code_challenge={}&code_challenge_method=S256",
"{}&code_challenge={}&code_challenge_method=S256&redirect_uri=https%3A%2F%2Flogin.microsoftonline.com%2Fcommon%2Foauth2%2Fnativeclient",
url, code_challenge
);

WebviewWindow::builder(
&app_handle,
&app_handle.clone(),
"Onedrive",
tauri::WebviewUrl::External(url.parse().unwrap()),
)
.inner_size(480.0, 670.0)
.title("Onedrive 登录")
.on_navigation(move |url| {
dbg!(url.path());
if url.path() == "/common/oauth2/nativeclient" {
let pair = url.query_pairs().next().unwrap();
let code = pair.1.to_string();
// dbg!(code);
let app_handle = app_handle.clone();
tauri::async_runtime::spawn(async move {
let _ = code_to_access_token(app_handle, code).await;
});
}
true
})
.build()
.map_err(|e| e.to_string())?;
Ok(())
Expand All @@ -51,11 +59,8 @@ struct TokenResponse {
refresh_token: Option<String>,
}

#[tauri::command(async)]
pub async fn code_to_access_token(
app_handle: tauri::AppHandle,
code: String,
) -> Result<(), String> {
async fn code_to_access_token(app_handle: tauri::AppHandle, code: String) -> Result<(), String> {
dbg!(&code);
let window = app_handle
.get_webview_window("Onedrive")
.ok_or("?".to_string())?;
Expand All @@ -71,10 +76,10 @@ pub async fn code_to_access_token(
let response = match Client::new()
.post("https://login.microsoftonline.com/common/oauth2/v2.0/token")
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Origin", "http://localhost:1420")
// .header("Origin", "https://login.microsoftonline.com/common/oauth2/nativeclient")
.form(&[
("client_id", "6c2e411f-bea9-4598-8cb9-bebadac59bdc"),
("redirect_uri", "http://localhost:1420/"),
("redirect_uri", "https://login.microsoftonline.com/common/oauth2/nativeclient"),
("code", &code),
("grant_type", "authorization_code"),
("code_verifier", &code_verifier),
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"productName": "ustb-wifi-tools",
"mainBinaryName": "ustb-wifi-tools",
"version": "0.9.0",
"version": "0.9.1",
"identifier": "ustb.wifi.tools",
"build": {
"beforeDevCommand": "pnpm dev",
Expand Down
Loading

0 comments on commit 36eb258

Please sign in to comment.