@proconnect-gouv/proconnect.identite 10.3.0 → 10.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/connectors/index.d.ts +224 -8
  3. package/dist/connectors/index.d.ts.map +1 -1
  4. package/dist/connectors/index.js +6 -0
  5. package/dist/managers/certification/match-identity-to-dirigeant.d.ts +44 -0
  6. package/dist/managers/certification/match-identity-to-dirigeant.d.ts.map +1 -0
  7. package/dist/managers/certification/match-identity-to-dirigeant.js +32 -0
  8. package/dist/managers/certification/process-certification-dirigeant.d.ts.map +1 -1
  9. package/dist/managers/certification/process-certification-dirigeant.js +1 -29
  10. package/dist/managers/organization/mark-domain-as-verified.d.ts.map +1 -1
  11. package/dist/managers/organization/mark-domain-as-verified.js +2 -0
  12. package/dist/repositories/moderation/get-moderation-by-id.d.ts.map +1 -1
  13. package/dist/repositories/moderation/get-moderation-by-id.js +1 -1
  14. package/dist/repositories/organization/find-users-by-organization.d.ts +28 -2
  15. package/dist/repositories/organization/find-users-by-organization.d.ts.map +1 -1
  16. package/dist/repositories/organization/find-users-by-organization.js +3 -6
  17. package/dist/repositories/recovery-words/create.d.ts +6 -0
  18. package/dist/repositories/recovery-words/create.d.ts.map +1 -0
  19. package/dist/repositories/recovery-words/create.js +14 -0
  20. package/dist/repositories/recovery-words/delete-all-by-user-id.d.ts +3 -0
  21. package/dist/repositories/recovery-words/delete-all-by-user-id.d.ts.map +1 -0
  22. package/dist/repositories/recovery-words/delete-all-by-user-id.js +11 -0
  23. package/dist/repositories/recovery-words/find-by-user-id.d.ts +3 -0
  24. package/dist/repositories/recovery-words/find-by-user-id.d.ts.map +1 -0
  25. package/dist/repositories/recovery-words/find-by-user-id.js +12 -0
  26. package/dist/repositories/recovery-words/index.d.ts +4 -0
  27. package/dist/repositories/recovery-words/index.d.ts.map +1 -0
  28. package/dist/repositories/recovery-words/index.js +4 -0
  29. package/dist/repositories/user/create.d.ts +28 -2
  30. package/dist/repositories/user/create.d.ts.map +1 -1
  31. package/dist/repositories/user/find-by-email.d.ts +28 -2
  32. package/dist/repositories/user/find-by-email.d.ts.map +1 -1
  33. package/dist/repositories/user/find-by-id.d.ts +28 -2
  34. package/dist/repositories/user/find-by-id.d.ts.map +1 -1
  35. package/dist/repositories/user/find-by-magic-link-token.d.ts +28 -2
  36. package/dist/repositories/user/find-by-magic-link-token.d.ts.map +1 -1
  37. package/dist/repositories/user/find-by-reset-password-token.d.ts +28 -2
  38. package/dist/repositories/user/find-by-reset-password-token.d.ts.map +1 -1
  39. package/dist/repositories/user/get-by-id.d.ts +27 -1
  40. package/dist/repositories/user/get-by-id.d.ts.map +1 -1
  41. package/dist/repositories/user/update.d.ts +27 -1
  42. package/dist/repositories/user/update.d.ts.map +1 -1
  43. package/dist/types/index.d.ts +1 -0
  44. package/dist/types/index.d.ts.map +1 -1
  45. package/dist/types/index.js +1 -0
  46. package/dist/types/recovery-words.d.ts +9 -0
  47. package/dist/types/recovery-words.d.ts.map +1 -0
  48. package/dist/types/recovery-words.js +1 -0
  49. package/dist/types/user.d.ts +29 -27
  50. package/dist/types/user.d.ts.map +1 -1
  51. package/dist/types/user.js +29 -1
  52. package/package.json +5 -5
  53. package/src/connectors/index.ts +10 -0
  54. package/src/managers/certification/match-identity-to-dirigeant.test.ts +62 -0
  55. package/src/managers/certification/match-identity-to-dirigeant.ts +40 -0
  56. package/src/managers/certification/process-certification-dirigeant.ts +1 -35
  57. package/src/managers/organization/mark-domain-as-verified.test.ts +174 -0
  58. package/src/managers/organization/mark-domain-as-verified.ts +2 -0
  59. package/src/repositories/moderation/get-moderation-by-id.test.ts +1 -1
  60. package/src/repositories/moderation/get-moderation-by-id.ts +1 -1
  61. package/src/repositories/organization/find-users-by-organization.ts +4 -12
  62. package/src/repositories/recovery-words/create.ts +28 -0
  63. package/src/repositories/recovery-words/delete-all-by-user-id.ts +20 -0
  64. package/src/repositories/recovery-words/find-by-user-id.ts +20 -0
  65. package/src/repositories/recovery-words/index.ts +5 -0
  66. package/src/types/index.ts +1 -0
  67. package/src/types/recovery-words.ts +9 -0
  68. package/src/types/user.ts +33 -27
