Skip to content

Plat 719 fix user metadata importation #1189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "openhim-core",
"description": "The OpenHIM core application that provides logging and routing of http requests",
"version": "7.2.0",
"version": "7.2.1",
"main": "./lib/server.js",
"bin": {
"openhim-core": "./bin/openhim-core.js"
Expand Down
28 changes: 14 additions & 14 deletions src/api/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {ContactGroupModelAPI} from '../model/contactGroups'
import {KeystoreModelAPI} from '../model/keystore'
import {MediatorModelAPI} from '../model/mediators'
import {UserModelAPI} from '../model/users'
import {PassportModelAPI} from '../model/passport'
import * as polling from '../polling'

// Map string parameters to collections
Expand All @@ -18,6 +19,7 @@ const collections = {
Clients: ClientModelAPI,
Mediators: MediatorModelAPI,
Users: UserModelAPI,
Passports: PassportModelAPI,
ContactGroups: ContactGroupModelAPI,
KeystoreModelAPI
}
Expand All @@ -39,37 +41,35 @@ function removeProperties(obj) {

// Function to return unique identifier key and value for a collection
function getUniqueIdentifierForCollection(collection, doc) {
let uid
let uidKey
const returnObj = {}

switch (collection) {
case 'Channels':
uidKey = 'name'
uid = doc.name
returnObj['name'] = doc.name
break
case 'Clients':
uidKey = 'clientID'
uid = doc.clientID
returnObj['clientID'] = doc.clientID
break
case 'Mediators':
uidKey = 'urn'
uid = doc.urn
returnObj['urn'] = doc.urn
break
case 'Users':
uidKey = 'email'
uid = doc.email
returnObj['email'] = doc.email
break
case 'Passports':
returnObj['protocol'] = doc.protocol
returnObj['email'] = doc.email
break
case 'ContactGroups':
uidKey = 'groups'
uid = doc.groups
returnObj['groups'] = doc.groups
break
default:
logger.debug(
`Unhandeled case for ${collection} in getUniqueIdentifierForCollection`
)
break
}
const returnObj = {}
returnObj[uidKey] = uid

return returnObj
}

Expand Down
2 changes: 1 addition & 1 deletion src/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export async function authenticateToken(ctx, email) {
} else {
const passport = await PassportModelAPI.findOne({
protocol: 'token',
user: user.id
email: user.email
})

if (!passport) {
Expand Down
9 changes: 6 additions & 3 deletions src/model/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ const PassportSchema = new Schema({

// Associations
//
// Associate every passport with one, and only one, user.
user: {type: Schema.Types.ObjectId, ref: 'User'}
// Associate every passport with a user.
email: {
type: String,
required: true
}
})

// compile the Passport Schema into a Model
Expand All @@ -90,7 +93,7 @@ export const createPassport = async function (user, passwordInfo) {
let result = {error: null, user: null}
return await PassportModelAPI.create({
protocol: passwordInfo.password ? 'local' : 'token',
user: user.id,
email: user.email,
...passwordInfo
})
.then(async function () {
Expand Down
4 changes: 2 additions & 2 deletions src/model/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const updateUser = async function (newUserData) {
if (password) {
await PassportModelAPI.findOne({
protocol: 'local',
user: user.id
email: user.email
}).then(async function (passport) {
if (passport) {
passport.password = password
Expand Down Expand Up @@ -174,7 +174,7 @@ export const updateTokenUser = async function (newUserData) {
if (passwordHash && passwordAlgorithm && passwordSalt) {
await PassportModelAPI.findOne({
protocol: 'token',
user: user.id
email: user.email
}).then(async function (passport) {
if (passport) {
passport.passwordHash = passwordHash
Expand Down
2 changes: 1 addition & 1 deletion src/protocols/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const login = async function (email, password, next) {
}
return PassportModelAPI.findOne({
protocol: user.provider === 'token' ? 'token' : 'local',
user: user.id
email: user.email
})
.then(function (passport) {
if (passport) {
Expand Down
8 changes: 3 additions & 5 deletions src/protocols/openid.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export const login = async (
tokens,
issuer,
identifier: identifier,
protocol: 'openid'
protocol: 'openid',
email: user.email
}

PassportModelAPI.findOne({
Expand All @@ -111,10 +112,7 @@ export const login = async (
return createOrUpdateUser(user)
.then(function (retrievedUser) {
return PassportModelAPI.create(
{
user: retrievedUser.id,
...newPassport
},
newPassport,
function () {
next(null, retrievedUser)
}
Expand Down
2 changes: 1 addition & 1 deletion src/protocols/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const login = async function (req, next) {

const passport = await PassportModelAPI.findOne({
protocol: 'token',
user: user.id
email: user.email
})

if (passport == null) {
Expand Down
2 changes: 1 addition & 1 deletion src/upgradeDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ upgradeFuncs.push({
passwordHash: user.passwordHash,
passwordAlgorithm: user.passwordAlgorithm,
passwordSalt: user.passwordSalt,
user: user.id,
email: user.email,
protocol: 'token'
}

Expand Down
9 changes: 5 additions & 4 deletions test/integration/usersAPITests.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ describe('API Integration Tests', () => {
.send(updates)
.expect(200)

const user = await UserModelAPI.findOne({email: '[email protected]'})
const email = '[email protected]'
const user = await UserModelAPI.findOne({email})

user.should.have.property('firstname', 'Jane Sally')
user.should.have.property('surname', 'Doe')
Expand All @@ -281,7 +282,7 @@ describe('API Integration Tests', () => {
user.should.have.property('locked', false)
user.should.have.property('expiry', null)

const passport = await PassportModelAPI.findOne({user: user.id})
const passport = await PassportModelAPI.findOne({email})

passport.should.have.property('password')
})
Expand Down Expand Up @@ -920,7 +921,7 @@ describe('API Integration Tests', () => {
user.should.have.property('expiry', null)

const passport = await PassportModelAPI.findOne({
user: user._id,
email: user.email,
protocol: 'token'
})

Expand Down Expand Up @@ -1144,7 +1145,7 @@ describe('API Integration Tests', () => {
user.groups.should.have.length(3)

const passport = await PassportModelAPI.findOne({
user: user.id,
email: user.email,
protocol: 'token'
})

Expand Down
33 changes: 11 additions & 22 deletions test/unit/passportTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import should from 'should'
import * as model from '../../src/model'

describe('PassportModel tests', () => {
let userId
const email = '[email protected]'

const user = new model.UserModelAPI({
firstname: 'Bill',
surname: 'Murray',
email: '[email protected]',
email,
groups: ['HISP', 'group2']
})

before(async () => {
const res = await user.save()
userId = res.id
await user.save()
})

after(async () => {
Expand All @@ -28,45 +27,35 @@ describe('PassportModel tests', () => {

describe('.createPassport()', () => {
it('should create a new password', async () => {
const {error, user} = await model.createPassport({id: userId}, 'password')
const {error, user} = await model.createPassport({email}, 'password')

should.equal(error, null)
user.should.have.property('id', userId)
user.should.have.property('email', email)

const passportResult = await model.PassportModelAPI.findOne({
user: userId
email
})

passportResult.should.have.property('password')
})

it('should return error for non existent user', async () => {
const {error, user} = await model.createPassport(
{id: 'non_existent_id'},
'password'
)

should.equal(user, null)
error.should.have.property('message')
})
})

describe('.updatePassport()', () => {
it('should update passport of a user', async () => {
const passportResult = await model.PassportModelAPI.findOne({
user: userId
email
})

const {error, user} = await model.updatePassport(
{id: userId},
{email},
{id: passportResult.id, password: 'new_password'}
)

should.equal(error, null)
user.should.have.property('id', userId)
user.should.have.property('email', email)

const newPassportResult = await model.PassportModelAPI.findOne({
user: userId
email
})

newPassportResult.should.have.property('password')
Expand All @@ -75,7 +64,7 @@ describe('PassportModel tests', () => {

it('should return error for non existent passport', async () => {
const {error, user} = await model.updatePassport(
{id: userId},
{email},
{id: 'non_existent_id'}
)

Expand Down
8 changes: 2 additions & 6 deletions test/unit/upgradeDBTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,6 @@ describe('Upgrade DB Tests', () => {

describe(`updateFunction3 - Migrate password properties of token auth strategy from User collection to Passport collection`, () => {
const upgradeFunc = originalUpgradeFuncs[3].func
let userId1 = '',
userId2 = ''

const userObj1 = {
// password: "password"
Expand Down Expand Up @@ -471,9 +469,7 @@ describe('Upgrade DB Tests', () => {

beforeEach(async () => {
const res1 = await new UserModel(userObj1).save()
userId1 = res1._id
const res2 = await new UserModel(userObj2).save()
userId2 = res2._id
})

it('should migrate password properties of token auth strategy from User to Passport collection', async () => {
Expand All @@ -492,12 +488,12 @@ describe('Upgrade DB Tests', () => {
idx1.should.be.above(-1)
passports[idx1].passwordHash.should.be.equal(userObj1.passwordHash)
passports[idx1].passwordSalt.should.be.equal(userObj1.passwordSalt)
passports[idx1].user.should.be.deepEqual(userId1)
passports[idx1].email.should.be.equal(userObj1.email)
passports[idx1].protocol.should.be.equal('token')
idx2.should.be.above(-1)
passports[idx2].passwordHash.should.be.equal(userObj2.passwordHash)
passports[idx2].passwordSalt.should.be.equal(userObj2.passwordSalt)
passports[idx2].user.should.be.deepEqual(userId2)
passports[idx2].email.should.be.equal(userObj2.email)
passports[idx2].protocol.should.be.equal('token')
})

Expand Down
Loading