@sharadox/account-web-library 0.1.0-development.27 → 0.1.0-development.30

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 (92) hide show
  1. package/README.md +44 -0
  2. package/distribution/account-ban/index.d.ts +67 -0
  3. package/distribution/account-ban/index.d.ts.map +1 -0
  4. package/distribution/account-ban/index.js +233 -0
  5. package/distribution/account-ban/index.js.map +1 -0
  6. package/distribution/account-ban/type.d.ts +44 -0
  7. package/distribution/account-ban/type.d.ts.map +1 -0
  8. package/distribution/account-ban/type.js +2 -0
  9. package/distribution/account-ban/type.js.map +1 -0
  10. package/distribution/account-database/type.d.ts +114 -0
  11. package/distribution/account-database/type.d.ts.map +1 -1
  12. package/distribution/account-deletion/index.d.ts +7 -4
  13. package/distribution/account-deletion/index.d.ts.map +1 -1
  14. package/distribution/account-deletion/index.js +43 -5
  15. package/distribution/account-deletion/index.js.map +1 -1
  16. package/distribution/account-inactivity/index.d.ts +25 -0
  17. package/distribution/account-inactivity/index.d.ts.map +1 -0
  18. package/distribution/account-inactivity/index.js +100 -0
  19. package/distribution/account-inactivity/index.js.map +1 -0
  20. package/distribution/account-migration/index.d.ts.map +1 -1
  21. package/distribution/account-migration/index.js +120 -0
  22. package/distribution/account-migration/index.js.map +1 -1
  23. package/distribution/account-notice/index.d.ts +15 -0
  24. package/distribution/account-notice/index.d.ts.map +1 -0
  25. package/distribution/account-notice/index.js +41 -0
  26. package/distribution/account-notice/index.js.map +1 -0
  27. package/distribution/account-permission/index.d.ts +14 -0
  28. package/distribution/account-permission/index.d.ts.map +1 -0
  29. package/distribution/account-permission/index.js +91 -0
  30. package/distribution/account-permission/index.js.map +1 -0
  31. package/distribution/account-permission/type.d.ts +12 -0
  32. package/distribution/account-permission/type.d.ts.map +1 -0
  33. package/distribution/account-permission/type.js +2 -0
  34. package/distribution/account-permission/type.js.map +1 -0
  35. package/distribution/application-sync/index.d.ts +14 -0
  36. package/distribution/application-sync/index.d.ts.map +1 -0
  37. package/distribution/application-sync/index.js +96 -0
  38. package/distribution/application-sync/index.js.map +1 -0
  39. package/distribution/application-sync/type.d.ts +29 -0
  40. package/distribution/application-sync/type.d.ts.map +1 -0
  41. package/distribution/application-sync/type.js +2 -0
  42. package/distribution/application-sync/type.js.map +1 -0
  43. package/distribution/banned-contact/index.d.ts +34 -0
  44. package/distribution/banned-contact/index.d.ts.map +1 -0
  45. package/distribution/banned-contact/index.js +52 -0
  46. package/distribution/banned-contact/index.js.map +1 -0
  47. package/distribution/contact-addition-flow/index.d.ts +2 -1
  48. package/distribution/contact-addition-flow/index.d.ts.map +1 -1
  49. package/distribution/contact-addition-flow/index.js +15 -4
  50. package/distribution/contact-addition-flow/index.js.map +1 -1
  51. package/distribution/contact-addition-flow/type.d.ts +5 -2
  52. package/distribution/contact-addition-flow/type.d.ts.map +1 -1
  53. package/distribution/contact-channel/type.d.ts +24 -0
  54. package/distribution/contact-channel/type.d.ts.map +1 -1
  55. package/distribution/email-channel/index.d.ts.map +1 -1
  56. package/distribution/email-channel/index.js +78 -0
  57. package/distribution/email-channel/index.js.map +1 -1
  58. package/distribution/person-account/index.d.ts +1 -1
  59. package/distribution/person-account/index.d.ts.map +1 -1
  60. package/distribution/person-account/index.js +10 -2
  61. package/distribution/person-account/index.js.map +1 -1
  62. package/distribution/person-account/type.d.ts +2 -0
  63. package/distribution/person-account/type.d.ts.map +1 -1
  64. package/distribution/role-grant/index.d.ts +38 -0
  65. package/distribution/role-grant/index.d.ts.map +1 -0
  66. package/distribution/role-grant/index.js +142 -0
  67. package/distribution/role-grant/index.js.map +1 -0
  68. package/distribution/role-grant/type.d.ts +40 -0
  69. package/distribution/role-grant/type.d.ts.map +1 -0
  70. package/distribution/role-grant/type.js +2 -0
  71. package/distribution/role-grant/type.js.map +1 -0
  72. package/distribution/rule-sync/index.d.ts +28 -0
  73. package/distribution/rule-sync/index.d.ts.map +1 -0
  74. package/distribution/rule-sync/index.js +80 -0
  75. package/distribution/rule-sync/index.js.map +1 -0
  76. package/distribution/rule-sync/type.d.ts +17 -0
  77. package/distribution/rule-sync/type.d.ts.map +1 -0
  78. package/distribution/rule-sync/type.js +2 -0
  79. package/distribution/rule-sync/type.js.map +1 -0
  80. package/distribution/second-factor-finish/index.d.ts +2 -1
  81. package/distribution/second-factor-finish/index.d.ts.map +1 -1
  82. package/distribution/second-factor-finish/index.js +12 -7
  83. package/distribution/second-factor-finish/index.js.map +1 -1
  84. package/distribution/second-factor-finish/type.d.ts +4 -1
  85. package/distribution/second-factor-finish/type.d.ts.map +1 -1
  86. package/distribution/sign-up-finish/index.d.ts +3 -2
  87. package/distribution/sign-up-finish/index.d.ts.map +1 -1
  88. package/distribution/sign-up-finish/index.js +8 -2
  89. package/distribution/sign-up-finish/index.js.map +1 -1
  90. package/distribution/sign-up-finish/type.d.ts +3 -1
  91. package/distribution/sign-up-finish/type.d.ts.map +1 -1
  92. package/package.json +1 -1
