From 171386d93ca096bc7e6575b8d384a8fe2ae16cb4 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sat, 4 Jan 2025 10:40:02 +0000 Subject: [PATCH] feat: gpauth support macos --- crates/auth/src/webview/response_reader.rs | 8 +++---- crates/auth/src/webview/unix.rs | 26 ++++++++-------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/crates/auth/src/webview/response_reader.rs b/crates/auth/src/webview/response_reader.rs index c8c111f..f9babbd 100644 --- a/crates/auth/src/webview/response_reader.rs +++ b/crates/auth/src/webview/response_reader.rs @@ -17,10 +17,10 @@ pub trait ResponseReader { fn get_header(&self, key: &str) -> Option; fn get_body(&self, cb: Box>>) + 'static>); -} -fn is_acs_endpoint(auth_response: &impl ResponseReader) -> bool { - auth_response.url().map_or(false, |url| url.ends_with("/SAML20/SP/ACS")) + fn is_acs_endpoint(&self) -> bool { + self.url().map_or(false, |url| url.ends_with("/SAML20/SP/ACS")) + } } pub fn read_auth_data(auth_response: &impl ResponseReader, cb: F) @@ -36,7 +36,7 @@ where Err(header_err) => { info!("Failed to read auth data from headers: {}", header_err); - let is_acs_endpoint = is_acs_endpoint(auth_response); + let is_acs_endpoint = auth_response.is_acs_endpoint(); read_from_body(auth_response, move |auth_result| { // If the endpoint is `/SAML20/SP/ACS` and no auth data found in body, it should be considered as invalid let auth_result = auth_result.map_err(move |e| { diff --git a/crates/auth/src/webview/unix.rs b/crates/auth/src/webview/unix.rs index f54e1b4..98a5c1a 100644 --- a/crates/auth/src/webview/unix.rs +++ b/crates/auth/src/webview/unix.rs @@ -7,30 +7,24 @@ use wry::WebViewExtUnix; use crate::webview::auth_messenger::AuthError; -pub struct AuthResponse { - web_resource: WebResource, -} +use super::response_reader::ResponseReader; -impl AuthResponse { - pub fn url(&self) -> Option { - self.web_resource.uri().map(GString::into) +impl ResponseReader for WebResource { + fn url(&self) -> Option { + self.uri().map(GString::into) } - pub fn get_header(&self, key: &str) -> Option { + fn get_header(&self, key: &str) -> Option { self - .web_resource .response() .and_then(|response| response.http_headers()) .and_then(|headers| headers.one(key)) .map(GString::into) } - pub fn get_body(&self, cb: F) - where - F: FnOnce(anyhow::Result>>) + 'static, - { + fn get_body(&self, cb: Box>>) + 'static>) { let cancellable = Cancellable::NONE; - self.web_resource.data(cancellable, move |data| { + self.data(cancellable, move |data| { let body = data .map_err(|e| anyhow::anyhow!(e)) .map(|data| String::from_utf8_lossy(&data).into_owned()) @@ -43,7 +37,7 @@ impl AuthResponse { pub fn connect_webview_response(wv: &wry::WebView, cb: F) where - F: Fn(anyhow::Result) + 'static, + F: Fn(anyhow::Result) + 'static, { let wv = wv.webview(); let cb = Arc::new(cb); @@ -71,9 +65,7 @@ where return; } - let response = AuthResponse { web_resource }; - - cb_clone(Ok(response)); + cb_clone(Ok(web_resource)); }); wv.connect_load_failed_with_tls_errors(move |_wv, uri, cert, err| {