Skip to content

Commit

Permalink
updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienR1 committed Dec 8, 2023
1 parent cbdecbb commit 13c5cff
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion examples/bun/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ export default {
if (!publicKey) return new Response("missing required public key in headers", { status: 400 });
if (!body) return new Response("missing body", { status: 400 });

if (new Date().getTime() >= Number(expiry)) return new Response("signature expired", { status: 401 });
if (publicKey !== PUBLIC_KEY) return new Response("unknown public key", { status: 401 });

// validate signature using public key
const payload = JSON.stringify({ exp: expiry, id: publicKey });
const isVerified = nacl.sign.detached.verify(
Buffer.from(body),
Buffer.from(payload),
Buffer.from(signature, "hex"),
Buffer.from(publicKey, "hex")
);
Expand Down
4 changes: 3 additions & 1 deletion examples/deno/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ const handler = async (request: Request) => {
if (!publicKey) return new Response("missing required public key in headers", { status: 400 });
if (!body) return new Response("missing body", { status: 400 });

if (new Date().getTime() >= Number(expiry)) return new Response("signature expired", { status: 401 });
if (publicKey !== PUBLIC_KEY) return new Response("unknown public key", { status: 401 });

// TO-DO: 🚨 FIX CODE BELOW 🚨
// validate signature using public key
const isVerified = nacl.sign.detached.verify(encode(body), encode(signature), encode(PUBLIC_KEY));
const payload = JSON.stringify({ exp: expiry, id: publicKey });
const isVerified = nacl.sign.detached.verify(encode(payload), encode(signature), encode(PUBLIC_KEY));

console.dir({ signature, isVerified });
console.dir(body);
Expand Down
4 changes: 3 additions & 1 deletion examples/express/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ app.use(async (req, res) => {
if (!publicKey) return new Response("missing required public key in headers", { status: 400 });
if (!body) return new Response("missing body", { status: 400 });

if (new Date().getTime() >= Number(expiry)) return new Response("signature expired", { status: 401 });
if (publicKey !== PUBLIC_KEY) return new Response("unknown public key", { status: 401 });

// validate signature using public key
const payload = JSON.stringify({ exp: expiry, id: publicKey });
const isVerified = nacl.sign.detached.verify(
Buffer.from(body),
Buffer.from(payload),
Buffer.from(signature, "hex"),
Buffer.from(PUBLIC_KEY, "hex")
);
Expand Down
4 changes: 3 additions & 1 deletion examples/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ server.on("request", async (req, res) => {
if (!publicKey) return new Response("missing required public key in headers", { status: 400 });
if (!body) return new Response("missing body", { status: 400 });

if (new Date().getTime() >= Number(expiry)) return new Response("signature expired", { status: 401 });
if (publicKey !== PUBLIC_KEY) return new Response("unknown public key", { status: 401 });

// validate signature using public key
const payload = JSON.stringify({ exp: expiry, id: publicKey });
const isVerified = nacl.sign.detached.verify(
Buffer.from(body),
Buffer.from(payload),
Buffer.from(signature, "hex"),
Buffer.from(PUBLIC_KEY, "hex")
);
Expand Down

0 comments on commit 13c5cff

Please sign in to comment.