BlueGit

timfa filemanager

Add password change auth endpoint

timfa Tim Falken · pushed by timfa

Authored 2026-05-19 · 6 months ago

Hash
678bdf1ab4ad300f96e7cc1fc5a1088f4ec2ccb4
Parent
5c46b604
2 files changed +100 −0
server/routes/auth.js +37 −0
@@ -158,4 +158,41 @@ router.post('/logout', requireAuth, (req, res) =>
158 158 res.json({ ok: true });
159 159 });
160 160
161 // ---------------------------------------------------------------------------
162 // POST /api/auth/change-password
163 // Body: { currentPassword, newPassword }
164 // ---------------------------------------------------------------------------
165 router.post('/change-password', requireAuth, (req, res) =>
166 {
167 const currentPassword = String(req.body && req.body.currentPassword || '');
168 const newPassword = String(req.body && req.body.newPassword || '');
169
170 if (!currentPassword || !newPassword)
171 {
172 return res.status(400).json({ error: 'Current and new password are required.' });
173 }
174
175 if (newPassword.length < 8)
176 {
177 return res.status(400).json({ error: 'New password must be at least 8 characters.' });
178 }
179
180 const db = getDb();
181 const user = db.prepare('SELECT username, password_hash FROM users WHERE id = ?').get([req.user.id]);
182 if (!user)
183 {
184 return res.status(404).json({ error: 'User not found.' });
185 }
186
187 if (!bcrypt.compareSync(currentPassword, user.password_hash))
188 {
189 return res.status(400).json({ error: 'Current password is incorrect.' });
190 }
191
192 const nextHash = bcrypt.hashSync(newPassword, BCRYPT_ROUNDS);
193 db.prepare('UPDATE users SET password_hash = ? WHERE id = ?').run([nextHash, req.user.id]);
194
195 res.json({ ok: true });
196 });
197
161 198 module.exports = router;
@@ -158,4 +158,41 @@ router.post('/logout', requireAuth, (req, res) =>
158 res.json({ ok: true });
158 res.json({ ok: true });
159});
159});
160
160
161// ---------------------------------------------------------------------------
162// POST /api/auth/change-password
163// Body: { currentPassword, newPassword }
164// ---------------------------------------------------------------------------
165router.post('/change-password', requireAuth, (req, res) =>
166{
167 const currentPassword = String(req.body && req.body.currentPassword || '');
168 const newPassword = String(req.body && req.body.newPassword || '');
169
170 if (!currentPassword || !newPassword)
171 {
172 return res.status(400).json({ error: 'Current and new password are required.' });
173 }
174
175 if (newPassword.length < 8)
176 {
177 return res.status(400).json({ error: 'New password must be at least 8 characters.' });
178 }
179
180 const db = getDb();
181 const user = db.prepare('SELECT username, password_hash FROM users WHERE id = ?').get([req.user.id]);
182 if (!user)
183 {
184 return res.status(404).json({ error: 'User not found.' });
185 }
186
187 if (!bcrypt.compareSync(currentPassword, user.password_hash))
188 {
189 return res.status(400).json({ error: 'Current password is incorrect.' });
190 }
191
192 const nextHash = bcrypt.hashSync(newPassword, BCRYPT_ROUNDS);
193 db.prepare('UPDATE users SET password_hash = ? WHERE id = ?').run([nextHash, req.user.id]);
194
195 res.json({ ok: true });
196});
197
161module.exports = router;
198module.exports = router;
tests/auth.routes.test.js +63 −0
@@ -365,3 +365,66 @@ describe('session lifecycle', () =>
365 365 }
366 366 });
367 367 });
368
369 describe('POST /api/auth/change-password', () =>
370 {
371 const fingerprint = 'test';
372
373 async function loginAndGetToken (password)
374 {
375 const kp = makeClientKeyPair();
376 const encrypted = clientEncrypt({ username: 'admin', password });
377 const { status, body } = await post('/api/auth/login', {
378 encrypted,
379 clientPublicKey: kp.publicKeyJwk,
380 fingerprint,
381 });
382 expect(status).toBe(200);
383 return clientDecrypt(body.encrypted, kp.privateKey);
384 }
385
386 test('rejects incorrect current password', async () =>
387 {
388 const token = await loginAndGetToken('adminpass1');
389 const res = await authed('POST', '/api/auth/change-password', token, fingerprint, {
390 currentPassword: 'wrong-pass',
391 newPassword: 'adminpass2',
392 });
393
394 expect(res.status).toBe(400);
395 expect(res.body.error).toMatch(/current password is incorrect/i);
396 });
397
398 test('updates password and invalidates old login credentials', async () =>
399 {
400 const token = await loginAndGetToken('adminpass1');
401
402 const change = await authed('POST', '/api/auth/change-password', token, fingerprint, {
403 currentPassword: 'adminpass1',
404 newPassword: 'adminpass2',
405 });
406 expect(change.status).toBe(200);
407 expect(change.body.ok).toBe(true);
408
409 const oldLoginEncrypted = clientEncrypt({ username: 'admin', password: 'adminpass1' });
410 const oldLoginKp = makeClientKeyPair();
411 const oldLogin = await post('/api/auth/login', {
412 encrypted: oldLoginEncrypted,
413 clientPublicKey: oldLoginKp.publicKeyJwk,
414 fingerprint,
415 });
416 expect(oldLogin.status).toBe(401);
417
418 const newLoginEncrypted = clientEncrypt({ username: 'admin', password: 'adminpass2' });
419 const newLoginKp = makeClientKeyPair();
420 const newLogin = await post('/api/auth/login', {
421 encrypted: newLoginEncrypted,
422 clientPublicKey: newLoginKp.publicKeyJwk,
423 fingerprint,
424 });
425 expect(newLogin.status).toBe(200);
426
427 const tokenAfterChange = clientDecrypt(newLogin.body.encrypted, newLoginKp.privateKey);
428 expect(tokenAfterChange.split('.').length).toBe(3);
429 });
430 });
@@ -365,3 +365,66 @@ describe('session lifecycle', () =>
365 }
365 }
366 });
366 });
367});
367});
368
369describe('POST /api/auth/change-password', () =>
370{
371 const fingerprint = 'test';
372
373 async function loginAndGetToken (password)
374 {
375 const kp = makeClientKeyPair();
376 const encrypted = clientEncrypt({ username: 'admin', password });
377 const { status, body } = await post('/api/auth/login', {
378 encrypted,
379 clientPublicKey: kp.publicKeyJwk,
380 fingerprint,
381 });
382 expect(status).toBe(200);
383 return clientDecrypt(body.encrypted, kp.privateKey);
384 }
385
386 test('rejects incorrect current password', async () =>
387 {
388 const token = await loginAndGetToken('adminpass1');
389 const res = await authed('POST', '/api/auth/change-password', token, fingerprint, {
390 currentPassword: 'wrong-pass',
391 newPassword: 'adminpass2',
392 });
393
394 expect(res.status).toBe(400);
395 expect(res.body.error).toMatch(/current password is incorrect/i);
396 });
397
398 test('updates password and invalidates old login credentials', async () =>
399 {
400 const token = await loginAndGetToken('adminpass1');
401
402 const change = await authed('POST', '/api/auth/change-password', token, fingerprint, {
403 currentPassword: 'adminpass1',
404 newPassword: 'adminpass2',
405 });
406 expect(change.status).toBe(200);
407 expect(change.body.ok).toBe(true);
408
409 const oldLoginEncrypted = clientEncrypt({ username: 'admin', password: 'adminpass1' });
410 const oldLoginKp = makeClientKeyPair();
411 const oldLogin = await post('/api/auth/login', {
412 encrypted: oldLoginEncrypted,
413 clientPublicKey: oldLoginKp.publicKeyJwk,
414 fingerprint,
415 });
416 expect(oldLogin.status).toBe(401);
417
418 const newLoginEncrypted = clientEncrypt({ username: 'admin', password: 'adminpass2' });
419 const newLoginKp = makeClientKeyPair();
420 const newLogin = await post('/api/auth/login', {
421 encrypted: newLoginEncrypted,
422 clientPublicKey: newLoginKp.publicKeyJwk,
423 fingerprint,
424 });
425 expect(newLogin.status).toBe(200);
426
427 const tokenAfterChange = clientDecrypt(newLogin.body.encrypted, newLoginKp.privateKey);
428 expect(tokenAfterChange.split('.').length).toBe(3);
429 });
430});