@@ -1,15 +1,18 @@
1
+ import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
1
2
  import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
3
+ import { bannedContactHash } from "@sharadox/account-web-library/banned-contact";
2
4
  import { pictureKey } from "@sharadox/account-web-library/picture-key";
3
5
  /**
4
6
  * Deletes an account for good: its picture file, then the account with everything that belongs to it (identifier,
5
7
  * contacts, sessions, two-factor, agents), in one transaction. Its identifier is free at once. The audit log keeps
6
- * `account.deleted` and earlier events under the bare id, with no personal data. Ask for a fresh code first
7
- * (step-up).
8
+ * `account.deleted` and earlier events under the bare id, with no personal data. An account banned somewhere leaves
9
+ * its contacts in the banned-contact ledger (HMACs under `ledgerPepper`) until the ban ends. Ask for a fresh code
10
+ * first (step-up).
8
11
  *
9
12
  * @example
10
- * await deleteAccount(database, storage, { accountId }); // { deleted: true }
13
+ * await deleteAccount(database, storage, { accountId, ledgerPepper }); // { deleted: true }
11
14
  */
12
- export async function deleteAccount(database, storage, { accountId }) {
15
+ export async function deleteAccount(database, storage, { accountId, ledgerPepper, now = Date.now() }) {
13
16
  const picture = await database
14
17
  .selectFrom("account_picture")
15
18
  .select("id")
@@ -18,10 +21,45 @@ export async function deleteAccount(database, storage, { accountId }) {
18
21
  if (picture)
19
22
  await storage.remove(pictureKey(picture.id));
20
23
  return database.transaction().execute(async (transaction) => {
24
+ // An account banned somewhere leaves its contacts' HMACs, so it can't come back as new until the ban ends.
25
+ const bans = await transaction
26
+ .selectFrom("ban")
27
+ .innerJoin("role_grant", "role_grant.id", "ban.grant_id")
28
+ .innerJoin("role", "role.id", "role_grant.role_id")
29
+ .select(["ban.id", "role.application_id", "role_grant.ends_at"])
30
+ .where("ban.account_id", "=", accountId)
31
+ .where((where) => where.or([where("role_grant.ends_at", "is", null), where("role_grant.ends_at", ">", new Date(now))]))
32
+ .execute();
33
+ if (bans.length > 0) {
34
+ const contacts = await transaction
35
+ .selectFrom("contact")
36
+ .select(["kind", "value"])
37
+ .where("account_id", "=", accountId)
38
+ .where("verified_at", "is not", null)
39
+ .execute();
40
+ for (const ban of bans) {
41
+ const rules = await transaction
42
+ .selectFrom("ban_rule")
43
+ .innerJoin("rule", "rule.id", "ban_rule.rule_id")
44
+ .select("rule.code")
45
+ .where("ban_rule.ban_id", "=", ban.id)
46
+ .execute();
47
+ await transaction
48
+ .insertInto("banned_contact")
49
+ .values(contacts.map((contact) => ({
50
+ id: uuidVersion7({ now }),
51
+ contact_hash: bannedContactHash(contact, ledgerPepper),
52
+ application_id: ban.application_id,
53
+ ends_at: ban.ends_at,
54
+ rule_codes: rules.map(({ code }) => code),
55
+ })))
56
+ .execute();
57
+ }
58
+ }
21
59
  const deleted = await transaction.deleteFrom("account").where("id", "=", accountId).executeTakeFirst();
22
60
  if (deleted.numDeletedRows === 0n)
23
61
  return { deleted: false };
24
- await recordAuditEvent(transaction, { accountId, action: "account.deleted" });
62
+ await recordAuditEvent(transaction, { accountId, action: "account.deleted", now });
25
63
  return { deleted: true };
26
64
  });
27
65
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-deletion/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,MAAM,2CAA2C,CAAC;AAGvE;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAiC,EACjC,OAAuB,EACvB,EAAE,SAAS,EAAyB;IAEpC,MAAM,OAAO,GAAG,MAAM,QAAQ;SAC3B,UAAU,CAAC,iBAAiB,CAAC;SAC7B,MAAM,CAAC,IAAI,CAAC;SACZ,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,SAAS,CAAC;SACnC,gBAAgB,EAAE,CAAC;IACtB,IAAI,OAAO;QAAE,MAAM,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;IAC1D,OAAO,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;QAC1D,MAAM,OAAO,GAAG,MAAM,WAAW,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC,CAAC,gBAAgB,EAAE,CAAC;QACvG,IAAI,OAAO,CAAC,cAAc,KAAK,EAAE;YAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;QAC7D,MAAM,gBAAgB,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC,CAAC;QAC9E,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC3B,CAAC,CAAC,CAAC;AACL,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-deletion/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAC/E,OAAO,EAAE,gBAAgB,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AAC,gBAAgB,EAAE,CAAC;QACvG,IAAI,OAAO,CAAC,cAAc,KAAK,EAAE;YAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;QAC7D,MAAM,gBAAgB,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,iBAAiB,EAAE,GAAG,EAAE,CAAC,CAAC;QACnF,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC3B,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,25 @@
1
+ import type { AccountServer } from "@sharadox/account-web-library/account-server/type";
2
+ import type { PictureStorage } from "@sharadox/account-web-library/picture-storage/type";
3
+ /**
4
+ * When an account last active at `activeAt` is deleted: the same moment two years on (29 February leading to
5
+ * 1 March).
6
+ *
7
+ * @example
8
+ * deletionDate(account.last_active_at); // two years later
9
+ */
10
+ export declare function deletionDate(activeAt: Date): Date;
11
+ /**
12
+ * The daily inactivity job: a person not signed in for two years is deleted (with their agents), after warnings to
13
+ * every contact 6 months, 3 months, 1 month, 1 week and 1 day before, each sent once and in the account's language.
14
+ * After days without a run, only the latest warning due goes out. Signing in starts the two years again.
15
+ *
16
+ * @example
17
+ * await runInactivity(server, pictureStorage); // { warned: 3, deleted: 1 }
18
+ */
19
+ export declare function runInactivity(server: AccountServer, storage: PictureStorage, { now }?: {
20
+ now?: number;
21
+ }): Promise<{
22
+ warned: number;
23
+ deleted: number;
24
+ }>;
25
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-inactivity/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAEvF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oDAAoD,CAAC;AAezF;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,IAAI,GAAG,IAAI,CAIjD;AASD;;;;;;;GAOG;AACH,wBAAsB,aAAa,CACjC,MAAM,EAAE,aAAa,EACrB,OAAO,EAAE,cAAc,EACvB,EAAE,GAAgB,EAAE,GAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAA;CAAO,GAC1C,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,CAwD9C"}
@@ -0,0 +1,100 @@
1
+ import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
2
+ import { deleteAccount } from "@sharadox/account-web-library/account-deletion";
3
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
4
+ import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
5
+ /** How long an account may go unused before it is deleted. */
6
+ const YEARS = 2;
7
+ /** The warnings before deletion, earliest first: how far before it, in months or days. */
8
+ const STAGES = [
9
+ { stage: "6-months", months: 6 },
10
+ { stage: "3-months", months: 3 },
11
+ { stage: "1-month", months: 1 },
12
+ { stage: "1-week", days: 7 },
13
+ { stage: "1-day", days: 1 },
14
+ ];
15
+ /**
16
+ * When an account last active at `activeAt` is deleted: the same moment two years on (29 February leading to
17
+ * 1 March).
18
+ *
19
+ * @example
20
+ * deletionDate(account.last_active_at); // two years later
21
+ */
22
+ export function deletionDate(activeAt) {
23
+ const at = new Date(activeAt);
24
+ at.setUTCFullYear(at.getUTCFullYear() + YEARS);
25
+ return at;
26
+ }
27
+ const before = (deletion, { months = 0, days = 0 }) => {
28
+ const at = new Date(deletion);
29
+ at.setUTCMonth(at.getUTCMonth() - months);
30
+ at.setUTCDate(at.getUTCDate() - days);
31
+ return at;
32
+ };
33
+ /**
34
+ * The daily inactivity job: a person not signed in for two years is deleted (with their agents), after warnings to
35
+ * every contact 6 months, 3 months, 1 month, 1 week and 1 day before, each sent once and in the account's language.
36
+ * After days without a run, only the latest warning due goes out. Signing in starts the two years again.
37
+ *
38
+ * @example
39
+ * await runInactivity(server, pictureStorage); // { warned: 3, deleted: 1 }
40
+ */
41
+ export async function runInactivity(server, storage, { now = Date.now() } = {}) {
42
+ const at = new Date(now);
43
+ const firstWarning = new Date(now);
44
+ firstWarning.setUTCFullYear(firstWarning.getUTCFullYear() - YEARS);
45
+ // A week's margin: the warnings' own dates decide (months have different lengths).
46
+ firstWarning.setUTCMonth(firstWarning.getUTCMonth() + 6);
47
+ firstWarning.setUTCDate(firstWarning.getUTCDate() + 7);
48
+ const accounts = await server.database
49
+ .selectFrom("account")
50
+ .select(["id", "last_active_at", "language"])
51
+ .where("kind", "=", "person")
52
+ .where("last_active_at", "<=", firstWarning)
53
+ .execute();
54
+ let warned = 0;
55
+ let deleted = 0;
56
+ for (const account of accounts) {
57
+ const deletion = deletionDate(account.last_active_at);
58
+ if (deletion <= at) {
59
+ const ledgerPepper = deriveSecret(server.secret, "banned-contact");
60
+ await recordAuditEvent(server.database, { accountId: account.id, action: "account.inactive", now });
61
+ await deleteAccount(server.database, storage, { accountId: account.id, ledgerPepper, now });
62
+ deleted++;
63
+ continue;
64
+ }
65
+ const due = STAGES.filter((stage) => before(deletion, stage) <= at).at(-1);
66
+ if (!due)
67
+ continue;
68
+ const recorded = await server.database
69
+ .insertInto("inactivity_warning")
70
+ .values({
71
+ id: uuidVersion7({ now }),
72
+ account_id: account.id,
73
+ stage: due.stage,
74
+ active_at: account.last_active_at,
75
+ sent_at: at,
76
+ })
77
+ .onConflict((conflict) => conflict.columns(["account_id", "stage", "active_at"]).doNothing())
78
+ .returning("id")
79
+ .executeTakeFirst();
80
+ if (!recorded)
81
+ continue;
82
+ const contacts = await server.database
83
+ .selectFrom("contact")
84
+ .select(["kind", "value"])
85
+ .where("account_id", "=", account.id)
86
+ .where("verified_at", "is not", null)
87
+ .execute();
88
+ for (const contact of contacts) {
89
+ const channel = server.channels.find(({ kind }) => kind === contact.kind);
90
+ await channel?.notify?.({
91
+ contact,
92
+ language: account.language ?? "en",
93
+ notice: { kind: "inactivity-warning", deletionAt: deletion },
94
+ });
95
+ }
96
+ warned++;
97
+ }
98
+ return { warned, deleted };
99
+ }
100
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-inactivity/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAC/E,OAAO,EAAE,aAAa,EAAE,MAAM,gDAAgD,CAAC;AAG/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAE7E,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAE/E,8DAA8D;AAC9D,MAAM,KAAK,GAAG,CAAC,CAAC;AAEhB,0FAA0F;AAC1F,MAAM,MAAM,GAAiF;IAC3F,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC,EAAE;IAChC,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC,EAAE;IAChC,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC,EAAE;IAC/B,EAAE,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,EAAE;IAC5B,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE;CAC5B,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,QAAc;IACzC,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9B,EAAE,CAAC,cAAc,CAAC,EAAE,CAAC,cAAc,EAAE,GAAG,KAAK,CAAC,CAAC;IAC/C,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,MAAM,MAAM,GAAG,CAAC,QAAc,EAAE,EAAE,MAAM,GAAG,CAAC,EAAE,IAAI,GAAG,CAAC,EAAsC,EAAE,EAAE;IAC9F,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9B,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC,WAAW,EAAE,GAAG,MAAM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ACzB,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,CAAC;aACpC,KAAK,CAAC,aAAa,EAAE,QAAQ,EAAE,IAAI,CAAC;aACpC,OAAO,EAAE,CAAC;QACb,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YAC/B,MAAM,OAAO,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,IAAI,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;YAC1E,MAAM,OAAO,EAAE,MAAM,EAAE,CAAC;gBACtB,OAAO;gBACP,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,IAAI;gBAClC,MAAM,EAAE,EAAE,IAAI,EAAE,oBAAoB,EAAE,UAAU,EAAE,QAAQ,EAAE;aAC7D,CAAC,CAAC;QACL,CAAC;QACD,MAAM,EAAE,CAAC;IACX,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;AAC7B,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-migration/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAE/E,iHAAiH;AACjH,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAE3D;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,SAAS,EA6OxC,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAElF"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-migration/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAE/E,iHAAiH;AACjH,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAE3D;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,SAAS,EAqWxC,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAElF"}
@@ -243,6 +243,126 @@ export const accountMigrations = [
243
243
  alter table account_session add column step_up_at timestamptz;
244
244
  `,
245
245
  },
246
+ {
247
+ name: "0005-role",
248
+ sql: `
249
+ create table permission (
250
+ id uuid primary key check (uuid_extract_version(id) = 7),
251
+ application_id uuid not null references application (id) on update cascade on delete cascade,
252
+ key text not null unique
253
+ check (length(key) <= 100 and key ~ '^[a-z][a-z0-9]*(-[a-z0-9]+)*(\\.[a-z][a-z0-9]*(-[a-z0-9]+)*)+$'),
254
+ description text not null default '' check (length(description) <= 500),
255
+ created_at timestamptz not null default now()
256
+ );
257
+
258
+ create table role (
259
+ id uuid primary key check (uuid_extract_version(id) = 7),
260
+ application_id uuid references application (id) on update cascade on delete cascade,
261
+ key text not null check (length(key) <= 50 and key ~ '^[a-z][a-z0-9]*(-[a-z0-9]+)*$'),
262
+ name text not null check (length(name) between 1 and 100),
263
+ exclusive boolean not null default false,
264
+ is_default boolean not null default false,
265
+ created_at timestamptz not null default now(),
266
+ constraint role_default_check check (not (is_default and (exclusive or application_id is null)))
267
+ );
268
+ create unique index role_key on role (coalesce(application_id::text, ''), key);
269
+ create unique index role_default on role (application_id) where is_default;
270
+
271
+ create table role_permission (
272
+ id uuid primary key check (uuid_extract_version(id) = 7),
273
+ role_id uuid not null references role (id) on update cascade on delete cascade,
274
+ permission_id uuid not null references permission (id) on update cascade on delete cascade,
275
+ unique (role_id, permission_id)
276
+ );
277
+
278
+ create table role_grant (
279
+ id uuid primary key check (uuid_extract_version(id) = 7),
280
+ account_id uuid not null references account (id) on update cascade on delete cascade,
281
+ role_id uuid not null references role (id) on update cascade on delete restrict,
282
+ resource text check (length(resource) <= 200),
283
+ starts_at timestamptz not null default now(),
284
+ ends_at timestamptz,
285
+ source text not null check (source in ('manual', 'membership', 'ban', 'system')),
286
+ granted_by uuid references account (id) on update cascade on delete set null,
287
+ reason text check (length(reason) <= 1000),
288
+ created_at timestamptz not null default now(),
289
+ constraint role_grant_check check (ends_at is null or ends_at > starts_at)
290
+ );
291
+ create index role_grant_account on role_grant (account_id);
292
+ create index role_grant_role on role_grant (role_id);
293
+ `,
294
+ },
295
+ {
296
+ name: "0006-rule-ban",
297
+ sql: `
298
+ create table rule (
299
+ id uuid primary key check (uuid_extract_version(id) = 7),
300
+ application_id uuid references application (id) on update cascade on delete cascade,
301
+ code text not null check (length(code) <= 50 and code ~ '^[a-z][a-z0-9]*(-[a-z0-9]+)*$'),
302
+ version integer not null check (version >= 1),
303
+ title jsonb not null,
304
+ description jsonb not null,
305
+ schedule jsonb not null check (jsonb_typeof(schedule) = 'array' and jsonb_array_length(schedule) >= 1),
306
+ created_at timestamptz not null default now()
307
+ );
308
+ create unique index rule_version_key on rule (coalesce(application_id::text, ''), code, version);
309
+
310
+ create table ban (
311
+ id uuid primary key check (uuid_extract_version(id) = 7),
312
+ grant_id uuid not null unique references role_grant (id) on update cascade on delete cascade,
313
+ account_id uuid not null references account (id) on update cascade on delete cascade,
314
+ statement text not null check (length(statement) between 1 and 2000),
315
+ evidence text check (length(evidence) <= 5000),
316
+ penalty text not null check (penalty = 'permanent' or penalty ~ '^[0-9]+$'),
317
+ created_by uuid references account (id) on update cascade on delete set null,
318
+ created_at timestamptz not null default now()
319
+ );
320
+ create index ban_account on ban (account_id);
321
+
322
+ create table ban_rule (
323
+ id uuid primary key check (uuid_extract_version(id) = 7),
324
+ ban_id uuid not null references ban (id) on update cascade on delete cascade,
325
+ rule_id uuid not null references rule (id) on update cascade on delete restrict,
326
+ unique (ban_id, rule_id)
327
+ );
328
+
329
+ create table appeal (
330
+ id uuid primary key check (uuid_extract_version(id) = 7),
331
+ ban_id uuid not null unique references ban (id) on update cascade on delete cascade,
332
+ text text not null check (length(text) between 1 and 5000),
333
+ created_at timestamptz not null default now(),
334
+ decision text check (decision in ('upheld', 'shortened', 'lifted')),
335
+ reasons text check (length(reasons) <= 5000),
336
+ decided_by uuid references account (id) on update cascade on delete set null,
337
+ decided_at timestamptz
338
+ );
339
+
340
+ create table banned_contact (
341
+ id uuid primary key check (uuid_extract_version(id) = 7),
342
+ contact_hash text not null,
343
+ application_id uuid references application (id) on update cascade on delete cascade,
344
+ ends_at timestamptz,
345
+ rule_codes text[] not null,
346
+ created_at timestamptz not null default now()
347
+ );
348
+ create index banned_contact_hash on banned_contact (contact_hash);
349
+ `,
350
+ },
351
+ {
352
+ name: "0007-language-inactivity",
353
+ sql: `
354
+ alter table account add column language text check (length(language) <= 20);
355
+
356
+ create table inactivity_warning (
357
+ id uuid primary key check (uuid_extract_version(id) = 7),
358
+ account_id uuid not null references account (id) on update cascade on delete cascade,
359
+ stage text not null check (stage in ('6-months', '3-months', '1-month', '1-week', '1-day')),
360
+ active_at timestamptz not null,
361
+ sent_at timestamptz not null default now(),
362
+ unique (account_id, stage, active_at)
363
+ );
364
+ `,
365
+ },
246
366
  ];
247
367
  /**
248
368
  * Creates or updates the account tables, tracked in their own migration table. Returns the names applied.
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,0CAA0C,CAAC;AAGnE,iHAAiH;AACjH,MAAM,CAAC,MAAM,uBAAuB,GAAG,mBAAmB,CAAC;AAE3D;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAgB;IAC5C;QACE,IAAI,EAAE,wCAAwC;QAC9C,GAAG,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA0GJ;KACF;IACD;QACE,IAAI,EAAE,WAAW;QACjB,GAAG,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA6CJ;KACF;IACD;QACE,IAAI,EAAE,eAAe;QACrB,GAAG,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAoDJ;KACF;IACD;QACE,IAAI,EAAE,0BAA0B;QAChC,GAAG,EAAE;;;;;;;;;;;KAWJ;KACF;CACF,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAS,QAAwB;IAC7D,OAAO,OAAO,CAAC,QAAQ,EAAE,iBAAiB,EAAE,EAAE,KAAK,EAAE,uBAAuB,EAAE,CAAC,CAAC;AAClF,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { AccountServer } from "@sharadox/account-web-library/account-server/type";
2
+ import type { Notice } from "@sharadox/account-web-library/contact-channel/type";
3
+ /**
4
+ * Tells an account's owner something they must know (a new email, a recovery code used, a coming deletion), at every
5
+ * verified contact but `except`, in the account's language. A failed send is recorded and doesn't stop the others.
6
+ *
7
+ * @example
8
+ * after(() => notifyAccount(server, { accountId, notice: { kind: "recovery-code-used" } }));
9
+ */
10
+ export declare function notifyAccount(server: AccountServer, { accountId, notice, except }: {
11
+ accountId: string;
12
+ notice: Notice;
13
+ except?: string;
14
+ }): Promise<void>;
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-notice/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAEvF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oDAAoD,CAAC;AAEjF;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,MAAM,EAAE,aAAa,EACrB,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GACpF,OAAO,CAAC,IAAI,CAAC,CA6Bf"}
@@ -0,0 +1,41 @@
1
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
2
+ /**
3
+ * Tells an account's owner something they must know (a new email, a recovery code used, a coming deletion), at every
4
+ * verified contact but `except`, in the account's language. A failed send is recorded and doesn't stop the others.
5
+ *
6
+ * @example
7
+ * after(() => notifyAccount(server, { accountId, notice: { kind: "recovery-code-used" } }));
8
+ */
9
+ export async function notifyAccount(server, { accountId, notice, except }) {
10
+ const account = await server.database
11
+ .selectFrom("account")
12
+ .select("language")
13
+ .where("id", "=", accountId)
14
+ .executeTakeFirst();
15
+ let contacts = server.database
16
+ .selectFrom("contact")
17
+ .select(["id", "kind", "value"])
18
+ .where("account_id", "=", accountId)
19
+ .where("verified_at", "is not", null);
20
+ if (except)
21
+ contacts = contacts.where("id", "!=", except);
22
+ for (const contact of await contacts.orderBy("id").execute()) {
23
+ const channel = server.channels.find(({ kind }) => kind === contact.kind);
24
+ try {
25
+ await channel?.notify?.({
26
+ contact: { kind: contact.kind, value: contact.value },
27
+ language: account?.language ?? "en",
28
+ notice,
29
+ });
30
+ }
31
+ catch (error) {
32
+ const { name, message } = error;
33
+ await recordAuditEvent(server.database, {
34
+ accountId,
35
+ action: "notice.delivery-failed",
36
+ detail: { kind: notice.kind, error: `${name}: ${message}`.slice(0, 500) },
37
+ });
38
+ }
39
+ }
40
+ }
41
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-notice/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAG7E;;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AE,GAAG,KAAc,CAAC;YACzC,MAAM,gBAAgB,CAAC,MAAM,CAAC,QAAQ,EAAE;gBACtC,SAAS;gBACT,MAAM,EAAE,wBAAwB;gBAChC,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,IAAI,KAAK,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE;aAC1E,CAAC,CAAC;QACL,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ import type { AccountPermissionQuestion } from "@sharadox/account-web-library/account-permission/type";
4
+ /**
5
+ * What an account may do in one application now, from the database: the application's default role, the account's
6
+ * active grants (an active exclusive one, a ban, leaving only its own permissions), limited to `scope`, and for an
7
+ * agent to what its owner may do right now. An unknown application gives nothing.
8
+ *
9
+ * @example
10
+ * const permissions = await accountPermissions(database, { accountId, application: "platform" });
11
+ * if (!permissions.has("platform.job.create")) return forbidden();
12
+ */
13
+ export declare function accountPermissions(database: Kysely<AccountDatabase>, { accountId, application, resource, scope, now }: AccountPermissionQuestion): Promise<Set<string>>;
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-permission/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAC3F,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,uDAAuD,CAAC;AAIvG;;;;;;;;GAQG;AACH,wBAAsB,kBAAkB,CACtC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAgB,EAAE,EAAE,yBAAyB,GACvF,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CA+EtB"}
@@ -0,0 +1,91 @@
1
+ import { effectivePermissions } from "@sharadox/account-web-library/effective-permissions";
2
+ /**
3
+ * What an account may do in one application now, from the database: the application's default role, the account's
4
+ * active grants (an active exclusive one, a ban, leaving only its own permissions), limited to `scope`, and for an
5
+ * agent to what its owner may do right now. An unknown application gives nothing.
6
+ *
7
+ * @example
8
+ * const permissions = await accountPermissions(database, { accountId, application: "platform" });
9
+ * if (!permissions.has("platform.job.create")) return forbidden();
10
+ */
11
+ export async function accountPermissions(database, { accountId, application, resource, scope, now = Date.now() }) {
12
+ const app = await database.selectFrom("application").select("id").where("key", "=", application).executeTakeFirst();
13
+ if (!app)
14
+ return new Set();
15
+ const rows = await database
16
+ .selectFrom("role")
17
+ .leftJoin("application", "application.id", "role.application_id")
18
+ .leftJoin("role_permission", "role_permission.role_id", "role.id")
19
+ .leftJoin("permission", "permission.id", "role_permission.permission_id")
20
+ .select([
21
+ "role.id",
22
+ "application.key as application",
23
+ "role.exclusive",
24
+ "role.is_default",
25
+ "permission.key as permission",
26
+ ])
27
+ .where((where) => where.or([where("role.application_id", "=", app.id), where("role.application_id", "is", null)]))
28
+ .execute();
29
+ const roles = new Map();
30
+ for (const row of rows) {
31
+ const role = roles.get(row.id) ?? {
32
+ id: row.id,
33
+ application: row.application,
34
+ permissions: [],
35
+ exclusive: row.exclusive,
36
+ isDefault: row.is_default,
37
+ };
38
+ if (row.permission)
39
+ role.permissions.push(row.permission);
40
+ roles.set(row.id, role);
41
+ }
42
+ const grants = (await database
43
+ .selectFrom("role_grant")
44
+ .innerJoin("role", "role.id", "role_grant.role_id")
45
+ .leftJoin("application", "application.id", "role.application_id")
46
+ .select([
47
+ "role_grant.role_id",
48
+ "application.key as application",
49
+ "role_grant.resource",
50
+ "role_grant.starts_at",
51
+ "role_grant.ends_at",
52
+ ])
53
+ .where("role_grant.account_id", "=", accountId)
54
+ .execute()).map((grant) => ({
55
+ roleId: grant.role_id,
56
+ application: grant.application,
57
+ resource: grant.resource,
58
+ startsAt: grant.starts_at.getTime(),
59
+ endsAt: grant.ends_at ? grant.ends_at.getTime() : null,
60
+ }));
61
+ const account = await database
62
+ .selectFrom("account")
63
+ .select("owner_id")
64
+ .where("id", "=", accountId)
65
+ .executeTakeFirst();
66
+ const ownerPermissions = account?.owner_id
67
+ ? await accountPermissions(database, {
68
+ accountId: account.owner_id,
69
+ application,
70
+ ...(resource ? { resource } : {}),
71
+ now,
72
+ })
73
+ : undefined;
74
+ const defaultRole = [...roles.values()].find((role) => role.isDefault) ?? {
75
+ id: "",
76
+ application,
77
+ permissions: [],
78
+ exclusive: false,
79
+ };
80
+ return effectivePermissions({
81
+ application,
82
+ ...(resource ? { resource } : {}),
83
+ now,
84
+ defaultRole,
85
+ roles: [...roles.values()],
86
+ grants,
87
+ ...(scope ? { scope } : {}),
88
+ ...(ownerPermissions ? { ownerPermissions } : {}),
89
+ });
90
+ }
91
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-permission/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,oBAAoB,EAAE,MAAM,qDAAqD,CAAC;AAG3F;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,QAAiC,EACjC,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAA6B;IAExF,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,EAAE,WAAW,CAAC,CAAC,gBAAgB,EAAE,CAAC;IACpH,IAAI,CAAC,GAAG;QAAE,OAAO,IAAI,GAAG,EAAE,CAAC;IAC3B,MAAM,IAAI,GAAG,MAAM,QAAQ;SACxB,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ACX,WAAW,EAAE,EAAE;QACf,SAAS,EAAE,KAAK;KACjB,CAAC;IACF,OAAO,oBAAoB,CAAC;QAC1B,WAAW;QACX,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACjC,GAAG;QACH,WAAW;QACX,KAAK,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;QAC1B,MAAM;QACN,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3B,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,gBAAgB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAClD,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,12 @@
1
+ export interface AccountPermissionQuestion {
2
+ accountId: string;
3
+ /** The application's key (`platform`). */
4
+ application: string;
5
+ /** The resource the action is on (`platform:project:42`), for roles limited to one. */
6
+ resource?: string;
7
+ /** A token's or key's scope: at most these. */
8
+ scope?: string[];
9
+ /** Milliseconds since 1970 (now by default). */
10
+ now?: number;
11
+ }
12
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-permission/type.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,yBAAyB;IACxC,SAAS,EAAE,MAAM,CAAC;IAClB,0CAA0C;IAC1C,WAAW,EAAE,MAAM,CAAC;IACpB,uFAAuF;IACvF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,+CAA+C;IAC/C,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/account-permission/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,14 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ import type { ApplicationDeclaration, ApplicationSyncResult } from "@sharadox/account-web-library/application-sync/type";
4
+ /**
5
+ * Makes the database follow what an application declares in its code, all or nothing: the application, its
6
+ * permissions (only its own: each starts with `<key>.`), and its roles with their permissions, the default one
7
+ * (held by every account) and exclusive ones (bans). Permissions and roles no longer declared go, except a role
8
+ * still granted, which stays until its grants end. Run it at every deploy.
9
+ *
10
+ * @example
11
+ * await syncApplication(database, { key: "platform", name: "Platform", permissions, roles });
12
+ */
13
+ export declare function syncApplication(database: Kysely<AccountDatabase>, { key, name, permissions, roles }: ApplicationDeclaration): Promise<ApplicationSyncResult>;
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/application-sync/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAC3F,OAAO,KAAK,EACV,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,qDAAqD,CAAC;AAG7D;;;;;;;;GAQG;AACH,wBAAsB,eAAe,CACnC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,GAAG,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,EAAE,sBAAsB,GACxD,OAAO,CAAC,qBAAqB,CAAC,CA0FhC"}