kriasoft / web-auth-library

Authentication library for the browser environment using Web Crypto API
https://developer.mozilla.org/docs/Web/API/Web_Crypto_API
MIT License
100 stars 9 forks source link

getAccessToken not working on Cloudflare Workers #28

Open oriollpz opened 6 months ago

oriollpz commented 6 months ago

The function

const accessToken = await getAccessToken({
      credentials: credentials,
      scope: "https://www.googleapis.com/auth/cloud-platform",
      waitUntil: c.executionCtx.waitUntil,
});

Isn't working for me, it works locally, but when I use it in the worker it says this: TypeError: Illegal invocation

Anyone faced the same problem?

zewelor commented 5 months ago

I have the same problem, did you solve it ?

oriollpz commented 5 months ago

@zewelor I stopped using this library and I used this instead.

zewelor commented 5 months ago

@zewelor I stopped using this library and I used this instead.

Hmm i see link to this ( web-auth-library ) library

Manouchehri commented 4 months ago

Same issue here.

Manouchehri commented 4 months ago

Use waitUntil: ctx.waitUntil.bind(ctx) instead.

RickYangzz commented 3 months ago

Use waitUntil: ctx.waitUntil.bind(ctx) instead.

but I got '{}' from my firestore.

RickYangzz commented 3 months ago
image

Thanks everybody below this problem. I have been successful. This is my code.

arekgotfryd commented 1 month ago

I tried it today and I'm getting following info:

TypeError: Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details.
A ReadableStream branch was created but never consumed. Such branches can be created, for instance, by calling the tee() method on a ReadableStream, or by calling the clone() method on a Request or Response object. If a branch is created but never consumed, it can force the runtime to buffer the entire body of the stream in memory, which may cause the Worker to exceed its memory limit and be terminated. To avoid this, ensure that all branches created are consumed.

 * Unused stream created:
    at null.<anonymous> (file:///C:/projects/parcels-api/node_modules/web-auth-library/dist/google/accessToken.js:105:36)
    at async getAccessToken (file:///C:/projects/parcels-api/node_modules/web-auth-library/dist/google/accessToken.js:127:12)
    at async getFirebaseToken (file:///C:/projects/parcels-api/src/utils/firebase-auth.ts:21:23)
    at async getAuthHeaders (file:///C:/projects/parcels-api/src/clients/firestore-client.ts:8:17)
    at async getUserAvailableCoverage (file:///C:/projects/parcels-api/src/clients/firestore-client.ts:17:19)
    at null.<anonymous> (async file:///C:/projects/parcels-api/.wrangler/tmp/dev-RVTltV/index.js:39419:38)
    at async dispatch (file:///C:/projects/parcels-api/node_modules/hono/dist/compose.js:29:17)
    at async cors2 (file:///C:/projects/parcels-api/node_modules/hono/dist/middleware/cors/index.js:65:5)
    at async dispatch (file:///C:/projects/parcels-api/node_modules/hono/dist/compose.js:29:17)
    at async poweredBy2 (file:///C:/projects/parcels-api/node_modules/hono/dist/middleware/powered-by/index.js:4:5)
auniverseaway commented 1 month ago

@arekgotfryd I had the same issue when using CF Workers without hono and executionCtx.

Here's how I ended up solving...

return getAccessToken({
    credentials: env.GOOGLE_CLOUD_CREDENTIALS,
    scope: 'https://www.googleapis.com/auth/cloud-translation',
    waitUntil: ctx.waitUntil.bind(ctx),
    env,
  });

Note how the waitUntil function is re-bound to (CF Worker native) ctx.