Review feedback

This commit is contained in:
Hendrik Eeckhaut
2024-09-18 14:53:15 +02:00
parent eff1d6439f
commit 3eef626f1a
5 changed files with 51 additions and 83 deletions

View File

@@ -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"

View File

@@ -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.

View File

@@ -72,43 +72,36 @@ pub fn start() -> FnResult<Json<bool>> {
#[plugin_fn]
pub fn two() -> FnResult<Json<Option<String>>> {
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)));
}

View File

@@ -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<HashMap<String, String>, 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<HashMap<String, String>, 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<String, HashMap<String, String>> =
serde_json::from_str(&cookies_json).map_err(|e| format!("Failed to parse JSON: {}", e))?;
let cookies: HashMap<String, HashMap<String, String>> = 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))
}

View File

@@ -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