@@ -63,6 +63,180 @@ describe("markDomainAsVerified", () => {
63
63
  ]);
64
64
  });
65
65
 
66
+ it("approving a domain should replace an existing rejection for the same domain", async () => {
67
+ await pg.sql`
68
+ INSERT INTO organizations
69
+ (id, cached_libelle, cached_nom_complet, siret, created_at, updated_at)
70
+ VALUES
71
+ (1, 'Night Lords', 'Night Lords Legion', '🦇', '4444-04-04', '4444-04-04')
72
+ ;
73
+ `;
74
+ await pg.sql`
75
+ INSERT INTO email_domains (organization_id, domain, verification_type)
76
+ VALUES (1, 'nightlords.world', 'refused')
77
+ `;
78
+
79
+ await markDomainAsVerified({
80
+ domain: "nightlords.world",
81
+ domain_verification_type: "verified",
82
+ organization_id: 1,
83
+ });
84
+
85
+ const { rows: emailDomains } = await pg.sql`
86
+ SELECT domain, verification_type
87
+ FROM email_domains
88
+ WHERE organization_id = 1
89
+ ORDER BY verification_type
90
+ `;
91
+ assert.deepEqual(emailDomains, [
92
+ { domain: "nightlords.world", verification_type: "verified" },
93
+ ]);
94
+ });
95
+
96
+ it("rejecting a domain should replace an existing approval for the same domain", async () => {
97
+ await pg.sql`
98
+ INSERT INTO organizations
99
+ (id, cached_libelle, cached_nom_complet, siret, created_at, updated_at)
100
+ VALUES
101
+ (1, 'Thousand Sons', 'Thousand Sons Legion', '📖', '4444-04-04', '4444-04-04')
102
+ ;
103
+ `;
104
+ await pg.sql`
105
+ INSERT INTO email_domains (organization_id, domain, verification_type)
106
+ VALUES (1, 'thousandsons.world', 'verified')
107
+ `;
108
+
109
+ await markDomainAsVerified({
110
+ domain: "thousandsons.world",
111
+ domain_verification_type: "refused",
112
+ organization_id: 1,
113
+ });
114
+
115
+ const { rows: emailDomains } = await pg.sql`
116
+ SELECT domain, verification_type
117
+ FROM email_domains
118
+ WHERE organization_id = 1
119
+ ORDER BY verification_type
120
+ `;
121
+ assert.deepEqual(emailDomains, [
122
+ { domain: "thousandsons.world", verification_type: "refused" },
123
+ ]);
124
+ });
125
+
126
+ it("should reject a pending domain", async () => {
127
+ await pg.sql`
128
+ INSERT INTO organizations
129
+ (id, cached_libelle, cached_nom_complet, siret, created_at, updated_at)
130
+ VALUES
131
+ (1, 'Black Legion', 'Black Legion Warband', '👁️', '4444-04-04', '4444-04-04')
132
+ ;
133
+ `;
134
+ await pg.sql`
135
+ INSERT INTO email_domains (organization_id, domain, verification_type)
136
+ VALUES (1, 'blacklegion.world', 'not_verified_yet')
137
+ `;
138
+
139
+ await markDomainAsVerified({
140
+ domain: "blacklegion.world",
141
+ domain_verification_type: "refused",
142
+ organization_id: 1,
143
+ });
144
+
145
+ const { rows: emailDomains } = await pg.sql`
146
+ SELECT domain, verification_type
147
+ FROM email_domains
148
+ WHERE organization_id = 1
149
+ ORDER BY verification_type
150
+ `;
151
+ assert.deepEqual(emailDomains, [
152
+ { domain: "blacklegion.world", verification_type: "refused" },
153
+ ]);
154
+ });
155
+
156
+ it("rejecting a domain should not demote an already domain-verified member", async () => {
157
+ await pg.sql`
158
+ INSERT INTO organizations
159
+ (id, cached_libelle, cached_nom_complet, siret, created_at, updated_at)
160
+ VALUES
161
+ (1, 'Word Bearers', 'Word Bearers Legion', '🔥', '4444-04-04', '4444-04-04')
162
+ ;
163
+ `;
164
+ await pg.sql`
165
+ INSERT INTO users (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
166
+ VALUES (1, 'lorgar.aurelian@wordbearers.world', '4444-04-04', '4444-04-04', 'Lorgar', 'Aurelian', '0', 'Primarque')
167
+ `;
168
+ await pg.sql`
169
+ INSERT INTO users_organizations
170
+ (user_id, organization_id, created_at, updated_at, is_external, verification_type)
171
+ VALUES
172
+ (1, 1, '4444-04-04', '4444-04-04', false, 'domain')
173
+ ;
174
+ `;
175
+ await pg.sql`
176
+ INSERT INTO email_domains (organization_id, domain, verification_type)
177
+ VALUES (1, 'wordbearers.world', 'not_verified_yet')
178
+ `;
179
+
180
+ await markDomainAsVerified({
181
+ domain: "wordbearers.world",
182
+ domain_verification_type: "refused",
183
+ organization_id: 1,
184
+ });
185
+
186
+ const { rows: userLinks } = await pg.sql`
187
+ SELECT user_id, verification_type
188
+ FROM users_organizations
189
+ WHERE organization_id = 1
190
+ `;
191
+ assert.deepEqual(userLinks, [{ user_id: 1, verification_type: "domain" }]);
192
+ });
193
+
194
+ it("approving a domain should only promote eligible member links", async () => {
195
+ await pg.sql`
196
+ INSERT INTO organizations
197
+ (id, cached_libelle, cached_nom_complet, siret, created_at, updated_at)
198
+ VALUES
199
+ (1, 'Emperor''s Children', 'Emperor''s Children Legion', '🎭', '4444-04-04', '4444-04-04')
200
+ ;
201
+ `;
202
+ await pg.sql`
203
+ INSERT INTO users (id, email, created_at, updated_at, given_name, family_name, phone_number, job)
204
+ VALUES
205
+ (1, 'fulgrim@emperorschildren.world', '4444-04-04', '4444-04-04', 'Fulgrim', 'Primarque', '0', 'Primarque'),
206
+ (2, 'lucius@emperorschildren.world', '4444-04-04', '4444-04-04', 'Lucius', 'The Eternal', '0', 'Champion')
207
+ ;
208
+ `;
209
+ await pg.sql`
210
+ INSERT INTO users_organizations
211
+ (user_id, organization_id, created_at, updated_at, is_external, verification_type)
212
+ VALUES
213
+ (1, 1, '4444-04-04', '4444-04-04', false, 'no_verification_means_for_entreprise_unipersonnelle'),
214
+ (2, 1, '4444-04-04', '4444-04-04', false, 'organization_dirigeant')
215
+ ;
216
+ `;
217
+ await pg.sql`
218
+ INSERT INTO email_domains (organization_id, domain, verification_type)
219
+ VALUES (1, 'emperorschildren.world', 'not_verified_yet')
220
+ `;
221
+
222
+ await markDomainAsVerified({
223
+ domain: "emperorschildren.world",
224
+ domain_verification_type: "verified",
225
+ organization_id: 1,
226
+ });
227
+
228
+ const { rows: userLinks } = await pg.sql`
229
+ SELECT user_id, verification_type
230
+ FROM users_organizations
231
+ WHERE organization_id = 1
232
+ ORDER BY user_id
233
+ `;
234
+ assert.deepEqual(userLinks, [
235
+ { user_id: 1, verification_type: "domain" },
236
+ { user_id: 2, verification_type: "organization_dirigeant" },
237
+ ]);
238
+ });
239
+
66
240
  it("❎ throws NotFoundError for unknown organization", async () => {
67
241
  await assert.rejects(
68
242
  markDomainAsVerified({
@@ -80,6 +80,7 @@ export function markDomainAsVerifiedFactory(context: Context) {
80
80
  domain_verification_types: [
81
81
  ...EmailDomainApprovedVerificationValues,
82
82
  ...EmailDomainPendingVerificationValues,
83
+ ...EmailDomainRejectedVerificationValues,
83
84
  ],
84
85
  });
85
86
  return email_domains.addDomain({
@@ -103,6 +104,7 @@ export function markDomainAsVerifiedFactory(context: Context) {
103
104
  organization_id,
104
105
  domain,
105
106
  domain_verification_types: [
107
+ ...EmailDomainApprovedVerificationValues,
106
108
  ...EmailDomainPendingVerificationValues,
107
109
  ...EmailDomainRejectedVerificationValues,
108
110
  ],
@@ -1,7 +1,7 @@
1
1
  //
2
2
 
3
+ import { ModerationNotFoundError } from "#src/errors";
3
4
  import { emptyDatabase, migrate, pg } from "#testing";
4
- import { ModerationNotFoundError } from "@proconnect-gouv/proconnect.identite/errors";
5
5
  import assert from "node:assert/strict";
6
6
  import { before, beforeEach, suite, test } from "node:test";
7
7
  import { getModerationByIdFactory } from "./get-moderation-by-id.js";
@@ -1,7 +1,7 @@
1
1
  //
2
2
 
3
+ import { ModerationNotFoundError } from "#src/errors";
3
4
  import { type DatabaseContext, type Moderation } from "#src/types";
4
- import { ModerationNotFoundError } from "@proconnect-gouv/proconnect.identite/errors";
5
5
  import { findModerationByIdFactory } from "./find-moderation-by-id.js";
6
6
 
7
7
  //
@@ -10,16 +10,9 @@ import type { QueryResult } from "pg";
10
10
  //
11
11
 
12
12
  export function findUsersByOrganizationFactory({ pg }: DatabaseContext) {
13
- return async function findUsersByOrganization(
14
- organization_id: number,
15
- additionalWhereClause: string = "",
16
- additionalParams: any[] = [],
17
- ) {
18
- const connection = pg;
19
- const baseParams = [organization_id];
20
-
13
+ return async function findUsersByOrganization(organization_id: number) {
21
14
  const { rows }: QueryResult<User & BaseUserOrganizationLink> =
22
- await connection.query(
15
+ await pg.query(
23
16
  `
24
17
  SELECT u.*,
25
18
  uo.is_external,
@@ -28,9 +21,8 @@ export function findUsersByOrganizationFactory({ pg }: DatabaseContext) {
28
21
  uo.has_been_greeted
29
22
  FROM users u
30
23
  INNER JOIN users_organizations AS uo ON uo.user_id = u.id
31
- WHERE uo.organization_id = $1
32
- ${additionalWhereClause}`,
33
- [...baseParams, ...additionalParams],
24
+ WHERE uo.organization_id = $1`,
25
+ [organization_id],
34
26
  );
35
27
 
36
28
  return rows;
@@ -0,0 +1,28 @@
1
+ //
2
+
3
+ import type { DatabaseContext, RecoveryWords } from "#src/types";
4
+ import { type QueryResult } from "pg";
5
+
6
+ //
7
+
8
+ export function createRecoveryWordsFactory({ pg }: DatabaseContext) {
9
+ return async function createRecoveryWords({
10
+ user_id,
11
+ hashed_words,
12
+ }: {
13
+ user_id: number;
14
+ hashed_words: string;
15
+ }) {
16
+ const { rows }: QueryResult<RecoveryWords> = await pg.query(
17
+ `
18
+ INSERT INTO recovery_words
19
+ (user_id, hashed_words)
20
+ VALUES
21
+ ($1, $2)
22
+ RETURNING *;`,
23
+ [user_id, hashed_words],
24
+ );
25
+
26
+ return rows.shift()!;
27
+ };
28
+ }
@@ -0,0 +1,20 @@
1
+ //
2
+
3
+ import type { DatabaseContext } from "#src/types";
4
+ import type { QueryResult } from "pg";
5
+
6
+ //
7
+
8
+ export function deleteRecoveryWordsByUserIdFactory({ pg }: DatabaseContext) {
9
+ return async function deleteRecoveryWordsByUserId(user_id: number) {
10
+ const { rowCount, affectedRows } = (await pg.query(
11
+ `
12
+ DELETE FROM recovery_words
13
+ WHERE user_id = $1
14
+ RETURNING *`,
15
+
16
+ [user_id],
17
+ )) as QueryResult & { affectedRows?: number };
18
+ return (affectedRows ?? rowCount ?? 0) > 0;
19
+ };
20
+ }
@@ -0,0 +1,20 @@
1
+ //
2
+
3
+ import type { DatabaseContext, RecoveryWords } from "#src/types";
4
+ import { type QueryResult } from "pg";
5
+
6
+ //
7
+
8
+ export function findRecoveryWordsByUserIdFactory({ pg }: DatabaseContext) {
9
+ return async function findRecoveryWordsByUserId(user_id: number) {
10
+ const { rows }: QueryResult<RecoveryWords> = await pg.query(
11
+ `
12
+ SELECT *
13
+ FROM recovery_words
14
+ WHERE user_id = $1;`,
15
+ [user_id],
16
+ );
17
+
18
+ return rows.shift();
19
+ };
20
+ }
@@ -0,0 +1,5 @@
1
+ //
2
+
3
+ export * from "./create.js";
4
+ export * from "./delete-all-by-user-id.js";
5
+ export * from "./find-by-user-id.js";
@@ -12,5 +12,6 @@ export * from "./official-contact-email-verification.js";
12
12
  export * from "./oidc-client.js";
13
13
  export * from "./organization-info.js";
14
14
  export * from "./organization.js";
15
+ export * from "./recovery-words.js";
15
16
  export * from "./user-organization-link.js";
16
17
  export * from "./user.js";
@@ -0,0 +1,9 @@
1
+ export interface BaseRecoveryWords {
2
+ hashed_words: string;
3
+ }
4
+
5
+ export interface RecoveryWords extends BaseRecoveryWords {
6
+ id: number;
7
+ user_id: number;
8
+ created_at: Date;
9
+ }
package/src/types/user.ts CHANGED
@@ -1,29 +1,35 @@
1
1
  //
2
2
 
3
- export interface User {
4
- created_at: Date;
5
- current_challenge: string | null;
6
- email_verified_at: Date | null;
7
- email_verified: boolean;
8
- email: string;
9
- encrypted_password: string | null;
10
- encrypted_totp_key: string | null;
11
- family_name: string | null;
12
- force_2fa: boolean;
13
- given_name: string | null;
14
- id: number;
15
- job: string | null;
16
- last_sign_in_at: Date | null;
17
- magic_link_sent_at: Date | null;
18
- magic_link_token: string | null;
19
- needs_inclusionconnect_onboarding_help: boolean;
20
- needs_inclusionconnect_welcome_page: boolean;
21
- phone_number: string | null;
22
- reset_password_sent_at: Date | null;
23
- reset_password_token: string | null;
24
- sign_in_count: number;
25
- totp_key_verified_at: Date | null;
26
- updated_at: Date;
27
- verify_email_sent_at: Date | null;
28
- verify_email_token: string | null;
29
- }
3
+ import { z } from "zod";
4
+
5
+ //
6
+
7
+ export const UserSchema = z.object({
8
+ created_at: z.date(),
9
+ current_challenge: z.string().nullable(),
10
+ email: z.string(),
11
+ email_verified: z.boolean(),
12
+ email_verified_at: z.date().nullable(),
13
+ encrypted_password: z.string().nullable(),
14
+ encrypted_totp_key: z.string().nullable(),
15
+ family_name: z.string().nullable(),
16
+ force_2fa: z.boolean(),
17
+ given_name: z.string().nullable(),
18
+ id: z.number(),
19
+ job: z.string().nullable(),
20
+ last_sign_in_at: z.date().nullable(),
21
+ magic_link_sent_at: z.date().nullable(),
22
+ magic_link_token: z.string().nullable(),
23
+ needs_inclusionconnect_onboarding_help: z.boolean(),
24
+ needs_inclusionconnect_welcome_page: z.boolean(),
25
+ phone_number: z.string().nullable(),
26
+ reset_password_sent_at: z.date().nullable(),
27
+ reset_password_token: z.string().nullable(),
28
+ sign_in_count: z.number(),
29
+ totp_key_verified_at: z.date().nullable(),
30
+ updated_at: z.date(),
31
+ verify_email_sent_at: z.date().nullable(),
32
+ verify_email_token: z.string().nullable(),
33
+ });
34
+
35
+ export type User = z.output<typeof UserSchema>;