@vardario/cognito-client 0.1.4 → 0.1.5
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.
- package/lib/cognito-client.d.ts +292 -0
- package/lib/cognito-client.js +409 -0
- package/lib/cognito-client.test.d.ts +1 -0
- package/lib/cognito-client.test.js +99 -0
- package/lib/error.d.ts +55 -0
- package/lib/error.js +70 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -0
- package/lib/session-storage/cookie-session-storage/cookie-session-storage.d.ts +21 -0
- package/lib/session-storage/cookie-session-storage/cookie-session-storage.js +42 -0
- package/lib/session-storage/cookie-session-storage/index.d.ts +1 -0
- package/lib/session-storage/cookie-session-storage/index.js +1 -0
- package/lib/session-storage/index.d.ts +4 -0
- package/lib/session-storage/index.js +4 -0
- package/lib/session-storage/local-storage-session-storage.d.ts +20 -0
- package/lib/session-storage/local-storage-session-storage.js +38 -0
- package/lib/session-storage/memory-session-storage.d.ts +13 -0
- package/lib/session-storage/memory-session-storage.js +18 -0
- package/lib/session-storage/session-storage.d.ts +14 -0
- package/lib/session-storage/session-storage.js +5 -0
- package/lib/session-storage/session-storage.test.d.ts +1 -0
- package/lib/session-storage/session-storage.test.js +33 -0
- package/lib/test-utils.d.ts +17 -0
- package/lib/test-utils.js +81 -0
- package/lib/utils.d.ts +20 -0
- package/lib/utils.js +111 -0
- package/package.json +7 -8
- package/dist/index.js +0 -3814
package/lib/utils.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import formatInTimeZone from 'date-fns-tz/formatInTimeZone';
|
|
2
|
+
import { hmac, sha256 } from 'hash.js';
|
|
3
|
+
import { BigInteger } from 'jsbn';
|
|
4
|
+
import randomBytes from 'randombytes';
|
|
5
|
+
const initN = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' +
|
|
6
|
+
'29024E088A67CC74020BBEA63B139B22514A08798E3404DD' +
|
|
7
|
+
'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' +
|
|
8
|
+
'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' +
|
|
9
|
+
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' +
|
|
10
|
+
'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' +
|
|
11
|
+
'83655D23DCA3AD961C62F356208552BB9ED529077096966D' +
|
|
12
|
+
'670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' +
|
|
13
|
+
'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' +
|
|
14
|
+
'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' +
|
|
15
|
+
'15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' +
|
|
16
|
+
'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' +
|
|
17
|
+
'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' +
|
|
18
|
+
'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' +
|
|
19
|
+
'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' +
|
|
20
|
+
'43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF';
|
|
21
|
+
const N = new BigInteger(initN, 16);
|
|
22
|
+
const g = new BigInteger('2', 16);
|
|
23
|
+
const k = new BigInteger(hashHexString(`${padHex(N)}${padHex(g)}`), 16);
|
|
24
|
+
export function padHex(bigInt) {
|
|
25
|
+
const HEX_MSB_REGEX = /^[89a-f]/i;
|
|
26
|
+
const isNegative = bigInt.compareTo(BigInteger.ZERO) < 0;
|
|
27
|
+
let hexStr = bigInt.abs().toString(16);
|
|
28
|
+
hexStr = hexStr.length % 2 !== 0 ? `0${hexStr}` : hexStr;
|
|
29
|
+
hexStr = HEX_MSB_REGEX.test(hexStr) ? `00${hexStr}` : hexStr;
|
|
30
|
+
if (isNegative) {
|
|
31
|
+
const invertedNibbles = hexStr
|
|
32
|
+
.split('')
|
|
33
|
+
.map((x) => {
|
|
34
|
+
const invertedNibble = ~parseInt(x, 16) & 0xf;
|
|
35
|
+
return '0123456789ABCDEF'.charAt(invertedNibble);
|
|
36
|
+
})
|
|
37
|
+
.join('');
|
|
38
|
+
const flippedBitsBI = new BigInteger(invertedNibbles, 16).add(BigInteger.ONE);
|
|
39
|
+
hexStr = flippedBitsBI.toString(16);
|
|
40
|
+
if (hexStr.toUpperCase().startsWith('FF8')) {
|
|
41
|
+
hexStr = hexStr.substring(2);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return hexStr;
|
|
45
|
+
}
|
|
46
|
+
export function hashHexString(str) {
|
|
47
|
+
return hashBuffer(Buffer.from(str, 'hex'));
|
|
48
|
+
}
|
|
49
|
+
export function hashBuffer(buffer) {
|
|
50
|
+
const hash = sha256().update(buffer).digest('hex');
|
|
51
|
+
return new Array(64 - hash.length).join('0') + hash;
|
|
52
|
+
}
|
|
53
|
+
export function generateSmallA() {
|
|
54
|
+
return new BigInteger(randomBytes(128).toString('hex'), 16);
|
|
55
|
+
}
|
|
56
|
+
export function generateA(smallA) {
|
|
57
|
+
const A = g.modPow(smallA, N);
|
|
58
|
+
return A;
|
|
59
|
+
}
|
|
60
|
+
export function calculateU(A, B) {
|
|
61
|
+
return new BigInteger(hashHexString(padHex(A) + padHex(B)), 16);
|
|
62
|
+
}
|
|
63
|
+
export function calculateS(X, B, U, smallA) {
|
|
64
|
+
const gModPowXN = g.modPow(X, N);
|
|
65
|
+
const bMinusKMult = B.subtract(k.multiply(gModPowXN));
|
|
66
|
+
return bMinusKMult.modPow(smallA.add(U.multiply(X)), N).mod(N);
|
|
67
|
+
}
|
|
68
|
+
export function calculateHKDF(ikm, salt) {
|
|
69
|
+
const infoBitsBuffer = Buffer.concat([
|
|
70
|
+
Buffer.from('Caldera Derived Key', 'utf8'),
|
|
71
|
+
Buffer.from(String.fromCharCode(1), 'utf8'),
|
|
72
|
+
]);
|
|
73
|
+
const prk = hmac(sha256, salt)
|
|
74
|
+
.update(ikm)
|
|
75
|
+
.digest();
|
|
76
|
+
const hmacResult = hmac(sha256, prk)
|
|
77
|
+
.update(infoBitsBuffer)
|
|
78
|
+
.digest();
|
|
79
|
+
return hmacResult.slice(0, 16);
|
|
80
|
+
}
|
|
81
|
+
export function getPasswordAuthenticationKey(poolName, username, password, B, U, smallA, salt) {
|
|
82
|
+
const usernamePassword = `${poolName}${username}:${password}`;
|
|
83
|
+
const usernamePasswordHash = hashBuffer(Buffer.from(usernamePassword, 'utf-8'));
|
|
84
|
+
const X = new BigInteger(hashHexString(padHex(salt) + usernamePasswordHash), 16);
|
|
85
|
+
const S = calculateS(X, B, U, smallA);
|
|
86
|
+
return calculateHKDF(Buffer.from(padHex(S), 'hex'), Buffer.from(padHex(U), 'hex'));
|
|
87
|
+
}
|
|
88
|
+
export function calculateSignature(poolName, userId, secretBlock, hkdf) {
|
|
89
|
+
const timeStamp = formatInTimeZone(new Date(), 'UTC', "EEE MMM d HH:mm:ss 'UTC' yyyy");
|
|
90
|
+
const concatBuffer = Buffer.concat([
|
|
91
|
+
Buffer.from(poolName, 'utf8'),
|
|
92
|
+
Buffer.from(userId, 'utf8'),
|
|
93
|
+
Buffer.from(secretBlock, 'base64'),
|
|
94
|
+
Buffer.from(timeStamp, 'utf8'),
|
|
95
|
+
]);
|
|
96
|
+
const signature = Buffer.from(hmac(sha256, hkdf)
|
|
97
|
+
.update(concatBuffer)
|
|
98
|
+
.digest()).toString('base64');
|
|
99
|
+
return {
|
|
100
|
+
signature,
|
|
101
|
+
timeStamp,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
export function decodeJwt(jwt) {
|
|
105
|
+
const [header, payload, signature] = jwt.split('.');
|
|
106
|
+
return {
|
|
107
|
+
header: JSON.parse(Buffer.from(header, 'base64').toString('utf-8')),
|
|
108
|
+
payload: JSON.parse(Buffer.from(payload, 'base64').toString('utf-8')),
|
|
109
|
+
signature: signature,
|
|
110
|
+
};
|
|
111
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vardario/cognito-client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
"author": "Sahin Vardar",
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@vardario/cookies": "^0.1.
|
|
13
|
+
"@vardario/cookies": "^0.1.4",
|
|
14
14
|
"date-fns": "^2.29.3",
|
|
15
|
-
"date-fns-tz": "^1.3.7"
|
|
16
|
-
},
|
|
17
|
-
"devDependencies": {
|
|
18
|
-
"hash.js": "^1.1.7",
|
|
15
|
+
"date-fns-tz": "^1.3.7",
|
|
19
16
|
"jsbn": "^1.1.0",
|
|
20
17
|
"randombytes": "^2.1.0",
|
|
18
|
+
"hash.js": "^1.1.7"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
21
|
"@aws-sdk/client-cognito-identity-provider": "^3.209.0",
|
|
22
22
|
"@types/jsbn": "^1.2.30",
|
|
23
23
|
"@types/jsdom": "^20.0.1",
|
|
@@ -26,7 +26,6 @@
|
|
|
26
26
|
"jsdom": "^20.0.2",
|
|
27
27
|
"testcontainers": "^9.0.0",
|
|
28
28
|
"ts-jest": "^29.0.3",
|
|
29
|
-
"tsup": "^6.7.0",
|
|
30
29
|
"vitest": "^0.31.0"
|
|
31
30
|
},
|
|
32
31
|
"repository": {
|
|
@@ -36,7 +35,7 @@
|
|
|
36
35
|
"packageManager": "pnpm@8.3.1",
|
|
37
36
|
"scripts": {
|
|
38
37
|
"test": "vitest run",
|
|
39
|
-
"build": "
|
|
38
|
+
"build": "tsc --build",
|
|
40
39
|
"watch": "tsc --build --watch"
|
|
41
40
|
}
|
|
42
41
|
}
|