Skip to content
Merged
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
10 changes: 9 additions & 1 deletion examples/verification-workers/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from "cloudflare:test";
import { afterEach, describe, it, expect, vi } from "vitest";
import { sign } from "web-bot-auth";
import { signerFromJWK } from "web-bot-auth/crypto";
import { signerFromJWK, verifierFromJWK } from "web-bot-auth/crypto";
import worker from "../src/index";
import jwk from "../../rfc9421-keys/ed25519.json" with { type: "json" };

Expand Down Expand Up @@ -75,6 +75,14 @@ afterEach(() => {
vi.restoreAllMocks();
});

it("signs and verifies with Ed25519 JWKs labeled Ed25519", async () => {
const { kty, crv, x } = jwk;
const signer = await signerFromJWK({ ...jwk, alg: "Ed25519" });
const verifier = await verifierFromJWK({ kty, crv, x, alg: "Ed25519" });
const data = new TextEncoder().encode("test message");
expect(await verifier.verify(data, await signer.sign(data))).toBe(true);
});

describe("/ endpoint", () => {
it("responds with HTTP 200", async () => {
const request = new IncomingRequest(sampleURL);
Expand Down
8 changes: 5 additions & 3 deletions packages/web-bot-auth/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ async function keyid(jwk: JsonWebKey): Promise<string> {

function jwkAlgorithm(jwk: JsonWebKey): WebBotAlgorithm {
if (jwk.kty === "OKP" && jwk.crv === "Ed25519") {
if (jwk.alg !== undefined && jwk.alg !== "EdDSA") {
throw new Error("Ed25519 JWK alg must be EdDSA when present");
if (jwk.alg !== undefined && jwk.alg !== "Ed25519" && jwk.alg !== "EdDSA") {
throw new Error("Ed25519 JWK alg must be Ed25519 or EdDSA when present");
}
return "ed25519";
}
Expand All @@ -47,9 +47,11 @@ function publicJwk(jwk: JsonWebKey, algorithm: WebBotAlgorithm): JsonWebKey {

export async function signerFromJWK(jwk: JsonWebKey): Promise<WebBotSigner> {
const algorithm = jwkAlgorithm(jwk);
// Some Web Crypto runtimes only accept the legacy EdDSA label.
const importJwk = jwk.alg === "Ed25519" ? { ...jwk, alg: undefined } : jwk;
const key = await crypto.subtle.importKey(
"jwk",
jwk,
importJwk,
importAlgorithm(algorithm),
false,
["sign"]
Expand Down