Skip to content

Commit a30f8f2

Browse files
[TECH] Amélioration des performances du database builder
#11495
2 parents 9d542b1 + ed59720 commit a30f8f2

20 files changed

+49
-84
lines changed

api/db/database-builder/factory/build-authentication-method.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import { NON_OIDC_IDENTITY_PROVIDERS } from '../../../src/identity-access-manage
44
import * as OidcIdentityProviders from '../../../src/identity-access-management/domain/constants/oidc-identity-providers.js';
55
import { AuthenticationMethod } from '../../../src/identity-access-management/domain/models/AuthenticationMethod.js';
66
import { cryptoService } from '../../../src/shared/domain/services/crypto-service.js';
7+
import { DEFAULT_PASSWORD } from '../../constants.js';
78
import { databaseBuffer } from '../database-buffer.js';
89
import { buildUser } from './build-user.js';
910

11+
// eslint-disable-next-line no-sync
12+
const DEFAULT_HASHED_PASSWORD = cryptoService.hashPasswordSync(DEFAULT_PASSWORD);
13+
1014
const buildAuthenticationMethod = {};
1115

1216
buildAuthenticationMethod.withGarAsIdentityProvider = function ({
@@ -69,21 +73,19 @@ buildAuthenticationMethod.withPixAsIdentityProviderAndHashedPassword = function
6973

7074
buildAuthenticationMethod.withPixAsIdentityProviderAndPassword = function ({
7175
id = databaseBuffer.getNextId(),
72-
password = 'Password123',
76+
password = DEFAULT_PASSWORD,
7377
shouldChangePassword = false,
7478
userId,
7579
createdAt = new Date('2020-01-01'),
7680
updatedAt = new Date('2020-01-02'),
7781
} = {}) {
78-
// eslint-disable-next-line no-sync
79-
const hashedPassword = cryptoService.hashPasswordSync(password);
8082
userId = isUndefined(userId) ? buildUser().id : userId;
8183

8284
const values = {
8385
id,
8486
identityProvider: NON_OIDC_IDENTITY_PROVIDERS.PIX.code,
8587
authenticationComplement: new AuthenticationMethod.PixAuthenticationComplement({
86-
password: hashedPassword,
88+
password: getUserHashedPassword(password),
8789
shouldChangePassword,
8890
}),
8991
externalIdentifier: undefined,
@@ -190,4 +192,12 @@ buildAuthenticationMethod.withIdentityProvider = function ({
190192
});
191193
};
192194

193-
export { buildAuthenticationMethod };
195+
function getUserHashedPassword(password) {
196+
if (password === DEFAULT_PASSWORD) {
197+
return DEFAULT_HASHED_PASSWORD;
198+
}
199+
// eslint-disable-next-line no-sync
200+
return cryptoService.hashPasswordSync(password);
201+
}
202+
203+
export { buildAuthenticationMethod, getUserHashedPassword };
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
import { cryptoService } from '../../../src/shared/domain/services/crypto-service.js';
22
import { databaseBuffer } from '../database-buffer.js';
33

4+
const DEFAULT_CLIENT_SECRET = 'super-secret';
5+
// eslint-disable-next-line no-sync
6+
const DEFAULT_CLIENT_SECRET_HASH = cryptoService.hashPasswordSync(DEFAULT_CLIENT_SECRET);
7+
48
export function buildClientApplication({
59
id = databaseBuffer.getNextId(),
610
name = 'clientApplication',
711
clientId = 'client-id',
812
clientSecret = 'super-secret',
913
scopes = ['scope1', 'scope2'],
1014
} = {}) {
11-
// eslint-disable-next-line no-sync
12-
const hashedSecret = cryptoService.hashPasswordSync(clientSecret);
1315
return databaseBuffer.pushInsertable({
1416
tableName: 'client_applications',
1517
values: {
1618
id,
1719
name,
1820
clientId,
19-
clientSecret: hashedSecret,
21+
clientSecret: _getHashedSecret(clientSecret),
2022
scopes,
2123
},
2224
});
2325
}
26+
27+
function _getHashedSecret(clientSecret) {
28+
if (clientSecret === DEFAULT_CLIENT_SECRET) {
29+
return DEFAULT_CLIENT_SECRET_HASH;
30+
}
31+
// eslint-disable-next-line no-sync
32+
return cryptoService.hashPasswordSync(clientSecret);
33+
}

api/db/database-builder/factory/build-user.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ const { isUndefined, isNil } = lodash;
55
import { PIX_ADMIN } from '../../../src/authorization/domain/constants.js';
66
import { NON_OIDC_IDENTITY_PROVIDERS } from '../../../src/identity-access-management/domain/constants/identity-providers.js';
77
import { AuthenticationMethod, Membership } from '../../../src/shared/domain/models/index.js';
8-
import { cryptoService } from '../../../src/shared/domain/services/crypto-service.js';
8+
import { DEFAULT_PASSWORD } from '../../constants.js';
99
import { databaseBuffer } from '../database-buffer.js';
10+
import { getUserHashedPassword } from './build-authentication-method.js';
1011
import { buildCertificationCenter } from './build-certification-center.js';
1112
import { buildCertificationCenterMembership } from './build-certification-center-membership.js';
1213
import { buildMembership } from './build-membership.js';
1314
import { buildOrganization } from './build-organization.js';
1415
import { buildPixAdminRole } from './build-pix-admin-role.js';
1516

16-
const DEFAULT_PASSWORD = 'pix123';
1717
const { ROLES } = PIX_ADMIN;
1818

1919
/**
@@ -375,15 +375,12 @@ function _buildPixAuthenticationMethod({
375375
createdAt,
376376
updatedAt,
377377
} = {}) {
378-
// eslint-disable-next-line no-sync
379-
const hashedPassword = cryptoService.hashPasswordSync(rawPassword);
380-
381378
const values = {
382379
id,
383380
userId,
384381
identityProvider: NON_OIDC_IDENTITY_PROVIDERS.PIX.code,
385382
authenticationComplement: new AuthenticationMethod.PixAuthenticationComplement({
386-
password: hashedPassword,
383+
password: getUserHashedPassword(rawPassword),
387384
shouldChangePassword,
388385
}),
389386
externalIdentifier: undefined,

api/db/seeds/data/common/common-builder.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PIX_ADMIN } from '../../../../src/authorization/domain/constants.js';
2-
import { DEFAULT_PASSWORD, PIX_PUBLIC_TARGET_PROFILE_ID, REAL_PIX_SUPER_ADMIN_ID } from './constants.js';
2+
import { PIX_PUBLIC_TARGET_PROFILE_ID, REAL_PIX_SUPER_ADMIN_ID } from './constants.js';
33
import { acceptPixOrgaTermsOfService, createPixOrgaTermsOfService } from './tooling/legal-documents.js';
44
import { createTargetProfile } from './tooling/target-profile-tooling.js';
55

@@ -27,7 +27,6 @@ function _createSuperAdmin(databaseBuilder) {
2727
firstName: 'Admin',
2828
lastName: 'Admin',
2929
email: 'superadmin@example.net',
30-
rawPassword: DEFAULT_PASSWORD,
3130
});
3231
databaseBuilder.factory.buildPixAdminRole({ userId: REAL_PIX_SUPER_ADMIN_ID, role: ROLES.SUPER_ADMIN });
3332
acceptPixOrgaTermsOfService(databaseBuilder, REAL_PIX_SUPER_ADMIN_ID);
@@ -40,7 +39,6 @@ function _createMetierAdmin(databaseBuilder) {
4039
firstName: 'Admin',
4140
lastName: 'Metier',
4241
email: 'metieradmin@example.net',
43-
rawPassword: DEFAULT_PASSWORD,
4442
});
4543
databaseBuilder.factory.buildPixAdminRole({ userId, role: ROLES.METIER });
4644
acceptPixOrgaTermsOfService(databaseBuilder, userId);
@@ -53,7 +51,6 @@ function _createSupportAdmin(databaseBuilder) {
5351
firstName: 'Admin',
5452
lastName: 'Support',
5553
email: 'supportadmin@example.net',
56-
rawPassword: DEFAULT_PASSWORD,
5754
});
5855
databaseBuilder.factory.buildPixAdminRole({ userId, role: ROLES.SUPPORT });
5956
acceptPixOrgaTermsOfService(databaseBuilder, userId);
@@ -66,7 +63,6 @@ function _createCertifAdmin(databaseBuilder) {
6663
firstName: 'Admin',
6764
lastName: 'Certif',
6865
email: 'certifadmin@example.net',
69-
rawPassword: DEFAULT_PASSWORD,
7066
});
7167
databaseBuilder.factory.buildPixAdminRole({ userId, role: ROLES.CERTIF });
7268
acceptPixOrgaTermsOfService(databaseBuilder, userId);

api/db/seeds/data/common/constants.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Tag } from '../../../../src/organizational-entities/domain/models/Tag.js';
22

3-
const DEFAULT_PASSWORD = 'pix123';
43
const COMMON_OFFSET_ID = 1000;
54

65
//USERS
@@ -86,7 +85,6 @@ export {
8685
AGRICULTURE_TAG,
8786
CFA_TAG,
8887
COLLEGE_TAG,
89-
DEFAULT_PASSWORD,
9088
FEATURE_ATTESTATIONS_MANAGEMENT_ID,
9189
FEATURE_CAMPAIGN_EXTERNAL_ID,
9290
FEATURE_CAMPAIGN_WITHOUT_USER_PROFILE_ID,

api/db/seeds/data/common/organization-builder.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
AGRICULTURE_TAG,
3-
DEFAULT_PASSWORD,
43
FEATURE_ATTESTATIONS_MANAGEMENT_ID,
54
FEATURE_CAMPAIGN_WITHOUT_USER_PROFILE_ID,
65
FEATURE_COMPUTE_ORGANIZATION_LEARNER_CERTIFICABILITY_ID,
@@ -132,8 +131,6 @@ function _createUserAdminForOrganizations(databaseBuilder) {
132131
email: 'admin-orga@example.net',
133132
cgu: true,
134133
lang: 'fr',
135-
rawPassword: DEFAULT_PASSWORD,
136-
shouldChangePassword: false,
137134
});
138135
acceptPixOrgaTermsOfService(databaseBuilder, USER_ID_ADMIN_ORGANIZATION);
139136
}
@@ -146,8 +143,6 @@ function _createUserMemberForOrganizations(databaseBuilder) {
146143
email: 'member-orga@example.net',
147144
cgu: true,
148145
lang: 'en',
149-
rawPassword: DEFAULT_PASSWORD,
150-
shouldChangePassword: false,
151146
});
152147
acceptPixOrgaTermsOfService(databaseBuilder, USER_ID_MEMBER_ORGANIZATION);
153148
}

api/db/seeds/data/team-1d/data-builder.js

-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ async function _createSco1dUser(databaseBuilder) {
3333
mustValidateTermsOfService: false,
3434
pixCertifTermsOfServiceAccepted: true,
3535
hasSeenAssessmentInstructions: false,
36-
rawPassword: 'pix123',
37-
shouldChangePassword: false,
3836
});
3937
acceptPixOrgaTermsOfService(databaseBuilder, TEAM_1D_USER_ID);
4038
await databaseBuilder.commit();

api/db/seeds/data/team-acces/build-blocked-users.js

-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { config } from '../../../../src/shared/config.js';
2-
import { DEFAULT_PASSWORD } from '../../../constants.js';
32

43
function _buildBlockedUser(databaseBuilder) {
54
const blockedUser = databaseBuilder.factory.buildUser.withRawPassword({
65
firstName: 'Goldi',
76
lastName: 'Locks',
87
email: 'blocked@example.net',
98
username: 'goldi.locks',
10-
rawPassword: DEFAULT_PASSWORD,
119
cgu: false,
1210
});
1311

@@ -24,7 +22,6 @@ function _buildAlmostBlockedUser(databaseBuilder) {
2422
lastName: 'Locks',
2523
email: 'almost-blocked@example.net',
2624
username: 'silveri.locks',
27-
rawPassword: DEFAULT_PASSWORD,
2825
cgu: false,
2926
});
3027

@@ -40,7 +37,6 @@ function _buildAlmostTemporarilyBlockedUser(databaseBuilder) {
4037
lastName: 'Bear',
4138
email: 'almost-temporarily-blocked@example.net',
4239
username: 'small.bear',
43-
rawPassword: DEFAULT_PASSWORD,
4440
cgu: false,
4541
});
4642

api/db/seeds/data/team-acces/build-certification-centers.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { DEFAULT_PASSWORD } from '../../../constants.js';
21
import { createCertificationCenter } from '../common/tooling/certification-center-tooling.js';
32

43
const CERTIFICATION_CENTER_OFFSET_ID = 8000;
@@ -95,20 +94,12 @@ export async function buildCertificationCenters(databaseBuilder) {
9594
});
9695
}
9796

98-
function _buildUsersWithDefaultPassword({
99-
databaseBuilder,
100-
firstName,
101-
lastName,
102-
email,
103-
username,
104-
rawPassword = DEFAULT_PASSWORD,
105-
}) {
97+
function _buildUsersWithDefaultPassword({ databaseBuilder, firstName, lastName, email, username }) {
10698
return databaseBuilder.factory.buildUser.withRawPassword({
10799
firstName,
108100
lastName,
109101
email,
110102
username,
111-
rawPassword,
112103
cgu: true,
113104
lastPixCertifTermsOfServiceValidatedAt: new Date(),
114105
pixCertifTermsOfServiceAccepted: true,

api/db/seeds/data/team-acces/build-organization-users.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { DEFAULT_PASSWORD } from '../../../constants.js';
21
import { acceptPixOrgaTermsOfService } from '../common/tooling/legal-documents.js';
32

43
export const PIX_ORGA_ALL_ORGA_ID = 10001;
@@ -31,13 +30,12 @@ export function buildOrganizationUsers(databaseBuilder) {
3130
}
3231

3332
function _buildUser(databaseBuilder) {
34-
return function ({ id, firstName, lastName, email, rawPassword = DEFAULT_PASSWORD, cguVersion }) {
33+
return function ({ id, firstName, lastName, email, cguVersion }) {
3534
databaseBuilder.factory.buildUser.withRawPassword({
3635
id,
3736
firstName,
3837
lastName,
3938
email,
40-
rawPassword,
4139
cgu: true,
4240
});
4341

api/db/seeds/data/team-acces/build-pix-admin-roles.js

-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { PIX_ADMIN } from '../../../../src/authorization/domain/constants.js';
2-
import { DEFAULT_PASSWORD } from '../../../constants.js';
32

43
const { ROLES } = PIX_ADMIN;
54

@@ -8,7 +7,6 @@ function _buildCertifRole(databaseBuilder) {
87
firstName: 'Pix',
98
lastName: 'Certif',
109
email: 'pixcertif@example.net',
11-
rawPassword: DEFAULT_PASSWORD,
1210
role: ROLES.CERTIF,
1311
});
1412
}
@@ -18,7 +16,6 @@ function _buildMetierRole(databaseBuilder) {
1816
firstName: 'Pix',
1917
lastName: 'Metier',
2018
email: 'pixmetier@example.net',
21-
rawPassword: DEFAULT_PASSWORD,
2219
role: ROLES.METIER,
2320
});
2421
}
@@ -28,7 +25,6 @@ function _buildSupportRole(databaseBuilder) {
2825
firstName: 'Pix',
2926
lastName: 'Support',
3027
email: 'pixsupport@example.net',
31-
rawPassword: DEFAULT_PASSWORD,
3228
role: ROLES.SUPPORT,
3329
});
3430
}

api/db/seeds/data/team-acces/build-reset-password-users.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import { DEFAULT_PASSWORD } from '../../../constants.js';
2-
31
function _buildUserWithShouldChangePassword(databaseBuilder) {
42
databaseBuilder.factory.buildUser.withRawPassword({
53
firstName: 'Kaa',
64
lastName: 'Reboot',
75
email: 'change-password@example.net',
86
username: 'kaa.reboot',
9-
rawPassword: DEFAULT_PASSWORD,
107
shouldChangePassword: true,
118
cgu: false,
129
});

api/db/seeds/data/team-certification/data-builder.js

-12
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ async function _createScoCertificationCenter({ databaseBuilder }) {
9797
mustValidateTermsOfService: false,
9898
pixCertifTermsOfServiceAccepted: false,
9999
hasSeenAssessmentInstructions: false,
100-
rawPassword: 'pix123',
101-
shouldChangePassword: false,
102100
});
103101

104102
acceptPixOrgaTermsOfService(databaseBuilder, SCO_CERTIFICATION_MANAGING_STUDENTS_CERTIFICATION_CENTER_USER_ID);
@@ -140,8 +138,6 @@ async function _createV3PilotCertificationCenter({ databaseBuilder }) {
140138
mustValidateTermsOfService: false,
141139
pixCertifTermsOfServiceAccepted: false,
142140
hasSeenAssessmentInstructions: false,
143-
rawPassword: 'pix123',
144-
shouldChangePassword: false,
145141
});
146142

147143
acceptPixOrgaTermsOfService(databaseBuilder, V3_CERTIFICATION_CENTER_USER_ID);
@@ -186,8 +182,6 @@ async function _createProCertificationCenter({ databaseBuilder }) {
186182
mustValidateTermsOfService: false,
187183
pixCertifTermsOfServiceAccepted: false,
188184
hasSeenAssessmentInstructions: false,
189-
rawPassword: 'pix123',
190-
shouldChangePassword: false,
191185
});
192186

193187
acceptPixOrgaTermsOfService(databaseBuilder, PRO_ADMIN_CERTIFICATION_CENTER_USER_ID);
@@ -203,8 +197,6 @@ async function _createProCertificationCenter({ databaseBuilder }) {
203197
mustValidateTermsOfService: false,
204198
pixCertifTermsOfServiceAccepted: false,
205199
hasSeenAssessmentInstructions: false,
206-
rawPassword: 'pix123',
207-
shouldChangePassword: false,
208200
});
209201

210202
acceptPixOrgaTermsOfService(databaseBuilder, PRO_MEMBER_CERTIFICATION_CENTER_USER_ID);
@@ -237,8 +229,6 @@ async function _createScoOrganization({ databaseBuilder }) {
237229
mustValidateTermsOfService: false,
238230
pixCertifTermsOfServiceAccepted: false,
239231
hasSeenAssessmentInstructions: false,
240-
rawPassword: 'pix123',
241-
shouldChangePassword: false,
242232
});
243233

244234
acceptPixOrgaTermsOfService(databaseBuilder, SCO_CERTIFICATION_MANAGING_STUDENTS_ORGANIZATION_USER_ID);
@@ -270,8 +260,6 @@ async function _createProOrganization({ databaseBuilder }) {
270260
mustValidateTermsOfService: false,
271261
pixCertifTermsOfServiceAccepted: false,
272262
hasSeenAssessmentInstructions: false,
273-
rawPassword: 'pix123',
274-
shouldChangePassword: false,
275263
});
276264

277265
acceptPixOrgaTermsOfService(databaseBuilder, PRO_ORGANIZATION_USER_ID);

0 commit comments

Comments
 (0)