Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not implemented: crypto.Hmac error with Jsonwebtoken #2557

Closed
penberg opened this issue Aug 24, 2022 · 22 comments
Closed

Not implemented: crypto.Hmac error with Jsonwebtoken #2557

penberg opened this issue Aug 24, 2022 · 22 comments
Labels
bug Something isn't working

Comments

@penberg
Copy link

penberg commented Aug 24, 2022

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
@luk3skyw4lker
Copy link
Contributor

I'm taking a stab at this one!

@penberg
Copy link
Author

penberg commented Sep 23, 2022

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

@luk3skyw4lker
Copy link
Contributor

@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

@ricardodalarme
Copy link

hey, @luk3skyw4lker any progress on that?

@luk3skyw4lker
Copy link
Contributor

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

@ricardodalarme
Copy link

okkk, thank you so much @luk3skyw4lker

@gabobaxx
Copy link

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
Copy link

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

@AaronDewes
Copy link

AaronDewes commented Oct 23, 2022

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 denoland/deno#3326 (comment).

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

@josephrocca
Copy link

josephrocca commented Oct 28, 2022

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?

@iuioiua
Copy link
Contributor

iuioiua commented Nov 17, 2022

I'll give this a crack.

@elekram
Copy link

elekram commented Nov 18, 2022

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?

@cody880910
Copy link

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

@WilsonNet
Copy link

I'll give this a crack.

thanks, it will help me a lot

@kt3k
Copy link
Member

kt3k commented Nov 23, 2022

closed by #2664

@kt3k kt3k closed this as completed Nov 23, 2022
@henningko
Copy link

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

@bombillazo
Copy link

bombillazo commented May 11, 2023

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

@Ciscodefran
Copy link

No solutions yet?

@iuioiua
Copy link
Contributor

iuioiua commented Nov 8, 2023

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.

@elekram
Copy link

elekram commented Nov 8, 2023

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.

@iuioiua
Copy link
Contributor

iuioiua commented Nov 8, 2023

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

@Ciscodefran
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests