simonmcallister0210 / cognito-srp-helper

A helper for SRP authentication in AWS Cognito
Apache License 2.0
11 stars 1 forks source link

Error During SRP Authentication Using "User_SRP_Auth" #39

Closed varuna-peiris closed 3 weeks ago

varuna-peiris commented 4 weeks ago

I am trying to authenticate a user using the User_SRP_Auth authflow from the repository. While following the steps provided, I encountered an error. Steps to Reproduce:

  1. Cloned the repository.
  2. Use the repo by following the code. ``
    import CognitoSrpHelper from "./src/cognito-srp-helper"; import { CognitoIdentityProviderClient, InitiateAuthCommand, RespondToAuthChallengeCommand } from "@aws-sdk/client-cognito-identity-provider";

const username = "xxxxxx"; const password = "xxxxxx"; const clientId = "xxxxxxxxxxxxxxxxxxxxxx"; const secretId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; const poolId = "xxxxxxx_xxxxxxxxx";

const secretHash = CognitoSrpHelper.createSecretHash(username, clientId, secretId); const srpSession = CognitoSrpHelper.createSrpSession(username, password, poolId, false);

async function run() { try { const initiateAuthRes = await CognitoIdentityProviderClient.send( new InitiateAuthCommand( CognitoSrpHelper.wrapInitiateAuth(srpSession, { ClientId: clientId, AuthFlow: "USER_SRP_AUTH", AuthParameters: { CHALLENGE_NAME: "SRP_A", SECRET_HASH: secretHash, USERNAME: username, }, }), ), );

const signedSrpSession = CognitoSrpHelper.signSrpSession(srpSession, initiateAuthRes);

const respondToAuthChallengeRes = await CognitoIdentityProviderClient.send(
  new RespondToAuthChallengeCommand(
    CognitoSrpHelper.wrapAuthChallenge(signedSrpSession, {
      ClientId: clientId,
      ChallengeName: "PASSWORD_VERIFIER",
      ChallengeResponses: {
        SECRET_HASH: secretHash,
        USERNAME: username,
      },
    }),
  ),
);

} catch (err) { console.error(err); } }

run(); ``

  1. Run the code on Windows cmd $ node When I ran this (windows 10, Node.js v20.16.0), I encountered the following error: image

Please guide me, in completing the SRP authentication process successfully, resulting in a valid authentication response using your repo.

I appreciate any help you can provide.

Thanks in advance.

simonmcallister0210 commented 4 weeks ago

Hey Varuna, thanks for checking out the project 🙂

Importing src/* files won't work if you're importing inside a .js file, because the project is written in TypeScript. It needs to be compiled before you can import it in JavaScript

If you build the project with npm run build then you should be able to import it by refering to the package name:

import * as CognitoSrpHelper from "cognito-srp-helper";

// . . .

Let me know if that works for you

varuna-peiris commented 4 weeks ago

Thank you for your response. I tried what you suggested steps but the error is still the same. Here I attached a screenshot for your reference. image

simonmcallister0210 commented 4 weeks ago

That's odd 🤔

Did you update the import statement? from:

import CognitoSrpHelper from "./src/cognito-srp-helper";

.. to ..

import * as CognitoSrpHelper from "cognito-srp-helper";

varuna-peiris commented 4 weeks ago

Yes, I updated it. Now I did the following to a new clone and the error was changed. Here I attached a screenshot for your reference. image

simonmcallister0210 commented 4 weeks ago

Oh, that's to do with CognitoIdentityProviderClient. You need to instantiate the client first:

const cognitoIdentityProviderClient = new CognitoIdentityProviderClient({
  region: "eu-west-2", // <-- this should be the AWS region
});  

Then you can call send on the instance you created:

const respondToAuthChallengeRes = await cognitoIdentityProviderClient.send(
  // . . .
);
varuna-peiris commented 3 weeks ago

I ran the code applying the above changes and it runs without errors. But it does not appear to pop up or anything for the challenge. I've attached a screenshot for you to look over. image Please guide me.

simonmcallister0210 commented 3 weeks ago

Ah, you may just need to log something to the console

Since a lot of these questions are unrelated to the package itself I'll close this issue. But if any issues specific to the package does come up feel free to open up this issue again