|
1 | 1 | import assert from 'node:assert/strict' |
| 2 | +import crypto from 'node:crypto' |
2 | 3 | import { describe, it, after, before } from 'node:test' |
3 | 4 |
|
4 | 5 | import User from './index.js' |
5 | 6 | import Group from '../group/index.js' |
| 7 | +import Mysql from '../mysql.js' |
6 | 8 |
|
7 | 9 | import userCase from '../test/user.json' with { type: 'json' } |
8 | 10 | import groupCase from '../test/group.json' with { type: 'json' } |
@@ -144,4 +146,117 @@ describe('user', function () { |
144 | 146 | assert.ok(u) |
145 | 147 | }) |
146 | 148 | }) |
| 149 | + |
| 150 | + describe('password upgrade on login', () => { |
| 151 | + const upgradeUserId = 4200 |
| 152 | + const upgradeUser = { |
| 153 | + nt_user_id: upgradeUserId, |
| 154 | + nt_group_id: groupCase.id, |
| 155 | + username: 'upgrade-test', |
| 156 | + email: 'upgrade-test@example.com', |
| 157 | + first_name: 'Upgrade', |
| 158 | + last_name: 'Test', |
| 159 | + } |
| 160 | + const testPass = 'UpgradeMe!123' |
| 161 | + const authCreds = { |
| 162 | + username: `${upgradeUser.username}@${groupCase.name}`, |
| 163 | + password: testPass, |
| 164 | + } |
| 165 | + |
| 166 | + async function getDbPassword() { |
| 167 | + const rows = await Mysql.execute( |
| 168 | + 'SELECT password, pass_salt FROM nt_user WHERE nt_user_id = ?', |
| 169 | + [upgradeUserId], |
| 170 | + ) |
| 171 | + return rows[0] |
| 172 | + } |
| 173 | + |
| 174 | + async function insertUser(password, passSalt) { |
| 175 | + await Mysql.execute( |
| 176 | + 'INSERT INTO nt_user (nt_user_id, nt_group_id, username, email, first_name, last_name, password, pass_salt) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', |
| 177 | + [upgradeUserId, upgradeUser.nt_group_id, upgradeUser.username, upgradeUser.email, upgradeUser.first_name, upgradeUser.last_name, password, passSalt], |
| 178 | + ) |
| 179 | + } |
| 180 | + |
| 181 | + async function cleanup() { |
| 182 | + await Mysql.execute( |
| 183 | + 'DELETE FROM nt_user WHERE nt_user_id = ?', |
| 184 | + [upgradeUserId], |
| 185 | + ) |
| 186 | + } |
| 187 | + |
| 188 | + before(cleanup) |
| 189 | + after(cleanup) |
| 190 | + |
| 191 | + it('upgrades plain text password to PBKDF2 on login', async () => { |
| 192 | + await cleanup() |
| 193 | + await insertUser(testPass, null) |
| 194 | + |
| 195 | + const result = await User.authenticate(authCreds) |
| 196 | + assert.ok(result, 'login should succeed') |
| 197 | + |
| 198 | + const row = await getDbPassword() |
| 199 | + assert.ok(row.pass_salt, 'pass_salt should be set after upgrade') |
| 200 | + assert.notEqual(row.password, testPass, 'password should be hashed') |
| 201 | + |
| 202 | + // verify round-trip: can still log in with the upgraded hash |
| 203 | + const again = await User.authenticate(authCreds) |
| 204 | + assert.ok(again, 'login should succeed after upgrade') |
| 205 | + await cleanup() |
| 206 | + }) |
| 207 | + |
| 208 | + it('upgrades SHA1 password to PBKDF2 on login', async () => { |
| 209 | + // authenticate() passes the full authTry.username (including @group) to |
| 210 | + // validPassword(), so the HMAC key must match that full string |
| 211 | + const sha1Hash = crypto |
| 212 | + .createHmac('sha1', authCreds.username.toLowerCase()) |
| 213 | + .update(testPass) |
| 214 | + .digest('hex') |
| 215 | + await cleanup() |
| 216 | + await insertUser(sha1Hash, null) |
| 217 | + |
| 218 | + const result = await User.authenticate(authCreds) |
| 219 | + assert.ok(result, 'login should succeed with SHA1 hash') |
| 220 | + |
| 221 | + const row = await getDbPassword() |
| 222 | + assert.ok(row.pass_salt, 'pass_salt should be set after upgrade') |
| 223 | + assert.notEqual(row.password, sha1Hash, 'password should be re-hashed') |
| 224 | + |
| 225 | + const again = await User.authenticate(authCreds) |
| 226 | + assert.ok(again, 'login should succeed after upgrade') |
| 227 | + await cleanup() |
| 228 | + }) |
| 229 | + |
| 230 | + it('upgrades PBKDF2-5000 to current iterations on login', async () => { |
| 231 | + const legacySalt = User.generateSalt() |
| 232 | + const legacyHash = await User.hashAuthPbkdf2(testPass, legacySalt, 5000) |
| 233 | + await cleanup() |
| 234 | + await insertUser(legacyHash, legacySalt) |
| 235 | + |
| 236 | + const result = await User.authenticate(authCreds) |
| 237 | + assert.ok(result, 'login should succeed with legacy PBKDF2') |
| 238 | + |
| 239 | + const row = await getDbPassword() |
| 240 | + assert.notEqual(row.password, legacyHash, 'password should be re-hashed') |
| 241 | + assert.notEqual(row.pass_salt, legacySalt, 'salt should be regenerated') |
| 242 | + |
| 243 | + const again = await User.authenticate(authCreds) |
| 244 | + assert.ok(again, 'login should succeed after upgrade') |
| 245 | + await cleanup() |
| 246 | + }) |
| 247 | + |
| 248 | + it('does not re-hash password already at current iterations', async () => { |
| 249 | + const salt = User.generateSalt() |
| 250 | + const hash = await User.hashAuthPbkdf2(testPass, salt) |
| 251 | + await cleanup() |
| 252 | + await insertUser(hash, salt) |
| 253 | + |
| 254 | + await User.authenticate(authCreds) |
| 255 | + |
| 256 | + const row = await getDbPassword() |
| 257 | + assert.equal(row.password, hash, 'password should be unchanged') |
| 258 | + assert.equal(row.pass_salt, salt, 'salt should be unchanged') |
| 259 | + await cleanup() |
| 260 | + }) |
| 261 | + }) |
147 | 262 | }) |
0 commit comments