Skip to content

fix: update old password hash (deprecated token strategy) #1191

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 9 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions src/model/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

import {Schema} from 'mongoose'
import * as crypto from 'crypto'

import {PassportModelAPI, createPassport, updatePassport} from './passport'
import {connectionAPI, connectionDefault} from '../config'
Expand Down Expand Up @@ -104,8 +105,23 @@ export const updateUser = async function (newUserData) {
try {
let password
if (userToBeUpdated.password) {
// Compute passport new hash for local strategy
password = await hashPassword(userToBeUpdated.password)

/* --- @deprecated : Update password hash for token strategy --- */
const passport = await PassportModelAPI.findOne({
protocol: 'token',
email: userToBeUpdated.email
})
if (passport) {
let shasum = crypto.createHash(passport.passwordAlgorithm || 'sha512')
shasum.update(passport.passwordSalt + userToBeUpdated.password)
const passhash = shasum.digest('hex')
passport.passwordHash = passhash
await PassportModelAPI.findByIdAndUpdate(passport.id, passport)
}
/* --- ----------- --- */

delete userToBeUpdated.password
}

Expand Down
104 changes: 104 additions & 0 deletions test/unit/usersTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,5 +364,109 @@ describe('UserModel tests', () => {
should.equal(user, null)
error.should.have.property('message')
})

it('should update both passwords if user have two passports token and local', async () => {
const userToBeUpdated = {
id: userId,
firstname: 'Elena',
surname: 'Smith',
email: '[email protected]',
password: 'new_password',
groups: ['group1']
}

const {error, user} = await model.updateUser(userToBeUpdated)

should.equal(error, null)

user.should.have.property('id')
user.should.have.property('firstname', userToBeUpdated.firstname)
user.should.have.property('surname', userToBeUpdated.surname)
user.should.have.property('email', userToBeUpdated.email)
user.should.not.have.property('password')

const localResult = await model.PassportModelAPI.find({
email: user.email,
protocol: 'local'
})
.limit(1)
.sort({$natural: -1})

localResult.length.should.equal(1)
const localPassportResult = localResult[0]._doc
localPassportResult.should.have.property('password')

const res = await model.PassportModelAPI.find({
email: user.email,
protocol: 'token'
})
.limit(1)
.sort({$natural: -1})

res.length.should.equal(1)
const tokenPassportResult = res[0]._doc
tokenPassportResult.should.not.have.property('password')
tokenPassportResult.should.have.property('passwordAlgorithm')
tokenPassportResult.should.have.property('passwordHash')
tokenPassportResult.should.have.property('passwordSalt')
tokenPassportResult.passwordHash.should.not.equal(
oldPassport.passwordHash
)
})

it('should update both passwords even if user do not have a passwordAlgorithm field', async () => {
const localUserToBeUpdated = {
id: userId,
firstname: 'Elena',
surname: 'Smith',
email: '[email protected]',
password: 'new_password',
groups: ['group1']
}

const passwordRes = await model.PassportModelAPI.find({
email: localUserToBeUpdated.email,
protocol: 'token'
})
.limit(1)
.sort({$natural: -1})

passwordRes.length.should.equal(1)
await model.PassportModelAPI.findByIdAndUpdate(passwordRes[0]._id, {
passwordAlgorithm: null
})

const {error, user} = await model.updateUser(localUserToBeUpdated)

should.equal(error, null)

const localResult = await model.PassportModelAPI.find({
email: user.email,
protocol: 'local'
})
.limit(1)
.sort({$natural: -1})

localResult.length.should.equal(1)
const localPassportResult = localResult[0]._doc
localPassportResult.should.have.property('password')

const res = await model.PassportModelAPI.find({
email: user.email,
protocol: 'token'
})
.limit(1)
.sort({$natural: -1})

res.length.should.equal(1)
const tokenPassportResult = res[0]._doc
tokenPassportResult.should.not.have.property('password')
tokenPassportResult.should.have.property('passwordAlgorithm')
tokenPassportResult.should.have.property('passwordHash')
tokenPassportResult.should.have.property('passwordSalt')
tokenPassportResult.passwordHash.should.not.equal(
oldPassport.passwordHash
)
})
})
})