UNPKG

cognito-srp

Version:

Secure Remote Password protocol implementation compatible with Amazon Cognito.

34 lines (33 loc) 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const BigInteger_1 = require("./BigInteger"); const util_1 = require("./util"); const constants_1 = require("./constants"); const Session_1 = require("./Session"); class ClientPasswordChallenge { constructor(poolname, user, a) { this.poolname = poolname; this.user = user; this.a = util_1.getBigInteger(a); } calculateA() { if (!this.A) { this.A = constants_1.g.modPow(this.a, constants_1.N).toBuffer(constants_1.Nbytes); } return this.A; } getSession(B, salt) { const Bint = new BigInteger_1.BigInteger(B, 16); if (Bint.compareTo(BigInteger_1.BigInteger.ZERO) <= 0 || Bint.compareTo(constants_1.N) >= 0) { throw new Error('A should be between 0 and N exclusive'); } const privateKey = util_1.calculatePrivateKey(this.poolname, this.user, salt); const scramblingParameter = util_1.calculateScramblingParameter(this.calculateA(), Buffer.from(B, 'hex')); const sessionKey = Bint.subtract(constants_1.multiplierParameter.multiply(constants_1.g.modPow(privateKey, constants_1.N))) .modPow(this.a.add(scramblingParameter.multiply(privateKey)), constants_1.N) .mod(constants_1.N) .toBuffer(constants_1.Nbytes); return new Session_1.Session(this.poolname, this.user.username, sessionKey, scramblingParameter.toBuffer()); } } exports.ClientPasswordChallenge = ClientPasswordChallenge;