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 3 years ago • 1 comments
trafficstars

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

I'll give this a crack.

iuioiua avatar Nov 17 '22 04:11 iuioiua

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?

elekram avatar Nov 18 '22 04:11 elekram

All aliyun npm prompts: Not implemented: crypto.Hmac, is there a solution?

cody880910 avatar Nov 20 '22 03:11 cody880910

I'll give this a crack.

thanks, it will help me a lot

WilsonNet avatar Nov 23 '22 14:11 WilsonNet

closed by #2664

kt3k avatar Nov 23 '22 16:11 kt3k

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

henningko avatar Dec 02 '22 00:12 henningko

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

bombillazo avatar May 11 '23 11:05 bombillazo

No solutions yet?

Ciscodefran avatar Nov 08 '23 05:11 Ciscodefran

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.

iuioiua avatar Nov 08 '23 05:11 iuioiua

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.

elekram avatar Nov 08 '23 22:11 elekram

To clarify, @Ciscodefran, if you still have issues, please open an issue in the Deno CLI repo.

iuioiua avatar Nov 08 '23 23:11 iuioiua

Thank you so much @elekram . you are a legend :)

Ciscodefran avatar Nov 09 '23 00:11 Ciscodefran