AWS Cognito SRP Client

1.0.0 · active · verified Wed Apr 22

This library provides a client-side implementation for the Secure Remote Password (SRP) authentication flow specifically designed for AWS Cognito User Pools. It abstracts away the complex cryptographic calculations required for SRP_A generation and signature verification, enabling developers to integrate SRP authentication into both browser and Node.js environments. The current stable version is 1.0.0, indicating a relatively new, but stable, initial release. Its primary function is to work in conjunction with the AWS SDK's `initiateAuth` and `respondToAuthChallenge` APIs for the `USER_SRP_AUTH` and `PASSWORD_VERIFIER` flows, respectively, handling the core SRP computations rather than the network requests themselves. It is differentiated by its focused scope on SRP, providing a streamlined experience for this specific Cognito authentication method.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the full four-step SRP authentication flow with AWS Cognito, showing how to generate SRP_A, initiate authentication, calculate the password verifier signature, and respond to the authentication challenge using the `aws-cognito-srp-client` library alongside the AWS SDK.

import Srp from 'aws-cognito-srp-client';
import { CognitoIdentityServiceProvider } from '@aws-sdk/client-cognito-identity-service-provider';

const userPoolId = 'us-east-1_XXXXX'; // Replace with your Cognito User Pool ID
const clientId = 'YYYYYYYYYYYYYYYYYYYYYYYYY'; // Replace with your Cognito App Client ID
const username = 'testuser'; // The user's username
const password = 'StrongPassword123!'; // The user's password

const cognitoClient = new CognitoIdentityServiceProvider({
  region: 'us-east-1' // Replace with your AWS Region
});

async function authenticateUser() {
  const srp = new Srp(userPoolId);
  const srpA = srp.getA();

  console.log('Step 1: Generated SRP_A');

  // Step 2: Initiate Auth with Cognito
  const initiateAuthResponse = await cognitoClient.initiateAuth({
    AuthFlow: 'USER_SRP_AUTH',
    AuthParameters: {
      USERNAME: username,
      SRP_A: srpA
    },
    ClientId: clientId
  });

  console.log('Step 2: Initiate Auth response received');

  const challengeParameters = initiateAuthResponse.ChallengeParameters;
  if (!challengeParameters) {
    throw new Error('No challenge parameters received.');
  }

  const srpB = challengeParameters.SRP_B;
  const salt = challengeParameters.SALT;
  const secretBlock = challengeParameters.SECRET_BLOCK;

  // Step 3: Calculate signature and timestamp
  const { signature, timestamp } = srp.getSignature(
    username,
    srpB,
    salt,
    secretBlock,
    password
  );

  console.log('Step 3: Calculated signature and timestamp');

  // Step 4: Respond to Auth Challenge
  const respondToChallengeResponse = await cognitoClient.respondToAuthChallenge({
    ChallengeName: 'PASSWORD_VERIFIER',
    ChallengeResponses: {
      USERNAME: username,
      PASSWORD_CLAIM_SECRET_BLOCK: secretBlock,
      PASSWORD_CLAIM_SIGNATURE: signature,
      TIMESTAMP: timestamp
    },
    ClientId: clientId,
    Session: initiateAuthResponse.Session // Pass the session token
  });

  console.log('Authentication successful! Token:', respondToChallengeResponse.AuthenticationResult?.AccessToken);
  return respondToChallengeResponse.AuthenticationResult;
}

authenticateUser().catch(console.error);

view raw JSON →