deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

Not implemented: crypto.Hmac error with Jsonwebtoken

Open penberg opened this issue 1 year ago • 1 comments

Describe the bug

I am trying to use jsonwebtoken with Deno's compatibility mode, but I am seeing the following error:

Uncaught Error: Not implemented: crypto.Hmac

Steps to Reproduce

With the following webtoken.mjs file:

import jwt from 'jsonwebtoken'

const token = jwt.sign({ foo: 'bar' }, 'shhhhh');
console.log(token);

deno run (in compat mode) fails as follows:

% deno run --unstable --allow-env --allow-read --compat webtoken.mjs
error: Uncaught Error: Not implemented: crypto.Hmac
  throw new Error(message);
        ^
    at notImplemented (https://deno.land/[email protected]/node/_utils.ts:23:9)
    at new Hmac (https://deno.land/[email protected]/node/internal/crypto/hash.ts:136:5)
    at Object.createHmac (https://deno.land/[email protected]/node/crypto.ts:264:10)
    at Object.sign (file:///Users/penberg/src/chiselstrike/webtoken/node_modules/jwa/index.js:132:23)
    at Object.jwsSign [as sign] (file:///Users/penberg/src/chiselstrike/webtoken/node_modules/jws/lib/sign-stream.js:32:24)
    at Object.module.exports [as sign] (file:///Users/penberg/src/chiselstrike/webtoken/node_modules/jsonwebtoken/sign.js:204:16)
    at file:///Users/penberg/src/chiselstrike/webtoken/webtoken.mjs:3:19

Expected behavior

% node webtoken.mjs
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE2NjEzMzIwOTR9.dmuExDy4XoLwze8PDox3xRH7UCRklkFbrbhLLXIaNrQ

Environment

  • OS: MacOS 12.4
  • deno version: 1.24.3
  • std version: 0.152.0

penberg avatar Aug 24 '22 09:08 penberg

I'm taking a stab at this one!

luk3skyw4lker avatar Sep 03 '22 01:09 luk3skyw4lker

Looks like there's some more unimplemented bits. As pointed out by @ricardodalarme, Octokit seems to use jsonwebtoken with RS256 algorith, which hits a different unimplemented path in crypto:

% cat webtoken.mjs
import jwt from 'jsonwebtoken'

const token = jwt.sign({ foo: 'bar' }, 'shhhhh', { algorithm: 'RS256' });

console.log(token);
% deno run --unstable --allow-env --allow-read --compat webtoken.mjs
error: Uncaught Error: Not implemented: crypto.Sign
  throw new Error(message);
        ^
    at notImplemented (https://deno.land/[email protected]/node/_utils.ts:23:9)
    at new Sign (https://deno.land/[email protected]/node/internal/crypto/sig.ts:45:5)
    at Object.createSign (https://deno.land/[email protected]/node/crypto.ts:268:10)
    at Object.sign (file:///Users/penberg/src/chiselstrike/deno-test/node_modules/jwa/index.js:151:25)
    at Object.jwsSign [as sign] (file:///Users/penberg/src/chiselstrike/deno-test/node_modules/jws/lib/sign-stream.js:32:24)
    at Object.module.exports [as sign] (file:///Users/penberg/src/chiselstrike/deno-test/node_modules/jsonwebtoken/sign.js:204:16)
    at file:///Users/penberg/src/chiselstrike/deno-test/webtoken.mjs:3:19

penberg avatar Sep 23 '22 14:09 penberg

@penberg alright! I'll take a look at it too, the HMAC implementation is kinda done, but there are still some tests missing and the type checking is not right too. I guess that I can take a look this Sunday

luk3skyw4lker avatar Sep 26 '22 19:09 luk3skyw4lker

hey, @luk3skyw4lker any progress on that?

ricardodalarme avatar Oct 07 '22 02:10 ricardodalarme

@ricardodalarme yep! I have made some progress on the PR, the tests are the only thing missing, I'll do them probably this weekend

luk3skyw4lker avatar Oct 08 '22 00:10 luk3skyw4lker

okkk, thank you so much @luk3skyw4lker

ricardodalarme avatar Oct 18 '22 14:10 ricardodalarme

Hey! I'm trying to implement an application that use the google api method but i'm getting this error:

error: Uncaught Error: Not implemented: crypto.Sign
  throw new Error(message);
        ^
    at notImplemented (https://deno.land/[email protected]/node/_utils.ts:23:9)
    at new Sign (https://deno.land/[email protected]/node/internal/crypto/sig.ts:45:5)
    at Object.createSign (https://deno.land/[email protected]/node/crypto.ts:268:10)
    at Object.sign (https://esm.sh/v96/[email protected]/deno/jwa.js:4:1429)
    at Object._ [as sign] (https://esm.sh/v96/[email protected]/deno/jws.js:2:2447)
    at f.requestToken (https://esm.sh/v96/[email protected]/deno/gtoken.js:2:4605)
    at f.getTokenAsyncInner (https://esm.sh/v96/[email protected]/deno/gtoken.js:2:3592)
    at f.getTokenAsync (https://esm.sh/v96/[email protected]/deno/gtoken.js:2:3169)
    at f.getToken (https://esm.sh/v96/[email protected]/deno/gtoken.js:2:2390)
    at V.refreshTokenNoCache (https://esm.sh/v96/[email protected]/deno/google-auth-library.js:3:22114)
FAIL

This is the code: deno-google-api-code

I ran this same code in nodejs and it's worked well. More info:

deno 1.26.1 (release, x86_64-unknown-linux-gnu)
v8 10.7.193.3
typescript 4.8.3

gabobaxx avatar Oct 21 '22 19:10 gabobaxx

I know that googleapis is an npm packages and that is intended for nodejs but i wanted to try it with deno. Is there any solution any solution for this or do I have to go back to nodejs? xd

gabobaxx avatar Oct 21 '22 19:10 gabobaxx

I know that googleapis is an npm packages and that is intended for nodejs but i wanted to try it with deno. Is there any solution any solution for this or do I have to go back to nodejs? xd

googleapis seems to depend on gRPC, which is not yet supported in Deno, although it seems that it is being worked on https://github.com/denoland/deno/issues/3326#issuecomment-1198729530.

For the REST APIs, you may want to try the browser version: https://github.com/google/google-api-javascript-client

AaronDewes avatar Oct 23 '22 07:10 AaronDewes

Maybe worth mentioning: This is also blocking npm AWS SDK usage. I wonder if there's any way to polyfill this with something like npm:node-forge in the meantime?

josephrocca avatar Oct 28 '22 18:10 josephrocca