From 39b3d6b45d4810df3ca8715d3ce7dad5062a8bcd Mon Sep 17 00:00:00 2001 From: Manuel Emilio Urena Date: Mon, 22 Apr 2019 16:33:19 -0400 Subject: [PATCH 1/3] 1.0.0 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 69b0288..621f52b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "amphora-auth", - "version": "0.0.1-beta.4", + "version": "1.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index dc37a6d..a91fc46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "amphora-auth", - "version": "0.0.1-beta.4", + "version": "1.0.0", "description": "An auth adapter for Amphora", "main": "index.js", "scripts": { From 11282f5d21ff09b5f3ae8a1529b49c8b867609e6 Mon Sep 17 00:00:00 2001 From: Jordan Paulino Date: Thu, 11 Jul 2019 13:41:37 -0400 Subject: [PATCH 2/3] Trigger elastic when first login happens --- controllers/users.js | 2 +- index.js | 2 +- services/bus.js | 15 +++++++++++++++ utils.js | 7 ++++++- utils.test.js | 6 +++++- 5 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 services/bus.js diff --git a/controllers/users.js b/controllers/users.js index d07d744..2d16025 100644 --- a/controllers/users.js +++ b/controllers/users.js @@ -3,7 +3,7 @@ const { encode } = require('../utils'), encrypt = require('../services/encrypt'); let db = require('../services/storage'), - bus; + bus = require('../services/bus'); /** * Adds an user into the db and publishes the action to the bus diff --git a/index.js b/index.js index e55fc03..ca5e338 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ const _isEmpty = require('lodash/isEmpty'), { AUTH_LEVELS } = require('./constants'), { withAuthLevel } = require('./services/auth'), { setDb } = require('./services/storage'), - { setBus } = require('./controllers/users'); + { setBus } = require('./services/bus'); /** * determine if a route is protected diff --git a/services/bus.js b/services/bus.js new file mode 100644 index 0000000..48bff4a --- /dev/null +++ b/services/bus.js @@ -0,0 +1,15 @@ +'use strict'; + +let bus; // Redis bus passed from Amphora. Assigned value at initialization + +/** + * Retrieves an item from the database. + * @param {string} eventName + * @param {Object} data + */ +function publish(eventName, data) { + bus.publish(eventName, data); +} + +module.exports.publish = publish; +module.exports.setBus = redisBus => bus = redisBus; diff --git a/utils.js b/utils.js index 2f0b669..e040561 100644 --- a/utils.js +++ b/utils.js @@ -13,7 +13,8 @@ const _get = require('lodash/get'), references = require('./services/references'), { isValidPassword } = require('./services/encrypt'); -let db = require('./services/storage'); +let db = require('./services/storage'), + bus = require('./services/bus'); /** * encode username and provider to base64 @@ -97,6 +98,9 @@ function verify(properties) { return done(null, false, { message: 'Invalid Password' }); } + data._ref = uid; + + bus.publish('saveUser', { key: uid, value: data }); // Tell elastic about any update changes to maintain data consistency return done(null, data); }) .catch(e => done(e)); @@ -254,5 +258,6 @@ module.exports.getUri = getUri; // For testing purposes module.exports.setDb = mock => db = mock; +module.exports.setBus = mock => bus = mock; module.exports.removeQueryString = removeQueryString; module.exports.removeExtension = removeExtension; diff --git a/utils.test.js b/utils.test.js index 1b1f412..971913c 100644 --- a/utils.test.js +++ b/utils.test.js @@ -8,12 +8,16 @@ const _startCase = require('lodash/startCase'), storage = require('./test/fixtures/mocks/storage'); describe(_startCase(filename), function () { - let fakeDb; + let fakeDb, fakeBus; beforeEach(function () { fakeDb = storage(); + fakeBus = { + publish: jest.fn() + }; lib.setDb(fakeDb); + lib.setBus(fakeBus); }); describe('compileTemplate', function () { From 87c855dade649c011b53bcb80c03c12425dd1a8b Mon Sep 17 00:00:00 2001 From: Manuel Emilio Urena Date: Fri, 2 Aug 2019 16:57:33 -0400 Subject: [PATCH 3/3] Removes password before publishing to elastic --- services/bus.js | 2 +- services/bus.test.js | 22 ++++++++++++++++++++++ utils.js | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 services/bus.test.js diff --git a/services/bus.js b/services/bus.js index 48bff4a..bb183f7 100644 --- a/services/bus.js +++ b/services/bus.js @@ -3,7 +3,7 @@ let bus; // Redis bus passed from Amphora. Assigned value at initialization /** - * Retrieves an item from the database. + * Publishes an event to the bus. * @param {string} eventName * @param {Object} data */ diff --git a/services/bus.test.js b/services/bus.test.js new file mode 100644 index 0000000..5ccc489 --- /dev/null +++ b/services/bus.test.js @@ -0,0 +1,22 @@ +'use strict'; + +const _startCase = require('lodash/startCase'), + filename = __filename.split('/').pop().split('.').shift(), + lib = require(`./${filename}`); + +describe(_startCase(filename), function () { + describe('publish', function () { + const fakeBus = { publish: jest.fn() }, + fn = lib[this.description]; + + lib.setBus(fakeBus); + + it('calls publish method from the bus service', () => { + fakeBus.publish.mockResolvedValue(); + + fn('save', {}); + + expect(fakeBus.publish).toBeCalledWith('save', {}); + }); + }); +}); diff --git a/utils.js b/utils.js index e040561..1dbf9c8 100644 --- a/utils.js +++ b/utils.js @@ -99,6 +99,7 @@ function verify(properties) { } data._ref = uid; + delete data.password; bus.publish('saveUser', { key: uid, value: data }); // Tell elastic about any update changes to maintain data consistency return done(null, data);