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
594 stars 43 forks source link

[QUESTION] JwtRsaVerifier.create is not a function #158

Closed mufambisi closed 3 months ago

mufambisi commented 3 months ago

Question When trying to run verify in lambda, i get the following error:

{ "errorType": "TypeError", "errorMessage": "JwtRsaVerifier.create is not a function", "stack": [ "TypeError: JwtRsaVerifier.create is not a function", " at Runtime.module.exports.verifyToken [as handler] (/var/task/authorizer.js:35:37)", " at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)" ] }

Versions aws-jwt-verify: 4.0.1 Node.js 20.x

Below is my authorizer.js file:

`var CognitoJwtVerifier=require("aws-jwt-verify");

function generateAuthResponse(principalId, effect, methodArn) { const policyDocument = generatePolicyDocument(effect, methodArn);

return {
  principalId,
  policyDocument
};

}

function generatePolicyDocument(effect, methodArn) { if (!effect || !methodArn) return null;

const policyDocument = {
  Version: "2012-10-17",
  Statement: [
    {
      Action: "execute-api:Invoke",
      Effect: effect,
      Resource: methodArn
    }
  ]
};

return policyDocument;

} module.exports.verifyToken = async (event, context, callback) => {

const token = 'TEST TOKENXXXXXXXXXXX';
const methodArn = event.FunctionArn;

const verifier = CognitoJwtVerifier.create({
    issuer: "https://login.microsoftonline.com/XXXXXXXXXXX/v2.0", // set this to the expected "iss" claim on your JWTs
    audience: "XXXXXXXXX" // set this to the expected "aud" claim on your JWTs
    //jwksUri: "https://example.com/.well-known/jwks.json", // set this to the JWKS uri from your OpenID configuration
  });
  verifier.map((obj)=>{
    console.log(obj)
  })
  console.log('---------',verifier)
  try {
    const payload = await verifier.verify(token);
    console.log("Token is valid. Payload:", payload);
    return callback(null, generateAuthResponse(payload.id, "Allow", methodArn));
  } catch {
    return callback(null, generateAuthResponse(payload.id, "Deny", methodArn));
  }

};`