From 10f04436319d201012074364544c1dcf266f283f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Jacobo=20Vi=C3=B1a=20Rebolledo?= Date: Thu, 14 Dec 2023 01:53:22 +0100 Subject: [PATCH] [GH-122] feat(Plugin): add encryption adapter --- .env.test | 1 + package-lock.json | 226 +++++++++++++++++- package.json | 4 + src/Contexts/shared/plugins/CryptAdapter.ts | 50 ++++ src/Contexts/shared/plugins/EncrypterTool.ts | 9 + src/config/plugins/envs.plugin.ts | 3 +- .../shared/plugins/CryptAdapter.test.ts | 87 +++++++ 7 files changed, 371 insertions(+), 9 deletions(-) create mode 100644 src/Contexts/shared/plugins/CryptAdapter.ts create mode 100644 src/Contexts/shared/plugins/EncrypterTool.ts create mode 100644 tests/Contexts/shared/plugins/CryptAdapter.test.ts diff --git a/.env.test b/.env.test index 8d3d7a9..2819bd0 100644 --- a/.env.test +++ b/.env.test @@ -7,3 +7,4 @@ MONGO_URL=localhost:27017 MONGO_DB=test MONGO_USERNAME=localUser MONGO_PASSWORD=localPassword +JWT_SECRET=secret diff --git a/package-lock.json b/package-lock.json index bedf280..fab256c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.4.0", "license": "MIT", "dependencies": { + "bcryptjs": "^2.4.3", "body-parser": "^1.20.2", "bson": "^6.1.0", "cors": "^2.8.5", @@ -21,6 +22,7 @@ "glob": "^10.3.10", "helmet": "^7.0.0", "http-status": "^1.7.0", + "jsonwebtoken": "^9.0.2", "mongodb": "^4.0.0", "node-dependency-injection": "^2.7.3", "swagger-ui-express": "^5.0.0", @@ -33,6 +35,7 @@ }, "devDependencies": { "@cucumber/cucumber": "^10.0.0", + "@types/bcryptjs": "^2.4.6", "@types/chai": "^4.3.8", "@types/chance": "^1.1.4", "@types/convict": "^6.1.4", @@ -41,6 +44,7 @@ "@types/express": "^4.17.19", "@types/glob": "^8.1.0", "@types/jest": "^29.5.5", + "@types/jsonwebtoken": "^9.0.5", "@types/supertest": "^2.0.14", "@types/swagger-ui-express": "^4.1.5", "@types/uuid-validate": "^0.0.1", @@ -3021,6 +3025,12 @@ "@babel/types": "^7.20.7" } }, + "node_modules/@types/bcryptjs": { + "version": "2.4.6", + "resolved": "https://registry.npmjs.org/@types/bcryptjs/-/bcryptjs-2.4.6.tgz", + "integrity": "sha512-9xlo6R2qDs5uixm0bcIqCeMCE6HiQsIyel9KQySStiyqNl2tnj2mP3DX1Nf56MD6KMenNNlBBsy3LJ7gUEQPXQ==", + "dev": true + }, "node_modules/@types/body-parser": { "version": "1.19.3", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.3.tgz", @@ -3180,6 +3190,15 @@ "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==", "dev": true }, + "node_modules/@types/jsonwebtoken": { + "version": "9.0.5", + "resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-9.0.5.tgz", + "integrity": "sha512-VRLSGzik+Unrup6BsouBeHsf4d1hOEgYWTm/7Nmw1sXoN1+tRly/Gy/po3yeahnP4jfnQWWAhQAqcNfH7ngOkA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/mime": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", @@ -3938,6 +3957,11 @@ "tweetnacl": "^0.14.3" } }, + "node_modules/bcryptjs": { + "version": "2.4.3", + "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", + "integrity": "sha512-V/Hy/X9Vt7f3BbPJEi8BdVFMByHi+jNXrYkW3huaybV/kQ0KJg0Y6PkEMbn+zeT+i+SiKZ/HMqJGIIt4LZDqNQ==" + }, "node_modules/binary-extensions": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", @@ -4120,6 +4144,11 @@ "ieee754": "^1.1.13" } }, + "node_modules/buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==" + }, "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -5470,6 +5499,14 @@ "safer-buffer": "^2.1.0" } }, + "node_modules/ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -8133,6 +8170,27 @@ "node": ">=6" } }, + "node_modules/jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", + "dependencies": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^7.5.4" + }, + "engines": { + "node": ">=12", + "npm": ">=6" + } + }, "node_modules/jsprim": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-2.0.2.tgz", @@ -8171,6 +8229,25 @@ "extsprintf": "^1.2.0" } }, + "node_modules/jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "dependencies": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "dependencies": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "node_modules/keyv": { "version": "4.5.3", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.3.tgz", @@ -8544,6 +8621,36 @@ "resolved": "https://registry.npmjs.org/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz", "integrity": "sha512-uHaJFihxmJcEX3kT4I23ABqKKalJ/zDrDg0lsFtc1h+3uw49SIJ5beyhx5ExVRti3AvKoOJngIj7xz3oylPdWQ==" }, + "node_modules/lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==" + }, + "node_modules/lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==" + }, + "node_modules/lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==" + }, + "node_modules/lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==" + }, + "node_modules/lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==" + }, + "node_modules/lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==" + }, "node_modules/lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", @@ -8562,6 +8669,11 @@ "integrity": "sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==", "dev": true }, + "node_modules/lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==" + }, "node_modules/log-symbols": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", @@ -11345,7 +11457,6 @@ "version": "7.5.4", "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", - "dev": true, "dependencies": { "lru-cache": "^6.0.0" }, @@ -11360,7 +11471,6 @@ "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", - "dev": true, "dependencies": { "yallist": "^4.0.0" }, @@ -11371,8 +11481,7 @@ "node_modules/semver/node_modules/yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", - "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", - "dev": true + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, "node_modules/send": { "version": "0.18.0", @@ -15672,6 +15781,12 @@ "@babel/types": "^7.20.7" } }, + "@types/bcryptjs": { + "version": "2.4.6", + "resolved": "https://registry.npmjs.org/@types/bcryptjs/-/bcryptjs-2.4.6.tgz", + "integrity": "sha512-9xlo6R2qDs5uixm0bcIqCeMCE6HiQsIyel9KQySStiyqNl2tnj2mP3DX1Nf56MD6KMenNNlBBsy3LJ7gUEQPXQ==", + "dev": true + }, "@types/body-parser": { "version": "1.19.3", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.3.tgz", @@ -15831,6 +15946,15 @@ "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==", "dev": true }, + "@types/jsonwebtoken": { + "version": "9.0.5", + "resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-9.0.5.tgz", + "integrity": "sha512-VRLSGzik+Unrup6BsouBeHsf4d1hOEgYWTm/7Nmw1sXoN1+tRly/Gy/po3yeahnP4jfnQWWAhQAqcNfH7ngOkA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/mime": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", @@ -16400,6 +16524,11 @@ "tweetnacl": "^0.14.3" } }, + "bcryptjs": { + "version": "2.4.3", + "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", + "integrity": "sha512-V/Hy/X9Vt7f3BbPJEi8BdVFMByHi+jNXrYkW3huaybV/kQ0KJg0Y6PkEMbn+zeT+i+SiKZ/HMqJGIIt4LZDqNQ==" + }, "binary-extensions": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", @@ -16534,6 +16663,11 @@ "ieee754": "^1.1.13" } }, + "buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==" + }, "buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -17534,6 +17668,14 @@ "safer-buffer": "^2.1.0" } }, + "ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "requires": { + "safe-buffer": "^5.0.1" + } + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -19510,6 +19652,23 @@ "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true }, + "jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", + "requires": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^7.5.4" + } + }, "jsprim": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-2.0.2.tgz", @@ -19541,6 +19700,25 @@ } } }, + "jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "requires": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "requires": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "keyv": { "version": "4.5.3", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.3.tgz", @@ -19801,6 +19979,36 @@ "resolved": "https://registry.npmjs.org/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz", "integrity": "sha512-uHaJFihxmJcEX3kT4I23ABqKKalJ/zDrDg0lsFtc1h+3uw49SIJ5beyhx5ExVRti3AvKoOJngIj7xz3oylPdWQ==" }, + "lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==" + }, + "lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==" + }, + "lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==" + }, + "lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==" + }, + "lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==" + }, + "lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==" + }, "lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", @@ -19819,6 +20027,11 @@ "integrity": "sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==", "dev": true }, + "lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==" + }, "log-symbols": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", @@ -21869,7 +22082,6 @@ "version": "7.5.4", "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", - "dev": true, "requires": { "lru-cache": "^6.0.0" }, @@ -21878,7 +22090,6 @@ "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", - "dev": true, "requires": { "yallist": "^4.0.0" } @@ -21886,8 +22097,7 @@ "yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", - "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", - "dev": true + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" } } }, diff --git a/package.json b/package.json index bd4fc82..09fe696 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "url": "https://github.com/vinjatovix/ts-api/issues" }, "dependencies": { + "bcryptjs": "^2.4.3", "body-parser": "^1.20.2", "bson": "^6.1.0", "cors": "^2.8.5", @@ -73,6 +74,7 @@ "glob": "^10.3.10", "helmet": "^7.0.0", "http-status": "^1.7.0", + "jsonwebtoken": "^9.0.2", "mongodb": "^4.0.0", "node-dependency-injection": "^2.7.3", "swagger-ui-express": "^5.0.0", @@ -85,6 +87,7 @@ }, "devDependencies": { "@cucumber/cucumber": "^10.0.0", + "@types/bcryptjs": "^2.4.6", "@types/chai": "^4.3.8", "@types/chance": "^1.1.4", "@types/convict": "^6.1.4", @@ -93,6 +96,7 @@ "@types/express": "^4.17.19", "@types/glob": "^8.1.0", "@types/jest": "^29.5.5", + "@types/jsonwebtoken": "^9.0.5", "@types/supertest": "^2.0.14", "@types/swagger-ui-express": "^4.1.5", "@types/uuid-validate": "^0.0.1", diff --git a/src/Contexts/shared/plugins/CryptAdapter.ts b/src/Contexts/shared/plugins/CryptAdapter.ts new file mode 100644 index 0000000..9732c79 --- /dev/null +++ b/src/Contexts/shared/plugins/CryptAdapter.ts @@ -0,0 +1,50 @@ +import { compareSync, genSaltSync, hashSync } from 'bcryptjs'; +import { EncrypterTool } from './EncrypterTool'; +import { sign, verify } from 'jsonwebtoken'; +import { envs } from '../../../config/plugins/envs.plugin'; +import { Nullable } from '../domain/Nullable'; + +const JWT_SECRET = envs.JWT_SECRET; +const SALT_ROUNDS = 12; + +export class CryptAdapter implements EncrypterTool { + hash(password: string): string { + const salt = genSaltSync(SALT_ROUNDS); + return hashSync(password, salt); + } + + compare(password: string, hash: string): boolean { + return compareSync(password, hash); + } + + async generateToken( + payload: Record, + duration: string = '2h' + ): Promise> { + return new Promise((resolve) => { + sign(payload, JWT_SECRET, { expiresIn: duration }, (err, token) => + err ? resolve(null) : resolve(token as string) + ); + }); + } + + async verifyToken(token: string): Promise>> { + return new Promise((resolve) => { + verify(token, JWT_SECRET, (err, decoded) => + err ? resolve(null) : resolve(decoded as Record) + ); + }); + } + + async refreshToken(token: string): Promise> { + const decoded = await this.verifyToken(token); + if (!decoded) { + return null; + } + + const now = Math.floor(Date.now() / 1000); + const { exp, ...payload } = decoded as { exp: number; iat: number }; + + return exp > now ? this.generateToken(payload) : null; + } +} diff --git a/src/Contexts/shared/plugins/EncrypterTool.ts b/src/Contexts/shared/plugins/EncrypterTool.ts new file mode 100644 index 0000000..d4abf1b --- /dev/null +++ b/src/Contexts/shared/plugins/EncrypterTool.ts @@ -0,0 +1,9 @@ +import { Nullable } from '../domain/Nullable'; + +export interface EncrypterTool { + hash(value: string): string; + compare(value: string, encryptedValue: string): boolean; + generateToken(payload: Record): Promise>; + verifyToken(token: string): Promise>>; + refreshToken(token: string): Promise>; +} diff --git a/src/config/plugins/envs.plugin.ts b/src/config/plugins/envs.plugin.ts index fc58f08..40afaf0 100644 --- a/src/config/plugins/envs.plugin.ts +++ b/src/config/plugins/envs.plugin.ts @@ -10,5 +10,6 @@ export const envs = { MONGO_URL: env.get('MONGO_URL').required().asString(), MONGO_DB: env.get('MONGO_DB').required().asString(), MONGO_USERNAME: env.get('MONGO_USERNAME').required().asString(), - MONGO_PASSWORD: env.get('MONGO_PASSWORD').required().asString() + MONGO_PASSWORD: env.get('MONGO_PASSWORD').required().asString(), + JWT_SECRET: env.get('JWT_SECRET').required().asString() }; diff --git a/tests/Contexts/shared/plugins/CryptAdapter.test.ts b/tests/Contexts/shared/plugins/CryptAdapter.test.ts new file mode 100644 index 0000000..27e7593 --- /dev/null +++ b/tests/Contexts/shared/plugins/CryptAdapter.test.ts @@ -0,0 +1,87 @@ +import { CryptAdapter } from '../../../../src/Contexts/shared/plugins/CryptAdapter'; +import { random } from '../../fixtures/shared'; + +describe('CryptAdapter', () => { + const cryptAdapter = new CryptAdapter(); + + describe('hash', () => { + it('should return a hash', () => { + const password = random.word(); + + const hash = cryptAdapter.hash(password); + + expect(hash).toBeDefined(); + expect(hash).not.toBe(password); + }); + }); + + describe('compare', () => { + it('should return true if password and hash match', () => { + const password = random.word(); + const hash = cryptAdapter.hash(password); + + expect(cryptAdapter.compare(password, hash)).toBe(true); + }); + + it('should return false if password and hash do not match', () => { + const password = random.word(); + const unMatchedPassword = random.word(); + const hash = cryptAdapter.hash(password); + + expect(cryptAdapter.compare(unMatchedPassword, hash)).toBe(false); + }); + }); + + describe('generateToken', () => { + it('should return a token with default duration', async () => { + const payload = { id: random.uuid() }; + const token = await cryptAdapter.generateToken(payload); + + expect(token).toBeDefined(); + expect(typeof token).toBe('string'); + }); + + it('should return a token with custom duration', async () => { + const payload = { id: random.uuid() }; + const duration = '1h'; + + const token = await cryptAdapter.generateToken(payload, duration); + + expect(token).toBeDefined(); + }); + }); + + describe('verifyToken', () => { + it('should return a payload', async () => { + const seed = { id: random.uuid() }; + const token = await cryptAdapter.generateToken(seed); + + const payload = await cryptAdapter.verifyToken(token as string); + + expect(payload).toMatchObject(seed); + }); + }); + + describe('refreshToken', () => { + it('should return a new token', async () => { + const seed = { id: random.uuid() }; + const duration = '12h'; + const token = await cryptAdapter.generateToken(seed, duration); + + const newToken = await cryptAdapter.refreshToken(token as string); + + expect(newToken).toBeDefined(); + expect(newToken).not.toBe(token); + }); + + it('should return null if token has expired', async () => { + const seed = { id: random.uuid() }; + const duration = '0s'; + const token = await cryptAdapter.generateToken(seed, duration); + + const newToken = await cryptAdapter.refreshToken(token as string); + + expect(newToken).toBeNull(); + }); + }); +});