From 3eef626f1a342f6dd8fd77e75a366d7b60edfcbf Mon Sep 17 00:00:00 2001 From: Hendrik Eeckhaut Date: Wed, 18 Sep 2024 14:53:15 +0200 Subject: [PATCH] Review feedback --- examples/fitbit-rs/Cargo.toml | 5 ++- examples/fitbit-rs/LICENSE | 28 -------------- examples/fitbit-rs/src/lib.rs | 67 +++++++++++++++------------------ examples/fitbit-rs/src/utils.rs | 25 +++++------- examples/fitbit-rs/testing.md | 9 +++++ 5 files changed, 51 insertions(+), 83 deletions(-) delete mode 100644 examples/fitbit-rs/LICENSE create mode 100644 examples/fitbit-rs/testing.md diff --git a/examples/fitbit-rs/Cargo.toml b/examples/fitbit-rs/Cargo.toml index 0c07770..c5d0574 100644 --- a/examples/fitbit-rs/Cargo.toml +++ b/examples/fitbit-rs/Cargo.toml @@ -8,7 +8,8 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] +anyhow = "1.0.89" base64 = "0.22.1" extism-pdk = "1.1.0" -serde = { version = "1.0.197", features = ["derive"] } -serde_json = "1.0.125" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" diff --git a/examples/fitbit-rs/LICENSE b/examples/fitbit-rs/LICENSE deleted file mode 100644 index 4440418..0000000 --- a/examples/fitbit-rs/LICENSE +++ /dev/null @@ -1,28 +0,0 @@ -BSD 3-Clause License - -Copyright (c) 2024, Extism - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/examples/fitbit-rs/src/lib.rs b/examples/fitbit-rs/src/lib.rs index 84edbd7..1d25ccb 100644 --- a/examples/fitbit-rs/src/lib.rs +++ b/examples/fitbit-rs/src/lib.rs @@ -72,43 +72,36 @@ pub fn start() -> FnResult> { #[plugin_fn] pub fn two() -> FnResult>> { - let a = "oauth_access_token"; - match get_cookies_by_host("www.fitbit.com") { - Ok(cookies) => { - log!(LogLevel::Info, "cookies: {cookies:?}"); - match cookies.get(a) { - Some(token) => { - log!(LogLevel::Info, "found token: {token:?}"); - let bearer_header = format!("Bearer {}", token); - let headers: HashMap<&str, &str> = [ - ("authorization", bearer_header.as_str()), - ("Accept-Encoding", "identity"), - ("Connection", "close"), - ] - .into_iter() - .collect(); - let secret_headers = vec![bearer_header.as_str()]; - let request = RequestConfig { - url: "https://web-api.fitbit.com/1/user/4G7WVQ/profile.json", - method: "GET", - headers, - secret_headers, - }; + let cookies = get_cookies_by_host("www.fitbit.com")?; + log!(LogLevel::Info, "cookies: {cookies:?}"); - let request_json = serde_json::to_string(&request)?; - log!(LogLevel::Info, "request: {:?}", &request_json); - let id = unsafe { - let id = notarize(&request_json); - log!(LogLevel::Info, "Notarization result: {:?}", id); - id? - }; + let token = cookies + .get("oauth_access_token") + .ok_or_else(|| Error::msg("oauth_access_token not found"))?; + log!(LogLevel::Info, "found token: {token:?}"); + let bearer_header = format!("Bearer {}", token); + let headers: HashMap<&str, &str> = [ + ("authorization", bearer_header.as_str()), + ("Accept-Encoding", "identity"), + ("Connection", "close"), + ] + .into_iter() + .collect(); + let secret_headers = vec![bearer_header.as_str()]; + let request = RequestConfig { + url: "https://web-api.fitbit.com/1/user/4G7WVQ/profile.json", + method: "GET", + headers, + secret_headers, + }; - return Ok(Json(Some(id))); - } - _ => log!(LogLevel::Error, "TODO"), - } - } - Err(err) => log!(LogLevel::Error, "{:?}", err), - } - Ok(Json(None)) + let request_json = serde_json::to_string(&request)?; + log!(LogLevel::Info, "request: {:?}", &request_json); + let id = unsafe { + let id = notarize(&request_json); + log!(LogLevel::Info, "Notarization result: {:?}", id); + id? + }; + + return Ok(Json(Some(id))); } diff --git a/examples/fitbit-rs/src/utils.rs b/examples/fitbit-rs/src/utils.rs index c5fa3eb..b80674f 100644 --- a/examples/fitbit-rs/src/utils.rs +++ b/examples/fitbit-rs/src/utils.rs @@ -1,26 +1,19 @@ use std::collections::HashMap; +use anyhow::Context; use config::get; use extism_pdk::*; -pub fn get_cookies_by_host(hostname: &str) -> Result, String> { - let cookies_result = get("cookies"); - - // Check if the cookies were retrieved successfully - let cookies_json = match cookies_result { - Ok(Some(json_str)) => json_str, - Ok(None) => return Err("No cookies found in the configuration.".to_string()), - Err(e) => return Err(format!("Error retrieving cookies: {}", e)), - }; +pub fn get_cookies_by_host(hostname: &str) -> Result, Error> { + // Get Cookies via Extism + let cookies_json: String = get("cookies")?.context("No cookies found in the configuration.")?; // Parse the JSON string directly into a HashMap - let cookies: HashMap> = - serde_json::from_str(&cookies_json).map_err(|e| format!("Failed to parse JSON: {}", e))?; + let cookies: HashMap> = serde_json::from_str(&cookies_json)?; // Attempt to find the hostname in the map - if let Some(host_cookies) = cookies.get(hostname) { - Ok(host_cookies.clone()) - } else { - Err(format!("Cannot find cookies for {}", hostname)) - } + cookies + .get(hostname) + .cloned() + .context(format!("Cannot find cookies for {}", hostname)) } diff --git a/examples/fitbit-rs/testing.md b/examples/fitbit-rs/testing.md new file mode 100644 index 0000000..a3a5a72 --- /dev/null +++ b/examples/fitbit-rs/testing.md @@ -0,0 +1,9 @@ +# Testing + +1. start notary server (**TLS off** for local testing, check version!) +2. Start proxy server: +``` +websocat --binary -v ws-l:0.0.0.0:55688 tcp:web-api.fitbit.com:443 +``` +3. `cargo build --release` +4. Load plugin in browser extension