UNPKG

cognito-srp

Version:

Secure Remote Password protocol implementation compatible with Amazon Cognito.

71 lines (70 loc) 3.08 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const createCognitoEndpoint_1 = require("./createCognitoEndpoint"); const amazon_cognito_identity_js_1 = require("amazon-cognito-identity-js"); const UserPool_1 = require("./UserPool"); const userPoolId = 'us-east-2_7DZy4Fkn7'; const clientId = '65vtqzygk2k3yzqe5c2qn8wxe'; const username = 'testuser'; const password = 'pass123'; function test1() { return __awaiter(this, void 0, void 0, function* () { const endpoint = yield createCognitoEndpoint_1.createCognitoEndpoint(userPoolId, [ { username, password } ]); const port = yield endpoint.start(); try { const userPool = new amazon_cognito_identity_js_1.CognitoUserPool({ UserPoolId: userPoolId, ClientId: clientId, endpoint: `http://localhost:${port}/` }); const authenticationDetails = new amazon_cognito_identity_js_1.AuthenticationDetails({ Username: username, Password: password }); const cognitoUser = new amazon_cognito_identity_js_1.CognitoUser({ Username: username, Pool: userPool }); yield new Promise((resolve, reject) => { cognitoUser.authenticateUser(authenticationDetails, { onSuccess: resolve, onFailure: reject }); }); } finally { endpoint.stop(); } }); } function test2() { return __awaiter(this, void 0, void 0, function* () { const userPool = new UserPool_1.UserPool(userPoolId.split('_')[1]); const clientUser = { username: 'testuser', password: 'pass123' }; const serverUser = yield userPool.createUser(clientUser); const client = yield userPool.getClientChallenge(clientUser); const server = yield userPool.getServerChallenge(serverUser); const A = client.calculateA().toString('hex'); const B = server.calculateB().toString('hex'); const clientSignature = client .getSession(B, serverUser.salt) .calculateSignature('', ''); const serverSignature = server.getSession(A).calculateSignature('', ''); console.log({ clientSignature, serverSignature }); }); } test1().then(null, console.error); test2().then(null, console.error);