@carecard/auth-util 1.0.0
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/dist/cjs/cryptoUtilAuth.cjs +124 -0
- package/dist/cjs/cryptoUtilAuth.d.ts +48 -0
- package/dist/cjs/index.cjs +22 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/jwtUtilAuth.cjs +110 -0
- package/dist/cjs/jwtUtilAuth.d.ts +35 -0
- package/dist/cjs/keyGen.cjs +16 -0
- package/dist/cjs/keyGen.d.ts +11 -0
- package/dist/cjs/pwdUtilAuth.cjs +97 -0
- package/dist/cjs/pwdUtilAuth.d.ts +39 -0
- package/dist/cjs/strEncryptUtil.cjs +138 -0
- package/dist/cjs/strEncryptUtil.d.ts +46 -0
- package/dist/cjs/stringUtilAuth.cjs +107 -0
- package/dist/cjs/stringUtilAuth.d.ts +64 -0
- package/dist/esm/cryptoUtilAuth.d.ts +48 -0
- package/dist/esm/cryptoUtilAuth.js +82 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/jwtUtilAuth.d.ts +35 -0
- package/dist/esm/jwtUtilAuth.js +69 -0
- package/dist/esm/keyGen.d.ts +11 -0
- package/dist/esm/keyGen.js +12 -0
- package/dist/esm/pwdUtilAuth.d.ts +39 -0
- package/dist/esm/pwdUtilAuth.js +56 -0
- package/dist/esm/strEncryptUtil.d.ts +46 -0
- package/dist/esm/strEncryptUtil.js +97 -0
- package/dist/esm/stringUtilAuth.d.ts +64 -0
- package/dist/esm/stringUtilAuth.js +96 -0
- package/package.json +54 -0
- package/readme.md +51 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// src/strEncryptUtil.ts
|
|
2
|
+
import * as crypto from 'crypto';
|
|
3
|
+
/**
|
|
4
|
+
* Derive a key using scrypt.
|
|
5
|
+
*/
|
|
6
|
+
export function createKey(key, keyLength = 32) {
|
|
7
|
+
// scryptSync returns a Buffer
|
|
8
|
+
return crypto.scryptSync(key, key, keyLength);
|
|
9
|
+
}
|
|
10
|
+
/* --------------------------------------------------
|
|
11
|
+
* Helpers
|
|
12
|
+
* -------------------------------------------------- */
|
|
13
|
+
function getErrorCodeOrFallback(error) {
|
|
14
|
+
// Safely narrow 'unknown' to read 'code' when available
|
|
15
|
+
if (typeof error === 'object' &&
|
|
16
|
+
error !== null &&
|
|
17
|
+
'code' in error &&
|
|
18
|
+
typeof error.code === 'string') {
|
|
19
|
+
return error.code;
|
|
20
|
+
}
|
|
21
|
+
if (error instanceof Error) {
|
|
22
|
+
// You can return message or name; using name keeps it short
|
|
23
|
+
return `ERROR:${error.name}`;
|
|
24
|
+
}
|
|
25
|
+
return 'UNKNOWN_ERROR';
|
|
26
|
+
}
|
|
27
|
+
/* --------------------------------------------------
|
|
28
|
+
* Asymmetric Encryption (RSA or similar)
|
|
29
|
+
* -------------------------------------------------- */
|
|
30
|
+
/**
|
|
31
|
+
* Encrypts text using a private key, returning an encoded cipher text string.
|
|
32
|
+
* On error, returns a code string if present, otherwise a fallback.
|
|
33
|
+
*/
|
|
34
|
+
export const encryptByPrivateKey = (encryptionConfigObj, textToEncrypt) => {
|
|
35
|
+
try {
|
|
36
|
+
const encrypted = crypto.privateEncrypt(encryptionConfigObj.privateKey, Buffer.from(textToEncrypt, 'utf8'));
|
|
37
|
+
return encrypted.toString(encryptionConfigObj.encryptedTextEncoding);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
return getErrorCodeOrFallback(error);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Decrypts text using a public key, returning a plain text string.
|
|
45
|
+
* On error, returns a code string if present, otherwise a fallback.
|
|
46
|
+
*/
|
|
47
|
+
export const decryptByPublicKey = (decryptionConfigObj, textToDecrypt) => {
|
|
48
|
+
try {
|
|
49
|
+
const decrypted = crypto.publicDecrypt(decryptionConfigObj.publicKey, Buffer.from(textToDecrypt, decryptionConfigObj.encryptedTextEncoding));
|
|
50
|
+
return decrypted.toString(decryptionConfigObj.plainTextEncoding);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
return getErrorCodeOrFallback(error);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
/* --------------------------------------------------
|
|
57
|
+
* Symmetric Encryption (AES or similar)
|
|
58
|
+
* -------------------------------------------------- */
|
|
59
|
+
/**
|
|
60
|
+
* Encrypts text using a symmetric algorithm and derived key, returning an encoded cipher string.
|
|
61
|
+
* On error, returns a code string if present, otherwise a fallback.
|
|
62
|
+
*
|
|
63
|
+
* NOTE: This uses a zero IV (Buffer.alloc(16, 0)) which is generally **not recommended** for production.
|
|
64
|
+
* Prefer a random IV per encryption and prepend/append it to the output for decryption.
|
|
65
|
+
*/
|
|
66
|
+
export const encryptByKey = (encryptConfigObj, textToEncrypt) => {
|
|
67
|
+
try {
|
|
68
|
+
const iv = Buffer.alloc(16, 0); // ⚠️ consider using a random IV for security
|
|
69
|
+
const key = createKey(encryptConfigObj.encryptionKey, encryptConfigObj.keyLength);
|
|
70
|
+
const cipher = crypto.createCipheriv(encryptConfigObj.cipherAlgorithm, key, iv);
|
|
71
|
+
let encrypted = cipher.update(textToEncrypt, encryptConfigObj.plainTextEncoding, encryptConfigObj.encryptedTextEncoding);
|
|
72
|
+
encrypted += cipher.final(encryptConfigObj.encryptedTextEncoding);
|
|
73
|
+
return encrypted;
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
return getErrorCodeOrFallback(error);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Decrypts a cipher string using a symmetric algorithm and derived key,
|
|
81
|
+
* returning the plain text string. On error, returns a code string or fallback.
|
|
82
|
+
*
|
|
83
|
+
* NOTE: Must use the same IV that was used during encryption. Here it assumes a zero IV.
|
|
84
|
+
*/
|
|
85
|
+
export const decryptByKey = (encryptConfigObj, textToDecrypt) => {
|
|
86
|
+
try {
|
|
87
|
+
const iv = Buffer.alloc(16, 0); // ⚠️ must match the IV used in encryptByKey
|
|
88
|
+
const key = createKey(encryptConfigObj.encryptionKey, encryptConfigObj.keyLength);
|
|
89
|
+
const decipher = crypto.createDecipheriv(encryptConfigObj.cipherAlgorithm, key, iv);
|
|
90
|
+
let decrypted = decipher.update(textToDecrypt, encryptConfigObj.encryptedTextEncoding, encryptConfigObj.plainTextEncoding);
|
|
91
|
+
decrypted += decipher.final(encryptConfigObj.plainTextEncoding);
|
|
92
|
+
return decrypted;
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
return getErrorCodeOrFallback(error);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* For incoming jwt token validation, splitting and parsing.
|
|
3
|
+
* For outgoing jwt token assembling to jwt, make it url safe.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Adjusts padding of base64String
|
|
7
|
+
* @param base64String
|
|
8
|
+
* @return {*}
|
|
9
|
+
*/
|
|
10
|
+
export declare const adjustBase64Padding: (base64String: string) => string;
|
|
11
|
+
/**
|
|
12
|
+
* Removes /, + and = from the string
|
|
13
|
+
* @returns {string}
|
|
14
|
+
*/
|
|
15
|
+
export declare const makeStringUrlSafe: (urlUnsafeString?: string) => string;
|
|
16
|
+
/**
|
|
17
|
+
* Put back /, + and = into the string
|
|
18
|
+
* @returns {string}
|
|
19
|
+
*/
|
|
20
|
+
export declare const reverseStringUrlSafe: (urlSafeString?: string) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Encode string to base64 string
|
|
23
|
+
* @param unCodedString
|
|
24
|
+
* @returns {string}
|
|
25
|
+
*/
|
|
26
|
+
export declare const asciiToBase64: (unCodedString: string) => string;
|
|
27
|
+
/** Decode string from base64
|
|
28
|
+
* @param codedString
|
|
29
|
+
* @returns {string}
|
|
30
|
+
*/
|
|
31
|
+
export declare const base64ToAscii: (codedString: string) => string;
|
|
32
|
+
/**
|
|
33
|
+
* Decompose $ connected string and return an object
|
|
34
|
+
* return null if error
|
|
35
|
+
* @param passwordHash
|
|
36
|
+
*/
|
|
37
|
+
export declare const dollarSignConnectedStringToAlgorithmHashSalt: (passwordHash: string) => {
|
|
38
|
+
version: string;
|
|
39
|
+
alg: string;
|
|
40
|
+
hash: string;
|
|
41
|
+
salt: string;
|
|
42
|
+
} | null;
|
|
43
|
+
/**
|
|
44
|
+
* Decompose . connected string and return an object with
|
|
45
|
+
* {header: 'string', payload: 'string', signature: 'string'}
|
|
46
|
+
* return null if error
|
|
47
|
+
*/
|
|
48
|
+
export declare const dotConnectedStringToHeaderPayloadSignature: (jwt: string) => {
|
|
49
|
+
header: string;
|
|
50
|
+
payload: string;
|
|
51
|
+
signature: string;
|
|
52
|
+
} | null;
|
|
53
|
+
/**
|
|
54
|
+
* Turns object into url safe string
|
|
55
|
+
* @param object
|
|
56
|
+
* @return {string}
|
|
57
|
+
*/
|
|
58
|
+
export declare const objectToBase64UrlSafeString: (object: any) => string;
|
|
59
|
+
/**
|
|
60
|
+
* Turns base64 into object
|
|
61
|
+
* @param urlSafeBase64String
|
|
62
|
+
* @return {any}
|
|
63
|
+
*/
|
|
64
|
+
export declare const urlSafeBase64ToObject: (urlSafeBase64String: string) => any;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* For incoming jwt token validation, splitting and parsing.
|
|
4
|
+
* For outgoing jwt token assembling to jwt, make it url safe.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Adjusts padding of base64String
|
|
8
|
+
* @param base64String
|
|
9
|
+
* @return {*}
|
|
10
|
+
*/
|
|
11
|
+
export const adjustBase64Padding = (base64String) => {
|
|
12
|
+
while (base64String.length % 4)
|
|
13
|
+
base64String += '=';
|
|
14
|
+
return base64String;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Removes /, + and = from the string
|
|
18
|
+
* @returns {string}
|
|
19
|
+
*/
|
|
20
|
+
export const makeStringUrlSafe = (urlUnsafeString = '') => {
|
|
21
|
+
return urlUnsafeString.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Put back /, + and = into the string
|
|
25
|
+
* @returns {string}
|
|
26
|
+
*/
|
|
27
|
+
export const reverseStringUrlSafe = (urlSafeString = '') => {
|
|
28
|
+
let myString = urlSafeString.replace(/-/g, '+').replace(/_/g, '/');
|
|
29
|
+
return adjustBase64Padding(myString);
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Encode string to base64 string
|
|
33
|
+
* @param unCodedString
|
|
34
|
+
* @returns {string}
|
|
35
|
+
*/
|
|
36
|
+
export const asciiToBase64 = (unCodedString) => {
|
|
37
|
+
return Buffer.from(unCodedString).toString('base64');
|
|
38
|
+
};
|
|
39
|
+
/** Decode string from base64
|
|
40
|
+
* @param codedString
|
|
41
|
+
* @returns {string}
|
|
42
|
+
*/
|
|
43
|
+
export const base64ToAscii = (codedString) => {
|
|
44
|
+
return Buffer.from(codedString, 'base64').toString('ascii');
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Decompose $ connected string and return an object
|
|
48
|
+
* return null if error
|
|
49
|
+
* @param passwordHash
|
|
50
|
+
*/
|
|
51
|
+
export const dollarSignConnectedStringToAlgorithmHashSalt = (passwordHash) => {
|
|
52
|
+
const splitStringArray = passwordHash.split('$');
|
|
53
|
+
if (splitStringArray.length !== 6)
|
|
54
|
+
return null;
|
|
55
|
+
return {
|
|
56
|
+
version: splitStringArray[1],
|
|
57
|
+
alg: splitStringArray[2],
|
|
58
|
+
hash: splitStringArray[3],
|
|
59
|
+
salt: splitStringArray[4],
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Decompose . connected string and return an object with
|
|
64
|
+
* {header: 'string', payload: 'string', signature: 'string'}
|
|
65
|
+
* return null if error
|
|
66
|
+
*/
|
|
67
|
+
export const dotConnectedStringToHeaderPayloadSignature = (jwt) => {
|
|
68
|
+
const splitJWT = jwt.split('.');
|
|
69
|
+
if (splitJWT.length !== 3)
|
|
70
|
+
return null;
|
|
71
|
+
return {
|
|
72
|
+
header: splitJWT[0],
|
|
73
|
+
payload: splitJWT[1],
|
|
74
|
+
signature: splitJWT[2],
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Turns object into url safe string
|
|
79
|
+
* @param object
|
|
80
|
+
* @return {string}
|
|
81
|
+
*/
|
|
82
|
+
export const objectToBase64UrlSafeString = (object) => {
|
|
83
|
+
let stringAscii = JSON.stringify(object);
|
|
84
|
+
let base64String = asciiToBase64(stringAscii);
|
|
85
|
+
return makeStringUrlSafe(base64String);
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Turns base64 into object
|
|
89
|
+
* @param urlSafeBase64String
|
|
90
|
+
* @return {any}
|
|
91
|
+
*/
|
|
92
|
+
export const urlSafeBase64ToObject = (urlSafeBase64String) => {
|
|
93
|
+
let base64String = reverseStringUrlSafe(urlSafeBase64String);
|
|
94
|
+
let stringAscii = base64ToAscii(base64String);
|
|
95
|
+
return JSON.parse(stringAscii);
|
|
96
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@carecard/auth-util",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Auth utility functions",
|
|
6
|
+
"license": "ISC",
|
|
7
|
+
"author": "PK Singh",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https:github.com/CareCard-ca/pkg-auth-util.git"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"auth",
|
|
14
|
+
"utility",
|
|
15
|
+
"cryptology"
|
|
16
|
+
],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"main": "./dist/cjs/index.cjs",
|
|
22
|
+
"module": "./dist/esm/index.js",
|
|
23
|
+
"types": "./dist/esm/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"import": "./dist/esm/index.js",
|
|
27
|
+
"require": "./dist/cjs/index.cjs",
|
|
28
|
+
"types": "./dist/esm/index.d.ts"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "npm run build:esm && npm run build:cjs",
|
|
36
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
37
|
+
"build:cjs": "tsc -p tsconfig.cjs.json && node ./scripts/rename-cjs.js",
|
|
38
|
+
"test": "NODE_NO_WARNINGS=1 jest --coverage",
|
|
39
|
+
"format": "prettier --write .",
|
|
40
|
+
"format:check": "prettier --check .",
|
|
41
|
+
"lint": "eslint",
|
|
42
|
+
"prepare": "husky"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/jest": "30.0.0",
|
|
46
|
+
"eslint": "9.39.2",
|
|
47
|
+
"husky": "9.1.7",
|
|
48
|
+
"jest": "30.2.0",
|
|
49
|
+
"prettier": "3.7.4",
|
|
50
|
+
"ts-jest": "29.4.6",
|
|
51
|
+
"typescript": "5.9.3",
|
|
52
|
+
"typescript-eslint": "8.50.1"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Auth utilities class
|
|
2
|
+
|
|
3
|
+
## Main Functionality
|
|
4
|
+
|
|
5
|
+
This is a collection of utility functions for use in authentication
|
|
6
|
+
|
|
7
|
+
### Main functions
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
const { jwtUtilAuth, pwdUtilAuth } = require( '@chatpta/auth-util' );
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Create jwt
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
const headerObject = {
|
|
17
|
+
alg: "SHA256", // Mendatory acceptable algorithm
|
|
18
|
+
...
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const payloadObject = {
|
|
22
|
+
...
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const jwt = jwtUtilAuth.createSignedJwtFromObject( headerObject, payloadObject, privateKey );
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Verify jwt signature returns ```true``` or ```false```
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
const isVerified = jwtUtilAuth.verifyJwtSignature( jwt, publicKey );
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Get header and payload object from jwt.
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
const { header, payload } = jwtUtilAuth.getHeaderPayloadFromJwt( jwt );
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Create password hash to save in database
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
const hash = pwdUtilAuth.createPasswordHashWithRandomSalt( password, secret, algorithm );
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Create another password hash based on saved hash to compare.
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
const hashForLogin = pwdUtilAuth.createPasswordHashBasedOnSavedAlgorithmSalt( passwordForLogin, savedPasswordHash, secret );
|
|
50
|
+
```
|
|
51
|
+
|