-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Integrate API, Client and backoffice auth
- Loading branch information
Showing
57 changed files
with
682 additions
and
100 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,25 @@ | ||
import { BackOfficeSession } from '@shared/entities/users/backoffice-session'; | ||
import * as crypto from 'crypto'; | ||
import { ApiConfigService } from '../config/app-config.service'; | ||
import { Inject } from '@nestjs/common'; | ||
|
||
export class BackofficeService { | ||
constructor( | ||
@Inject(ApiConfigService) | ||
private readonly configService: ApiConfigService, | ||
) {} | ||
|
||
public generateCookieFromBackofficeSession( | ||
backofficeSession: BackOfficeSession, | ||
): string { | ||
const cookieSecret = this.configService.get( | ||
'BACKOFFICE_SESSION_COOKIE_SECRET', | ||
); | ||
const hmac = crypto | ||
.createHmac('sha256', cookieSecret) | ||
.update(backofficeSession.sid) | ||
.digest('base64') | ||
.replace(/=+$/, ''); | ||
return `s:${backofficeSession.sid}.${hmac}`; | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -77,6 +77,34 @@ describe('Sign In', () => { | |
expect(response.body.accessToken).toBeDefined(); | ||
}); | ||
|
||
test('Should return 201 an access token and set a backoffice cookie when an admin user successfully signs in', async () => { | ||
// Given a user exists with valid credentials | ||
const user = await testManager.mocks().createUser({ | ||
role: ROLES.ADMIN, | ||
email: '[email protected]', | ||
isActive: true, | ||
password: '12345678', | ||
}); | ||
|
||
// And the user tries to sign in with valid credentials | ||
const response = await testManager | ||
.request() | ||
.post(authContract.login.path) | ||
.send({ | ||
email: '[email protected]', | ||
password: '12345678', | ||
}); | ||
|
||
// We should get back OK response and an access token | ||
expect(response.status).toBe(HttpStatus.CREATED); | ||
expect(response.body.accessToken).toBeDefined(); | ||
const setCookieHeader = response.headers['set-cookie']; | ||
expect(setCookieHeader).toHaveLength(1); | ||
expect(decodeURIComponent(setCookieHeader[0])).toMatch( | ||
/^backoffice=s:[^\s]+\.[^\s]+;/, | ||
); | ||
}); | ||
|
||
test('Should return UNAUTHORIZED when trying to sign in with an inactive account', async () => { | ||
// Given a user exists with valid credentials | ||
const user = await testManager.mocks().createUser({ | ||
|
File renamed without changes.
File renamed without changes.
This file contains 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 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.