From 28ed252041e56afcebbde6d550a578a7d3cf1ab5 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sun, 28 Aug 2022 14:49:29 -0700 Subject: [PATCH] Closes #353 - Replaced bcrypt by bcryptjs --- package.json | 2 +- src/models/admin.js | 6 +++--- src/models/component.js | 8 ++++---- tests/client-api.test.js | 6 +++--- tests/component.test.js | 4 ++-- tests/fixtures/db_api.js | 6 +++--- tests/fixtures/db_client.js | 6 +++--- tests/fixtures/db_client_payload.js | 6 +++--- tests/fixtures/db_factory.js | 6 +++--- tests/fixtures/db_metrics.js | 6 +++--- 10 files changed, 28 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 239d46d..995a59f 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "license": "MIT", "dependencies": { "axios": "^0.27.2", - "bcrypt": "^5.0.1", + "bcryptjs": "^2.4.3", "cors": "^2.8.5", "express": "^4.18.1", "express-basic-auth": "^1.2.1", diff --git a/src/models/admin.js b/src/models/admin.js index a4dcfcd..730714b 100644 --- a/src/models/admin.js +++ b/src/models/admin.js @@ -1,6 +1,6 @@ import mongoose from 'mongoose'; import moment from 'moment'; -import bcrypt from 'bcrypt'; +import bcryptjs from 'bcryptjs'; import crypto from 'crypto'; import jwt from 'jsonwebtoken'; import Domain from './domain'; @@ -142,7 +142,7 @@ adminSchema.statics.findByCredentials = async (email, password) => { throw new Error('Unable to login'); } - const isMatch = await bcrypt.compare(password, admin.password); + const isMatch = await bcryptjs.compare(password, admin.password); if (!isMatch) { throw new Error('Unable to login'); @@ -196,7 +196,7 @@ adminSchema.pre('save', async function (next) { const admin = this; if (admin.isModified('password')) { - admin.password = await bcrypt.hash(admin.password, 8); + admin.password = await bcryptjs.hash(admin.password, 8); notifyAcCreation(admin._id); } diff --git a/src/models/component.js b/src/models/component.js index 0f4e021..6558a81 100644 --- a/src/models/component.js +++ b/src/models/component.js @@ -1,6 +1,6 @@ import mongoose from 'mongoose'; import moment from 'moment'; -import bcrypt from 'bcrypt'; +import bcryptjs from 'bcryptjs'; import jwt from 'jsonwebtoken'; import { Config } from './config'; import Domain from './domain'; @@ -53,8 +53,8 @@ componentSchema.options.toJSON = { componentSchema.methods.generateApiKey = async function () { const component = this; - const apiKey = await bcrypt.hash(component._id + component.name, 8); - const hash = await bcrypt.hash(apiKey, 8); + const apiKey = await bcryptjs.hash(component._id + component.name, 8); + const hash = await bcryptjs.hash(apiKey, 8); component.apihash = hash; await component.save(); @@ -84,7 +84,7 @@ componentSchema.statics.findByCredentials = async (domainName, componentName, ap } let decoded = Buffer.from(apiKey, 'base64').toString('ascii'); - const isMatch = await bcrypt.compare(decoded, component.apihash); + const isMatch = await bcryptjs.compare(decoded, component.apihash); if (!isMatch) { throw new Error('Unable to find this Component'); diff --git a/tests/client-api.test.js b/tests/client-api.test.js index 14a6ec7..31e947c 100644 --- a/tests/client-api.test.js +++ b/tests/client-api.test.js @@ -1,6 +1,6 @@ import mongoose from 'mongoose'; import request from 'supertest'; -import bcrypt from 'bcrypt'; +import bcryptjs from 'bcryptjs'; import app from '../src/app'; import Domain from '../src/models/domain'; import GroupConfig from '../src/models/group-config'; @@ -557,8 +557,8 @@ describe('Testing criteria [REST] ', () => { owner: adminMasterAccountId }; - const hashApiKey = await bcrypt.hash(component._id + component.name, 8); - const hash = await bcrypt.hash(hashApiKey, 8); + const hashApiKey = await bcryptjs.hash(component._id + component.name, 8); + const hash = await bcryptjs.hash(hashApiKey, 8); component.apihash = hash; await new Component(component).save(); const generatedApiKey = Buffer.from(hashApiKey).toString('base64'); diff --git a/tests/component.test.js b/tests/component.test.js index a169ace..c9805a8 100644 --- a/tests/component.test.js +++ b/tests/component.test.js @@ -1,6 +1,6 @@ import mongoose from 'mongoose'; import request from 'supertest'; -import bcrypt from 'bcrypt'; +import bcryptjs from 'bcryptjs'; import app from '../src/app'; import Component from '../src/models/component'; import { @@ -97,7 +97,7 @@ describe('Insertion tests', () => { // DB validation - current Domain token should not be as the same as the generated const apiKey = Buffer.from(response.body.apiKey, 'base64').toString('ascii'); const component = await Component.findById(component1Id).lean(); - const isMatch = await bcrypt.compare(apiKey, component.apihash); + const isMatch = await bcryptjs.compare(apiKey, component.apihash); expect(isMatch).toEqual(true); }); diff --git a/tests/fixtures/db_api.js b/tests/fixtures/db_api.js index 621a0a9..fa396a8 100644 --- a/tests/fixtures/db_api.js +++ b/tests/fixtures/db_api.js @@ -1,5 +1,5 @@ import mongoose from 'mongoose'; -import bcrypt from 'bcrypt'; +import bcryptjs from 'bcryptjs'; import jwt from 'jsonwebtoken'; import Admin from '../../src/models/admin'; import Domain from '../../src/models/domain'; @@ -245,8 +245,8 @@ export const setupDatabase = async () => { await new Permission(permissionAll3).save(); await new Permission(permissionAll4).save(); - const hashApiKey = await bcrypt.hash(component1._id + component1.name, 8); - const hash = await bcrypt.hash(hashApiKey, 8); + const hashApiKey = await bcryptjs.hash(component1._id + component1.name, 8); + const hash = await bcryptjs.hash(hashApiKey, 8); component1.apihash = hash; await new Component(component1).save(); }; \ No newline at end of file diff --git a/tests/fixtures/db_client.js b/tests/fixtures/db_client.js index a5afeee..d049d20 100644 --- a/tests/fixtures/db_client.js +++ b/tests/fixtures/db_client.js @@ -1,6 +1,6 @@ import mongoose from 'mongoose'; import jwt from 'jsonwebtoken'; -import bcrypt from 'bcrypt'; +import bcryptjs from 'bcryptjs'; import Admin from '../../src/models/admin'; import Domain from '../../src/models/domain'; import GroupConfig from '../../src/models/group-config'; @@ -226,8 +226,8 @@ export const setupDatabase = async () => { await new ConfigStrategy(configStrategyTIME_BETWEENDocument).save(); await new ConfigStrategy(configStrategyTIME_GREATDocument).save(); - const hashApiKey = await bcrypt.hash(component1._id + component1.name, 8); - const hash = await bcrypt.hash(hashApiKey, 8); + const hashApiKey = await bcryptjs.hash(component1._id + component1.name, 8); + const hash = await bcryptjs.hash(hashApiKey, 8); component1.apihash = hash; await new Component(component1).save(); apiKey = Buffer.from(hashApiKey).toString('base64'); diff --git a/tests/fixtures/db_client_payload.js b/tests/fixtures/db_client_payload.js index 1575860..970aeb5 100644 --- a/tests/fixtures/db_client_payload.js +++ b/tests/fixtures/db_client_payload.js @@ -1,6 +1,6 @@ import mongoose from 'mongoose'; import jwt from 'jsonwebtoken'; -import bcrypt from 'bcrypt'; +import bcryptjs from 'bcryptjs'; import Admin from '../../src/models/admin'; import Domain from '../../src/models/domain'; import GroupConfig from '../../src/models/group-config'; @@ -115,8 +115,8 @@ export const setupDatabase = async () => { await new Config(configPayloadDocument).save(); await new ConfigStrategy(configStrategyPAYLOAD_HAS_ONEDocument).save(); - const hashApiKey = await bcrypt.hash(component1._id + component1.name, 8); - const hash = await bcrypt.hash(hashApiKey, 8); + const hashApiKey = await bcryptjs.hash(component1._id + component1.name, 8); + const hash = await bcryptjs.hash(hashApiKey, 8); component1.apihash = hash; await new Component(component1).save(); apiKey = Buffer.from(hashApiKey).toString('base64'); diff --git a/tests/fixtures/db_factory.js b/tests/fixtures/db_factory.js index 3689602..d3fb029 100644 --- a/tests/fixtures/db_factory.js +++ b/tests/fixtures/db_factory.js @@ -1,5 +1,5 @@ import mongoose from 'mongoose'; -import bcrypt from 'bcrypt'; +import bcryptjs from 'bcryptjs'; import Component from '../../src/models/component'; import Domain from '../../src/models/domain'; import { EnvType } from '../../src/models/environment'; @@ -26,8 +26,8 @@ export async function createDummyComponent(componentName, domainId, accountId) { owner: accountId }; - const apiKey = await bcrypt.hash(componentDocument._id + componentDocument.name, 8); - const hash = await bcrypt.hash(apiKey, 8); + const apiKey = await bcryptjs.hash(componentDocument._id + componentDocument.name, 8); + const hash = await bcryptjs.hash(apiKey, 8); componentDocument.apihash = hash; await new Component(componentDocument).save(); diff --git a/tests/fixtures/db_metrics.js b/tests/fixtures/db_metrics.js index d2cbeee..aba68ed 100644 --- a/tests/fixtures/db_metrics.js +++ b/tests/fixtures/db_metrics.js @@ -1,6 +1,6 @@ import mongoose from 'mongoose'; import jwt from 'jsonwebtoken'; -import bcrypt from 'bcrypt'; +import bcryptjs from 'bcryptjs'; import { Metric } from '../../src/models/metric'; import Admin from '../../src/models/admin'; import { EnvType } from '../../src/models/environment'; @@ -169,8 +169,8 @@ export const setupDatabase = async () => { await new Metric(entry3).save(); await new Metric(entry4).save(); - const apiKey = await bcrypt.hash(component1._id + component1.name, 8); - const hash = await bcrypt.hash(apiKey, 8); + const apiKey = await bcryptjs.hash(component1._id + component1.name, 8); + const hash = await bcryptjs.hash(apiKey, 8); component1.apihash = hash; await new Component(component1).save(); }; \ No newline at end of file