Skip to content

Duolingo #20

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/duolingo_rs/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "wasm32-unknown-unknown"
2 changes: 2 additions & 0 deletions examples/duolingo_rs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
17 changes: 17 additions & 0 deletions examples/duolingo_rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "duolingo_rs"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
anyhow = "1.0.89"
base64 = "0.22.1"
## The version needs to be locked to 1.2.0 until Extism in the browser extension is updated
extism-pdk = "=1.2.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
spansy = { git = "https://github.com/tlsnotary/tlsn-utils", rev = "45370cc" }
url = "2.5.4"
Binary file added examples/duolingo_rs/assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/duolingo_rs/assets/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions examples/duolingo_rs/src/host_functions.rs
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;
}
136 changes: 136 additions & 0 deletions examples/duolingo_rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use std::{collections::HashMap, vec};

use anyhow::Context;
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 url::Url;
use utils::get_cookies_by_host;

const REQUEST: RequestObject = RequestObject {
url: "https://www.duolingo.com/2017-06-30/users/*?fields=streak,email,name,username",
method: "GET",
};

#[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: "Duolingo (Rust)",
description: "Notarize your email and current streak",
steps: vec![
StepConfig {
title: "Visit Duolingo",
description: None,
cta: "Go",
action: "start",
prover: false,
},
StepConfig {
title: "Collect credentials",
cta: "Go",
action: "two",
prover: false,
description: Some("Login to your account if you haven't already"),
},
StepConfig {
title: "Notarize",
cta: "Notarize",
action: "three",
prover: true,
description: None,
},
],
host_functions: vec!["redirect", "notarize"],
cookies: vec!["www.duolingo.com"],
headers: vec![],
requests: vec![REQUEST],
notary_urls: None,
proxy_urls: None,
icon,
};
Ok(Json(config))
}

/// Implementation of the first (start) plugin step
#[plugin_fn]
pub fn start() -> FnResult<Json<bool>> {
let duolingo_url = Url::parse("https://www.duolingo.com")?;
let tab_url = get("tabUrl")?.context("Error getting tab url")?;
let tab_url = Url::parse(&tab_url)?;

if tab_url.host_str() != duolingo_url.host_str() {
unsafe {
let _ = redirect(duolingo_url.as_str());
};
return Ok(Json(false));
}

Ok(Json(true))
}

#[plugin_fn]
pub fn two() -> FnResult<Json<RequestConfig>> {
let cookies = get_cookies_by_host("www.duolingo.com")?;

log!(LogLevel::Info, "cookies: {cookies:?}");

let uuid = cookies
.get("logged_out_uuid")
.ok_or_else(|| Error::msg("uuid not found"))?;

let jwt_token = cookies
.get("jwt_token")
.ok_or_else(|| Error::msg("jwt_token not found"))?;

let headers: HashMap<String, String> = [
(
String::from("Authorization"),
format!("Bearer {}", jwt_token),
),
(String::from("Accept-Encoding"), String::from("identity")),
(String::from("Connection"), String::from("close")),
]
.into_iter()
.collect();
let secret_headers = vec![jwt_token.clone()];
let request = RequestConfig {
url: format!(
"https://www.duolingo.com/2017-06-30/users/{uuid}?fields=streak,email,name,username"
)
.to_string(),
method: REQUEST.method.to_string(),
headers,
secret_headers,
get_secret_response: None,
};

let request_json = serde_json::to_string(&request)?;
log!(LogLevel::Info, "request: {:?}", &request_json);

return Ok(Json(request));
}

#[plugin_fn]
pub fn three() -> FnResult<Json<String>> {
let request_json: String = input()?;
log!(LogLevel::Info, "Input: {request_json:?}");

let id = unsafe {
let id = notarize(&request_json);
log!(LogLevel::Info, "Notarization result: {:?}", id);
id?
};

return Ok(Json(id));
}
53 changes: 53 additions & 0 deletions examples/duolingo_rs/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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> {
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, Clone)]
#[serde(rename_all = "camelCase")]
#[encoding(Json)]
pub struct RequestConfig {
pub url: String,
pub method: String,
pub headers: HashMap<String, String>,
pub secret_headers: Vec<String>,
pub get_secret_response: Option<String>,
}
29 changes: 29 additions & 0 deletions examples/duolingo_rs/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::collections::HashMap;

use anyhow::Context;
use config::get;
use extism_pdk::*;

#[allow(dead_code)]
pub fn get_cookies_by_host(hostname: &str) -> Result<HashMap<String, String>, Error> {
get_by_host("cookies", hostname)
}

#[allow(dead_code)]
pub fn get_headers_by_host(hostname: &str) -> Result<HashMap<String, String>, Error> {
get_by_host("headers", hostname)
}

fn get_by_host(key: &str, hostname: &str) -> Result<HashMap<String, String>, Error> {
// Get key via Extism
let cookies_json: String =
get(key)?.context(format!("No {}s found in the configuration.", key))?;

// Parse the JSON string directly into a HashMap
let map: HashMap<String, HashMap<String, String>> = serde_json::from_str(&cookies_json)?;

// Attempt to find the hostname in the map
map.get(hostname)
.cloned()
.context(format!("Cannot find {}s for {}", key, hostname))
}
9 changes: 9 additions & 0 deletions examples/duolingo_rs/testing.md
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:
```
wstcp --bind-addr 127.0.0.1:55688 www.duolingo.com:443
```
3. `cargo build --release`
4. Load plugin in browser extension