passwordless-id / webauthn

Webauthn / passkeys helper library to make your life easier. Client side, server side and demo included.
https://webauthn.passwordless.id
MIT License
436 stars 51 forks source link

ReferenceError: crypto is not defined #54

Closed rkreienbuehl closed 3 months ago

rkreienbuehl commented 3 months ago

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 commented 3 months ago

EDIT: removed useless misleading comment

dagnelies commented 3 months ago

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 commented 3 months ago

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

rkreienbuehl commented 3 months ago

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 commented 3 months ago

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

xkid1 commented 3 months ago

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

dagnelies commented 3 months ago

@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