-
Notifications
You must be signed in to change notification settings - Fork 4
[PB-5526] Opaque PoC for login only #830
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
Open
TamaraFinogina
wants to merge
17
commits into
master
Choose a base branch
from
opaque_login_poc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ce68572
add only opaque login
TamaraFinogina 083c408
rename env variable
TamaraFinogina 1428dc0
add tests
TamaraFinogina a37ff9d
merge master
TamaraFinogina 1a3bd4f
add test
TamaraFinogina 798df9d
mv eslintrc.js to .cjs
TamaraFinogina 9585695
fix unit tests
TamaraFinogina 20341a9
fix e2e tests, reduce code re-use
TamaraFinogina b55348f
revert type module
TamaraFinogina 5a89baa
revert change to .cjs
TamaraFinogina e59bac3
add more tests to fix coverage
TamaraFinogina 67fc057
add usecase test for opaque
TamaraFinogina 41edba1
return settings to jest, remove isOpaque from User
TamaraFinogina 8496385
fix sonar complaint
TamaraFinogina 69a3e5a
add bigger limit for registration record
TamaraFinogina 1627825
remove logs
TamaraFinogina 7af4374
Merge remote-tracking branch 'origin/master' into opaque_login_poc
TamaraFinogina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import type { JestConfigWithTsJest } from 'ts-jest'; | ||
|
|
||
| const config: JestConfigWithTsJest = { | ||
| preset: 'ts-jest', | ||
| testEnvironment: 'node', | ||
| transform: { | ||
| '^.+\\.ts$': [ | ||
| 'ts-jest', | ||
| { | ||
| tsconfig: { | ||
| esModuleInterop: true, | ||
| allowSyntheticDefaultImports: true, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| roots: ['src', 'test'], | ||
| collectCoverageFrom: ['**/*.(t|j)s'], | ||
| testRegex: String.raw`.*\.spec\.ts$`, | ||
| transformIgnorePatterns: ['node_modules/(?!@serenity-kit/opaque)'], | ||
| setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], | ||
| }; | ||
|
|
||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| 'use strict'; | ||
|
|
||
| const tableName = 'users'; | ||
| const newColumn = 'registration_record'; | ||
|
|
||
| /** @type {import('sequelize-cli').Migration} */ | ||
| module.exports = { | ||
| async up(queryInterface, Sequelize) { | ||
| await queryInterface.addColumn(tableName, newColumn, { | ||
| type: Sequelize.STRING(300), | ||
| }); | ||
| }, | ||
|
|
||
| async down(queryInterface, Sequelize) { | ||
| await queryInterface.removeColumn(tableName, newColumn, { | ||
| type: Sequelize.STRING(300), | ||
| }); | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { AuthController } from './auth.controller'; | ||
| import { UserUseCases } from '../user/user.usecase'; | ||
| import { CryptoService } from '../../externals/crypto/crypto.service'; | ||
|
|
||
| import { | ||
| LoginAccessOpaqueFinishDto, | ||
| LoginAccessOpaqueStartDto, | ||
| } from './dto/login-access.dto'; | ||
| import { Logger } from '@nestjs/common'; | ||
| import { DeepMocked, createMock } from '@golevelup/ts-jest'; | ||
| import { v4 } from 'uuid'; | ||
|
|
||
| import { Test } from '@nestjs/testing'; | ||
| import * as opaque from '@serenity-kit/opaque'; | ||
| import { ConfigService } from '@nestjs/config'; | ||
|
|
||
| describe('AuthController', () => { | ||
| let authController: AuthController; | ||
| let userUseCases: DeepMocked<UserUseCases>; | ||
| let cryptoService: DeepMocked<CryptoService>; | ||
|
|
||
| let serverSetupMock: string; | ||
|
|
||
| beforeAll(async () => { | ||
| serverSetupMock = opaque.server.createSetup(); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| const moduleRef = await Test.createTestingModule({ | ||
| controllers: [AuthController], | ||
| providers: [ | ||
| CryptoService, | ||
| { | ||
| provide: ConfigService, | ||
| useValue: { | ||
| get: (key: string) => { | ||
| if (key === 'secrets.serverSetup') return serverSetupMock; | ||
| if (key === 'secrets.cryptoSecret2') return 'a'.repeat(64); // Valid hex key | ||
| if (key === 'secrets.cryptoSecret') return 'b'.repeat(64); // Valid hex key | ||
| return null; | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }) | ||
| .setLogger(createMock<Logger>()) | ||
| .useMocker((token) => { | ||
| if (token === CryptoService || token === ConfigService) { | ||
| return undefined; | ||
| } | ||
| return createMock(); | ||
| }) | ||
| .compile(); | ||
|
|
||
| authController = moduleRef.get(AuthController); | ||
| userUseCases = moduleRef.get(UserUseCases); | ||
| cryptoService = moduleRef.get(CryptoService); | ||
| }); | ||
|
|
||
| describe('POST /login-opaque', () => { | ||
| it('Should sucessfully finish both phases of the login', async () => { | ||
| const email = 'USER_test@gmail.com'; | ||
| const password = v4(); | ||
| const { startLoginRequest, clientLoginState } = opaque.client.startLogin({ | ||
| password, | ||
| }); | ||
| const { clientRegistrationState, registrationRequest } = | ||
| opaque.client.startRegistration({ password }); | ||
| const { registrationResponse } = opaque.server.createRegistrationResponse( | ||
| { | ||
| serverSetup: serverSetupMock, | ||
| userIdentifier: email.toLowerCase(), | ||
| registrationRequest, | ||
| }, | ||
| ); | ||
| const { registrationRecord: registrationRecordMock } = | ||
| opaque.client.finishRegistration({ | ||
| clientRegistrationState, | ||
| registrationResponse, | ||
| password, | ||
| }); | ||
| const loginOpaqueDto = new LoginAccessOpaqueStartDto(); | ||
| loginOpaqueDto.email = email; | ||
| loginOpaqueDto.startLoginRequest = startLoginRequest; | ||
|
|
||
| jest.spyOn(userUseCases, 'findByEmail').mockResolvedValue({ | ||
| registrationRecord: registrationRecordMock, | ||
| } as any); | ||
| const startLoginOpaqueSpy = jest.spyOn(cryptoService, 'startLoginOpaque'); | ||
| const resultPhaseOne = | ||
| await authController.loginOpaqueStart(loginOpaqueDto); | ||
| const serverLoginStateValue = userUseCases.setLoginState.mock.calls[0][1]; | ||
|
|
||
| expect(startLoginOpaqueSpy).toHaveBeenCalledTimes(1); | ||
| expect(startLoginOpaqueSpy).toHaveBeenCalledWith( | ||
| loginOpaqueDto.email.toLowerCase(), | ||
| registrationRecordMock, | ||
| loginOpaqueDto.startLoginRequest, | ||
| ); | ||
| expect(resultPhaseOne.loginResponse).toBeDefined(); | ||
| expect(resultPhaseOne).toEqual({ loginResponse: expect.any(String) }); | ||
|
|
||
| const loginOpaqueFinishDto = new LoginAccessOpaqueFinishDto(); | ||
| loginOpaqueFinishDto.email = email; | ||
|
|
||
| jest | ||
| .spyOn(userUseCases, 'getLoginState') | ||
| .mockResolvedValue(serverLoginStateValue); | ||
|
|
||
| const { finishLoginRequest, sessionKey } = opaque.client.finishLogin({ | ||
| clientLoginState, | ||
| loginResponse: resultPhaseOne.loginResponse, | ||
| password, | ||
| }); | ||
|
|
||
| loginOpaqueFinishDto.finishLoginRequest = finishLoginRequest; | ||
|
|
||
| const finishLoginOpaqueSpy = jest.spyOn( | ||
| cryptoService, | ||
| 'finishLoginOpaque', | ||
| ); | ||
|
|
||
| const resultPhaseTwo = | ||
| await authController.loginOpaqueFinish(loginOpaqueFinishDto); | ||
|
|
||
| expect(finishLoginOpaqueSpy).toHaveBeenCalledTimes(1); | ||
| expect(finishLoginOpaqueSpy).toHaveBeenCalledWith( | ||
| finishLoginRequest, | ||
| serverLoginStateValue, | ||
| ); | ||
|
|
||
| expect(resultPhaseTwo.user).toBeDefined(); | ||
| expect(resultPhaseTwo.token).toBeDefined(); | ||
| expect(resultPhaseTwo.sessionID).toBeDefined(); | ||
| expect(userUseCases.setSessionKey).toHaveBeenCalledWith( | ||
| resultPhaseTwo.sessionID, | ||
| sessionKey, | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be careful, we use jest to generate the report SonarCloud uses to determine the test coverage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved it to jest.config.ts