deno_std: Not implemented: crypto.Hmac error with Jsonwebtoken

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/std@0.152.0/node/_utils.ts:23:9)
    at new Hmac (https://deno.land/std@0.152.0/node/internal/crypto/hash.ts:136:5)
    at Object.createHmac (https://deno.land/std@0.152.0/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

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 8
  • Comments: 22 (4 by maintainers)

Most upvoted comments

I’m getting error: Uncaught Error: Not implemented: crypto.Sign when trying to use Google APIs with NPM Specifiers. Is there a suggested work around for this in the meantime or should I trundle back to Node?

closed by #2664

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/std@0.152.0/node/_utils.ts:23:9)
    at new Sign (https://deno.land/std@0.152.0/node/internal/crypto/sig.ts:45:5)
    at Object.createSign (https://deno.land/std@0.152.0/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

Not sure if this is of any help to anyone but I wrote a module that gets Google auth tokens and now I just use Google’s REST apis/endpoints directly rather than using and relying on the googleapis package. Use it everyday for work.

@gabobaxx @elekram Any solution found to solve this issue when trying to use with the googleapis?

For people stumbling here and using esm.sh to get JSON Web Token into deno; I had to force esm to use deno-std@0.166.0 via import jwt from "https://esm.sh/jsonwebtoken@8.5.1?deno-std=0.166.0”;, else it defaults to 0.165.0

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

@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

The Standard Library no longer contains the implementation for the Node compatibility layer. If you’re still having issues, please see the repo for the Deno CLI.

I’ll give this a crack.

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?

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/std@0.153.0/node/_utils.ts:23:9)
    at new Sign (https://deno.land/std@0.153.0/node/internal/crypto/sig.ts:45:5)
    at Object.createSign (https://deno.land/std@0.153.0/node/crypto.ts:268:10)
    at Object.sign (https://esm.sh/v96/jwa@2.0.0/deno/jwa.js:4:1429)
    at Object._ [as sign] (https://esm.sh/v96/jws@4.0.0/deno/jws.js:2:2447)
    at f.requestToken (https://esm.sh/v96/gtoken@6.1.2/deno/gtoken.js:2:4605)
    at f.getTokenAsyncInner (https://esm.sh/v96/gtoken@6.1.2/deno/gtoken.js:2:3592)
    at f.getTokenAsync (https://esm.sh/v96/gtoken@6.1.2/deno/gtoken.js:2:3169)
    at f.getToken (https://esm.sh/v96/gtoken@6.1.2/deno/gtoken.js:2:2390)
    at V.refreshTokenNoCache (https://esm.sh/v96/google-auth-library@8.5.2/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

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

I’m taking a stab at this one!