awslabs / aws-jwt-verify

JS library for verifying JWTs signed by Amazon Cognito, and any OIDC-compatible IDP that signs JWTs with RS256, RS384, and RS512
Apache License 2.0
606 stars 42 forks source link

[QUESTION] Verify function throwing loader errors #50

Closed AlejandroMAL closed 2 years ago

AlejandroMAL commented 2 years ago

Question How can we help you?

I am using Nodejs 16 with TS, each time I try to verify the token throws the following error:


Failure: Module parse failed: Unexpected token (319:63)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|     // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|     static create(verifyProperties, additionalProperties) {
>         return new this(verifyProperties, additionalProperties?.jwksCache);
|     }
| }
Error: Module parse failed: Unexpected token (319:63)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|     // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|     static create(verifyProperties, additionalProperties) {
>         return new this(verifyProperties, additionalProperties?.jwksCache);
|     }
| }
    at Object.../../aws-jwt-verify/dist/esm/jwt-rsa.js (/Users/alex/Documents/company/is/my-project/is/.webpack/service/src/handler.js:29633:7)
    at __webpack_require__ (/Users/alex/Documents/company/is/my-project/is/.webpack/service/src/webpack:/webpack/bootstrap:19:1)
    at Module.../../aws-jwt-verify/dist/esm/index.js (/Users/alex/Documents/company/is/my-project/is/.webpack/service/src/webpack:/Users/alex/Documents/company/is/my-project/is/node_modules/aws-jwt-verify/dist/esm/index.js:1:1)
    at __webpack_require__ (/Users/alex/Documents/company/is/my-project/is/.webpack/service/src/webpack:/webpack/bootstrap:19:1)
    at Module.../../../src/handler.ts (/Users/alex/Documents/company/is/my-project/is/.webpack/service/src/handler.js:653:73)
    at __webpack_require__ (/Users/alex/Documents/company/is/my-project/is/.webpack/service/src/webpack:/webpack/bootstrap:19:1)
    at /Users/alex/Documents/company/is/my-project/is/.webpack/service/src/webpack:/webpack/bootstrap:83:1
    at Object.<anonymous> (/Users/alex/Documents/company/is/my-project/is/.webpack/service/src/handler.js:87:10)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at /Users/alex/Documents/company/is/my-project/is/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:157:133
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at InProcessRunner.run (/Users/alex/Documents/company/is/my-project/is/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:157:9)

This is my code:

const verifier = CognitoJwtVerifier.create({
  userPoolId: process.env.USER_POOL_ID!,
  tokenUse: "access",
  clientId: process.env.CLIENT_ID!,
  jwksUri: `https://cognito-idp.${process.env.REGION}.amazonaws.com/${process.env.USER_POOL_ID}/.well-known/jwks.json`
});

async function testVerifier() {
  const token = `eyJraWQiOiJjWXdKYlpnR1dXd09rekJ...`;
  try {
    const payload = await verifier.verify(token);
    console.log("Token is valid. Payload:", payload);
  } catch {
    console.log("Token not valid!");
  }
}

testVerifier();

I wonder if this is an error on the library or if it is related to the code implementation, hopefully, you can help. Thanks.

Versions Which version of aws-jwt-verify are you using? 2.1.1

Which version of Node.js are you using? (Should be at least 14) 16.13.1

If using TypeScript, which version of TypeScript are you using? (Should be at least 4) 4.5.4

ottokruse commented 2 years ago

Hi @AlejandroMAL Seems to be a webpack related issue, it struggles with the optional chaining operator (the ?), which has been around now for quite some time in JS. Suggest to update webpack. A quick google revealed this thread with a comment that might work for you, if you don't want to update webpack: https://github.com/webpack/webpack/issues/10227#issuecomment-950242155

Side note: you don't need to provide the jwksUri when creating the CognitoJwtVerifier because it will understand that uri itself, based on your User Pool ID, exactly as you have coded it yourself. Saves you 1 line of code 😄

AlejandroMAL commented 2 years ago

Thank you so much! That solved the issue.

Edit: Fixed upgrading Webpackl if that might help anyone else.

hero9 commented 2 years ago

@ottokruse I'm using latest version of webpack, but I still have these errors

ottokruse commented 2 years ago

Please open a new issue @hero9 and provide the requested details for your specific case.