mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-24 18:56:32 -05:00
feat(auth): password change requires old password
By checking the "old" password of the user prior to a password change, the password change function is more secured against abuse. Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
parent
20b0ded223
commit
277e2fb1ca
4 changed files with 37 additions and 2 deletions
|
@ -9,10 +9,10 @@ import {
|
||||||
ConflictException,
|
ConflictException,
|
||||||
Controller,
|
Controller,
|
||||||
Delete,
|
Delete,
|
||||||
NotFoundException,
|
|
||||||
Post,
|
Post,
|
||||||
Put,
|
Put,
|
||||||
Req,
|
Req,
|
||||||
|
UnauthorizedException,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { Session } from 'express-session';
|
import { Session } from 'express-session';
|
||||||
|
@ -70,6 +70,10 @@ export class AuthController {
|
||||||
@Body() changePasswordDto: UpdatePasswordDto,
|
@Body() changePasswordDto: UpdatePasswordDto,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
await this.identityService.loginWithLocalIdentity(
|
||||||
|
user,
|
||||||
|
changePasswordDto.currentPassword,
|
||||||
|
);
|
||||||
await this.identityService.updateLocalPassword(
|
await this.identityService.updateLocalPassword(
|
||||||
user,
|
user,
|
||||||
changePasswordDto.newPassword,
|
changePasswordDto.newPassword,
|
||||||
|
@ -77,7 +81,9 @@ export class AuthController {
|
||||||
return;
|
return;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof NotInDBError) {
|
if (e instanceof NotInDBError) {
|
||||||
throw new NotFoundException(e.message);
|
throw new UnauthorizedException(
|
||||||
|
'Verifying your identity with the current password did not work.',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
import { IsString } from 'class-validator';
|
import { IsString } from 'class-validator';
|
||||||
|
|
||||||
export class UpdatePasswordDto {
|
export class UpdatePasswordDto {
|
||||||
|
@IsString()
|
||||||
|
currentPassword: string;
|
||||||
@IsString()
|
@IsString()
|
||||||
newPassword: string;
|
newPassword: string;
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,6 +112,7 @@ describe('Auth', () => {
|
||||||
it('works', async () => {
|
it('works', async () => {
|
||||||
// Change password
|
// Change password
|
||||||
const changePasswordDto: UpdatePasswordDto = {
|
const changePasswordDto: UpdatePasswordDto = {
|
||||||
|
currentPassword: password,
|
||||||
newPassword: newPassword,
|
newPassword: newPassword,
|
||||||
};
|
};
|
||||||
await request(testSetup.app.getHttpServer())
|
await request(testSetup.app.getHttpServer())
|
||||||
|
@ -133,6 +134,7 @@ describe('Auth', () => {
|
||||||
cookie = response.get('Set-Cookie')[0];
|
cookie = response.get('Set-Cookie')[0];
|
||||||
// Reset password
|
// Reset password
|
||||||
const changePasswordBackDto: UpdatePasswordDto = {
|
const changePasswordBackDto: UpdatePasswordDto = {
|
||||||
|
currentPassword: newPassword,
|
||||||
newPassword: password,
|
newPassword: password,
|
||||||
};
|
};
|
||||||
await request(testSetup.app.getHttpServer())
|
await request(testSetup.app.getHttpServer())
|
||||||
|
@ -146,6 +148,7 @@ describe('Auth', () => {
|
||||||
testSetup.configService.get('authConfig').local.enableLogin = false;
|
testSetup.configService.get('authConfig').local.enableLogin = false;
|
||||||
// Try to change password
|
// Try to change password
|
||||||
const changePasswordDto: UpdatePasswordDto = {
|
const changePasswordDto: UpdatePasswordDto = {
|
||||||
|
currentPassword: password,
|
||||||
newPassword: newPassword,
|
newPassword: newPassword,
|
||||||
};
|
};
|
||||||
await request(testSetup.app.getHttpServer())
|
await request(testSetup.app.getHttpServer())
|
||||||
|
@ -177,6 +180,29 @@ describe('Auth', () => {
|
||||||
.send(JSON.stringify(loginOldPasswordDto))
|
.send(JSON.stringify(loginOldPasswordDto))
|
||||||
.expect(201);
|
.expect(201);
|
||||||
});
|
});
|
||||||
|
it('fails, when old password is wrong', async () => {
|
||||||
|
// Try to change password
|
||||||
|
const changePasswordDto: UpdatePasswordDto = {
|
||||||
|
currentPassword: 'wrong',
|
||||||
|
newPassword: newPassword,
|
||||||
|
};
|
||||||
|
await request(testSetup.app.getHttpServer())
|
||||||
|
.put('/api/private/auth/local')
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Cookie', cookie)
|
||||||
|
.send(JSON.stringify(changePasswordDto))
|
||||||
|
.expect(401);
|
||||||
|
// old password still does work for login
|
||||||
|
const loginOldPasswordDto: LoginDto = {
|
||||||
|
password: password,
|
||||||
|
username: username,
|
||||||
|
};
|
||||||
|
await request(testSetup.app.getHttpServer())
|
||||||
|
.post('/api/private/auth/local/login')
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.send(JSON.stringify(loginOldPasswordDto))
|
||||||
|
.expect(201);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('POST /auth/local/login', () => {
|
describe('POST /auth/local/login', () => {
|
||||||
|
|
|
@ -114,6 +114,7 @@ describe('Register and Login', () => {
|
||||||
.set('Content-Type', 'application/json')
|
.set('Content-Type', 'application/json')
|
||||||
.send(
|
.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
|
currentPassword: PASSWORD,
|
||||||
newPassword: 'newPassword',
|
newPassword: 'newPassword',
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue