@sharadox/account-web-library 0.0.0-stage → 0.1.0-code-request.8

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 (149) hide show
  1. package/LICENCA.md +76 -0
  2. package/LICENSE.md +76 -0
  3. package/NOTICE.md +3 -0
  4. package/README.md +165 -2
  5. package/distribution/account-database/type.d.ts +73 -0
  6. package/distribution/account-database/type.d.ts.map +1 -0
  7. package/distribution/account-database/type.js +2 -0
  8. package/distribution/account-database/type.js.map +1 -0
  9. package/distribution/account-migration/index.d.ts +18 -0
  10. package/distribution/account-migration/index.d.ts.map +1 -0
  11. package/distribution/account-migration/index.js +122 -0
  12. package/distribution/account-migration/index.js.map +1 -0
  13. package/distribution/audit-event/index.d.ts +12 -0
  14. package/distribution/audit-event/index.d.ts.map +1 -0
  15. package/distribution/audit-event/index.js +25 -0
  16. package/distribution/audit-event/index.js.map +1 -0
  17. package/distribution/audit-event/type.d.ts +15 -0
  18. package/distribution/audit-event/type.d.ts.map +1 -0
  19. package/distribution/audit-event/type.js +2 -0
  20. package/distribution/audit-event/type.js.map +1 -0
  21. package/distribution/ban-length/index.d.ts +11 -0
  22. package/distribution/ban-length/index.d.ts.map +1 -0
  23. package/distribution/ban-length/index.js +21 -0
  24. package/distribution/ban-length/index.js.map +1 -0
  25. package/distribution/ban-length/type.d.ts +8 -0
  26. package/distribution/ban-length/type.d.ts.map +1 -0
  27. package/distribution/ban-length/type.js +2 -0
  28. package/distribution/ban-length/type.js.map +1 -0
  29. package/distribution/base-32/index.d.ts +16 -0
  30. package/distribution/base-32/index.d.ts.map +1 -0
  31. package/distribution/base-32/index.js +50 -0
  32. package/distribution/base-32/index.js.map +1 -0
  33. package/distribution/code-request/index.d.ts +16 -0
  34. package/distribution/code-request/index.d.ts.map +1 -0
  35. package/distribution/code-request/index.js +52 -0
  36. package/distribution/code-request/index.js.map +1 -0
  37. package/distribution/code-request/type.d.ts +25 -0
  38. package/distribution/code-request/type.d.ts.map +1 -0
  39. package/distribution/code-request/type.js +2 -0
  40. package/distribution/code-request/type.js.map +1 -0
  41. package/distribution/code-request-prune/index.d.ts +13 -0
  42. package/distribution/code-request-prune/index.d.ts.map +1 -0
  43. package/distribution/code-request-prune/index.js +12 -0
  44. package/distribution/code-request-prune/index.js.map +1 -0
  45. package/distribution/code-verification/index.d.ts +14 -0
  46. package/distribution/code-verification/index.d.ts.map +1 -0
  47. package/distribution/code-verification/index.js +43 -0
  48. package/distribution/code-verification/index.js.map +1 -0
  49. package/distribution/code-verification/type.d.ts +22 -0
  50. package/distribution/code-verification/type.d.ts.map +1 -0
  51. package/distribution/code-verification/type.js +2 -0
  52. package/distribution/code-verification/type.js.map +1 -0
  53. package/distribution/contact-addition/index.d.ts +13 -0
  54. package/distribution/contact-addition/index.d.ts.map +1 -0
  55. package/distribution/contact-addition/index.js +48 -0
  56. package/distribution/contact-addition/index.js.map +1 -0
  57. package/distribution/contact-addition/type.d.ts +15 -0
  58. package/distribution/contact-addition/type.d.ts.map +1 -0
  59. package/distribution/contact-addition/type.js +2 -0
  60. package/distribution/contact-addition/type.js.map +1 -0
  61. package/distribution/contact-kind/index.d.ts +12 -0
  62. package/distribution/contact-kind/index.d.ts.map +1 -0
  63. package/distribution/contact-kind/index.js +18 -0
  64. package/distribution/contact-kind/index.js.map +1 -0
  65. package/distribution/contact-kind/type.d.ts +6 -0
  66. package/distribution/contact-kind/type.d.ts.map +1 -0
  67. package/distribution/contact-kind/type.js +2 -0
  68. package/distribution/contact-kind/type.js.map +1 -0
  69. package/distribution/contact-lookup/index.d.ts +16 -0
  70. package/distribution/contact-lookup/index.d.ts.map +1 -0
  71. package/distribution/contact-lookup/index.js +20 -0
  72. package/distribution/contact-lookup/index.js.map +1 -0
  73. package/distribution/contact-removal/index.d.ts +19 -0
  74. package/distribution/contact-removal/index.d.ts.map +1 -0
  75. package/distribution/contact-removal/index.js +35 -0
  76. package/distribution/contact-removal/index.js.map +1 -0
  77. package/distribution/contact-verification/index.d.ts +20 -0
  78. package/distribution/contact-verification/index.d.ts.map +1 -0
  79. package/distribution/contact-verification/index.js +37 -0
  80. package/distribution/contact-verification/index.js.map +1 -0
  81. package/distribution/database-error/index.d.ts +12 -0
  82. package/distribution/database-error/index.d.ts.map +1 -0
  83. package/distribution/database-error/index.js +14 -0
  84. package/distribution/database-error/index.js.map +1 -0
  85. package/distribution/effective-permissions/index.d.ts +16 -0
  86. package/distribution/effective-permissions/index.d.ts.map +1 -0
  87. package/distribution/effective-permissions/index.js +31 -0
  88. package/distribution/effective-permissions/index.js.map +1 -0
  89. package/distribution/effective-permissions/type.d.ts +37 -0
  90. package/distribution/effective-permissions/type.d.ts.map +1 -0
  91. package/distribution/effective-permissions/type.js +2 -0
  92. package/distribution/effective-permissions/type.js.map +1 -0
  93. package/distribution/email-contact/index.d.ts +17 -0
  94. package/distribution/email-contact/index.d.ts.map +1 -0
  95. package/distribution/email-contact/index.js +27 -0
  96. package/distribution/email-contact/index.js.map +1 -0
  97. package/distribution/one-time-code/index.d.ts +26 -0
  98. package/distribution/one-time-code/index.d.ts.map +1 -0
  99. package/distribution/one-time-code/index.js +36 -0
  100. package/distribution/one-time-code/index.js.map +1 -0
  101. package/distribution/one-time-code/type.d.ts +6 -0
  102. package/distribution/one-time-code/type.d.ts.map +1 -0
  103. package/distribution/one-time-code/type.js +2 -0
  104. package/distribution/one-time-code/type.js.map +1 -0
  105. package/distribution/person-account/index.d.ts +14 -0
  106. package/distribution/person-account/index.d.ts.map +1 -0
  107. package/distribution/person-account/index.js +45 -0
  108. package/distribution/person-account/index.js.map +1 -0
  109. package/distribution/person-account/type.d.ts +17 -0
  110. package/distribution/person-account/type.d.ts.map +1 -0
  111. package/distribution/person-account/type.js +2 -0
  112. package/distribution/person-account/type.js.map +1 -0
  113. package/distribution/recovery-code/index.d.ts +17 -0
  114. package/distribution/recovery-code/index.d.ts.map +1 -0
  115. package/distribution/recovery-code/index.js +28 -0
  116. package/distribution/recovery-code/index.js.map +1 -0
  117. package/distribution/test-database/index.d.ts +13 -0
  118. package/distribution/test-database/index.d.ts.map +1 -0
  119. package/distribution/test-database/index.js +17 -0
  120. package/distribution/test-database/index.js.map +1 -0
  121. package/distribution/totp/index.d.ts +35 -0
  122. package/distribution/totp/index.d.ts.map +1 -0
  123. package/distribution/totp/index.js +70 -0
  124. package/distribution/totp/index.js.map +1 -0
  125. package/distribution/totp/type.d.ts +25 -0
  126. package/distribution/totp/type.d.ts.map +1 -0
  127. package/distribution/totp/type.js +2 -0
  128. package/distribution/totp/type.js.map +1 -0
  129. package/distribution/username/index.d.ts +23 -0
  130. package/distribution/username/index.d.ts.map +1 -0
  131. package/distribution/username/index.js +32 -0
  132. package/distribution/username/index.js.map +1 -0
  133. package/distribution/username/type.d.ts +10 -0
  134. package/distribution/username/type.d.ts.map +1 -0
  135. package/distribution/username/type.js +2 -0
  136. package/distribution/username/type.js.map +1 -0
  137. package/distribution/username-availability/index.d.ts +11 -0
  138. package/distribution/username-availability/index.d.ts.map +1 -0
  139. package/distribution/username-availability/index.js +21 -0
  140. package/distribution/username-availability/index.js.map +1 -0
  141. package/distribution/username-change/index.d.ts +15 -0
  142. package/distribution/username-change/index.d.ts.map +1 -0
  143. package/distribution/username-change/index.js +39 -0
  144. package/distribution/username-change/index.js.map +1 -0
  145. package/distribution/username-change/type.d.ts +7 -0
  146. package/distribution/username-change/type.d.ts.map +1 -0
  147. package/distribution/username-change/type.js +2 -0
  148. package/distribution/username-change/type.js.map +1 -0
  149. package/package.json +90 -3
