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

ReferenceError: crypto is not defined #54

Closed
rkreienbuehl opened this issue Jun 6, 2024 · 7 comments
Closed

ReferenceError: crypto is not defined #54

rkreienbuehl opened this issue Jun 6, 2024 · 7 comments

Comments

@rkreienbuehl
Copy link

I have setup @passwordless-id/webauthn and want to use the server as described in the docs to verify the authentication with server.verifyAuthentication(...). Sadly, I get the following error:

ReferenceError: crypto is not defined
    at null.parseCryptoKey (/Users/rogerkreienbuehl/Projekte/apmatic-next/node_modules/@passwordless-id/webauthn/dist/esm/server.js:93:5)
    at null.verifySignature (/Users/rogerkreienbuehl/Projekte/apmatic-next/node_modules/@passwordless-id/webauthn/dist/esm/server.js:108:27)
    at Object.verifyAuthentication (/Users/rogerkreienbuehl/Projekte/apmatic-next/node_modules/@passwordless-id/webauthn/dist/esm/server.js:30:36)
    at null.<anonymous> (/Users/rogerkreienbuehl/Projekte/apmatic-next/apps/apmatic-next-oauth/src/routes/webauthn.ts:58:53)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at null.noCache (/Users/rogerkreienbuehl/Projekte/apmatic-next/apps/apmatic-next-oauth/src/middleware/no-cache.middleware.ts:6:3)
    at null.contextEnsureOidc (/Users/rogerkreienbuehl/Projekte/apmatic-next/apps/apmatic-next-oauth/src/routes/helpers/ensureOidc.ts:4:5)
    at async serve (/Users/rogerkreienbuehl/Projekte/apmatic-next/node_modules/koa-static/index.js:53:9)
    at null.<anonymous> (/Users/rogerkreienbuehl/Projekte/apmatic-next/node_modules/oidc-provider/lib/helpers/initialize_app.js:222:5)
    at null.errorHandler (/Users/rogerkreienbuehl/Projekte/apmatic-next/node_modules/oidc-provider/lib/shared/error_handler.js:26:7)

Is there anything I missed?

I use node version v20.10.0 and @passwordless-id/webauthn version v1.6.1.

@dagnelies
Copy link
Collaborator

dagnelies commented Jun 6, 2024

EDIT: removed useless misleading comment

@dagnelies
Copy link
Collaborator

no ...wait ...there is something more fishy ....your whole crypto modules is not defined ...that strange ...what does it do if you start node in the terminal and you simply enter "crypto" in the prompt? On mine, I get the Crypto object back, what about yours?

@dagnelies
Copy link
Collaborator

Perhaps it has also something to do with the bundling of your app... crypto should be a global.

@rkreienbuehl
Copy link
Author

Thanks for your response. You are right, thats strange. As I read, since node 19 crypto should be globally available.
When I start node and enter crypto I get Crypto {} and when I type typeof crypto I get 'undefined'.
I got it working by using globalThis.crypto ??= require("node:crypto").webcrypto as a workaround, maybe this is a problem with tsx which I am using for running in dev environment.

@dagnelies
Copy link
Collaborator

weird ...but I'm afraid I don't really know what's going on. Thanks for the workaround.

@xkid1
Copy link

xkid1 commented Jun 9, 2024

crypto in node is not available in globals, used the module crypto.
import { webcrypto } from 'crypto';

@dagnelies
Copy link
Collaborator

@xkid1 I beg to differ: https://nodejs.org/docs/latest-v22.x/api/globals.html#crypto_1

Well, it's weird. Good thing @rkreienbuehl found a workaround.

globalThis.crypto ??= require("node:crypto").webcrypto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants