UNPKG

cognito-srp

Version:

Secure Remote Password protocol implementation compatible with Amazon Cognito.

39 lines (38 loc) 1.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("./util"); const BigInteger_1 = require("./BigInteger"); const constants_1 = require("./constants"); const Session_1 = require("./Session"); class ServerPasswordChallenge { constructor(poolname, user, b) { this.poolname = poolname; this.user = user; this.verifier = new BigInteger_1.BigInteger(user.verifier, 16); this.b = util_1.getBigInteger(b); } calculateB() { if (!this.B) { this.B = constants_1.multiplierParameter .multiply(this.verifier) .add(constants_1.g.modPow(this.b, constants_1.N)) .mod(constants_1.N) .toBuffer(constants_1.Nbytes); } return this.B; } getSession(A) { A = util_1.padHex(A); const Aint = new BigInteger_1.BigInteger(A, 16); if (Aint.compareTo(BigInteger_1.BigInteger.ZERO) <= 0 || Aint.compareTo(constants_1.N) >= 0) { throw new Error('A should be between 0 and N exclusive'); } const scramblingParameter = util_1.calculateScramblingParameter(Buffer.from(A, 'hex'), this.calculateB()); const sessionKey = Aint.multiply(this.verifier.modPow(scramblingParameter, constants_1.N)) .modPow(this.b, 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.ServerPasswordChallenge = ServerPasswordChallenge;