Skip to content
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

[PB-2153] feat: remove password prompt in security section #423

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions src/modules/folder/folder.usecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ describe('FolderUseCases', () => {
},
});

jest.spyOn(cryptoService, 'decryptName').mockReturnValueOnce('');

expect(() => service.decryptFolderName(folder)).toThrow(
'Unable to decrypt folder name',
);
Expand Down
7 changes: 0 additions & 7 deletions src/modules/user/dto/update-password.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';

export class UpdatePasswordDto {
@IsString()
@ApiProperty({
example: 'currentPassword',
description: 'Current password',
})
currentPassword: string;

@IsString()
@ApiProperty({
example: 'newPassword',
Expand Down
61 changes: 60 additions & 1 deletion src/modules/user/user.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { DeepMocked, createMock } from '@golevelup/ts-jest';
import {
ForbiddenException,
InternalServerErrorException,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import getEnv from '../../config/configuration';
import { UserController } from './user.controller';
import { MailLimitReachedException, UserUseCases } from './user.usecase';
import {
KeyServerNotFoundError,
MailLimitReachedException,
UserUseCases,
} from './user.usecase';
import { NotificationService } from '../../externals/notifications/notification.service';
import { KeyServerUseCases } from '../keyserver/key-server.usecase';
import { CryptoService } from '../../externals/crypto/crypto.service';
Expand Down Expand Up @@ -205,4 +210,58 @@ describe('User Controller', () => {
).resolves.toBeUndefined();
});
});

describe('PATCH /password', () => {
const user = newUser();
const payload = {
newPassword: v4(),
newSalt: v4(),
mnemonic: v4(),
privateKey: v4(),
encryptVersion: null,
};
const mockTokens = {
newToken: v4(),
token: v4(),
};

it('When a new password is updated it should return the new tokens', async () => {
userUseCases.updatePassword.mockResolvedValueOnce();
userUseCases.getAuthTokens.mockReturnValueOnce(mockTokens);

await expect(
userController.updatePassword(payload, user),
).resolves.toStrictEqual({ status: 'success', ...mockTokens });
});

it('should throw an error if password update fails', async () => {
userUseCases.updatePassword.mockRejectedValueOnce(
new UnauthorizedException(),
);

await expect(
userController.updatePassword(payload, user),
).rejects.toThrow(UnauthorizedException);
});

it('should throw an error if password update fails', async () => {
userUseCases.updatePassword.mockRejectedValueOnce(
new KeyServerNotFoundError(),
);

await expect(
userController.updatePassword(payload, user),
).rejects.toThrow(NotFoundException);
});

it('should throw an error if fails', async () => {
userUseCases.updatePassword.mockRejectedValueOnce(
new InternalServerErrorException(),
);

await expect(
userController.updatePassword(payload, user),
).rejects.toThrow(InternalServerErrorException);
});
});
});
23 changes: 4 additions & 19 deletions src/modules/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,28 +423,18 @@ export class UserController {
@Patch('password')
@ApiBearerAuth()
async updatePassword(
@RequestDecorator() req,
@Body() updatePasswordDto: UpdatePasswordDto,
@Res({ passthrough: true }) res: Response,
@UserDecorator() user: User,
) {
try {
const currentPassword = this.cryptoService.decryptText(
updatePasswordDto.currentPassword,
);
const newPassword = this.cryptoService.decryptText(
updatePasswordDto.newPassword,
);
const newSalt = this.cryptoService.decryptText(updatePasswordDto.newSalt);

const { mnemonic, privateKey, encryptVersion } = updatePasswordDto;

if (user.password.toString() !== currentPassword) {
throw new UnauthorizedException();
}

await this.userUseCases.updatePassword(req.user, {
currentPassword,
await this.userUseCases.updatePassword(user, {
newPassword,
newSalt,
mnemonic,
Expand All @@ -459,12 +449,10 @@ export class UserController {

return { status: 'success', newToken, token };
} catch (err) {
let errorMessage = err.message;

if (err instanceof UnauthorizedException) {
res.status(HttpStatus.BAD_REQUEST);
throw new UnauthorizedException();
} else if (err instanceof KeyServerNotFoundError) {
res.status(HttpStatus.NOT_FOUND);
throw new NotFoundException();
} else {
new Logger().error(
`[AUTH/UPDATEPASSWORD] ERROR: ${
Expand All @@ -473,11 +461,8 @@ export class UserController {
(err as Error).stack
}`,
);
res.status(HttpStatus.INTERNAL_SERVER_ERROR);
errorMessage = 'Internal Server Error';
throw new InternalServerErrorException();
}

return { error: errorMessage };
}
}

Expand Down
Loading