@proconnect-gouv/proconnect.identite.runtime 0.4.0 → 0.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @proconnect-gouv/proconnect.identite.runtime
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#2407](https://github.com/proconnect-gouv/proconnect-identite/pull/2407) [`c801a10`](https://github.com/proconnect-gouv/proconnect-identite/commit/c801a108fd71b38f179dd2e21f62fc440e56fee6) Thanks [@douglasduteil](https://github.com/douglasduteil)! - Ajoute la commande `updatePersonalInformations` et le schéma `user.info_changed` : le changement de nom, téléphone ou fonction est journalisé avec les valeurs précédentes, et une demande sans changement n'écrit rien.
8
+
9
+ - [#2408](https://github.com/proconnect-gouv/proconnect-identite/pull/2408) [`478e680`](https://github.com/proconnect-gouv/proconnect-identite/commit/478e680e7f83f4c24f37e84f9134856dbaa753a2) Thanks [@douglasduteil](https://github.com/douglasduteil)! - Ajoute la commande `changePassword` et le schéma `user.password_changed` : un mot de passe défini à la réinitialisation ou à l'inscription est journalisé avec l'état précédent, sans jamais écrire le mot de passe ni son empreinte.
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [[`c1167a1`](https://github.com/proconnect-gouv/proconnect-identite/commit/c1167a150b16060ca704059e98040414bbf7a009), [`33604f3`](https://github.com/proconnect-gouv/proconnect-identite/commit/33604f32fb4393ec60e413f0b7bace8e94ac22fc), [`c1167a1`](https://github.com/proconnect-gouv/proconnect-identite/commit/c1167a150b16060ca704059e98040414bbf7a009)]:
14
+ - @proconnect-gouv/proconnect.identite.database@2.0.0
15
+ - @proconnect-gouv/proconnect.identite@15.0.0
16
+
3
17
  ## 0.4.0
4
18
 
5
19
  ### Minor Changes
@@ -15,6 +15,22 @@ export declare const activityContextSchemas: {
15
15
  }, import("zod/v4/core").$strip>>;
16
16
  }, import("zod/v4/core").$strip>;
17
17
  }, import("zod/v4/core").$strip>;
18
+ "user.info_changed": import("zod").ZodObject<{
19
+ inputs: import("zod").ZodObject<{
20
+ family_name: import("zod").ZodNullable<import("zod").ZodString>;
21
+ given_name: import("zod").ZodNullable<import("zod").ZodString>;
22
+ job: import("zod").ZodNullable<import("zod").ZodString>;
23
+ phone_number: import("zod").ZodNullable<import("zod").ZodString>;
24
+ }, import("zod/v4/core").$strip>;
25
+ outcome: import("zod").ZodObject<{
26
+ previous: import("zod").ZodObject<{
27
+ family_name: import("zod").ZodNullable<import("zod").ZodString>;
28
+ given_name: import("zod").ZodNullable<import("zod").ZodString>;
29
+ job: import("zod").ZodNullable<import("zod").ZodString>;
30
+ phone_number: import("zod").ZodNullable<import("zod").ZodString>;
31
+ }, import("zod/v4/core").$strip>;
32
+ }, import("zod/v4/core").$strip>;
33
+ }, import("zod/v4/core").$strip>;
18
34
  "user.mfa_reset": import("zod").ZodObject<{
19
35
  inputs: import("zod").ZodObject<{
20
36
  user_id: import("zod").ZodNumber;
@@ -29,6 +45,19 @@ export declare const activityContextSchemas: {
29
45
  }, import("zod/v4/core").$strip>;
30
46
  }, import("zod/v4/core").$strip>;
31
47
  }, import("zod/v4/core").$strip>;
48
+ "user.password_changed": import("zod").ZodObject<{
49
+ inputs: import("zod").ZodObject<{
50
+ source: import("zod").ZodEnum<{
51
+ reset_password: "reset_password";
52
+ signup: "signup";
53
+ }>;
54
+ }, import("zod/v4/core").$strip>;
55
+ outcome: import("zod").ZodObject<{
56
+ previous: import("zod").ZodObject<{
57
+ email_verified: import("zod").ZodBoolean;
58
+ }, import("zod/v4/core").$strip>;
59
+ }, import("zod/v4/core").$strip>;
60
+ }, import("zod/v4/core").$strip>;
32
61
  "moderation.opened": import("zod").ZodObject<{
33
62
  inputs: import("zod").ZodObject<{
34
63
  sp_name: import("zod").ZodNullable<import("zod").ZodString>;
@@ -1 +1 @@
1
- {"version":3,"file":"activity-context-schemas.d.ts","sourceRoot":"","sources":["../../src/activity/activity-context-schemas.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAI7C,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAMlC,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC"}
1
+ {"version":3,"file":"activity-context-schemas.d.ts","sourceRoot":"","sources":["../../src/activity/activity-context-schemas.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAI7C,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAMlC,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC"}
@@ -21,6 +21,17 @@ export declare function createIdentiteRuntime<OwnContexts extends ContextMap & {
21
21
  organization_id: number;
22
22
  user_id: number;
23
23
  }>;
24
+ changePassword: ({ encrypted_password, source, user_id, }: {
25
+ encrypted_password: string;
26
+ source: "reset_password" | "signup";
27
+ user_id: number;
28
+ }) => Promise<{
29
+ kind: "password_changed";
30
+ activity_id: number;
31
+ previous: {
32
+ email_verified: boolean;
33
+ };
34
+ }>;
24
35
  connectFranceConnect: ({ user_id, user_info, }: {
25
36
  user_id: number;
26
37
  user_info: import("@proconnect-gouv/proconnect.identite/types").FranceConnectUserInfoResponse;
@@ -143,6 +154,31 @@ export declare function createIdentiteRuntime<OwnContexts extends ContextMap & {
143
154
  organization_id: number;
144
155
  user_id: number;
145
156
  }>;
157
+ updatePersonalInformations: ({ information, user_id, }: {
158
+ information: Partial<Pick<import("@proconnect-gouv/proconnect.identite/types").User, "family_name" | "given_name" | "job" | "phone_number">>;
159
+ user_id: number;
160
+ }) => Promise<{
161
+ kind: "info_changed";
162
+ activity_id: number;
163
+ next: {
164
+ given_name: string | null;
165
+ family_name: string | null;
166
+ phone_number: string | null;
167
+ job: string | null;
168
+ };
169
+ previous: {
170
+ given_name: string | null;
171
+ family_name: string | null;
172
+ phone_number: string | null;
173
+ job: string | null;
174
+ };
175
+ } | {
176
+ kind: "info_unchanged";
177
+ user_id: number;
178
+ } | {
179
+ kind: "user_not_found";
180
+ user_id: number;
181
+ }>;
146
182
  };
147
183
  };
148
184
  //# sourceMappingURL=create-identite-runtime.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"create-identite-runtime.d.ts","sourceRoot":"","sources":["../../src/runtime/create-identite-runtime.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,gBAAgB,EAChB,UAAU,EACV,eAAe,EACf,KAAK,EACL,UAAU,EACV,cAAc,EACf,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAiBzD,wBAAgB,qBAAqB,CACnC,WAAW,SAAS,UAAU,GAAG;KAC9B,MAAM,IAAI,MAAM,gBAAgB,CAAC,CAAC,EAAE,KAAK;CAC3C,GAAG,EAAE,EACN,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,cAAc,EAAE,OAAO,CAAC;cAE9B,KAAK;;uBADf,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;sBAjBQ,gCACI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAWc,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;qBAoBlB,mBAAmB,YACb,UAAU,CAAC,WAAW,CAAC,GAAG,eAAe;;;;;;;;sBAhCxD,kCAAmC,WACpC;;;;;;uBAmBK,CAAC;;;;;;;8BAEqB,CAAC;6BAA0B,CAAC;;;;;;;;;;;;;;;;;;EAiB1D"}
1
+ {"version":3,"file":"create-identite-runtime.d.ts","sourceRoot":"","sources":["../../src/runtime/create-identite-runtime.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,gBAAgB,EAChB,UAAU,EACV,eAAe,EACf,KAAK,EACL,UAAU,EACV,cAAc,EACf,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAsBzD,wBAAgB,qBAAqB,CACnC,WAAW,SAAS,UAAU,GAAG;KAC9B,MAAM,IAAI,MAAM,gBAAgB,CAAC,CAAC,EAAE,KAAK;CAC3C,GAAG,EAAE,EACN,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,cAAc,EAAE,OAAO,CAAC;cAE9B,KAAK;;uBAJwB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;sBAnB/B,gCACI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAWU,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;qBA0Bd,mBAAmB,YACb,UAAU,CAAC,WAAW,CAAC,GAAG,eAAe;;;;;;;;sBAtCxD,kCAAmC,WACpC;;;;;;uBAuBI,CAAA;;;;;;;8BAE2B,CAAC;6BAA0B,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAwB9D"}
@@ -4,13 +4,14 @@ import { connectFranceConnectFactory, disconnectFranceConnectFactory, } from "#s
4
4
  import { insertActivity } from "#src/journal";
5
5
  import { addMembershipFactory, removeMembershipFactory, updateMembershipFactory, } from "#src/memberships";
6
6
  import { openModerationFactory } from "#src/moderations";
7
- import { deleteUserFactory, resetMfaFactory } from "#src/users";
7
+ import { changePasswordFactory, deleteUserFactory, resetMfaFactory, updatePersonalInformationsFactory, } from "#src/users";
8
8
  //
9
9
  export function createIdentiteRuntime({ app, db }) {
10
10
  return {
11
11
  as(actor) {
12
12
  return {
13
13
  addMembership: addMembershipFactory({ actor, app, db }),
14
+ changePassword: changePasswordFactory({ actor, app, db }),
14
15
  connectFranceConnect: connectFranceConnectFactory({ actor, app, db }),
15
16
  deleteUser: deleteUserFactory({ actor, app, db }),
16
17
  disconnectFranceConnect: disconnectFranceConnectFactory({
@@ -24,6 +25,11 @@ export function createIdentiteRuntime({ app, db }) {
24
25
  record: (tx, activity) => insertActivity(tx, { ...activity, actor, app }),
25
26
  resetMfa: resetMfaFactory({ actor, app, db }),
26
27
  updateMembership: updateMembershipFactory({ actor, app, db }),
28
+ updatePersonalInformations: updatePersonalInformationsFactory({
29
+ actor,
30
+ app,
31
+ db,
32
+ }),
27
33
  };
28
34
  },
29
35
  };
@@ -15,6 +15,22 @@ export declare const userActivities: {
15
15
  }, z.core.$strip>>;
16
16
  }, z.core.$strip>;
17
17
  }, z.core.$strip>;
18
+ "user.info_changed": z.ZodObject<{
19
+ inputs: z.ZodObject<{
20
+ family_name: z.ZodNullable<z.ZodString>;
21
+ given_name: z.ZodNullable<z.ZodString>;
22
+ job: z.ZodNullable<z.ZodString>;
23
+ phone_number: z.ZodNullable<z.ZodString>;
24
+ }, z.core.$strip>;
25
+ outcome: z.ZodObject<{
26
+ previous: z.ZodObject<{
27
+ family_name: z.ZodNullable<z.ZodString>;
28
+ given_name: z.ZodNullable<z.ZodString>;
29
+ job: z.ZodNullable<z.ZodString>;
30
+ phone_number: z.ZodNullable<z.ZodString>;
31
+ }, z.core.$strip>;
32
+ }, z.core.$strip>;
33
+ }, z.core.$strip>;
18
34
  "user.mfa_reset": z.ZodObject<{
19
35
  inputs: z.ZodObject<{
20
36
  user_id: z.ZodNumber;
@@ -29,5 +45,18 @@ export declare const userActivities: {
29
45
  }, z.core.$strip>;
30
46
  }, z.core.$strip>;
31
47
  }, z.core.$strip>;
48
+ "user.password_changed": z.ZodObject<{
49
+ inputs: z.ZodObject<{
50
+ source: z.ZodEnum<{
51
+ reset_password: "reset_password";
52
+ signup: "signup";
53
+ }>;
54
+ }, z.core.$strip>;
55
+ outcome: z.ZodObject<{
56
+ previous: z.ZodObject<{
57
+ email_verified: z.ZodBoolean;
58
+ }, z.core.$strip>;
59
+ }, z.core.$strip>;
60
+ }, z.core.$strip>;
32
61
  };
33
62
  //# sourceMappingURL=activities.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"activities.d.ts","sourceRoot":"","sources":["../../src/users/activities.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAIzB,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgC1B,CAAC"}
1
+ {"version":3,"file":"activities.d.ts","sourceRoot":"","sources":["../../src/users/activities.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAWzB,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8C1B,CAAC"}
@@ -1,6 +1,12 @@
1
1
  //
2
2
  import * as z from "zod";
3
3
  //
4
+ const userInformation = z.object({
5
+ family_name: z.string().nullable(),
6
+ given_name: z.string().nullable(),
7
+ job: z.string().nullable(),
8
+ phone_number: z.string().nullable(),
9
+ });
4
10
  export const userActivities = {
5
11
  "user.deleted": z.object({
6
12
  inputs: z.object({
@@ -17,6 +23,12 @@ export const userActivities = {
17
23
  })),
18
24
  }),
19
25
  }),
26
+ "user.info_changed": z.object({
27
+ inputs: userInformation,
28
+ outcome: z.object({
29
+ previous: userInformation,
30
+ }),
31
+ }),
20
32
  "user.mfa_reset": z.object({
21
33
  inputs: z.object({
22
34
  user_id: z.number(),
@@ -31,4 +43,12 @@ export const userActivities = {
31
43
  }),
32
44
  }),
33
45
  }),
46
+ "user.password_changed": z.object({
47
+ inputs: z.object({
48
+ source: z.enum(["reset_password", "signup"]),
49
+ }),
50
+ outcome: z.object({
51
+ previous: z.object({ email_verified: z.boolean() }),
52
+ }),
53
+ }),
34
54
  };
@@ -0,0 +1,16 @@
1
+ import type { RuntimeContext } from "#src/activity";
2
+ type PasswordSource = "reset_password" | "signup";
3
+ type ChangePasswordOutcome = {
4
+ kind: "password_changed";
5
+ activity_id: number;
6
+ previous: {
7
+ email_verified: boolean;
8
+ };
9
+ };
10
+ export declare function changePasswordFactory({ actor, app, db }: RuntimeContext): ({ encrypted_password, source, user_id, }: {
11
+ encrypted_password: string;
12
+ source: PasswordSource;
13
+ user_id: number;
14
+ }) => Promise<ChangePasswordOutcome>;
15
+ export {};
16
+ //# sourceMappingURL=change-password.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"change-password.d.ts","sourceRoot":"","sources":["../../src/users/change-password.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAoB,cAAc,EAAE,MAAM,eAAe,CAAC;AAQtE,KAAK,cAAc,GAAG,gBAAgB,GAAG,QAAQ,CAAC;AAElD,KAAK,qBAAqB,GAAG;IAC3B,IAAI,EAAE,kBAAkB,CAAC;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE;QAAE,cAAc,EAAE,OAAO,CAAA;KAAE,CAAC;CACvC,CAAC;AAEF,wBAAgB,qBAAqB,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,cAAc,IACvC,0CAI5B;IACD,kBAAkB,EAAE,MAAM,CAAC;IAC3B,MAAM,EAAE,cAAc,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;CACjB,KAAG,OAAO,CAAC,qBAAqB,CAAC,CAoCnC"}
@@ -0,0 +1,38 @@
1
+ //
2
+ import { insertActivity } from "#src/journal";
3
+ import { users } from "@proconnect-gouv/proconnect.identite.database/drizzle";
4
+ import { UserNotFoundError } from "@proconnect-gouv/proconnect.identite/errors";
5
+ import { eq, sql } from "drizzle-orm";
6
+ export function changePasswordFactory({ actor, app, db }) {
7
+ return function changePassword({ encrypted_password, source, user_id, }) {
8
+ return db.transaction(async (tx) => {
9
+ const [previous] = await tx
10
+ .select({ email_verified: users.email_verified })
11
+ .from(users)
12
+ .where(eq(users.id, user_id))
13
+ .for("update");
14
+ if (!previous) {
15
+ throw new UserNotFoundError(`user ${user_id} not found`);
16
+ }
17
+ await tx
18
+ .update(users)
19
+ .set({
20
+ email_verified: source === "reset_password",
21
+ email_verified_at: source === "reset_password" ? sql `now()` : null,
22
+ encrypted_password,
23
+ reset_password_sent_at: source === "reset_password" ? null : undefined,
24
+ reset_password_token: source === "reset_password" ? null : undefined,
25
+ updated_at: sql `now()`,
26
+ })
27
+ .where(eq(users.id, user_id));
28
+ const { activity_id } = await insertActivity(tx, {
29
+ action: "user.password_changed",
30
+ actor,
31
+ app,
32
+ context: { inputs: { source }, outcome: { previous } },
33
+ user_id,
34
+ });
35
+ return { kind: "password_changed", activity_id, previous };
36
+ });
37
+ };
38
+ }
@@ -1,4 +1,6 @@
1
1
  export * from "./activities.js";
2
+ export * from "./change-password.js";
2
3
  export * from "./delete-user.js";
3
4
  export * from "./reset-mfa.js";
5
+ export * from "./update-personal-informations.js";
4
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/users/index.ts"],"names":[],"mappings":"AAEA,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/users/index.ts"],"names":[],"mappings":"AAEA,cAAc,iBAAiB,CAAC;AAChC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mCAAmC,CAAC"}
@@ -1,4 +1,6 @@
1
1
  //
2
2
  export * from "./activities.js";
3
+ export * from "./change-password.js";
3
4
  export * from "./delete-user.js";
4
5
  export * from "./reset-mfa.js";
6
+ export * from "./update-personal-informations.js";
@@ -0,0 +1,21 @@
1
+ import type { RuntimeContext } from "#src/activity";
2
+ import type { User } from "@proconnect-gouv/proconnect.identite/types";
3
+ type PersonalInformation = Pick<User, "family_name" | "given_name" | "job" | "phone_number">;
4
+ type UpdatePersonalInformationsOutcome = {
5
+ kind: "info_changed";
6
+ activity_id: number;
7
+ next: PersonalInformation;
8
+ previous: PersonalInformation;
9
+ } | {
10
+ kind: "info_unchanged";
11
+ user_id: number;
12
+ } | {
13
+ kind: "user_not_found";
14
+ user_id: number;
15
+ };
16
+ export declare function updatePersonalInformationsFactory({ actor, app, db, }: RuntimeContext): ({ information, user_id, }: {
17
+ information: Partial<Pick<User, "family_name" | "given_name" | "job" | "phone_number">>;
18
+ user_id: number;
19
+ }) => Promise<UpdatePersonalInformationsOutcome>;
20
+ export {};
21
+ //# sourceMappingURL=update-personal-informations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"update-personal-informations.d.ts","sourceRoot":"","sources":["../../src/users/update-personal-informations.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAoB,cAAc,EAAE,MAAM,eAAe,CAAC;AAGtE,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,4CAA4C,CAAC;AAKvE,KAAK,mBAAmB,GAAG,IAAI,CAC7B,IAAI,EACJ,aAAa,GAAG,YAAY,GAAG,KAAK,GAAG,cAAc,CACtD,CAAC;AAEF,KAAK,iCAAiC,GAClC;IACE,IAAI,EAAE,cAAc,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,mBAAmB,CAAC;IAC1B,QAAQ,EAAE,mBAAmB,CAAC;CAC/B,GACD;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAC3C;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAEhD,wBAAgB,iCAAiC,CAAC,EAChD,KAAK,EACL,GAAG,EACH,EAAE,GACH,EAAE,cAAc,IAC4B,2BAGxC;IACD,WAAW,EAAE,OAAO,CAClB,IAAI,CAAC,IAAI,EAAE,aAAa,GAAG,YAAY,GAAG,KAAK,GAAG,cAAc,CAAC,CAClE,CAAC;IACF,OAAO,EAAE,MAAM,CAAC;CACjB,KAAG,OAAO,CAAC,iCAAiC,CAAC,CA0D/C"}
@@ -0,0 +1,53 @@
1
+ //
2
+ import { insertActivity } from "#src/journal";
3
+ import { users } from "@proconnect-gouv/proconnect.identite.database/drizzle";
4
+ import { eq, sql } from "drizzle-orm";
5
+ export function updatePersonalInformationsFactory({ actor, app, db, }) {
6
+ return function updatePersonalInformations({ information, user_id, }) {
7
+ return db.transaction(async (tx) => {
8
+ const [previous] = await tx
9
+ .select({
10
+ family_name: users.family_name,
11
+ given_name: users.given_name,
12
+ job: users.job,
13
+ phone_number: users.phone_number,
14
+ })
15
+ .from(users)
16
+ .where(eq(users.id, user_id))
17
+ .for("update");
18
+ if (!previous) {
19
+ return { kind: "user_not_found", user_id };
20
+ }
21
+ const next = {
22
+ family_name: information.family_name !== undefined
23
+ ? information.family_name
24
+ : previous.family_name,
25
+ given_name: information.given_name !== undefined
26
+ ? information.given_name
27
+ : previous.given_name,
28
+ job: information.job !== undefined ? information.job : previous.job,
29
+ phone_number: information.phone_number !== undefined
30
+ ? information.phone_number
31
+ : previous.phone_number,
32
+ };
33
+ if (next.family_name === previous.family_name &&
34
+ next.given_name === previous.given_name &&
35
+ next.job === previous.job &&
36
+ next.phone_number === previous.phone_number) {
37
+ return { kind: "info_unchanged", user_id };
38
+ }
39
+ await tx
40
+ .update(users)
41
+ .set({ ...next, updated_at: sql `now()` })
42
+ .where(eq(users.id, user_id));
43
+ const { activity_id } = await insertActivity(tx, {
44
+ action: "user.info_changed",
45
+ actor,
46
+ app,
47
+ context: { inputs: next, outcome: { previous } },
48
+ user_id,
49
+ });
50
+ return { kind: "info_changed", activity_id, next, previous };
51
+ });
52
+ };
53
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proconnect-gouv/proconnect.identite.runtime",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "homepage": "https://github.com/proconnect-gouv/proconnect-identite/tree/main/packages/runtime#readme",
5
5
  "bugs": "https://github.com/proconnect-gouv/proconnect-identite/issues",
6
6
  "repository": {
@@ -32,8 +32,8 @@
32
32
  "test": "tsc --noEmit && tsx --test --test-reporter=spec"
33
33
  },
34
34
  "dependencies": {
35
- "@proconnect-gouv/proconnect.identite": "^14.0.0",
36
- "@proconnect-gouv/proconnect.identite.database": "^1.12.0",
35
+ "@proconnect-gouv/proconnect.identite": "^15.0.0",
36
+ "@proconnect-gouv/proconnect.identite.database": "^2.0.0",
37
37
  "zod": "^4.6.7"
38
38
  },
39
39
  "devDependencies": {
@@ -21,7 +21,12 @@ import {
21
21
  updateMembershipFactory,
22
22
  } from "#src/memberships";
23
23
  import { openModerationFactory } from "#src/moderations";
24
- import { deleteUserFactory, resetMfaFactory } from "#src/users";
24
+ import {
25
+ changePasswordFactory,
26
+ deleteUserFactory,
27
+ resetMfaFactory,
28
+ updatePersonalInformationsFactory,
29
+ } from "#src/users";
25
30
 
26
31
  //
27
32
 
@@ -34,6 +39,7 @@ export function createIdentiteRuntime<
34
39
  as(actor: Actor) {
35
40
  return {
36
41
  addMembership: addMembershipFactory({ actor, app, db }),
42
+ changePassword: changePasswordFactory({ actor, app, db }),
37
43
  connectFranceConnect: connectFranceConnectFactory({ actor, app, db }),
38
44
  deleteUser: deleteUserFactory({ actor, app, db }),
39
45
  disconnectFranceConnect: disconnectFranceConnectFactory({
@@ -50,6 +56,11 @@ export function createIdentiteRuntime<
50
56
  ) => insertActivity<OwnContexts>(tx, { ...activity, actor, app }),
51
57
  resetMfa: resetMfaFactory({ actor, app, db }),
52
58
  updateMembership: updateMembershipFactory({ actor, app, db }),
59
+ updatePersonalInformations: updatePersonalInformationsFactory({
60
+ actor,
61
+ app,
62
+ db,
63
+ }),
53
64
  };
54
65
  },
55
66
  };
@@ -4,6 +4,13 @@ import * as z from "zod";
4
4
 
5
5
  //
6
6
 
7
+ const userInformation = z.object({
8
+ family_name: z.string().nullable(),
9
+ given_name: z.string().nullable(),
10
+ job: z.string().nullable(),
11
+ phone_number: z.string().nullable(),
12
+ });
13
+
7
14
  export const userActivities = {
8
15
  "user.deleted": z.object({
9
16
  inputs: z.object({
@@ -22,6 +29,12 @@ export const userActivities = {
22
29
  ),
23
30
  }),
24
31
  }),
32
+ "user.info_changed": z.object({
33
+ inputs: userInformation,
34
+ outcome: z.object({
35
+ previous: userInformation,
36
+ }),
37
+ }),
25
38
  "user.mfa_reset": z.object({
26
39
  inputs: z.object({
27
40
  user_id: z.number(),
@@ -36,4 +49,12 @@ export const userActivities = {
36
49
  }),
37
50
  }),
38
51
  }),
52
+ "user.password_changed": z.object({
53
+ inputs: z.object({
54
+ source: z.enum(["reset_password", "signup"]),
55
+ }),
56
+ outcome: z.object({
57
+ previous: z.object({ email_verified: z.boolean() }),
58
+ }),
59
+ }),
39
60
  };
@@ -0,0 +1,91 @@
1
+ //
2
+
3
+ import { createIdentiteRuntime } from "#src/runtime";
4
+ import { createTestDatabase, readActivityLogs } from "#testing";
5
+ import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
6
+ import assert from "node:assert/strict";
7
+ import { describe, it } from "node:test";
8
+
9
+ //
10
+
11
+ async function setup() {
12
+ const db = await createTestDatabase();
13
+ await db.insert(schema.users).values({
14
+ created_at: "4444-04-04",
15
+ email: "jean.valjean@republic.fr",
16
+ id: 1,
17
+ reset_password_token: "token",
18
+ updated_at: "4444-04-04",
19
+ });
20
+
21
+ return {
22
+ db,
23
+ runtime: createIdentiteRuntime({ app: "proconnect-identite", db }).as({
24
+ type: "user",
25
+ user_id: 1,
26
+ }),
27
+ };
28
+ }
29
+
30
+ describe("changePassword", () => {
31
+ it("✅ resets the password, verifies the email and clears the token", async () => {
32
+ const { db, runtime } = await setup();
33
+
34
+ const outcome = await runtime.changePassword({
35
+ encrypted_password: "hashed",
36
+ source: "reset_password",
37
+ user_id: 1,
38
+ });
39
+
40
+ assert.deepEqual(outcome, {
41
+ kind: "password_changed",
42
+ activity_id: 1,
43
+ previous: { email_verified: false },
44
+ });
45
+
46
+ const [user] = await db.query.users.findMany();
47
+ assert.equal(user.encrypted_password, "hashed");
48
+ assert.equal(user.email_verified, true);
49
+ assert.equal(user.reset_password_token, null);
50
+
51
+ assert.deepEqual(await readActivityLogs(db), [
52
+ {
53
+ action: "user.password_changed",
54
+ actor_ref: "1",
55
+ actor_type: "user",
56
+ app: "proconnect-identite",
57
+ cause_activity_id: null,
58
+ context: {
59
+ inputs: { source: "reset_password" },
60
+ outcome: {
61
+ previous: { email_verified: false },
62
+ },
63
+ },
64
+ interaction_cid: null,
65
+ moderation_id: null,
66
+ organization_id: null,
67
+ user_id: 1,
68
+ },
69
+ ]);
70
+ });
71
+
72
+ it("✅ keeps the email unverified when setting a first password at signup", async () => {
73
+ const { db, runtime } = await setup();
74
+
75
+ const outcome = await runtime.changePassword({
76
+ encrypted_password: "hashed",
77
+ source: "signup",
78
+ user_id: 1,
79
+ });
80
+
81
+ assert.deepEqual(outcome, {
82
+ kind: "password_changed",
83
+ activity_id: 1,
84
+ previous: { email_verified: false },
85
+ });
86
+
87
+ const [user] = await db.query.users.findMany();
88
+ assert.equal(user.email_verified, false);
89
+ assert.equal(user.email_verified_at, null);
90
+ });
91
+ });
@@ -0,0 +1,64 @@
1
+ //
2
+
3
+ import type { ActivityContexts, RuntimeContext } from "#src/activity";
4
+ import { insertActivity } from "#src/journal";
5
+ import { users } from "@proconnect-gouv/proconnect.identite.database/drizzle";
6
+ import { UserNotFoundError } from "@proconnect-gouv/proconnect.identite/errors";
7
+ import { eq, sql } from "drizzle-orm";
8
+
9
+ //
10
+
11
+ type PasswordSource = "reset_password" | "signup";
12
+
13
+ type ChangePasswordOutcome = {
14
+ kind: "password_changed";
15
+ activity_id: number;
16
+ previous: { email_verified: boolean };
17
+ };
18
+
19
+ export function changePasswordFactory({ actor, app, db }: RuntimeContext) {
20
+ return function changePassword({
21
+ encrypted_password,
22
+ source,
23
+ user_id,
24
+ }: {
25
+ encrypted_password: string;
26
+ source: PasswordSource;
27
+ user_id: number;
28
+ }): Promise<ChangePasswordOutcome> {
29
+ return db.transaction(async (tx) => {
30
+ const [previous] = await tx
31
+ .select({ email_verified: users.email_verified })
32
+ .from(users)
33
+ .where(eq(users.id, user_id))
34
+ .for("update");
35
+
36
+ if (!previous) {
37
+ throw new UserNotFoundError(`user ${user_id} not found`);
38
+ }
39
+
40
+ await tx
41
+ .update(users)
42
+ .set({
43
+ email_verified: source === "reset_password",
44
+ email_verified_at: source === "reset_password" ? sql`now()` : null,
45
+ encrypted_password,
46
+ reset_password_sent_at:
47
+ source === "reset_password" ? null : undefined,
48
+ reset_password_token: source === "reset_password" ? null : undefined,
49
+ updated_at: sql`now()`,
50
+ })
51
+ .where(eq(users.id, user_id));
52
+
53
+ const { activity_id } = await insertActivity<ActivityContexts>(tx, {
54
+ action: "user.password_changed",
55
+ actor,
56
+ app,
57
+ context: { inputs: { source }, outcome: { previous } },
58
+ user_id,
59
+ });
60
+
61
+ return { kind: "password_changed", activity_id, previous };
62
+ });
63
+ };
64
+ }
@@ -1,5 +1,7 @@
1
1
  //
2
2
 
3
3
  export * from "./activities.js";
4
+ export * from "./change-password.js";
4
5
  export * from "./delete-user.js";
5
6
  export * from "./reset-mfa.js";
7
+ export * from "./update-personal-informations.js";
@@ -0,0 +1,113 @@
1
+ //
2
+
3
+ import { createIdentiteRuntime } from "#src/runtime";
4
+ import { createTestDatabase, readActivityLogs } from "#testing";
5
+ import * as schema from "@proconnect-gouv/proconnect.identite.database/drizzle";
6
+ import assert from "node:assert/strict";
7
+ import { describe, it } from "node:test";
8
+
9
+ //
10
+
11
+ async function setup() {
12
+ const db = await createTestDatabase();
13
+ await db.insert(schema.users).values({
14
+ created_at: "4444-04-04",
15
+ email: "jean.valjean@republic.fr",
16
+ id: 1,
17
+ updated_at: "4444-04-04",
18
+ });
19
+
20
+ return {
21
+ db,
22
+ runtime: createIdentiteRuntime({ app: "proconnect-identite", db }).as({
23
+ type: "user",
24
+ user_id: 1,
25
+ }),
26
+ };
27
+ }
28
+
29
+ describe("updatePersonalInformations", () => {
30
+ it("✅ changes the profile and journals the previous values", async () => {
31
+ const { db, runtime } = await setup();
32
+
33
+ const outcome = await runtime.updatePersonalInformations({
34
+ information: {
35
+ family_name: "Valjean",
36
+ given_name: "Jean",
37
+ job: "Maire",
38
+ phone_number: null,
39
+ },
40
+ user_id: 1,
41
+ });
42
+
43
+ assert.deepEqual(outcome, {
44
+ kind: "info_changed",
45
+ activity_id: 1,
46
+ next: {
47
+ family_name: "Valjean",
48
+ given_name: "Jean",
49
+ job: "Maire",
50
+ phone_number: null,
51
+ },
52
+ previous: {
53
+ family_name: null,
54
+ given_name: null,
55
+ job: null,
56
+ phone_number: null,
57
+ },
58
+ });
59
+ assert.deepEqual(await readActivityLogs(db), [
60
+ {
61
+ action: "user.info_changed",
62
+ actor_ref: "1",
63
+ actor_type: "user",
64
+ app: "proconnect-identite",
65
+ cause_activity_id: null,
66
+ context: {
67
+ inputs: {
68
+ family_name: "Valjean",
69
+ given_name: "Jean",
70
+ job: "Maire",
71
+ phone_number: null,
72
+ },
73
+ outcome: {
74
+ previous: {
75
+ family_name: null,
76
+ given_name: null,
77
+ job: null,
78
+ phone_number: null,
79
+ },
80
+ },
81
+ },
82
+ interaction_cid: null,
83
+ moderation_id: null,
84
+ organization_id: null,
85
+ user_id: 1,
86
+ },
87
+ ]);
88
+ });
89
+
90
+ it("❎ writes nothing when the profile does not change", async () => {
91
+ const { db, runtime } = await setup();
92
+
93
+ const outcome = await runtime.updatePersonalInformations({
94
+ information: { given_name: null },
95
+ user_id: 1,
96
+ });
97
+
98
+ assert.deepEqual(outcome, { kind: "info_unchanged", user_id: 1 });
99
+ assert.deepEqual(await readActivityLogs(db), []);
100
+ });
101
+
102
+ it("❎ returns user_not_found and journals nothing", async () => {
103
+ const { db, runtime } = await setup();
104
+
105
+ const outcome = await runtime.updatePersonalInformations({
106
+ information: { given_name: "Jean" },
107
+ user_id: 42,
108
+ });
109
+
110
+ assert.deepEqual(outcome, { kind: "user_not_found", user_id: 42 });
111
+ assert.deepEqual(await readActivityLogs(db), []);
112
+ });
113
+ });
@@ -0,0 +1,97 @@
1
+ //
2
+
3
+ import type { ActivityContexts, RuntimeContext } from "#src/activity";
4
+ import { insertActivity } from "#src/journal";
5
+ import { users } from "@proconnect-gouv/proconnect.identite.database/drizzle";
6
+ import type { User } from "@proconnect-gouv/proconnect.identite/types";
7
+ import { eq, sql } from "drizzle-orm";
8
+
9
+ //
10
+
11
+ type PersonalInformation = Pick<
12
+ User,
13
+ "family_name" | "given_name" | "job" | "phone_number"
14
+ >;
15
+
16
+ type UpdatePersonalInformationsOutcome =
17
+ | {
18
+ kind: "info_changed";
19
+ activity_id: number;
20
+ next: PersonalInformation;
21
+ previous: PersonalInformation;
22
+ }
23
+ | { kind: "info_unchanged"; user_id: number }
24
+ | { kind: "user_not_found"; user_id: number };
25
+
26
+ export function updatePersonalInformationsFactory({
27
+ actor,
28
+ app,
29
+ db,
30
+ }: RuntimeContext) {
31
+ return function updatePersonalInformations({
32
+ information,
33
+ user_id,
34
+ }: {
35
+ information: Partial<
36
+ Pick<User, "family_name" | "given_name" | "job" | "phone_number">
37
+ >;
38
+ user_id: number;
39
+ }): Promise<UpdatePersonalInformationsOutcome> {
40
+ return db.transaction(async (tx) => {
41
+ const [previous] = await tx
42
+ .select({
43
+ family_name: users.family_name,
44
+ given_name: users.given_name,
45
+ job: users.job,
46
+ phone_number: users.phone_number,
47
+ })
48
+ .from(users)
49
+ .where(eq(users.id, user_id))
50
+ .for("update");
51
+
52
+ if (!previous) {
53
+ return { kind: "user_not_found", user_id };
54
+ }
55
+
56
+ const next = {
57
+ family_name:
58
+ information.family_name !== undefined
59
+ ? information.family_name
60
+ : previous.family_name,
61
+ given_name:
62
+ information.given_name !== undefined
63
+ ? information.given_name
64
+ : previous.given_name,
65
+ job: information.job !== undefined ? information.job : previous.job,
66
+ phone_number:
67
+ information.phone_number !== undefined
68
+ ? information.phone_number
69
+ : previous.phone_number,
70
+ };
71
+
72
+ if (
73
+ next.family_name === previous.family_name &&
74
+ next.given_name === previous.given_name &&
75
+ next.job === previous.job &&
76
+ next.phone_number === previous.phone_number
77
+ ) {
78
+ return { kind: "info_unchanged", user_id };
79
+ }
80
+
81
+ await tx
82
+ .update(users)
83
+ .set({ ...next, updated_at: sql`now()` })
84
+ .where(eq(users.id, user_id));
85
+
86
+ const { activity_id } = await insertActivity<ActivityContexts>(tx, {
87
+ action: "user.info_changed",
88
+ actor,
89
+ app,
90
+ context: { inputs: next, outcome: { previous } },
91
+ user_id,
92
+ });
93
+
94
+ return { kind: "info_changed", activity_id, next, previous };
95
+ });
96
+ };
97
+ }