@@ -0,0 +1,19 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ /**
4
+ * Removes a contact from an account. Refuses to remove the last verified sign-in contact of a person (the database
5
+ * checks it when the change commits). Ask for a fresh code first (step-up) and tell the other contacts.
6
+ *
7
+ * @example
8
+ * const result = await removeContact(database, { accountId, contactId });
9
+ */
10
+ export declare function removeContact(database: Kysely<AccountDatabase>, { accountId, contactId }: {
11
+ accountId: string;
12
+ contactId: string;
13
+ }): Promise<{
14
+ removed: true;
15
+ } | {
16
+ removed: false;
17
+ reason: "no-contact" | "last-sign-in-contact";
18
+ }>;
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-removal/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GACjE,OAAO,CAAC;IAAE,OAAO,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,YAAY,GAAG,sBAAsB,CAAA;CAAE,CAAC,CAqBhG"}
@@ -0,0 +1,35 @@
1
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
2
+ import { databaseError } from "@sharadox/account-web-library/database-error";
3
+ /**
4
+ * Removes a contact from an account. Refuses to remove the last verified sign-in contact of a person (the database
5
+ * checks it when the change commits). Ask for a fresh code first (step-up) and tell the other contacts.
6
+ *
7
+ * @example
8
+ * const result = await removeContact(database, { accountId, contactId });
9
+ */
10
+ export async function removeContact(database, { accountId, contactId }) {
11
+ try {
12
+ return await database.transaction().execute(async (transaction) => {
13
+ const removed = await transaction
14
+ .deleteFrom("contact")
15
+ .where("id", "=", contactId)
16
+ .where("account_id", "=", accountId)
17
+ .returning("kind")
18
+ .executeTakeFirst();
19
+ if (!removed)
20
+ return { removed: false, reason: "no-contact" };
21
+ await recordAuditEvent(transaction, {
22
+ accountId,
23
+ action: "contact.removed",
24
+ detail: { contactId, kind: removed.kind },
25
+ });
26
+ return { removed: true };
27
+ });
28
+ }
29
+ catch (error) {
30
+ if (databaseError(error)?.code === "23514")
31
+ return { removed: false, reason: "last-sign-in-contact" };
32
+ throw error;
33
+ }
34
+ }
35
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-removal/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAE7E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAiC,EACjC,EAAE,SAAS,EAAE,SAAS,EAA4C;IAElE,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;YAChE,MAAM,OAAO,GAAG,MAAM,WAAW;iBAC9B,UAAU,CAAC,SAAS,CAAC;iBACrB,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;iBAC3B,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,SAAS,CAAC;iBACnC,SAAS,CAAC,MAAM,CAAC;iBACjB,gBAAgB,EAAE,CAAC;YACtB,IAAI,CAAC,OAAO;gBAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAW,CAAC;YACvE,MAAM,gBAAgB,CAAC,WAAW,EAAE;gBAClC,SAAS;gBACT,MAAM,EAAE,iBAAiB;gBACzB,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE;aAC1C,CAAC,CAAC;YACH,OAAO,EAAE,OAAO,EAAE,IAAI,EAAW,CAAC;QACpC,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,IAAI,KAAK,OAAO;YAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,sBAAsB,EAAE,CAAC;QACtG,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,20 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ /**
4
+ * Marks an account's contact verified, once the one-time code sent to it came back. Refuses when another account
5
+ * verified the same contact first (a verified contact belongs to one account only).
6
+ *
7
+ * @example
8
+ * await verifyContact(database, { accountId, contactId }); // { verified: true }
9
+ */
10
+ export declare function verifyContact(database: Kysely<AccountDatabase>, { accountId, contactId, now }: {
11
+ accountId: string;
12
+ contactId: string;
13
+ now?: number;
14
+ }): Promise<{
15
+ verified: true;
16
+ } | {
17
+ verified: false;
18
+ reason: "no-contact" | "contact-taken";
19
+ }>;
20
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-verification/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,SAAS,EAAE,GAAgB,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GACjG,OAAO,CAAC;IAAE,QAAQ,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,QAAQ,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,YAAY,GAAG,eAAe,CAAA;CAAE,CAAC,CAuB3F"}
@@ -0,0 +1,37 @@
1
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
2
+ import { databaseError } from "@sharadox/account-web-library/database-error";
3
+ /**
4
+ * Marks an account's contact verified, once the one-time code sent to it came back. Refuses when another account
5
+ * verified the same contact first (a verified contact belongs to one account only).
6
+ *
7
+ * @example
8
+ * await verifyContact(database, { accountId, contactId }); // { verified: true }
9
+ */
10
+ export async function verifyContact(database, { accountId, contactId, now = Date.now() }) {
11
+ try {
12
+ return await database.transaction().execute(async (transaction) => {
13
+ const updated = await transaction
14
+ .updateTable("contact")
15
+ .set({ verified_at: new Date(now) })
16
+ .where("id", "=", contactId)
17
+ .where("account_id", "=", accountId)
18
+ .returning("kind")
19
+ .executeTakeFirst();
20
+ if (!updated)
21
+ return { verified: false, reason: "no-contact" };
22
+ await recordAuditEvent(transaction, {
23
+ accountId,
24
+ action: "contact.verified",
25
+ detail: { contactId, kind: updated.kind },
26
+ now,
27
+ });
28
+ return { verified: true };
29
+ });
30
+ }
31
+ catch (error) {
32
+ if (databaseError(error)?.constraint === "contact_verified")
33
+ return { verified: false, reason: "contact-taken" };
34
+ throw error;
35
+ }
36
+ }
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-verification/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAE7E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAiC,EACjC,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAA0D;IAElG,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;YAChE,MAAM,OAAO,GAAG,MAAM,WAAW;iBAC9B,WAAW,CAAC,SAAS,CAAC;iBACtB,GAAG,CAAC,EAAE,WAAW,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;iBACnC,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;iBAC3B,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,SAAS,CAAC;iBACnC,SAAS,CAAC,MAAM,CAAC;iBACjB,gBAAgB,EAAE,CAAC;YACtB,IAAI,CAAC,OAAO;gBAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAW,CAAC;YACxE,MAAM,gBAAgB,CAAC,WAAW,EAAE;gBAClC,SAAS;gBACT,MAAM,EAAE,kBAAkB;gBAC1B,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE;gBACzC,GAAG;aACJ,CAAC,CAAC;YACH,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAW,CAAC;QACrC,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,UAAU,KAAK,kBAAkB;YAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC;QACjH,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Postgres's error code (`23505` unique, `23514` check…) and the constraint that failed, from an error thrown by the
3
+ * driver (Neon's or PGlite's); undefined for any other error.
4
+ *
5
+ * @example
6
+ * if (databaseError(error)?.constraint === "account_username_key") return { reason: "username-taken" };
7
+ */
8
+ export declare function databaseError(error: unknown): {
9
+ code: string;
10
+ constraint: string | undefined;
11
+ } | undefined;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/database-error/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,GAAG,SAAS,CAK1G"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Postgres's error code (`23505` unique, `23514` check…) and the constraint that failed, from an error thrown by the
3
+ * driver (Neon's or PGlite's); undefined for any other error.
4
+ *
5
+ * @example
6
+ * if (databaseError(error)?.constraint === "account_username_key") return { reason: "username-taken" };
7
+ */
8
+ export function databaseError(error) {
9
+ if (typeof error !== "object" || error === null || typeof error.code !== "string")
10
+ return undefined;
11
+ const { code, constraint } = error;
12
+ return { code, constraint };
13
+ }
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/database-error/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc;IAC1C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,OAAQ,KAA4B,CAAC,IAAI,KAAK,QAAQ;QACvG,OAAO,SAAS,CAAC;IACnB,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,KAA8C,CAAC;IAC5E,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;AAC9B,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { EffectivePermissionsInput } from "@sharadox/account-web-library/effective-permissions/type";
2
+ /**
3
+ * What an account may do in one application, now. In order:
4
+ * 1. Active grants count: started, not ended, in this application or global, and for the whole application or this
5
+ * resource.
6
+ * 2. An active exclusive grant (a ban) leaves only the exclusive roles' permissions.
7
+ * 3. Otherwise the result is the default role plus every active role.
8
+ * 4. Only this application's permissions (`platform.…`) and the account system's own (`account.…`) are kept.
9
+ * 5. The result is limited to `scope` (a token or key) and to `ownerPermissions` (an agent).
10
+ *
11
+ * @example
12
+ * const permissions = effectivePermissions({ application: "platform", now: Date.now(), defaultRole, roles, grants });
13
+ * if (!permissions.has("platform.job.create")) return forbidden();
14
+ */
15
+ export declare function effectivePermissions({ application, resource, now, defaultRole, roles, grants, scope, ownerPermissions, }: EffectivePermissionsInput): Set<string>;
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/effective-permissions/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,yBAAyB,EAAS,MAAM,0DAA0D,CAAC;AAKjH;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAAC,EACnC,WAAW,EACX,QAAQ,EACR,GAAG,EACH,WAAW,EACX,KAAK,EACL,MAAM,EACN,KAAK,EACL,gBAAgB,GACjB,EAAE,yBAAyB,GAAG,GAAG,CAAC,MAAM,CAAC,CAiBzC"}
@@ -0,0 +1,31 @@
1
+ /** A permission the account system itself handles (`account.ban.read`, `account.data.export`…), in every application. */
2
+ const ACCOUNT_PERMISSION = "account.";
3
+ /**
4
+ * What an account may do in one application, now. In order:
5
+ * 1. Active grants count: started, not ended, in this application or global, and for the whole application or this
6
+ * resource.
7
+ * 2. An active exclusive grant (a ban) leaves only the exclusive roles' permissions.
8
+ * 3. Otherwise the result is the default role plus every active role.
9
+ * 4. Only this application's permissions (`platform.…`) and the account system's own (`account.…`) are kept.
10
+ * 5. The result is limited to `scope` (a token or key) and to `ownerPermissions` (an agent).
11
+ *
12
+ * @example
13
+ * const permissions = effectivePermissions({ application: "platform", now: Date.now(), defaultRole, roles, grants });
14
+ * if (!permissions.has("platform.job.create")) return forbidden();
15
+ */
16
+ export function effectivePermissions({ application, resource, now, defaultRole, roles, grants, scope, ownerPermissions, }) {
17
+ const byId = new Map(roles.map((role) => [role.id, role]));
18
+ const applies = (grant) => grant.startsAt <= now &&
19
+ (grant.endsAt === null || now < grant.endsAt) &&
20
+ (grant.application === null || grant.application === application) &&
21
+ (grant.resource === null || grant.resource === resource);
22
+ const active = grants.filter(applies).flatMap((grant) => byId.get(grant.roleId) ?? []);
23
+ const exclusive = active.filter((role) => role.exclusive);
24
+ const granted = exclusive.length > 0 ? exclusive : [defaultRole, ...active];
25
+ const permissions = new Set(granted
26
+ .flatMap((role) => role.permissions)
27
+ .filter((permission) => permission.startsWith(`${application}.`) || permission.startsWith(ACCOUNT_PERMISSION)));
28
+ const limits = [scope && new Set(scope), ownerPermissions].filter((limit) => limit !== undefined);
29
+ return new Set([...permissions].filter((permission) => limits.every((limit) => limit.has(permission))));
30
+ }
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/effective-permissions/index.ts"],"names":[],"mappings":"AAEA,yHAAyH;AACzH,MAAM,kBAAkB,GAAG,UAAU,CAAC;AAEtC;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,oBAAoB,CAAC,EACnC,WAAW,EACX,QAAQ,EACR,GAAG,EACH,WAAW,EACX,KAAK,EACL,MAAM,EACN,KAAK,EACL,gBAAgB,GACU;IAC1B,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;IAC3D,MAAM,OAAO,GAAG,CAAC,KAAY,EAAE,EAAE,CAC/B,KAAK,CAAC,QAAQ,IAAI,GAAG;QACrB,CAAC,KAAK,CAAC,MAAM,KAAK,IAAI,IAAI,GAAG,GAAG,KAAK,CAAC,MAAM,CAAC;QAC7C,CAAC,KAAK,CAAC,WAAW,KAAK,IAAI,IAAI,KAAK,CAAC,WAAW,KAAK,WAAW,CAAC;QACjE,CAAC,KAAK,CAAC,QAAQ,KAAK,IAAI,IAAI,KAAK,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC;IAC3D,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;IACvF,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC1D,MAAM,OAAO,GAAG,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,MAAM,CAAC,CAAC;IAC5E,MAAM,WAAW,GAAG,IAAI,GAAG,CACzB,OAAO;SACJ,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC;SACnC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,WAAW,GAAG,CAAC,IAAI,UAAU,CAAC,UAAU,CAAC,kBAAkB,CAAC,CAAC,CACjH,CAAC;IACF,MAAM,MAAM,GAAG,CAAC,KAAK,IAAI,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;IAClG,OAAO,IAAI,GAAG,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1G,CAAC"}
@@ -0,0 +1,37 @@
1
+ /** A role: its application (`null` for every application), its permissions, and whether it is a ban (`exclusive`). */
2
+ export interface Role {
3
+ id: string;
4
+ application: string | null;
5
+ permissions: string[];
6
+ /** While active, the account has only this role's permissions in its application (how bans work). */
7
+ exclusive: boolean;
8
+ }
9
+ /** A role given to an account: in an application (or `null`, all), maybe for one resource, between two moments. */
10
+ export interface Grant {
11
+ roleId: string;
12
+ application: string | null;
13
+ /** `platform:project:42`, or `null` for the whole application. */
14
+ resource: string | null;
15
+ /** Milliseconds since 1970. */
16
+ startsAt: number;
17
+ /** Milliseconds since 1970, or `null` for permanent. */
18
+ endsAt: number | null;
19
+ }
20
+ export interface EffectivePermissionsInput {
21
+ /** The application asking, for example `platform`; its permissions start with `platform.`. */
22
+ application: string;
23
+ /** The resource the action is on, for roles limited to one resource. */
24
+ resource?: string;
25
+ now: number;
26
+ /** The application's default role, which every account holds. */
27
+ defaultRole: Role;
28
+ /** Every role that may be granted. */
29
+ roles: Role[];
30
+ /** The account's grants. */
31
+ grants: Grant[];
32
+ /** A token's or key's scope: at most these. */
33
+ scope?: string[];
34
+ /** An agent's owner's effective permissions: at most these. */
35
+ ownerPermissions?: Set<string>;
36
+ }
37
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/effective-permissions/type.ts"],"names":[],"mappings":"AAAA,sHAAsH;AACtH,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,qGAAqG;IACrG,SAAS,EAAE,OAAO,CAAC;CACpB;AAED,mHAAmH;AACnH,MAAM,WAAW,KAAK;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,kEAAkE;IAClE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,+BAA+B;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,wDAAwD;IACxD,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB;AAED,MAAM,WAAW,yBAAyB;IACxC,8FAA8F;IAC9F,WAAW,EAAE,MAAM,CAAC;IACpB,wEAAwE;IACxE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,iEAAiE;IACjE,WAAW,EAAE,IAAI,CAAC;IAClB,sCAAsC;IACtC,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,4BAA4B;IAC5B,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,+CAA+C;IAC/C,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,+DAA+D;IAC/D,gBAAgB,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAChC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/effective-permissions/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * An email contact in the one form it is stored and compared in: trimmed and lowercase. Throws for text that isn't
3
+ * an email address.
4
+ *
5
+ * @example
6
+ * normalizeEmail(" Luis@Example.com "); // "luis@example.com"
7
+ */
8
+ export declare function normalizeEmail(address: string): string;
9
+ /**
10
+ * The form a banned email takes in the ban ledger: normalized, and without a `+tag`, so `name+1@…` can't stand in
11
+ * for a banned `name@…`.
12
+ *
13
+ * @example
14
+ * emailLedgerForm("Luis+Shop@Example.com"); // "luis@example.com"
15
+ */
16
+ export declare function emailLedgerForm(address: string): string;
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/email-contact/index.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAItD;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAGvD"}
@@ -0,0 +1,27 @@
1
+ /** One `@`, something either side, no spaces: the shape, not deliverability (the one-time code proves that). */
2
+ const SHAPE = /^[^\s@]+@[^\s@]+$/;
3
+ /**
4
+ * An email contact in the one form it is stored and compared in: trimmed and lowercase. Throws for text that isn't
5
+ * an email address.
6
+ *
7
+ * @example
8
+ * normalizeEmail(" Luis@Example.com "); // "luis@example.com"
9
+ */
10
+ export function normalizeEmail(address) {
11
+ const normalized = address.trim().toLowerCase();
12
+ if (!SHAPE.test(normalized))
13
+ throw new Error("this is not an email address");
14
+ return normalized;
15
+ }
16
+ /**
17
+ * The form a banned email takes in the ban ledger: normalized, and without a `+tag`, so `name+1@…` can't stand in
18
+ * for a banned `name@…`.
19
+ *
20
+ * @example
21
+ * emailLedgerForm("Luis+Shop@Example.com"); // "luis@example.com"
22
+ */
23
+ export function emailLedgerForm(address) {
24
+ const [local, domain] = normalizeEmail(address).split("@");
25
+ return `${local.split("+")[0]}@${domain}`;
26
+ }
27
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/email-contact/index.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH,MAAM,KAAK,GAAG,mBAAmB,CAAC;AAElC;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,OAAe;IAC5C,MAAM,UAAU,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IAChD,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;IAC7E,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,OAAe;IAC7C,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,cAAc,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,GAAG,CAAqB,CAAC;IAC/E,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,MAAM,EAAE,CAAC;AAC5C,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type { OneTimeCodeHashOptions } from "@sharadox/account-web-library/one-time-code/type";
2
+ /**
3
+ * A new one-time code: `length` digits (8 by default, one in a hundred million), each drawn uniformly by the
4
+ * operating system's secure random source. Send it to the contact; store only `hashOneTimeCode`.
5
+ *
6
+ * @example
7
+ * const code = createOneTimeCode(); // "04718263"
8
+ */
9
+ export declare function createOneTimeCode(length?: number): string;
10
+ /**
11
+ * What is stored instead of the code: HMAC-SHA-256 of the purpose and the code under a pepper kept outside the
12
+ * database. A leaked table gives nothing to try, and a code can't be used for another request or purpose.
13
+ *
14
+ * @example
15
+ * const codeHash = hashOneTimeCode(code, { pepper: process.env.ACCOUNT_PEPPER!, purpose: `sign-in:${request.id}` });
16
+ */
17
+ export declare function hashOneTimeCode(code: string, { pepper, purpose }: OneTimeCodeHashOptions): string;
18
+ /**
19
+ * Whether a typed code matches a stored hash (spaces ignored, compared in constant time). Count the attempt either
20
+ * way: after 5 wrong tries, the code is void.
21
+ *
22
+ * @example
23
+ * if (!matchesOneTimeCode(typed, request.codeHash, { pepper, purpose: `sign-in:${request.id}` })) await countFailure();
24
+ */
25
+ export declare function matchesOneTimeCode(code: string, hash: string, options: OneTimeCodeHashOptions): boolean;
26
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/one-time-code/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,kDAAkD,CAAC;AAE/F;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,SAAI,GAAG,MAAM,CAEpD;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,sBAAsB,GAAG,MAAM,CAGjG;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAIvG"}
@@ -0,0 +1,36 @@
1
+ import { createHmac, randomInt, timingSafeEqual } from "node:crypto";
2
+ /**
3
+ * A new one-time code: `length` digits (8 by default, one in a hundred million), each drawn uniformly by the
4
+ * operating system's secure random source. Send it to the contact; store only `hashOneTimeCode`.
5
+ *
6
+ * @example
7
+ * const code = createOneTimeCode(); // "04718263"
8
+ */
9
+ export function createOneTimeCode(length = 8) {
10
+ return Array.from({ length }, () => randomInt(10)).join("");
11
+ }
12
+ /**
13
+ * What is stored instead of the code: HMAC-SHA-256 of the purpose and the code under a pepper kept outside the
14
+ * database. A leaked table gives nothing to try, and a code can't be used for another request or purpose.
15
+ *
16
+ * @example
17
+ * const codeHash = hashOneTimeCode(code, { pepper: process.env.ACCOUNT_PEPPER!, purpose: `sign-in:${request.id}` });
18
+ */
19
+ export function hashOneTimeCode(code, { pepper, purpose }) {
20
+ if (pepper.length < 32)
21
+ throw new Error("the one-time code pepper must have at least 32 characters");
22
+ return createHmac("sha256", pepper).update(`${purpose}\n${code}`).digest("base64url");
23
+ }
24
+ /**
25
+ * Whether a typed code matches a stored hash (spaces ignored, compared in constant time). Count the attempt either
26
+ * way: after 5 wrong tries, the code is void.
27
+ *
28
+ * @example
29
+ * if (!matchesOneTimeCode(typed, request.codeHash, { pepper, purpose: `sign-in:${request.id}` })) await countFailure();
30
+ */
31
+ export function matchesOneTimeCode(code, hash, options) {
32
+ const expected = Buffer.from(hash);
33
+ const given = Buffer.from(hashOneTimeCode(code.replace(/\s/g, ""), options));
34
+ return expected.length === given.length && timingSafeEqual(expected, given);
35
+ }
36
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/one-time-code/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAGrE;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAM,GAAG,CAAC;IAC1C,OAAO,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,IAAY,EAAE,EAAE,MAAM,EAAE,OAAO,EAA0B;IACvF,IAAI,MAAM,CAAC,MAAM,GAAG,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAC;IACrG,OAAO,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,OAAO,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AACxF,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAY,EAAE,IAAY,EAAE,OAA+B;IAC5F,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACnC,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;IAC7E,OAAO,QAAQ,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,IAAI,eAAe,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;AAC9E,CAAC"}
@@ -0,0 +1,6 @@
1
+ /** The secret pepper (at least 32 characters, from the environment) and what the code is for (`sign-in:<request id>`). */
2
+ export interface OneTimeCodeHashOptions {
3
+ pepper: string;
4
+ purpose: string;
5
+ }
6
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/one-time-code/type.ts"],"names":[],"mappings":"AAAA,0HAA0H;AAC1H,MAAM,WAAW,sBAAsB;IACrC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/one-time-code/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 { PersonAccountRequest, PersonAccountResult } from "@sharadox/account-web-library/person-account/type";
4
+ /**
5
+ * Creates a person's account with the contact their one-time code just proved, as its verified sign-in contact, and
6
+ * records it — all or nothing. Refuses a username outside the rules or taken in any case, and a contact another
7
+ * account has verified.
8
+ *
9
+ * @example
10
+ * const result = await createPersonAccount(database, { username: "Luis-Pires", contact: { kind: "email", value } });
11
+ * if (!result.created) return explain(result.reason);
12
+ */
13
+ export declare function createPersonAccount(database: Kysely<AccountDatabase>, { username, contact, now }: PersonAccountRequest): Promise<PersonAccountResult>;
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/person-account/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,mDAAmD,CAAC;AAGnH;;;;;;;;GAQG;AACH,wBAAsB,mBAAmB,CACvC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAgB,EAAE,EAAE,oBAAoB,GAC5D,OAAO,CAAC,mBAAmB,CAAC,CAyB9B"}
@@ -0,0 +1,45 @@
1
+ import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
2
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
3
+ import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
4
+ import { databaseError } from "@sharadox/account-web-library/database-error";
5
+ import { checkUsername } from "@sharadox/account-web-library/username";
6
+ /**
7
+ * Creates a person's account with the contact their one-time code just proved, as its verified sign-in contact, and
8
+ * records it — all or nothing. Refuses a username outside the rules or taken in any case, and a contact another
9
+ * account has verified.
10
+ *
11
+ * @example
12
+ * const result = await createPersonAccount(database, { username: "Luis-Pires", contact: { kind: "email", value } });
13
+ * if (!result.created) return explain(result.reason);
14
+ */
15
+ export async function createPersonAccount(database, { username, contact, now = Date.now() }) {
16
+ if (!checkUsername(username).valid)
17
+ return { created: false, reason: "username-invalid" };
18
+ const { kind, value } = normalizeContact(contact);
19
+ const at = new Date(now);
20
+ const accountId = uuidVersion7({ now });
21
+ const contactId = uuidVersion7({ now });
22
+ try {
23
+ await database.transaction().execute(async (transaction) => {
24
+ await transaction
25
+ .insertInto("account")
26
+ .values({ id: accountId, kind: "person", username, owner_id: null, created_at: at, last_active_at: at })
27
+ .execute();
28
+ await transaction
29
+ .insertInto("contact")
30
+ .values({ id: contactId, account_id: accountId, kind, value, verified_at: at, created_at: at })
31
+ .execute();
32
+ await recordAuditEvent(transaction, { accountId, action: "account.created", detail: { contactId }, now });
33
+ });
34
+ }
35
+ catch (error) {
36
+ const constraint = databaseError(error)?.constraint;
37
+ if (constraint === "account_username_key")
38
+ return { created: false, reason: "username-taken" };
39
+ if (constraint === "contact_verified")
40
+ return { created: false, reason: "contact-taken" };
41
+ throw error;
42
+ }
43
+ return { created: true, accountId, contactId };
44
+ }
45
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/person-account/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAE/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAE7E,OAAO,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AAEvE;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,QAAiC,EACjC,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAwB;IAE7D,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,KAAK;QAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,kBAAkB,EAAE,CAAC;IAC1F,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAClD,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,MAAM,SAAS,GAAG,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IACxC,MAAM,SAAS,GAAG,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IACxC,IAAI,CAAC;QACH,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;YACzD,MAAM,WAAW;iBACd,UAAU,CAAC,SAAS,CAAC;iBACrB,MAAM,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,cAAc,EAAE,EAAE,EAAE,CAAC;iBACvG,OAAO,EAAE,CAAC;YACb,MAAM,WAAW;iBACd,UAAU,CAAC,SAAS,CAAC;iBACrB,MAAM,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;iBAC9F,OAAO,EAAE,CAAC;YACb,MAAM,gBAAgB,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;QAC5G,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,UAAU,GAAG,aAAa,CAAC,KAAK,CAAC,EAAE,UAAU,CAAC;QACpD,IAAI,UAAU,KAAK,sBAAsB;YAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,CAAC;QAC/F,IAAI,UAAU,KAAK,kBAAkB;YAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC;QAC1F,MAAM,KAAK,CAAC;IACd,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;AACjD,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
2
+ export interface PersonAccountRequest {
3
+ username: string;
4
+ /** Already proved by a one-time code: it is stored verified. */
5
+ contact: Contact;
6
+ /** Milliseconds since 1970 (now by default). */
7
+ now?: number;
8
+ }
9
+ export type PersonAccountResult = {
10
+ created: true;
11
+ accountId: string;
12
+ contactId: string;
13
+ } | {
14
+ created: false;
15
+ reason: "username-invalid" | "username-taken" | "contact-taken";
16
+ };
17
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/person-account/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,MAAM,WAAW,oBAAoB;IACnC,QAAQ,EAAE,MAAM,CAAC;IACjB,gEAAgE;IAChE,OAAO,EAAE,OAAO,CAAC;IACjB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,mBAAmB,GAC3B;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GACvD;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,kBAAkB,GAAG,gBAAgB,GAAG,eAAe,CAAA;CAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/person-account/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Two-factor recovery codes, offered once two-factor is on: `count` codes (10 by default) like `7KQM-W3HD-9TXA`,
3
+ * about 59 bits each, single use. Show them once, and store each with `hashOneTimeCode(normalizeRecoveryCode(code),
4
+ * { pepper, purpose: "recovery:<account id>" })`. Making new ones voids the old ones.
5
+ *
6
+ * @example
7
+ * const codes = createRecoveryCodes();
8
+ */
9
+ export declare function createRecoveryCodes(count?: number): string[];
10
+ /**
11
+ * A recovery code however it was typed (case, spaces, hyphens), in the one form that is hashed.
12
+ *
13
+ * @example
14
+ * normalizeRecoveryCode(" 7kqm w3hd-9txa "); // "7KQMW3HD9TXA"
15
+ */
16
+ export declare function normalizeRecoveryCode(code: string): string;
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/recovery-code/index.ts"],"names":[],"mappings":"AAOA;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,SAAK,GAAG,MAAM,EAAE,CAIxD;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE1D"}
@@ -0,0 +1,28 @@
1
+ import { randomInt } from "node:crypto";
2
+ /** Digits and capitals without 0/O, 1/I/L and U: nothing to misread when typing a code back. */
3
+ const ALPHABET = "23456789ABCDEFGHJKMNPQRSTVWXYZ";
4
+ const group = () => Array.from({ length: 4 }, () => ALPHABET[randomInt(ALPHABET.length)]).join("");
5
+ /**
6
+ * Two-factor recovery codes, offered once two-factor is on: `count` codes (10 by default) like `7KQM-W3HD-9TXA`,
7
+ * about 59 bits each, single use. Show them once, and store each with `hashOneTimeCode(normalizeRecoveryCode(code),
8
+ * { pepper, purpose: "recovery:<account id>" })`. Making new ones voids the old ones.
9
+ *
10
+ * @example
11
+ * const codes = createRecoveryCodes();
12
+ */
13
+ export function createRecoveryCodes(count = 10) {
14
+ const codes = new Set();
15
+ while (codes.size < count)
16
+ codes.add(`${group()}-${group()}-${group()}`);
17
+ return [...codes];
18
+ }
19
+ /**
20
+ * A recovery code however it was typed (case, spaces, hyphens), in the one form that is hashed.
21
+ *
22
+ * @example
23
+ * normalizeRecoveryCode(" 7kqm w3hd-9txa "); // "7KQMW3HD9TXA"
24
+ */
25
+ export function normalizeRecoveryCode(code) {
26
+ return code.toUpperCase().replace(/[\s-]/g, "");
27
+ }
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/recovery-code/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,gGAAgG;AAChG,MAAM,QAAQ,GAAG,gCAAgC,CAAC;AAElD,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAEnG;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAK,GAAG,EAAE;IAC5C,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU,CAAC;IAChC,OAAO,KAAK,CAAC,IAAI,GAAG,KAAK;QAAE,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,EAAE,IAAI,KAAK,EAAE,IAAI,KAAK,EAAE,EAAE,CAAC,CAAC;IACzE,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC;AACpB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAY;IAChD,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;AAClD,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { MemoryDatabase } from "@sharadox/database-web-library/memory-database/type";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ /**
4
+ * A migrated account database in memory, for this library's tests (and applications' own): Postgres 18 through
5
+ * PGlite, so every constraint and trigger runs as in production.
6
+ *
7
+ * @example
8
+ * const memory = await createTestAccountDatabase();
9
+ * // … test …
10
+ * await memory.close();
11
+ */
12
+ export declare function createTestAccountDatabase(): Promise<MemoryDatabase<AccountDatabase>>;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/test-database/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qDAAqD,CAAC;AAC1F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAG3F;;;;;;;;GAQG;AACH,wBAAsB,yBAAyB,IAAI,OAAO,CAAC,cAAc,CAAC,eAAe,CAAC,CAAC,CAI1F"}