@carecard/auth-util 3.1.13 → 3.1.15
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/.agents/config.toml +2 -0
- package/.agents/skills/carecard-workspace-standards/SKILL.md +388 -0
- package/.agents/skills/carecard-workspace-standards/agents/openai.yaml +4 -0
- package/.agents/skills/github-pr-create-update/SKILL.md +188 -0
- package/.agents/skills/github-pr-create-update/agents/openai.yaml +5 -0
- package/.agents/skills/github-pr-merge-cleanup/SKILL.md +175 -0
- package/.agents/skills/github-pr-merge-cleanup/agents/openai.yaml +5 -0
- package/.agents/skills/pkg-auth-util-auth-crypto-library/SKILL.md +236 -0
- package/.agents/skills/pkg-auth-util-auth-crypto-library/agents/openai.yaml +4 -0
- package/.agents/skills/software-design-patterns-and-clean-code/SKILL.md +73 -0
- package/.codex/config.toml +2 -0
- package/.prettierignore +17 -0
- package/.prettierrc.cjs +32 -0
- package/.prettierrc.js +12 -0
- package/eslint.config.mjs +14 -0
- package/index.d.ts +197 -151
- package/index.js +29 -28
- package/jest.config.js +16 -0
- package/lib/jwtUtilAuth.js +152 -86
- package/lib/keyGen.js +18 -22
- package/lib/pwdUtilAuth.js +25 -25
- package/lib/stringUtilAuth.js +43 -49
- package/package.json +24 -12
- package/readme.md +24 -5
- package/scripts/rename-cjs.js +13 -0
- package/src/cryptoUtilAuth.ts +111 -0
- package/src/index.ts +6 -0
- package/src/jwtUtilAuth.ts +85 -0
- package/src/keyGen.ts +14 -0
- package/src/pwdUtilAuth.ts +78 -0
- package/src/strEncryptUtil.ts +159 -0
- package/src/stringUtilAuth.ts +102 -0
- package/tests/cryptoUtilAuth.test.ts +97 -0
- package/tests/index.test.ts +157 -0
- package/tests/indexStringUtilAuth.test.ts +95 -0
- package/tests/jwtUtilAuth.test.ts +33 -0
- package/tests/keyGen.test.ts +26 -0
- package/tests/keys/keys.ts +24 -0
- package/tests/pwdUtilAuth.test.ts +40 -0
- package/tsconfig.base.json +14 -0
- package/tsconfig.cjs.json +7 -0
- package/tsconfig.esm.json +7 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import * as assert from 'assert';
|
|
2
|
+
import * as cryptoUtilAuth from '../src';
|
|
3
|
+
import * as keys from './keys/keys';
|
|
4
|
+
|
|
5
|
+
describe('CryptoUtilAuth test', function () {
|
|
6
|
+
it('createBase64SignatureOfToken returns base64 signature of token', function () {
|
|
7
|
+
const algorithm = 'SHA256';
|
|
8
|
+
const token = 'Hi I am token';
|
|
9
|
+
const signature =
|
|
10
|
+
'G3uHnG5DIoi3YWaj+umNmCtzAfBMmxAGfkWxlXP9qTfEr48qJjIVIdD5ic5T9YSDMt+6+XsembuL2NP6h4xoe+qE/wRjNKXCF6Hg/VvBciOdZyUqX8TaiAbGsh6J2d42rjX1vchrqfrBgCW5kiyeZcTic8LQeNdL/2gO+F9bW8A=';
|
|
11
|
+
|
|
12
|
+
const returnedSignature = cryptoUtilAuth.createBase64SignatureOfToken(
|
|
13
|
+
token,
|
|
14
|
+
keys.privateKey,
|
|
15
|
+
algorithm,
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
assert.deepStrictEqual(returnedSignature, signature);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('verifyBase64SignatureOfToken returns true or false', function () {
|
|
22
|
+
const algorithm = 'SHA256';
|
|
23
|
+
const token = 'Hi I am token';
|
|
24
|
+
const signature =
|
|
25
|
+
'G3uHnG5DIoi3YWaj+umNmCtzAfBMmxAGfkWxlXP9qTfEr48qJjIVIdD5ic5T9YSDMt+6+XsembuL2NP6h4xoe+qE/wRjNKXCF6Hg/VvBciOdZyUqX8TaiAbGsh6J2d42rjX1vchrqfrBgCW5kiyeZcTic8LQeNdL/2gO+F9bW8A=';
|
|
26
|
+
const temperedSignature = 'temper' + signature;
|
|
27
|
+
const expectedValueTrue = true;
|
|
28
|
+
const expectedValueFalse = false;
|
|
29
|
+
|
|
30
|
+
const returnedValue = cryptoUtilAuth.verifyBase64SignatureOfToken(
|
|
31
|
+
token,
|
|
32
|
+
signature,
|
|
33
|
+
keys.publicKey,
|
|
34
|
+
algorithm,
|
|
35
|
+
);
|
|
36
|
+
const returnedValueFalse = cryptoUtilAuth.verifyBase64SignatureOfToken(
|
|
37
|
+
token,
|
|
38
|
+
temperedSignature,
|
|
39
|
+
keys.publicKey,
|
|
40
|
+
algorithm,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
assert.deepStrictEqual(returnedValue, expectedValueTrue);
|
|
44
|
+
assert.deepStrictEqual(returnedValueFalse, expectedValueFalse);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('createHmacBase64 returns base 64 hmac', function () {
|
|
48
|
+
const algorithm = 'SHA256';
|
|
49
|
+
const token = 'Hi I am token';
|
|
50
|
+
const secret = 'My secret';
|
|
51
|
+
const expectedHmacBase64 = 'IpOkaPa1YPTXQYPr6adIGk3ACgeqWyV+nvB4+7Ox4Dg=';
|
|
52
|
+
|
|
53
|
+
const returnedHmacBase64 = cryptoUtilAuth.createHmacBase64(token, secret, algorithm);
|
|
54
|
+
|
|
55
|
+
assert.deepStrictEqual(returnedHmacBase64, expectedHmacBase64);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('createSaltBase64 returns random base 64 string', function () {
|
|
59
|
+
const randomSalt = cryptoUtilAuth.createSaltBase64();
|
|
60
|
+
|
|
61
|
+
assert.deepStrictEqual(randomSalt.length, 44);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('encryptStringAsciiToBase64 returns base 64 and encrypted string', function () {
|
|
65
|
+
const plainTextString = 'This is some text for encryption';
|
|
66
|
+
const encryptedString = 'juggjf+C81QzXvqa8qE1GHTbrMQydtoFszKw2kFjGDduCpXwS01cVnYyYl9an7l7';
|
|
67
|
+
const salt = 'my salt';
|
|
68
|
+
const secret = 'top secret';
|
|
69
|
+
const algorithm = 'aes-192-cbc';
|
|
70
|
+
|
|
71
|
+
const encryptedText = cryptoUtilAuth.encryptStringAsciiToBase64(
|
|
72
|
+
plainTextString,
|
|
73
|
+
salt,
|
|
74
|
+
secret,
|
|
75
|
+
algorithm,
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
assert.deepStrictEqual(encryptedText, encryptedString);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('decryptStringBase64ToAscii returns ascii string', function () {
|
|
82
|
+
const encryptedString = 'juggjf+C81QzXvqa8qE1GHTbrMQydtoFszKw2kFjGDduCpXwS01cVnYyYl9an7l7';
|
|
83
|
+
const plainTextString = 'This is some text for encryption';
|
|
84
|
+
const salt = 'my salt';
|
|
85
|
+
const secret = 'top secret';
|
|
86
|
+
const algorithm = 'aes-192-cbc';
|
|
87
|
+
|
|
88
|
+
const encryptedText = cryptoUtilAuth.decryptStringBase64ToAscii(
|
|
89
|
+
encryptedString,
|
|
90
|
+
salt,
|
|
91
|
+
secret,
|
|
92
|
+
algorithm,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
assert.deepStrictEqual(encryptedText, plainTextString);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import * as assert from 'assert';
|
|
2
|
+
import * as jwtUtilAuth from '../src/jwtUtilAuth';
|
|
3
|
+
import * as pwdUtilAuth from '../src/pwdUtilAuth';
|
|
4
|
+
import * as strEncryptUtil from '../src/strEncryptUtil';
|
|
5
|
+
import * as keys from './keys/keys';
|
|
6
|
+
|
|
7
|
+
describe('Index/JwtUtilAuth', function () {
|
|
8
|
+
it('createSignedJwtFromObject returns base64 url safe jwt', function () {
|
|
9
|
+
const header = {
|
|
10
|
+
alg: 'SHA512',
|
|
11
|
+
typ: 'JWT',
|
|
12
|
+
};
|
|
13
|
+
const payload = {
|
|
14
|
+
sub: '1234567890',
|
|
15
|
+
name: 'John Doe',
|
|
16
|
+
iat: 1516239022,
|
|
17
|
+
cpso: '81883',
|
|
18
|
+
roles: ['ph', 'ea'],
|
|
19
|
+
};
|
|
20
|
+
const expectedJwt =
|
|
21
|
+
'eyJhbGciOiJTSEE1MTIiLCJ0eXAiOiJKV1QifQ.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJjcHNvIjoiODE4ODMiLCJyb2xlcyI6WyJwaCIsImVhIl19.PWff8BvKP79ukuZrGEhyIw4HN86m99l4VZo9xL_Ul5EHQFC1RsEvxUig4z2sUZqAvQLcQjEhNR7hf0KkB7YeJTWZF4QLRX6GwC5SvE2kryrYSlZvop2SCbYdty38gzDw3xTdDzcJo0awE45Sk_ZlRnjgcDD-wXAW3i7ToXRcPxM';
|
|
22
|
+
|
|
23
|
+
const createdJwt = jwtUtilAuth.createSignedJwtFromObject(header, payload, keys.privateKey);
|
|
24
|
+
|
|
25
|
+
assert.deepStrictEqual(createdJwt, expectedJwt);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('verifyJwtSignature returns true or false', function () {
|
|
29
|
+
const jwt =
|
|
30
|
+
'eyJhbGciOiJTSEE1MTIiLCJ0eXAiOiJKV1QifQ.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJjcHNvIjoiODE4ODMiLCJyb2xlcyI6WyJwaCIsImVhIl19.PWff8BvKP79ukuZrGEhyIw4HN86m99l4VZo9xL_Ul5EHQFC1RsEvxUig4z2sUZqAvQLcQjEhNR7hf0KkB7YeJTWZF4QLRX6GwC5SvE2kryrYSlZvop2SCbYdty38gzDw3xTdDzcJo0awE45Sk_ZlRnjgcDD-wXAW3i7ToXRcPxM';
|
|
31
|
+
|
|
32
|
+
const isVerified = jwtUtilAuth.verifyJwtSignature(jwt, keys.publicKey);
|
|
33
|
+
|
|
34
|
+
assert.deepStrictEqual(isVerified, true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('getHeaderPayloadFromJwt returns header, payload object', function () {
|
|
38
|
+
const jwt =
|
|
39
|
+
'eyJhbGciOiJTSEE1MTIiLCJ0eXAiOiJKV1QifQ.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJjcHNvIjoiODE4ODMiLCJyb2xlcyI6WyJwaCIsImVhIl19.PWff8BvKP79ukuZrGEhyIw4HN86m99l4VZo9xL_Ul5EHQFC1RsEvxUig4z2sUZqAvQLcQjEhNR7hf0KkB7YeJTWZF4QLRX6GwC5SvE2kryrYSlZvop2SCbYdty38gzDw3xTdDzcJo0awE45Sk_ZlRnjgcDD-wXAW3i7ToXRcPxM';
|
|
40
|
+
|
|
41
|
+
const expectedHeader = {
|
|
42
|
+
alg: 'SHA512',
|
|
43
|
+
typ: 'JWT',
|
|
44
|
+
};
|
|
45
|
+
const expectedPayload = {
|
|
46
|
+
sub: '1234567890',
|
|
47
|
+
name: 'John Doe',
|
|
48
|
+
iat: 1516239022,
|
|
49
|
+
cpso: '81883',
|
|
50
|
+
roles: ['ph', 'ea'],
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const { header, payload } = jwtUtilAuth.getHeaderPayloadFromJwt(jwt);
|
|
54
|
+
|
|
55
|
+
assert.deepStrictEqual(header, expectedHeader);
|
|
56
|
+
assert.deepStrictEqual(payload, expectedPayload);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe('Index/PwdUtilAuth', function () {
|
|
61
|
+
it('createPasswordHashWithRandomSalt called with save hash', function () {
|
|
62
|
+
const password = 'mySecretPassword';
|
|
63
|
+
const secret = 'bigSecret';
|
|
64
|
+
const algorithm = 'sha512';
|
|
65
|
+
|
|
66
|
+
const hash = pwdUtilAuth.createPasswordHashWithRandomSalt(password, secret, algorithm);
|
|
67
|
+
|
|
68
|
+
assert.deepStrictEqual(hash.length > 40, true);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('createPasswordHashBasedOnSavedAlgorithmSalt called with saved hash', function () {
|
|
72
|
+
const savedHash =
|
|
73
|
+
'$1$c2hhNTEy$SOk/04Wn/ce1YIXHlUIqt5SgsuCCLIFjxpzHloVSxFh/z8JuLFshAaGNCkIRf47QSPCOJpkJ476N2eq1Yg1+yg==$6h29BnpUkqfrmtnY1xUrAGZcpcAl5cUEJ4Qjj+BGXbo=$';
|
|
74
|
+
const password = 'mySecretPassword';
|
|
75
|
+
const secret = 'bigSecret';
|
|
76
|
+
|
|
77
|
+
const hash = pwdUtilAuth.createPasswordHashBasedOnSavedAlgorithmSalt(
|
|
78
|
+
password,
|
|
79
|
+
savedHash,
|
|
80
|
+
secret,
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
assert.deepStrictEqual(hash, savedHash);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('Index/strEncryptUtil', function () {
|
|
88
|
+
it('encryptByPrivateKey', function () {
|
|
89
|
+
const textToEncrypt = 'Asymmetric encryption';
|
|
90
|
+
const expectedEncryptedString =
|
|
91
|
+
'VtwwLocyYdCreTBRifUmFuLRQ3Lrmw0RxDEN9zQh9lTJ2+6K/iLj7F5TDqm10hIKtfeajacs5HgEPGLb4whSpy7ggMtCNZQoujJNElNq2d7TScquYWi34cGlURzNTIUqC66afYYF2djq1QNVkWMzrnLMztrHem09+VlmA+eGLdc=';
|
|
92
|
+
const encryptionConfigObj = {
|
|
93
|
+
cipherAlgorithm: 'aes-256-cbc',
|
|
94
|
+
keyLength: 32,
|
|
95
|
+
privateKey: keys.privateKey,
|
|
96
|
+
plainTextEncoding: 'utf8',
|
|
97
|
+
encryptedTextEncoding: 'base64',
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const encryptedString = strEncryptUtil.encryptByPrivateKey(encryptionConfigObj, textToEncrypt);
|
|
101
|
+
|
|
102
|
+
assert.deepStrictEqual(encryptedString, expectedEncryptedString);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('decryptByPublicKey', function () {
|
|
106
|
+
const inputEncryptedString =
|
|
107
|
+
'VtwwLocyYdCreTBRifUmFuLRQ3Lrmw0RxDEN9zQh9lTJ2+6K/iLj7F5TDqm10hIKtfeajacs5HgEPGLb4whSpy7ggMtCNZQoujJNElNq2d7TScquYWi34cGlURzNTIUqC66afYYF2djq1QNVkWMzrnLMztrHem09+VlmA+eGLdc=';
|
|
108
|
+
const expectedText = 'Asymmetric encryption';
|
|
109
|
+
const decryptionConfigObj = {
|
|
110
|
+
cipherAlgorithm: 'aes-256-cbc',
|
|
111
|
+
keyLength: 32,
|
|
112
|
+
publicKey: keys.publicKey,
|
|
113
|
+
plainTextEncoding: 'utf8',
|
|
114
|
+
encryptedTextEncoding: 'base64',
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const decryptedString = strEncryptUtil.decryptByPublicKey(
|
|
118
|
+
decryptionConfigObj,
|
|
119
|
+
inputEncryptedString,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
assert.deepStrictEqual(decryptedString, expectedText);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('encryptByKey', function () {
|
|
126
|
+
const textToEncrypt = 'This is some text for encryption';
|
|
127
|
+
const expectedEncryptedString =
|
|
128
|
+
'/RMgsfS/ANEngXOwjFDYqxutOLnaY7XxDiJK403KZTcp8D76qPzwUYcYAF+lle4I';
|
|
129
|
+
const encryptConfigObj = {
|
|
130
|
+
cipherAlgorithm: 'aes-256-cbc',
|
|
131
|
+
keyLength: 32,
|
|
132
|
+
encryptionKey: keys.privateKey,
|
|
133
|
+
plainTextEncoding: 'utf8',
|
|
134
|
+
encryptedTextEncoding: 'base64',
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const encryptedString = strEncryptUtil.encryptByKey(encryptConfigObj, textToEncrypt);
|
|
138
|
+
|
|
139
|
+
assert.deepStrictEqual(encryptedString, expectedEncryptedString);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('decryptByKey', function () {
|
|
143
|
+
const encryptedString = '/RMgsfS/ANEngXOwjFDYqxutOLnaY7XxDiJK403KZTcp8D76qPzwUYcYAF+lle4I';
|
|
144
|
+
const expectedText = 'This is some text for encryption';
|
|
145
|
+
const encryptConfigObj = {
|
|
146
|
+
cipherAlgorithm: 'aes-256-cbc',
|
|
147
|
+
keyLength: 32,
|
|
148
|
+
encryptionKey: keys.privateKey,
|
|
149
|
+
plainTextEncoding: 'utf8',
|
|
150
|
+
encryptedTextEncoding: 'base64',
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const decryptedString = strEncryptUtil.decryptByKey(encryptConfigObj, encryptedString);
|
|
154
|
+
|
|
155
|
+
assert.deepStrictEqual(decryptedString, expectedText);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import * as assert from 'assert';
|
|
2
|
+
import * as stringUtilAuth from '../src/stringUtilAuth';
|
|
3
|
+
|
|
4
|
+
describe('StringUtilAuth test', function () {
|
|
5
|
+
it('makeStringUrlSafe returns url safe string', function (done) {
|
|
6
|
+
const urlUnsafeString = 'eyJhbGciOiJzaGE1MTIiL/CJ0e+XAiOiJKV1QifQ.eyJpZkkCI6IjEyMz===';
|
|
7
|
+
const urlSafeString = 'eyJhbGciOiJzaGE1MTIiL_CJ0e-XAiOiJKV1QifQ.eyJpZkkCI6IjEyMz';
|
|
8
|
+
|
|
9
|
+
const returnedString = stringUtilAuth.makeStringUrlSafe(urlUnsafeString);
|
|
10
|
+
assert.deepStrictEqual(returnedString, urlSafeString);
|
|
11
|
+
done();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('reverseStringUrlSafe returns url safe string', function (done) {
|
|
15
|
+
const urlSafeString = 'eyJhbGciOiJzaGE1MTIiL_CJ0e-XAiOiJKV1QifQ.eyJpZkkCI6IjEyMz';
|
|
16
|
+
const urlUnsafeString = 'eyJhbGciOiJzaGE1MTIiL/CJ0e+XAiOiJKV1QifQ.eyJpZkkCI6IjEyMz===';
|
|
17
|
+
|
|
18
|
+
const returnedString = stringUtilAuth.reverseStringUrlSafe(urlSafeString);
|
|
19
|
+
assert.deepStrictEqual(returnedString, urlUnsafeString);
|
|
20
|
+
done();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('asciiToBase64 returns base 64 string', function (done) {
|
|
24
|
+
const asciiString = 'How are you';
|
|
25
|
+
const base64String = 'SG93IGFyZSB5b3U=';
|
|
26
|
+
|
|
27
|
+
const returnedString = stringUtilAuth.asciiToBase64(asciiString);
|
|
28
|
+
assert.deepStrictEqual(returnedString, base64String);
|
|
29
|
+
done();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('base64ToAscii returns ascii string', function (done) {
|
|
33
|
+
const base64String = 'SG93IGFyZSB5b3U=';
|
|
34
|
+
const asciiString = 'How are you';
|
|
35
|
+
|
|
36
|
+
const returnedString = stringUtilAuth.base64ToAscii(base64String);
|
|
37
|
+
assert.deepStrictEqual(returnedString, asciiString);
|
|
38
|
+
done();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('dollarSignConnectedStringToAlgorithmHashSalt split at $ returns object', function (done) {
|
|
42
|
+
const inputHash = '$1$c2hhNTEy$eyJhbGciOiJzaG$OiJzaGE1MTIiL$';
|
|
43
|
+
const expectedObject = {
|
|
44
|
+
version: '1',
|
|
45
|
+
alg: 'c2hhNTEy',
|
|
46
|
+
hash: 'eyJhbGciOiJzaG',
|
|
47
|
+
salt: 'OiJzaGE1MTIiL',
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const returnedObject = stringUtilAuth.dollarSignConnectedStringToAlgorithmHashSalt(inputHash);
|
|
51
|
+
assert.deepStrictEqual(returnedObject, expectedObject);
|
|
52
|
+
done();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('dotConnectedStringToHeaderPayloadSignature split at . returns object', function (done) {
|
|
56
|
+
const inputString = 'c2hhNTEy.eyJhbGciOiJzaG.OiJzaGE1MTIiL';
|
|
57
|
+
const expectedObject = {
|
|
58
|
+
header: 'c2hhNTEy',
|
|
59
|
+
payload: 'eyJhbGciOiJzaG',
|
|
60
|
+
signature: 'OiJzaGE1MTIiL',
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const returnedObject = stringUtilAuth.dotConnectedStringToHeaderPayloadSignature(inputString);
|
|
64
|
+
assert.deepStrictEqual(expectedObject, returnedObject);
|
|
65
|
+
done();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('objectToBase64UrlSafeString returns url safe base64 string', function (done) {
|
|
69
|
+
const object = {
|
|
70
|
+
header: {
|
|
71
|
+
alg: 'HS512',
|
|
72
|
+
typ: 'JWT',
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
const expectedBase64String = 'eyJoZWFkZXIiOnsiYWxnIjoiSFM1MTIiLCJ0eXAiOiJKV1QifX0';
|
|
76
|
+
|
|
77
|
+
const returnedBase64String = stringUtilAuth.objectToBase64UrlSafeString(object);
|
|
78
|
+
assert.deepStrictEqual(returnedBase64String, expectedBase64String);
|
|
79
|
+
done();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('urlSafeBase64ToObject returns object', function (done) {
|
|
83
|
+
const base64String = 'eyJoZWFkZXIiOnsiYWxnIjoiSFM1MTIiLCJ0eXAiOiJKV1QifX0';
|
|
84
|
+
const expectedObject = {
|
|
85
|
+
header: {
|
|
86
|
+
alg: 'HS512',
|
|
87
|
+
typ: 'JWT',
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const returnedObject = stringUtilAuth.urlSafeBase64ToObject(base64String);
|
|
92
|
+
assert.deepStrictEqual(returnedObject, expectedObject);
|
|
93
|
+
done();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as assert from 'assert';
|
|
2
|
+
import * as jwtUtilAuthFromFile from '../src/jwtUtilAuth';
|
|
3
|
+
|
|
4
|
+
describe('JwtUtilAuth test', function () {
|
|
5
|
+
it('assembleJwt called with header, payload and signature returns jwt', function () {
|
|
6
|
+
const header = 'eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9';
|
|
7
|
+
const payload =
|
|
8
|
+
'eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0';
|
|
9
|
+
const signature =
|
|
10
|
+
'-DprLrW2OyqiAFiuWs14WO2TWp2EHtaX7a63dqrklk-xrjaZMrcPhpX4hkZw803SQx5HpGc-7VYBX8l82XlMZg';
|
|
11
|
+
const expectedJwt =
|
|
12
|
+
'eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.-DprLrW2OyqiAFiuWs14WO2TWp2EHtaX7a63dqrklk-xrjaZMrcPhpX4hkZw803SQx5HpGc-7VYBX8l82XlMZg';
|
|
13
|
+
|
|
14
|
+
const returnedJwt = jwtUtilAuthFromFile._assembleJwt(header, payload, signature);
|
|
15
|
+
assert.deepStrictEqual(returnedJwt, expectedJwt);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('splitJwtInToHeaderPayloadSignature splits jwt into its parts', function () {
|
|
19
|
+
const jwt =
|
|
20
|
+
'eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.-DprLrW2OyqiAFiuWs14WO2TWp2EHtaX7a63dqrklk-xrjaZMrcPhpX4hkZw803SQx5HpGc-7VYBX8l82XlMZg';
|
|
21
|
+
const headerExpected = 'eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9';
|
|
22
|
+
const payloadExpected =
|
|
23
|
+
'eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0';
|
|
24
|
+
const signatureExpected =
|
|
25
|
+
'-DprLrW2OyqiAFiuWs14WO2TWp2EHtaX7a63dqrklk-xrjaZMrcPhpX4hkZw803SQx5HpGc-7VYBX8l82XlMZg';
|
|
26
|
+
|
|
27
|
+
const { header, payload, signature } =
|
|
28
|
+
jwtUtilAuthFromFile._splitJwtInToHeaderPayloadSignature(jwt);
|
|
29
|
+
assert.deepStrictEqual(header, headerExpected);
|
|
30
|
+
assert.deepStrictEqual(payload, payloadExpected);
|
|
31
|
+
assert.deepStrictEqual(signature, signatureExpected);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as assert from 'assert';
|
|
2
|
+
import * as cryptoUtilAuth from '../src/cryptoUtilAuth';
|
|
3
|
+
import { generateKeyPair } from '../src';
|
|
4
|
+
|
|
5
|
+
describe('Create key to use with jwt', function () {
|
|
6
|
+
it('tests if keys created work', function () {
|
|
7
|
+
const { publicKey, privateKey } = generateKeyPair();
|
|
8
|
+
|
|
9
|
+
const algorithm = 'SHA256';
|
|
10
|
+
const token = 'Hi I am token';
|
|
11
|
+
|
|
12
|
+
const returnedSignature = cryptoUtilAuth.createBase64SignatureOfToken(
|
|
13
|
+
token,
|
|
14
|
+
privateKey,
|
|
15
|
+
algorithm,
|
|
16
|
+
);
|
|
17
|
+
const returnedValue = cryptoUtilAuth.verifyBase64SignatureOfToken(
|
|
18
|
+
token,
|
|
19
|
+
returnedSignature,
|
|
20
|
+
publicKey,
|
|
21
|
+
algorithm,
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
assert.deepStrictEqual(returnedValue, true);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const privateKey =
|
|
2
|
+
'-----BEGIN RSA PRIVATE KEY-----\n' +
|
|
3
|
+
'MIICWgIBAAKBgF/34KV8D2pL57AiOSBYaNeCdVZEbXTOjR2J/UEH/F240gTxVT2K' +
|
|
4
|
+
'nXOP4W6OdFkSEsdDWLqhLO2OP68oakh3dm0Yzb8U+4Eos5Z3ZS+hgZCiutmm4Ef2' +
|
|
5
|
+
'krUshS8BI8MJvI4YPn4meJzaDz3SccQ2O9V6YpaYX1VqHO48qNGpg/LdAgMBAAEC' +
|
|
6
|
+
'gYBGlYAK9uY3nkB4EZPGsH/cEj3PB2EiClFMKpO0YvVA0ZDWxdGWA/+uYWLC98+n' +
|
|
7
|
+
'O13IZUY7bhQnXPqGD8msJwFYcCJahMtDlr0UvYVgTlYj7pVUjZEdj975vbSeVpu+' +
|
|
8
|
+
'MNK5KwQEel5phrpajpvX0feWjAKZQ7rt+xbAQ66azP4RwQJBALD8zJavNcSCUcKX' +
|
|
9
|
+
'Ll3gjgUOCeNxWGmy4Pj9QQV/SCUF0OPYl1tfS5+8hrDJRhElLaFon8TqAvZI+CxP' +
|
|
10
|
+
'cSBDrpkCQQCKz7Ys/O+NyNYC9eQuyUmLqgMQD9z3nJKbZrmhpGQNnTmkdBhdME3F' +
|
|
11
|
+
'Bn71GM6g5roDZPv8jbue1Rnma2vXDWTlAkBoiVh55BEY+XQ7QDplvn2D5M/YZBk5' +
|
|
12
|
+
'jSsuRb+C9LPzEiVYfpSfpoCvX5YakzdsZw41mOtNTn7jYyQaX+3Mhc15AkAQAzX4' +
|
|
13
|
+
'qpTXvjsxzDuuSfn56nq/95R928nts0PmqZgBGgn0NpA478GI4N70VxA+71611Yhb' +
|
|
14
|
+
'8d2azOpX8jxWam0hAkBeLsgPso9MiFXtRnl4jLt7X5CT673EY3oFSRq+ZJNE1pGQ' +
|
|
15
|
+
'7Z/g7Yg/WeznAvpqYx3b6m3DpPzrPU6KWQ/Gfs+L\n' +
|
|
16
|
+
'-----END RSA PRIVATE KEY-----';
|
|
17
|
+
|
|
18
|
+
export const publicKey =
|
|
19
|
+
'-----BEGIN PUBLIC KEY-----\n' +
|
|
20
|
+
'MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgF/34KV8D2pL57AiOSBYaNeCdVZE' +
|
|
21
|
+
'bXTOjR2J/UEH/F240gTxVT2KnXOP4W6OdFkSEsdDWLqhLO2OP68oakh3dm0Yzb8U' +
|
|
22
|
+
'+4Eos5Z3ZS+hgZCiutmm4Ef2krUshS8BI8MJvI4YPn4meJzaDz3SccQ2O9V6YpaY' +
|
|
23
|
+
'X1VqHO48qNGpg/LdAgMBAAE=\n' +
|
|
24
|
+
'-----END PUBLIC KEY-----';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as assert from 'assert';
|
|
2
|
+
import * as pwdUtilAuth from '../src/pwdUtilAuth';
|
|
3
|
+
|
|
4
|
+
describe('PwdUtilAuth test', function () {
|
|
5
|
+
it('assemblePasswordHash called with algorithmBase64, hashBase64, saltBase64 returns password hash', function () {
|
|
6
|
+
const algorithmBase64 = 'c2hhMjU2';
|
|
7
|
+
const hashBase64 = 'c2hhMjU2';
|
|
8
|
+
const saltBase64 = 'c2hhMjU2';
|
|
9
|
+
const expectedHash = '$1$c2hhMjU2$c2hhMjU2$c2hhMjU2$';
|
|
10
|
+
|
|
11
|
+
const returnedHash = pwdUtilAuth._assemblePasswordHash(algorithmBase64, hashBase64, saltBase64);
|
|
12
|
+
|
|
13
|
+
assert.deepStrictEqual(returnedHash, expectedHash);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('disassemblePasswordHash called with save hash', function () {
|
|
17
|
+
const savedHash = '$1$c2hhMjU2$c2hhMjU$c2hhMjU2$';
|
|
18
|
+
const hashBase64 = 'c2hhMjU';
|
|
19
|
+
const saltBase64 = 'c2hhMjU2';
|
|
20
|
+
|
|
21
|
+
const { version, alg, hash, salt } = pwdUtilAuth._disassemblePasswordHash(savedHash);
|
|
22
|
+
|
|
23
|
+
assert.deepStrictEqual(salt, saltBase64);
|
|
24
|
+
assert.deepStrictEqual(hash, hashBase64);
|
|
25
|
+
assert.deepStrictEqual(version, '1');
|
|
26
|
+
assert.deepStrictEqual(alg, 'c2hhMjU2');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('createPasswordHash called with save hash', function () {
|
|
30
|
+
const password = 'bigSecret*2';
|
|
31
|
+
const secret = '240gTxVT2KnXOP4W6OdFkSEsdDWLqhLO2OP68o';
|
|
32
|
+
const algorithm = 'sha512';
|
|
33
|
+
const salt = '6h29BnpUkqfrmtnY1xUrAGZcpcAl5cUEJ4Qjj+BGXbo=';
|
|
34
|
+
const expectedHash =
|
|
35
|
+
'$1$c2hhNTEy$rNPXY0aVOYsdenIAWdfxDL6d6247s+ScI9kcDvEBkipNo7S9QDy6utTqTcQLY3+tufc0f7AvmdocotW7bDIyYA==$6h29BnpUkqfrmtnY1xUrAGZcpcAl5cUEJ4Qjj+BGXbo=$';
|
|
36
|
+
const hash = pwdUtilAuth._createPasswordHash(password, secret, salt, algorithm);
|
|
37
|
+
|
|
38
|
+
assert.deepStrictEqual(hash, expectedHash);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"lib": ["ES2020", "ES2021", "dom"],
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"strict": true,
|
|
7
|
+
"moduleResolution": "Node",
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"isolatedModules": true
|
|
11
|
+
},
|
|
12
|
+
"include": ["src"],
|
|
13
|
+
"exclude": ["node_modules", "dist", "tests"]
|
|
14
|
+
}
|