Skip to content

Commit

Permalink
WIP: UcanBuilder and friends
Browse files Browse the repository at this point in the history
Unfortunately wasm-bindgen can't expose structs with lifetimes,
so this commit removes the lifetime on UcanBuilder. This requires
an additional Clone bound added to KeyMaterial, breaking non-wasm builds.
  • Loading branch information
fabricedesre committed Aug 25, 2022
1 parent a4b3959 commit 674d8d1
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 44 deletions.
36 changes: 18 additions & 18 deletions ucan-key-support/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[package]
name = "ucan-key-support"
description = "Ready to use SigningKey implementations for the ucan crate"
edition = "2021"
keywords = ["ucan", "authz", "jwt", "pki"]
categories = [
"authorization",
"cryptography",
"encoding",
"web-programming"
"web-programming",
]
description = "Ready to use SigningKey implementations for the ucan crate"
documentation = "https://docs.rs/ucan"
repository = "https://github.com/cdata/rs-ucan/"
edition = "2021"
homepage = "https://github.com/cdata/rs-ucan"
keywords = ["ucan", "authz", "jwt", "pki"]
license = "Apache-2.0"
name = "ucan-key-support"
readme = "README.md"
repository = "https://github.com/cdata/rs-ucan/"
version = "0.4.0-alpha.1"

[lib]
Expand All @@ -24,42 +24,42 @@ default = []
web = ["wasm-bindgen", "wasm-bindgen-futures", "js-sys", "web-sys", "ucan/web", "getrandom/js"]

[dependencies]
ucan = {path = "../ucan", version = "0.6.0-alpha.1" }
anyhow = "1.0.52"
async-trait = "0.1.52"
bs58 = "0.4"
ed25519-zebra = "^3"
log = "0.4"
rsa = "0.6"
sha2 = "0.10"
bs58 = "0.4"
log = "0.4"
ucan = {path = "../ucan", version = "0.6.0-alpha.1"}

[build-dependencies]
npm_rs = "0.2.1"

[dev-dependencies]
rand = "0.8"
# NOTE: This is needed so that rand can be included in WASM builds
getrandom = { version = "0.2.5", features = ["js"] }
getrandom = {version = "0.2.5", features = ["js"]}
tokio = {version = "^1", features = ["macros", "rt"]}
wasm-bindgen-test = "0.3"
tokio = { version = "^1", features = ["macros", "rt"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2", optional = true }
wasm-bindgen-futures = { version = "0.4", optional = true }
js-sys = { version = "0.3", optional = true }
js-sys = {version = "0.3", optional = true}
wasm-bindgen = {version = "0.2", features = ["serde-serialize"], optional = true}
wasm-bindgen-futures = {version = "0.4", optional = true}
wee_alloc = {version = "0.4"}

[target.'cfg(target_arch="wasm32")'.dependencies.web-sys]
version = "0.3"
optional = true
features = [
'Window',
'SubtleCrypto',
'Crypto',
'CryptoKey',
'CryptoKeyPair',
'DedicatedWorkerGlobalScope'
'DedicatedWorkerGlobalScope',
]
optional = true
version = "0.3"

[target.'cfg(target_arch="wasm32")'.dev-dependencies]
pollster = "0.2.5"
pollster = "0.2.5"
35 changes: 25 additions & 10 deletions ucan-key-support/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<script type="module">
import {
WebCryptoRsaKeyMaterial,
WebCryptoRsaKeyMaterial as KeyMaterial,
WasmUcanBuilder as UcanBuilder,
WasmUcan as Ucan,
default as ucanInit,
} from "../static/ucan_key_support.js";

Expand All @@ -27,25 +29,38 @@

async function runTest() {
document.getElementById("log").innerHTML = "";
let keyMaterial = await WebCryptoRsaKeyMaterial.generate(2048);
let did = await keyMaterial.getDid();
log(`DID url: ${did}`);
log(`JWT algorithm: ${keyMaterial.jwtAlgorithm()}`);
let aliceKey = await KeyMaterial.generate(2048);
let did = await aliceKey.getDid();
log(`Alice's DID url: ${did}`);
log(`JWT algorithm: ${aliceKey.jwtAlgorithm()}`);

let data = new TextEncoder().encode("Hello World!");
let signature = await keyMaterial.sign(data);
let signature = await aliceKey.sign(data);
try {
await keyMaterial.verify(data, signature);
await aliceKey.verify(data, signature);
log(`Signature successfully verified`);
} catch (e) {
error(`Signature verification failed: ${e}`);
}

let bobKey = await KeyMaterial.generate(2048);

let builder = new UcanBuilder()
.issuedBy(aliceKey)
.forAudience(bobKey)
.withLifetime(60n);
let signable = builder.build();
let ucan = await signable.sign();
let encoded = ucan.encode();

log(`Encoded ucan: ${encoded}`);

let decodedUcan = Ucan.fromToken(encoded);
log(`Decoded ucan validity: ${await decodedUcan.validate()}`);
}

document.addEventListener("DOMContentLoaded", async () => {
document
.getElementById("run-test")
.addEventListener("click", runTest);
document.getElementById("run-test").addEventListener("click", runTest);

await ucanInit();
});
Expand Down
Loading

0 comments on commit 674d8d1

Please sign in to comment.