|
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' } |
@@ -80,23 +82,31 @@ describe('user', function () { |
80 | 82 | assert.equal(r, true) |
81 | 83 | }) |
82 | 84 |
|
83 | | - it('auths valid pbkdb2 password', async () => { |
84 | | - const r = await User.validPassword( |
85 | | - 'YouGuessedIt!', |
86 | | - '050cfa70c3582be0d5bfae25138a8486dc2e6790f39bc0c4e111223ba6034432', |
87 | | - 'unit-test', |
88 | | - '(ICzAm2.QfCa6.MN', |
89 | | - ) |
| 85 | + it('auths valid self-describing PBKDF2 password', async () => { |
| 86 | + const salt = '(ICzAm2.QfCa6.MN' |
| 87 | + const hash = await User.hashForStorage('YouGuessedIt!', salt) |
| 88 | + const r = await User.validPassword('YouGuessedIt!', hash, 'unit-test', salt) |
90 | 89 | assert.equal(r, true) |
91 | 90 | }) |
92 | 91 |
|
93 | | - it('rejects invalid pbkdb2 password', async () => { |
94 | | - const r = await User.validPassword( |
95 | | - 'YouMissedIt!', |
96 | | - '050cfa70c3582be0d5bfae25138a8486dc2e6790f39bc0c4e111223ba6034432', |
97 | | - 'unit-test', |
98 | | - '(ICzAm2.QfCa6.MN', |
99 | | - ) |
| 92 | + it('rejects invalid self-describing PBKDF2 password', async () => { |
| 93 | + const salt = '(ICzAm2.QfCa6.MN' |
| 94 | + const hash = await User.hashForStorage('YouGuessedIt!', salt) |
| 95 | + const r = await User.validPassword('YouMissedIt!', hash, 'unit-test', salt) |
| 96 | + assert.equal(r, false) |
| 97 | + }) |
| 98 | + |
| 99 | + it('auths valid legacy PBKDF2-5000 password', async () => { |
| 100 | + const salt = '(ICzAm2.QfCa6.MN' |
| 101 | + const hash = await User.hashAuthPbkdf2('YouGuessedIt!', salt, 5000) |
| 102 | + const r = await User.validPassword('YouGuessedIt!', hash, 'unit-test', salt) |
| 103 | + assert.equal(r, true) |
| 104 | + }) |
| 105 | + |
| 106 | + it('rejects invalid legacy PBKDF2-5000 password', async () => { |
| 107 | + const salt = '(ICzAm2.QfCa6.MN' |
| 108 | + const hash = await User.hashAuthPbkdf2('YouGuessedIt!', salt, 5000) |
| 109 | + const r = await User.validPassword('YouMissedIt!', hash, 'unit-test', salt) |
100 | 110 | assert.equal(r, false) |
101 | 111 | }) |
102 | 112 |
|
@@ -144,4 +154,120 @@ describe('user', function () { |
144 | 154 | assert.ok(u) |
145 | 155 | }) |
146 | 156 | }) |
| 157 | + |
| 158 | + describe('password upgrade on login', () => { |
| 159 | + const upgradeUserId = 4200 |
| 160 | + const upgradeUser = { |
| 161 | + nt_user_id: upgradeUserId, |
| 162 | + nt_group_id: groupCase.id, |
| 163 | + username: 'upgrade-test', |
| 164 | + email: 'upgrade-test@example.com', |
| 165 | + first_name: 'Upgrade', |
| 166 | + last_name: 'Test', |
| 167 | + } |
| 168 | + const testPass = 'UpgradeMe!123' |
| 169 | + const authCreds = { |
| 170 | + username: `${upgradeUser.username}@${groupCase.name}`, |
| 171 | + password: testPass, |
| 172 | + } |
| 173 | + |
| 174 | + async function getDbPassword() { |
| 175 | + const rows = await Mysql.execute( |
| 176 | + 'SELECT password, pass_salt FROM nt_user WHERE nt_user_id = ?', |
| 177 | + [upgradeUserId], |
| 178 | + ) |
| 179 | + return rows[0] |
| 180 | + } |
| 181 | + |
| 182 | + async function insertUser(password, passSalt) { |
| 183 | + await Mysql.execute( |
| 184 | + 'INSERT INTO nt_user (nt_user_id, nt_group_id, username, email, first_name, last_name, password, pass_salt) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', |
| 185 | + [upgradeUserId, upgradeUser.nt_group_id, upgradeUser.username, upgradeUser.email, upgradeUser.first_name, upgradeUser.last_name, password, passSalt], |
| 186 | + ) |
| 187 | + } |
| 188 | + |
| 189 | + async function cleanup() { |
| 190 | + await Mysql.execute( |
| 191 | + 'DELETE FROM nt_user WHERE nt_user_id = ?', |
| 192 | + [upgradeUserId], |
| 193 | + ) |
| 194 | + } |
| 195 | + |
| 196 | + before(cleanup) |
| 197 | + after(cleanup) |
| 198 | + |
| 199 | + it('upgrades plain text password to self-describing PBKDF2 on login', async () => { |
| 200 | + await cleanup() |
| 201 | + await insertUser(testPass, null) |
| 202 | + |
| 203 | + const result = await User.authenticate(authCreds) |
| 204 | + assert.ok(result, 'login should succeed') |
| 205 | + |
| 206 | + const row = await getDbPassword() |
| 207 | + assert.ok(row.pass_salt, 'pass_salt should be set after upgrade') |
| 208 | + assert.notEqual(row.password, testPass, 'password should be hashed') |
| 209 | + assert.ok(row.password.includes('$'), 'password should be in self-describing format') |
| 210 | + |
| 211 | + // verify round-trip: can still log in with the upgraded hash |
| 212 | + const again = await User.authenticate(authCreds) |
| 213 | + assert.ok(again, 'login should succeed after upgrade') |
| 214 | + await cleanup() |
| 215 | + }) |
| 216 | + |
| 217 | + it('upgrades SHA1 password to self-describing PBKDF2 on login', async () => { |
| 218 | + // authenticate() passes the full authTry.username (including @group) to |
| 219 | + // validPassword(), so the HMAC key must match that full string |
| 220 | + const sha1Hash = crypto |
| 221 | + .createHmac('sha1', authCreds.username.toLowerCase()) |
| 222 | + .update(testPass) |
| 223 | + .digest('hex') |
| 224 | + await cleanup() |
| 225 | + await insertUser(sha1Hash, null) |
| 226 | + |
| 227 | + const result = await User.authenticate(authCreds) |
| 228 | + assert.ok(result, 'login should succeed with SHA1 hash') |
| 229 | + |
| 230 | + const row = await getDbPassword() |
| 231 | + assert.ok(row.pass_salt, 'pass_salt should be set after upgrade') |
| 232 | + assert.notEqual(row.password, sha1Hash, 'password should be re-hashed') |
| 233 | + assert.ok(row.password.includes('$'), 'password should be in self-describing format') |
| 234 | + |
| 235 | + const again = await User.authenticate(authCreds) |
| 236 | + assert.ok(again, 'login should succeed after upgrade') |
| 237 | + await cleanup() |
| 238 | + }) |
| 239 | + |
| 240 | + it('upgrades PBKDF2-5000 to self-describing format on login', async () => { |
| 241 | + const legacySalt = User.generateSalt() |
| 242 | + const legacyHash = await User.hashAuthPbkdf2(testPass, legacySalt, 5000) |
| 243 | + await cleanup() |
| 244 | + await insertUser(legacyHash, legacySalt) |
| 245 | + |
| 246 | + const result = await User.authenticate(authCreds) |
| 247 | + assert.ok(result, 'login should succeed with legacy PBKDF2') |
| 248 | + |
| 249 | + const row = await getDbPassword() |
| 250 | + assert.notEqual(row.password, legacyHash, 'password should be re-hashed') |
| 251 | + assert.notEqual(row.pass_salt, legacySalt, 'salt should be regenerated') |
| 252 | + assert.ok(row.password.includes('$'), 'password should be in self-describing format') |
| 253 | + |
| 254 | + const again = await User.authenticate(authCreds) |
| 255 | + assert.ok(again, 'login should succeed after upgrade') |
| 256 | + await cleanup() |
| 257 | + }) |
| 258 | + |
| 259 | + it('does not re-hash password already in self-describing format', async () => { |
| 260 | + const salt = User.generateSalt() |
| 261 | + const hash = await User.hashForStorage(testPass, salt) |
| 262 | + await cleanup() |
| 263 | + await insertUser(hash, salt) |
| 264 | + |
| 265 | + await User.authenticate(authCreds) |
| 266 | + |
| 267 | + const row = await getDbPassword() |
| 268 | + assert.equal(row.password, hash, 'password should be unchanged') |
| 269 | + assert.equal(row.pass_salt, salt, 'salt should be unchanged') |
| 270 | + await cleanup() |
| 271 | + }) |
| 272 | + }) |
147 | 273 | }) |
0 commit comments