-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Example plugin in Rust #11
Open
heeckhau
wants to merge
7
commits into
main
Choose a base branch
from
fitbit-rs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
17f869d
WIP: Fitbit Browser plugin in Rust
heeckhau 5b692d3
Add icon
heeckhau 7a279b4
fix: correct auth header + return notarization ID when finished
heeckhau 21d34c4
wip
heeckhau f6ada23
Use &str over String to clean up code
heeckhau eff1d64
Code improvements
heeckhau 3eef626
Review feedback
heeckhau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[build] | ||
target = "wasm32-unknown-unknown" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "fitbit-rs" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
anyhow = "1.0.89" | ||
base64 = "0.22.1" | ||
extism-pdk = "1.1.0" | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0" |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
cargo build --target wasm32-unknown-unknown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use extism_pdk::*; | ||
|
||
#[host_fn] | ||
extern "ExtismHost" { | ||
pub fn redirect(url: &str); | ||
} | ||
|
||
#[host_fn] | ||
extern "ExtismHost" { | ||
pub fn notarize(params: &str) -> String; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use std::{collections::HashMap, vec}; | ||
|
||
use base64::{engine::general_purpose, Engine as _}; | ||
use config::get; | ||
use extism_pdk::*; | ||
|
||
mod types; | ||
use types::{PluginConfig, RequestConfig, RequestObject, StepConfig}; | ||
mod host_functions; | ||
use host_functions::{notarize, redirect}; | ||
mod utils; | ||
use utils::get_cookies_by_host; | ||
|
||
#[plugin_fn] | ||
pub fn config() -> FnResult<Json<PluginConfig<'static>>> { | ||
let icon: String = format!( | ||
"data:image/png;base64,{}", | ||
general_purpose::STANDARD.encode(include_bytes!("../assets/icon.png")) | ||
); | ||
|
||
let config = PluginConfig { | ||
title: "Fitbit Profile", | ||
description: "Notarize ownership of a fitbit profile", | ||
steps: vec![ | ||
StepConfig { | ||
title: "Open the Fitbit dashboard", | ||
description: Some("Login to your account if you haven't already"), | ||
cta: "Go to fitbit.com", | ||
action: "start", | ||
prover: false, | ||
}, | ||
StepConfig { | ||
title: "Notarize fitbit profile", | ||
cta: "Notarize", | ||
action: "two", | ||
prover: true, | ||
description: None, | ||
}, | ||
], | ||
host_functions: vec!["redirect", "notarize"], | ||
cookies: vec!["www.fitbit.com"], | ||
headers: vec!["www.fitbit.com"], | ||
requests: vec![RequestObject { | ||
url: "https://web-api.fitbit.com/1/user/*/profile.json", | ||
method: "GET", | ||
}], | ||
notary_urls: None, | ||
proxy_urls: None, | ||
icon, | ||
}; | ||
Ok(Json(config)) | ||
} | ||
|
||
#[plugin_fn] | ||
pub fn start() -> FnResult<Json<bool>> { | ||
let dashboard_url = "https://www.fitbit.com/dashboard"; | ||
let tab_url = get("tabUrl"); | ||
|
||
let Ok(Some(url)) = tab_url else { | ||
return Ok(Json(false)); | ||
}; | ||
|
||
if url != dashboard_url { | ||
unsafe { | ||
let _ = redirect(dashboard_url); | ||
}; | ||
return Ok(Json(false)); | ||
} | ||
|
||
Ok(Json(true)) | ||
} | ||
|
||
#[plugin_fn] | ||
pub fn two() -> FnResult<Json<Option<String>>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function never returns an error, but logs it. Is this because of the |
||
let cookies = get_cookies_by_host("www.fitbit.com")?; | ||
log!(LogLevel::Info, "cookies: {cookies:?}"); | ||
|
||
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, | ||
}; | ||
|
||
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))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::collections::HashMap; | ||
|
||
use extism_pdk::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(FromBytes, Deserialize, PartialEq, Debug, Serialize, ToBytes)] | ||
#[serde(rename_all = "camelCase")] | ||
#[encoding(Json)] | ||
pub struct PluginConfig<'a> { | ||
th4s marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub title: &'a str, | ||
pub description: &'a str, | ||
pub icon: String, | ||
pub steps: Vec<StepConfig<'a>>, | ||
pub host_functions: Vec<&'a str>, | ||
pub cookies: Vec<&'a str>, | ||
pub headers: Vec<&'a str>, | ||
pub requests: Vec<RequestObject<'a>>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub notary_urls: Option<Vec<&'a str>>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub proxy_urls: Option<Vec<&'a str>>, | ||
} | ||
|
||
#[derive(FromBytes, Deserialize, PartialEq, Debug, Serialize, ToBytes)] | ||
#[serde(rename_all = "camelCase")] | ||
#[encoding(Json)] | ||
pub struct StepConfig<'a> { | ||
pub title: &'a str, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub description: Option<&'a str>, | ||
pub cta: &'a str, | ||
pub action: &'a str, | ||
pub prover: bool, | ||
} | ||
|
||
#[derive(FromBytes, Deserialize, PartialEq, Debug, Serialize, ToBytes)] | ||
#[serde(rename_all = "camelCase")] | ||
#[encoding(Json)] | ||
pub struct RequestObject<'a> { | ||
pub url: &'a str, | ||
pub method: &'a str, | ||
} | ||
|
||
#[derive(FromBytes, Deserialize, PartialEq, Debug, Serialize, ToBytes)] | ||
#[serde(rename_all = "camelCase")] | ||
#[encoding(Json)] | ||
pub struct RequestConfig<'a> { | ||
pub url: &'a str, | ||
pub method: &'a str, | ||
pub headers: HashMap<&'a str, &'a str>, | ||
pub secret_headers: Vec<&'a str>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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>, 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)?; | ||
|
||
// Attempt to find the hostname in the map | ||
cookies | ||
.get(hostname) | ||
.cloned() | ||
.context(format!("Cannot find cookies for {}", hostname)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of a
bool
it would be nicer to have aanyhow::Result<()>
. Then you can also add error messages similarly to how you do inutils.rs
.