@vardario/cognito-client 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +0,0 @@
1
- import { Session } from '../cognito-client';
2
- export interface OAuthVerificationParams {
3
- pkce: string;
4
- state: string;
5
- }
6
- /**
7
- * Session storage interface class.
8
- */
9
- export declare abstract class SessionStorage {
10
- abstract getSession(): Session | undefined;
11
- abstract setSession(session: Session | undefined): void;
12
- abstract setOauthVerificationParams(oAuthParams: OAuthVerificationParams): void;
13
- abstract getOauthVerificationParams(): OAuthVerificationParams | undefined;
14
- }
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SessionStorage = void 0;
4
- /**
5
- * Session storage interface class.
6
- */
7
- var SessionStorage = /** @class */ (function () {
8
- function SessionStorage() {
9
- }
10
- return SessionStorage;
11
- }());
12
- exports.SessionStorage = SessionStorage;
@@ -1 +0,0 @@
1
- export {};
@@ -1,34 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var crypto_1 = require("crypto");
4
- var test_utils_1 = require("../test-utils");
5
- var cookie_session_storage_1 = require("./cookie-session-storage");
6
- var local_storage_session_storage_1 = require("./local-storage-session-storage");
7
- var memory_session_storage_1 = require("./memory-session-storage");
8
- (0, test_utils_1.setupJsDom)();
9
- var sessionStorages = [
10
- new memory_session_storage_1.MemorySessionStorage(),
11
- new local_storage_session_storage_1.LocalStorageSessionStorage({ storageName: 'session' }),
12
- new cookie_session_storage_1.CookieSessionStorage({
13
- domain: 'localhost',
14
- cookieName: 'session',
15
- }),
16
- ];
17
- var session = {
18
- accessToken: (0, crypto_1.randomBytes)(128).toString('base64'),
19
- expiresIn: 600,
20
- idToken: (0, crypto_1.randomBytes)(128).toString('base64'),
21
- refreshToken: (0, crypto_1.randomBytes)(128).toString('base64'),
22
- };
23
- var oAuthVerificationParams = {
24
- pkce: (0, crypto_1.randomBytes)(128).toString('base64'),
25
- state: (0, crypto_1.randomBytes)(128).toString('base64'),
26
- };
27
- test('SessionStorage', function () {
28
- sessionStorages.forEach(function (sessionStorage) {
29
- sessionStorage.setSession(session);
30
- expect(sessionStorage.getSession()).toStrictEqual(session);
31
- sessionStorage.setOauthVerificationParams(oAuthVerificationParams);
32
- expect(sessionStorage.getOauthVerificationParams()).toStrictEqual(oAuthVerificationParams);
33
- });
34
- });
@@ -1,17 +0,0 @@
1
- export declare const user: {
2
- email: string;
3
- password: string;
4
- givenName: string;
5
- familyName: string;
6
- };
7
- export declare const newUser: {
8
- email: string;
9
- password: string;
10
- givenName: string;
11
- familyName: string;
12
- };
13
- export declare function setupCognito(endpoint: string): Promise<{
14
- userPoolId: string;
15
- userPoolClientId: string;
16
- }>;
17
- export declare function setupJsDom(): void;
package/lib/test-utils.js DELETED
@@ -1,138 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.setupJsDom = exports.setupCognito = exports.newUser = exports.user = void 0;
40
- var client_cognito_identity_provider_1 = require("@aws-sdk/client-cognito-identity-provider");
41
- var jsdom_1 = require("jsdom");
42
- exports.user = {
43
- email: 'sahin@test.com',
44
- password: 'password',
45
- givenName: 'Sahin',
46
- familyName: 'Sahin',
47
- };
48
- exports.newUser = {
49
- email: 'john@test.com',
50
- password: 'password',
51
- givenName: 'John',
52
- familyName: 'John',
53
- };
54
- function setupCognito(endpoint) {
55
- var _a, _b, _c, _d, _e;
56
- return __awaiter(this, void 0, void 0, function () {
57
- var awsCognitoClient, createPoolResult, createUserPoolClientResult, createUserResult, setUserPasswordResult;
58
- return __generator(this, function (_f) {
59
- switch (_f.label) {
60
- case 0:
61
- awsCognitoClient = new client_cognito_identity_provider_1.CognitoIdentityProviderClient({
62
- endpoint: endpoint,
63
- credentials: {
64
- accessKeyId: 'test',
65
- secretAccessKey: 'test',
66
- },
67
- region: 'eu-central-1',
68
- });
69
- return [4 /*yield*/, awsCognitoClient.send(new client_cognito_identity_provider_1.CreateUserPoolCommand({
70
- PoolName: 'TestPool',
71
- Schema: [
72
- {
73
- Name: 'email',
74
- AttributeDataType: client_cognito_identity_provider_1.AttributeDataType.STRING,
75
- Required: true,
76
- },
77
- {
78
- Name: 'givenName',
79
- AttributeDataType: client_cognito_identity_provider_1.AttributeDataType.STRING,
80
- Required: true,
81
- },
82
- {
83
- Name: 'familyName',
84
- AttributeDataType: client_cognito_identity_provider_1.AttributeDataType.STRING,
85
- Required: true,
86
- },
87
- ],
88
- }))];
89
- case 1:
90
- createPoolResult = _f.sent();
91
- return [4 /*yield*/, awsCognitoClient.send(new client_cognito_identity_provider_1.CreateUserPoolClientCommand({
92
- ClientName: 'TestClient',
93
- UserPoolId: (_a = createPoolResult.UserPool) === null || _a === void 0 ? void 0 : _a.Id,
94
- }))];
95
- case 2:
96
- createUserPoolClientResult = _f.sent();
97
- return [4 /*yield*/, awsCognitoClient.send(new client_cognito_identity_provider_1.AdminCreateUserCommand({
98
- UserPoolId: (_b = createPoolResult.UserPool) === null || _b === void 0 ? void 0 : _b.Id,
99
- Username: exports.user.email,
100
- MessageAction: 'SUPPRESS',
101
- UserAttributes: [
102
- {
103
- Name: 'givenName',
104
- Value: exports.user.givenName,
105
- },
106
- {
107
- Name: 'familyName',
108
- Value: exports.user.familyName,
109
- },
110
- ],
111
- }))];
112
- case 3:
113
- createUserResult = _f.sent();
114
- return [4 /*yield*/, awsCognitoClient.send(new client_cognito_identity_provider_1.AdminSetUserPasswordCommand({
115
- UserPoolId: (_c = createPoolResult.UserPool) === null || _c === void 0 ? void 0 : _c.Id,
116
- Username: exports.user.email,
117
- Password: exports.user.password,
118
- Permanent: true,
119
- }))];
120
- case 4:
121
- setUserPasswordResult = _f.sent();
122
- return [2 /*return*/, {
123
- userPoolId: (_d = createPoolResult.UserPool) === null || _d === void 0 ? void 0 : _d.Id,
124
- userPoolClientId: (_e = createUserPoolClientResult.UserPoolClient) === null || _e === void 0 ? void 0 : _e.ClientId,
125
- }];
126
- }
127
- });
128
- });
129
- }
130
- exports.setupCognito = setupCognito;
131
- function setupJsDom() {
132
- var dom = new jsdom_1.JSDOM('', {
133
- url: 'http://localhost',
134
- });
135
- global.document = dom.window.document;
136
- global.window = dom.window;
137
- }
138
- exports.setupJsDom = setupJsDom;
package/lib/utils.d.ts DELETED
@@ -1,20 +0,0 @@
1
- /// <reference types="node" />
2
- import { BigInteger } from 'jsbn';
3
- export declare function padHex(bigInt: BigInteger): string;
4
- export declare function hashHexString(str: string): string;
5
- export declare function hashBuffer(buffer: Buffer): string;
6
- export declare function generateSmallA(): BigInteger;
7
- export declare function generateA(smallA: BigInteger): BigInteger;
8
- export declare function calculateU(A: BigInteger, B: BigInteger): BigInteger;
9
- export declare function calculateS(X: BigInteger, B: BigInteger, U: BigInteger, smallA: BigInteger): BigInteger;
10
- export declare function calculateHKDF(ikm: Buffer, salt: Buffer): number[];
11
- export declare function getPasswordAuthenticationKey(poolName: string, username: string, password: string, B: BigInteger, U: BigInteger, smallA: BigInteger, salt: BigInteger): number[];
12
- export declare function calculateSignature(poolName: string, userId: string, secretBlock: string, hkdf: number[]): {
13
- signature: string;
14
- timeStamp: string;
15
- };
16
- export declare function decodeJwt<T = unknown>(jwt: string): {
17
- header: any;
18
- payload: T;
19
- signature: string;
20
- };
package/lib/utils.js DELETED
@@ -1,128 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.decodeJwt = exports.calculateSignature = exports.getPasswordAuthenticationKey = exports.calculateHKDF = exports.calculateS = exports.calculateU = exports.generateA = exports.generateSmallA = exports.hashBuffer = exports.hashHexString = exports.padHex = void 0;
7
- var formatInTimeZone_1 = __importDefault(require("date-fns-tz/formatInTimeZone"));
8
- var hash_js_1 = require("hash.js");
9
- var jsbn_1 = require("jsbn");
10
- var randombytes_1 = __importDefault(require("randombytes"));
11
- var initN = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' +
12
- '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' +
13
- 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' +
14
- 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' +
15
- 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' +
16
- 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' +
17
- '83655D23DCA3AD961C62F356208552BB9ED529077096966D' +
18
- '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' +
19
- 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' +
20
- 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' +
21
- '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' +
22
- 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' +
23
- 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' +
24
- 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' +
25
- 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' +
26
- '43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF';
27
- var N = new jsbn_1.BigInteger(initN, 16);
28
- var g = new jsbn_1.BigInteger('2', 16);
29
- var k = new jsbn_1.BigInteger(hashHexString("".concat(padHex(N)).concat(padHex(g))), 16);
30
- function padHex(bigInt) {
31
- var HEX_MSB_REGEX = /^[89a-f]/i;
32
- var isNegative = bigInt.compareTo(jsbn_1.BigInteger.ZERO) < 0;
33
- var hexStr = bigInt.abs().toString(16);
34
- hexStr = hexStr.length % 2 !== 0 ? "0".concat(hexStr) : hexStr;
35
- hexStr = HEX_MSB_REGEX.test(hexStr) ? "00".concat(hexStr) : hexStr;
36
- if (isNegative) {
37
- var invertedNibbles = hexStr
38
- .split('')
39
- .map(function (x) {
40
- var invertedNibble = ~parseInt(x, 16) & 0xf;
41
- return '0123456789ABCDEF'.charAt(invertedNibble);
42
- })
43
- .join('');
44
- var flippedBitsBI = new jsbn_1.BigInteger(invertedNibbles, 16).add(jsbn_1.BigInteger.ONE);
45
- hexStr = flippedBitsBI.toString(16);
46
- if (hexStr.toUpperCase().startsWith('FF8')) {
47
- hexStr = hexStr.substring(2);
48
- }
49
- }
50
- return hexStr;
51
- }
52
- exports.padHex = padHex;
53
- function hashHexString(str) {
54
- return hashBuffer(Buffer.from(str, 'hex'));
55
- }
56
- exports.hashHexString = hashHexString;
57
- function hashBuffer(buffer) {
58
- var hash = (0, hash_js_1.sha256)().update(buffer).digest('hex');
59
- return new Array(64 - hash.length).join('0') + hash;
60
- }
61
- exports.hashBuffer = hashBuffer;
62
- function generateSmallA() {
63
- return new jsbn_1.BigInteger((0, randombytes_1.default)(128).toString('hex'), 16);
64
- }
65
- exports.generateSmallA = generateSmallA;
66
- function generateA(smallA) {
67
- var A = g.modPow(smallA, N);
68
- return A;
69
- }
70
- exports.generateA = generateA;
71
- function calculateU(A, B) {
72
- return new jsbn_1.BigInteger(hashHexString(padHex(A) + padHex(B)), 16);
73
- }
74
- exports.calculateU = calculateU;
75
- function calculateS(X, B, U, smallA) {
76
- var gModPowXN = g.modPow(X, N);
77
- var bMinusKMult = B.subtract(k.multiply(gModPowXN));
78
- return bMinusKMult.modPow(smallA.add(U.multiply(X)), N).mod(N);
79
- }
80
- exports.calculateS = calculateS;
81
- function calculateHKDF(ikm, salt) {
82
- var infoBitsBuffer = Buffer.concat([
83
- Buffer.from('Caldera Derived Key', 'utf8'),
84
- Buffer.from(String.fromCharCode(1), 'utf8'),
85
- ]);
86
- var prk = (0, hash_js_1.hmac)(hash_js_1.sha256, salt)
87
- .update(ikm)
88
- .digest();
89
- var hmacResult = (0, hash_js_1.hmac)(hash_js_1.sha256, prk)
90
- .update(infoBitsBuffer)
91
- .digest();
92
- return hmacResult.slice(0, 16);
93
- }
94
- exports.calculateHKDF = calculateHKDF;
95
- function getPasswordAuthenticationKey(poolName, username, password, B, U, smallA, salt) {
96
- var usernamePassword = "".concat(poolName).concat(username, ":").concat(password);
97
- var usernamePasswordHash = hashBuffer(Buffer.from(usernamePassword, 'utf-8'));
98
- var X = new jsbn_1.BigInteger(hashHexString(padHex(salt) + usernamePasswordHash), 16);
99
- var S = calculateS(X, B, U, smallA);
100
- return calculateHKDF(Buffer.from(padHex(S), 'hex'), Buffer.from(padHex(U), 'hex'));
101
- }
102
- exports.getPasswordAuthenticationKey = getPasswordAuthenticationKey;
103
- function calculateSignature(poolName, userId, secretBlock, hkdf) {
104
- var timeStamp = (0, formatInTimeZone_1.default)(new Date(), 'UTC', "EEE MMM d HH:mm:ss 'UTC' yyyy");
105
- var concatBuffer = Buffer.concat([
106
- Buffer.from(poolName, 'utf8'),
107
- Buffer.from(userId, 'utf8'),
108
- Buffer.from(secretBlock, 'base64'),
109
- Buffer.from(timeStamp, 'utf8'),
110
- ]);
111
- var signature = Buffer.from((0, hash_js_1.hmac)(hash_js_1.sha256, hkdf)
112
- .update(concatBuffer)
113
- .digest()).toString('base64');
114
- return {
115
- signature: signature,
116
- timeStamp: timeStamp,
117
- };
118
- }
119
- exports.calculateSignature = calculateSignature;
120
- function decodeJwt(jwt) {
121
- var _a = jwt.split('.'), header = _a[0], payload = _a[1], signature = _a[2];
122
- return {
123
- header: JSON.parse(Buffer.from(header, 'base64').toString('utf-8')),
124
- payload: JSON.parse(Buffer.from(payload, 'base64').toString('utf-8')),
125
- signature: signature,
126
- };
127
- }
128
- exports.decodeJwt = decodeJwt;