@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,11 @@
1
+ import type { BrokenRule } from "@sharadox/account-web-library/ban-length/type";
2
+ /**
3
+ * How long a ban lasts, from the rules it cites, so equal offenses get equal penalties:
4
+ * - each rule gives the penalty of its schedule for this offense (1st, 2nd…), and the last penalty repeats;
5
+ * - several rules broken at once give the longest of their penalties.
6
+ *
7
+ * @example
8
+ * banLength([{ schedule: [7 * 86_400, 30 * 86_400, "permanent"], previousOffenses: 1 }]); // 2_592_000 (30 days)
9
+ */
10
+ export declare function banLength(rules: BrokenRule[]): number | "permanent";
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/ban-length/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+CAA+C,CAAC;AAEhF;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,EAAE,GAAG,MAAM,GAAG,WAAW,CAQnE"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * How long a ban lasts, from the rules it cites, so equal offenses get equal penalties:
3
+ * - each rule gives the penalty of its schedule for this offense (1st, 2nd…), and the last penalty repeats;
4
+ * - several rules broken at once give the longest of their penalties.
5
+ *
6
+ * @example
7
+ * banLength([{ schedule: [7 * 86_400, 30 * 86_400, "permanent"], previousOffenses: 1 }]); // 2_592_000 (30 days)
8
+ */
9
+ export function banLength(rules) {
10
+ if (rules.length === 0)
11
+ throw new Error("a ban must cite at least one rule");
12
+ const penalties = rules.map(({ schedule, previousOffenses }) => {
13
+ if (schedule.length === 0)
14
+ throw new Error("a rule has no penalty");
15
+ return schedule[Math.min(previousOffenses, schedule.length - 1)];
16
+ });
17
+ if (penalties.includes("permanent"))
18
+ return "permanent";
19
+ return Math.max(...penalties);
20
+ }
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/ban-length/index.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CAAC,KAAmB;IAC3C,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IAC7E,MAAM,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,EAAE,gBAAgB,EAAE,EAAE,EAAE;QAC7D,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;QACpE,OAAO,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,gBAAgB,EAAE,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAE,CAAC;IACpE,CAAC,CAAC,CAAC;IACH,IAAI,SAAS,CAAC,QAAQ,CAAC,WAAW,CAAC;QAAE,OAAO,WAAW,CAAC;IACxD,OAAO,IAAI,CAAC,GAAG,CAAC,GAAI,SAAsB,CAAC,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,8 @@
1
+ /** A rule's penalties for the 1st, 2nd, 3rd… offense: seconds, or "permanent". The last one repeats. */
2
+ export type PenaltySchedule = Array<number | "permanent">;
3
+ /** One broken rule: its schedule and how many times the account broke it before (in that application). */
4
+ export interface BrokenRule {
5
+ schedule: PenaltySchedule;
6
+ previousOffenses: number;
7
+ }
8
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/ban-length/type.ts"],"names":[],"mappings":"AAAA,wGAAwG;AACxG,MAAM,MAAM,eAAe,GAAG,KAAK,CAAC,MAAM,GAAG,WAAW,CAAC,CAAC;AAE1D,0GAA0G;AAC1G,MAAM,WAAW,UAAU;IACzB,QAAQ,EAAE,eAAe,CAAC;IAC1B,gBAAgB,EAAE,MAAM,CAAC;CAC1B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/ban-length/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Bytes as RFC 4648 base 32, without padding: how authenticator apps take a two-factor secret.
3
+ *
4
+ * @example
5
+ * encodeBase32(new TextEncoder().encode("foobar")); // "MZXW6YTBOI"
6
+ */
7
+ export declare function encodeBase32(bytes: Uint8Array): string;
8
+ /**
9
+ * Base 32 back to bytes, forgiving how people type it: lowercase, spaces and `=` padding are fine. Throws on any
10
+ * other character.
11
+ *
12
+ * @example
13
+ * decodeBase32("mzxw 6ytb oi"); // the bytes of "foobar"
14
+ */
15
+ export declare function decodeBase32(text: string): Uint8Array;
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/base-32/index.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CActD;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAgBrD"}
@@ -0,0 +1,50 @@
1
+ /** RFC 4648's base 32 alphabet. */
2
+ const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
3
+ /**
4
+ * Bytes as RFC 4648 base 32, without padding: how authenticator apps take a two-factor secret.
5
+ *
6
+ * @example
7
+ * encodeBase32(new TextEncoder().encode("foobar")); // "MZXW6YTBOI"
8
+ */
9
+ export function encodeBase32(bytes) {
10
+ let bits = 0;
11
+ let value = 0;
12
+ let output = "";
13
+ for (const byte of bytes) {
14
+ value = (value << 8) | byte;
15
+ bits += 8;
16
+ while (bits >= 5) {
17
+ output += ALPHABET[(value >>> (bits - 5)) & 31];
18
+ bits -= 5;
19
+ }
20
+ }
21
+ if (bits > 0)
22
+ output += ALPHABET[(value << (5 - bits)) & 31];
23
+ return output;
24
+ }
25
+ /**
26
+ * Base 32 back to bytes, forgiving how people type it: lowercase, spaces and `=` padding are fine. Throws on any
27
+ * other character.
28
+ *
29
+ * @example
30
+ * decodeBase32("mzxw 6ytb oi"); // the bytes of "foobar"
31
+ */
32
+ export function decodeBase32(text) {
33
+ const clean = text.toUpperCase().replace(/[\s=]/g, "");
34
+ let bits = 0;
35
+ let value = 0;
36
+ const output = [];
37
+ for (const character of clean) {
38
+ const index = ALPHABET.indexOf(character);
39
+ if (index === -1)
40
+ throw new Error(`"${character}" is not base 32`);
41
+ value = (value << 5) | index;
42
+ bits += 5;
43
+ if (bits >= 8) {
44
+ output.push((value >>> (bits - 8)) & 255);
45
+ bits -= 8;
46
+ }
47
+ }
48
+ return new Uint8Array(output);
49
+ }
50
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/base-32/index.ts"],"names":[],"mappings":"AAAA,mCAAmC;AACnC,MAAM,QAAQ,GAAG,kCAAkC,CAAC;AAEpD;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,KAAiB;IAC5C,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,KAAK,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC;QAC5B,IAAI,IAAI,CAAC,CAAC;QACV,OAAO,IAAI,IAAI,CAAC,EAAE,CAAC;YACjB,MAAM,IAAI,QAAQ,CAAC,CAAC,KAAK,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;YAChD,IAAI,IAAI,CAAC,CAAC;QACZ,CAAC;IACH,CAAC;IACD,IAAI,IAAI,GAAG,CAAC;QAAE,MAAM,IAAI,QAAQ,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;IAC7D,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,IAAY;IACvC,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IACvD,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,KAAK,MAAM,SAAS,IAAI,KAAK,EAAE,CAAC;QAC9B,MAAM,KAAK,GAAG,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAC1C,IAAI,KAAK,KAAK,CAAC,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,IAAI,SAAS,kBAAkB,CAAC,CAAC;QACnE,KAAK,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,KAAK,CAAC;QAC7B,IAAI,IAAI,CAAC,CAAC;QACV,IAAI,IAAI,IAAI,CAAC,EAAE,CAAC;YACd,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC;YAC1C,IAAI,IAAI,CAAC,CAAC;QACZ,CAAC;IACH,CAAC;IACD,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;AAChC,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ import type { CodePurpose, CodeRequest, CodeRequestOptions } from "@sharadox/account-web-library/code-request/type";
4
+ /** The purposes a code can have. */
5
+ export declare const CODE_PURPOSES: readonly CodePurpose[];
6
+ /**
7
+ * Starts a one-time code for a contact: a new code (8 digits, 10 minutes by default), stored only as an HMAC bound
8
+ * to its purpose and request, replacing any open request for the same contact and purpose. Returns the code to send
9
+ * through the contact's channel. Rate limit and cap the sends before calling it (protection-web-library).
10
+ *
11
+ * @example
12
+ * const { requestId, code } = await requestCode(database, { purpose: "sign-in", contact, pepper });
13
+ * await channel.send(contact, code); // after answering the request, so timing reveals nothing
14
+ */
15
+ export declare function requestCode(database: Kysely<AccountDatabase>, { purpose, contact, accountId, pepper, length, lifetimeSeconds, now }: CodeRequestOptions): Promise<CodeRequest>;
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/code-request/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,EAAE,WAAW,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,iDAAiD,CAAC;AAIpH,oCAAoC;AACpC,eAAO,MAAM,aAAa,EAAE,SAAS,WAAW,EAAmD,CAAC;AAEpG;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAC/B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAU,EAAE,eAAqB,EAAE,GAAgB,EAAE,EAAE,kBAAkB,GAC/G,OAAO,CAAC,WAAW,CAAC,CAmCtB"}
@@ -0,0 +1,52 @@
1
+ import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
2
+ import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
3
+ import { createOneTimeCode, hashOneTimeCode } from "@sharadox/account-web-library/one-time-code";
4
+ /** The purposes a code can have. */
5
+ export const CODE_PURPOSES = ["sign-in", "contact-verification", "step-up"];
6
+ /**
7
+ * Starts a one-time code for a contact: a new code (8 digits, 10 minutes by default), stored only as an HMAC bound
8
+ * to its purpose and request, replacing any open request for the same contact and purpose. Returns the code to send
9
+ * through the contact's channel. Rate limit and cap the sends before calling it (protection-web-library).
10
+ *
11
+ * @example
12
+ * const { requestId, code } = await requestCode(database, { purpose: "sign-in", contact, pepper });
13
+ * await channel.send(contact, code); // after answering the request, so timing reveals nothing
14
+ */
15
+ export async function requestCode(database, { purpose, contact, accountId, pepper, length = 8, lifetimeSeconds = 600, now = Date.now() }) {
16
+ if (!CODE_PURPOSES.includes(purpose))
17
+ throw new Error(`unknown code purpose "${purpose}"`);
18
+ const { kind, value } = normalizeContact(contact);
19
+ const requestId = uuidVersion7({ now });
20
+ const code = createOneTimeCode(length);
21
+ const codeHash = hashOneTimeCode(code, { pepper, purpose: `${purpose}:${requestId}` });
22
+ const createdAt = new Date(now);
23
+ const expiresAt = new Date(now + lifetimeSeconds * 1000);
24
+ await database.transaction().execute(async (transaction) => {
25
+ await transaction
26
+ .updateTable("code_request")
27
+ .set({ voided_at: createdAt })
28
+ .where("kind", "=", kind)
29
+ .where("value", "=", value)
30
+ .where("purpose", "=", purpose)
31
+ .where("used_at", "is", null)
32
+ .where("voided_at", "is", null)
33
+ .execute();
34
+ await transaction
35
+ .insertInto("code_request")
36
+ .values({
37
+ id: requestId,
38
+ purpose,
39
+ account_id: accountId ?? null,
40
+ kind,
41
+ value,
42
+ code_hash: codeHash,
43
+ created_at: createdAt,
44
+ expires_at: expiresAt,
45
+ used_at: null,
46
+ voided_at: null,
47
+ })
48
+ .execute();
49
+ });
50
+ return { requestId, code, contact: { kind, value }, expiresAt };
51
+ }
52
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/code-request/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAG/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAC9E,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,6CAA6C,CAAC;AAEjG,oCAAoC;AACpC,MAAM,CAAC,MAAM,aAAa,GAA2B,CAAC,SAAS,EAAE,sBAAsB,EAAE,SAAS,CAAC,CAAC;AAEpG;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,QAAiC,EACjC,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,CAAC,EAAE,eAAe,GAAG,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAA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I;YACJ,KAAK;YACL,SAAS,EAAE,QAAQ;YACnB,UAAU,EAAE,SAAS;YACrB,UAAU,EAAE,SAAS;YACrB,OAAO,EAAE,IAAI;YACb,SAAS,EAAE,IAAI;SAChB,CAAC;aACD,OAAO,EAAE,CAAC;IACf,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,CAAC;AAClE,CAAC"}
@@ -0,0 +1,25 @@
1
+ import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
2
+ /** What a one-time code is for; a code works only for its own purpose and request. */
3
+ export type CodePurpose = "sign-in" | "contact-verification" | "step-up";
4
+ export interface CodeRequestOptions {
5
+ purpose: CodePurpose;
6
+ contact: Contact;
7
+ /** The account asking (step-up, contact verification). */
8
+ accountId?: string;
9
+ /** At least 32 characters, kept outside the database. */
10
+ pepper: string;
11
+ /** Digits (8 by default). */
12
+ length?: number;
13
+ /** 600 (10 minutes) by default. */
14
+ lifetimeSeconds?: number;
15
+ /** Milliseconds since 1970 (now by default). */
16
+ now?: number;
17
+ }
18
+ export interface CodeRequest {
19
+ requestId: string;
20
+ /** Send it, then forget it: only its hash is stored. */
21
+ code: string;
22
+ contact: Contact;
23
+ expiresAt: Date;
24
+ }
25
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/code-request/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,sFAAsF;AACtF,MAAM,MAAM,WAAW,GAAG,SAAS,GAAG,sBAAsB,GAAG,SAAS,CAAC;AAEzE,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,WAAW,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,0DAA0D;IAC1D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,yDAAyD;IACzD,MAAM,EAAE,MAAM,CAAC;IACf,6BAA6B;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mCAAmC;IACnC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,WAAW;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,IAAI,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/code-request/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,13 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ /**
4
+ * Deletes code requests that expired before `before`, with the contacts they hold, from a daily job. Returns how
5
+ * many were deleted.
6
+ *
7
+ * @example
8
+ * await pruneCodeRequests(database, { before: new Date(Date.now() - 86_400_000) });
9
+ */
10
+ export declare function pruneCodeRequests(database: Kysely<AccountDatabase>, { before }: {
11
+ before: Date;
12
+ }): Promise<number>;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/code-request-prune/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAE3F;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,MAAM,EAAE,EAAE;IAAE,MAAM,EAAE,IAAI,CAAA;CAAE,GAC3B,OAAO,CAAC,MAAM,CAAC,CAGjB"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Deletes code requests that expired before `before`, with the contacts they hold, from a daily job. Returns how
3
+ * many were deleted.
4
+ *
5
+ * @example
6
+ * await pruneCodeRequests(database, { before: new Date(Date.now() - 86_400_000) });
7
+ */
8
+ export async function pruneCodeRequests(database, { before }) {
9
+ const result = await database.deleteFrom("code_request").where("expires_at", "<", before).executeTakeFirst();
10
+ return Number(result.numDeletedRows);
11
+ }
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/code-request-prune/index.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,QAAiC,EACjC,EAAE,MAAM,EAAoB;IAE5B,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,UAAU,CAAC,cAAc,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC,gBAAgB,EAAE,CAAC;IAC7G,OAAO,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;AACvC,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 { CodeVerificationOptions, CodeVerificationResult } from "@sharadox/account-web-library/code-verification/type";
4
+ /**
5
+ * Checks a typed one-time code against its request, with the row locked so concurrent guesses can't pass the
6
+ * attempt limit. The right code works once, before it expires, unless a newer request replaced it or 5 wrong codes
7
+ * came first. Returns the proved contact and the account that asked (null for sign-in).
8
+ *
9
+ * @example
10
+ * const result = await verifyCode(database, { requestId, code: typed, purpose: "sign-in", pepper });
11
+ * if (result.valid) signIn(await findAccountByContact(database, result.contact));
12
+ */
13
+ export declare function verifyCode(database: Kysely<AccountDatabase>, { requestId, code, purpose, pepper, maximumAttempts, now }: CodeVerificationOptions): Promise<CodeVerificationResult>;
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/code-verification/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,EACV,uBAAuB,EACvB,sBAAsB,EACvB,MAAM,sDAAsD,CAAC;AAK9D;;;;;;;;GAQG;AACH,wBAAsB,UAAU,CAC9B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,eAAmB,EAAE,GAAgB,EAAE,EAAE,uBAAuB,GACnG,OAAO,CAAC,sBAAsB,CAAC,CAwBjC"}
@@ -0,0 +1,43 @@
1
+ import { matchesOneTimeCode } from "@sharadox/account-web-library/one-time-code";
2
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
3
+ /**
4
+ * Checks a typed one-time code against its request, with the row locked so concurrent guesses can't pass the
5
+ * attempt limit. The right code works once, before it expires, unless a newer request replaced it or 5 wrong codes
6
+ * came first. Returns the proved contact and the account that asked (null for sign-in).
7
+ *
8
+ * @example
9
+ * const result = await verifyCode(database, { requestId, code: typed, purpose: "sign-in", pepper });
10
+ * if (result.valid) signIn(await findAccountByContact(database, result.contact));
11
+ */
12
+ export async function verifyCode(database, { requestId, code, purpose, pepper, maximumAttempts = 5, now = Date.now() }) {
13
+ if (!UUID.test(requestId))
14
+ return { valid: false, reason: "unknown" };
15
+ return database.transaction().execute(async (transaction) => {
16
+ const request = await transaction
17
+ .selectFrom("code_request")
18
+ .selectAll()
19
+ .where("id", "=", requestId)
20
+ .where("purpose", "=", purpose)
21
+ .forUpdate()
22
+ .executeTakeFirst();
23
+ if (!request)
24
+ return { valid: false, reason: "unknown" };
25
+ if (request.used_at)
26
+ return { valid: false, reason: "used" };
27
+ if (request.voided_at)
28
+ return { valid: false, reason: "replaced" };
29
+ if (request.expires_at.getTime() <= now)
30
+ return { valid: false, reason: "expired" };
31
+ if (request.attempts >= maximumAttempts)
32
+ return { valid: false, reason: "attempts" };
33
+ const at = transaction.updateTable("code_request").where("id", "=", requestId);
34
+ if (matchesOneTimeCode(code, request.code_hash, { pepper, purpose: `${purpose}:${requestId}` })) {
35
+ await at.set({ used_at: new Date(now) }).execute();
36
+ return { valid: true, contact: { kind: request.kind, value: request.value }, accountId: request.account_id };
37
+ }
38
+ const attempts = request.attempts + 1;
39
+ await at.set({ attempts }).execute();
40
+ return { valid: false, reason: attempts >= maximumAttempts ? "attempts" : "wrong" };
41
+ });
42
+ }
43
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/code-verification/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,kBAAkB,EAAE,MAAM,6CAA6C,CAAC;AAEjF,MAAM,IAAI,GAAG,iEAAiE,CAAC;AAE/E;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,QAAiC,EACjC,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,eAAe,GAAG,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAA2B;IAEpG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IACtE,OAAO,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAmC,EAAE;QAC3F,MAAM,OAAO,GAAG,MAAM,WAAW;aAC9B,UAAU,CAAC,cAAc,CAAC;aAC1B,SAAS,EAAE;aACX,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;aAC3B,KAAK,CAAC,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AAG,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;QACtC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;QACrC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,IAAI,eAAe,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;IACtF,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,22 @@
1
+ import type { CodePurpose } from "@sharadox/account-web-library/code-request/type";
2
+ import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
3
+ export interface CodeVerificationOptions {
4
+ requestId: string;
5
+ /** As typed: spaces are ignored. */
6
+ code: string;
7
+ purpose: CodePurpose;
8
+ pepper: string;
9
+ /** Wrong codes allowed before the request is dead (5 by default). */
10
+ maximumAttempts?: number;
11
+ /** Milliseconds since 1970 (now by default). */
12
+ now?: number;
13
+ }
14
+ export type CodeVerificationResult = {
15
+ valid: true;
16
+ contact: Contact;
17
+ accountId: string | null;
18
+ } | {
19
+ valid: false;
20
+ reason: "unknown" | "used" | "replaced" | "expired" | "attempts" | "wrong";
21
+ };
22
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/code-verification/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iDAAiD,CAAC;AACnF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,MAAM,WAAW,uBAAuB;IACtC,SAAS,EAAE,MAAM,CAAC;IAClB,oCAAoC;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,WAAW,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,qEAAqE;IACrE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,sBAAsB,GAC9B;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,OAAO,EAAE,OAAO,CAAC;IAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GAC3D;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,SAAS,GAAG,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,UAAU,GAAG,OAAO,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/code-verification/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,13 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ import type { ContactAdditionRequest, ContactAdditionResult } from "@sharadox/account-web-library/contact-addition/type";
4
+ /**
5
+ * Adds a contact to an account, unverified until a one-time code sent to it comes back (`verifyContact`). Refuses
6
+ * one the account already has, or one another account has verified. Ask for a fresh code first (step-up) and tell
7
+ * the other contacts.
8
+ *
9
+ * @example
10
+ * const result = await addContact(database, { accountId, contact: { kind: "email", value } });
11
+ */
12
+ export declare function addContact(database: Kysely<AccountDatabase>, { accountId, contact, now }: ContactAdditionRequest): Promise<ContactAdditionResult>;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-addition/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAE3F,OAAO,KAAK,EACV,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,qDAAqD,CAAC;AAI7D;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAC9B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,OAAO,EAAE,GAAgB,EAAE,EAAE,sBAAsB,GAC/D,OAAO,CAAC,qBAAqB,CAAC,CA8BhC"}
@@ -0,0 +1,48 @@
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
+ /**
6
+ * Adds a contact to an account, unverified until a one-time code sent to it comes back (`verifyContact`). Refuses
7
+ * one the account already has, or one another account has verified. Ask for a fresh code first (step-up) and tell
8
+ * the other contacts.
9
+ *
10
+ * @example
11
+ * const result = await addContact(database, { accountId, contact: { kind: "email", value } });
12
+ */
13
+ export async function addContact(database, { accountId, contact, now = Date.now() }) {
14
+ const { kind, value } = normalizeContact(contact);
15
+ const contactId = uuidVersion7({ now });
16
+ try {
17
+ return await database.transaction().execute(async (transaction) => {
18
+ const account = await transaction
19
+ .selectFrom("account")
20
+ .select("id")
21
+ .where("id", "=", accountId)
22
+ .executeTakeFirst();
23
+ if (!account)
24
+ return { added: false, reason: "no-account" };
25
+ const owner = await transaction
26
+ .selectFrom("contact")
27
+ .select("account_id")
28
+ .where("kind", "=", kind)
29
+ .where("value", "=", value)
30
+ .where("verified_at", "is not", null)
31
+ .executeTakeFirst();
32
+ if (owner && owner.account_id !== accountId)
33
+ return { added: false, reason: "contact-taken" };
34
+ await transaction
35
+ .insertInto("contact")
36
+ .values({ id: contactId, account_id: accountId, kind, value, verified_at: null, created_at: new Date(now) })
37
+ .execute();
38
+ await recordAuditEvent(transaction, { accountId, action: "contact.added", detail: { contactId, kind }, now });
39
+ return { added: true, contactId };
40
+ });
41
+ }
42
+ catch (error) {
43
+ if (databaseError(error)?.constraint === "contact_per_account")
44
+ return { added: false, reason: "already-added" };
45
+ throw error;
46
+ }
47
+ }
48
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1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AI,CAAC;iBACpC,gBAAgB,EAAE,CAAC;YACtB,IAAI,KAAK,IAAI,KAAK,CAAC,UAAU,KAAK,SAAS;gBAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC;YAC9F,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,IAAI,EAAE,UAAU,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;iBAC3G,OAAO,EAAE,CAAC;YACb,MAAM,gBAAgB,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;YAC9G,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;QACpC,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,UAAU,KAAK,qBAAqB;YAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC;QACjH,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
2
+ export interface ContactAdditionRequest {
3
+ accountId: string;
4
+ contact: Contact;
5
+ /** Milliseconds since 1970 (now by default). */
6
+ now?: number;
7
+ }
8
+ export type ContactAdditionResult = {
9
+ added: true;
10
+ contactId: string;
11
+ } | {
12
+ added: false;
13
+ reason: "already-added" | "contact-taken" | "no-account";
14
+ };
15
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/contact-addition/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,MAAM,WAAW,sBAAsB;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;IACjB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,qBAAqB,GAC/B;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,eAAe,GAAG,eAAe,GAAG,YAAY,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/contact-addition/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,12 @@
1
+ import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
2
+ /** The kinds of contact this version can store and send codes to. */
3
+ export declare const CONTACT_KINDS: string[];
4
+ /**
5
+ * A contact in the one form it is stored and compared in (an email trimmed and lowercase…). Throws for a kind this
6
+ * version doesn't know, or a value that isn't valid for its kind.
7
+ *
8
+ * @example
9
+ * normalizeContact({ kind: "email", value: " Luis@Example.com " }); // { kind: "email", value: "luis@example.com" }
10
+ */
11
+ export declare function normalizeContact({ kind, value }: Contact): Contact;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-kind/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAI/E,qEAAqE;AACrE,eAAO,MAAM,aAAa,UAA2B,CAAC;AAEtD;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,OAAO,GAAG,OAAO,CAIlE"}
@@ -0,0 +1,18 @@
1
+ import { normalizeEmail } from "@sharadox/account-web-library/email-contact";
2
+ const NORMALIZERS = { email: normalizeEmail };
3
+ /** The kinds of contact this version can store and send codes to. */
4
+ export const CONTACT_KINDS = Object.keys(NORMALIZERS);
5
+ /**
6
+ * A contact in the one form it is stored and compared in (an email trimmed and lowercase…). Throws for a kind this
7
+ * version doesn't know, or a value that isn't valid for its kind.
8
+ *
9
+ * @example
10
+ * normalizeContact({ kind: "email", value: " Luis@Example.com " }); // { kind: "email", value: "luis@example.com" }
11
+ */
12
+ export function normalizeContact({ kind, value }) {
13
+ const normalize = NORMALIZERS[kind];
14
+ if (!normalize)
15
+ throw new Error(`unknown contact kind "${kind}"`);
16
+ return { kind, value: normalize(value) };
17
+ }
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-kind/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,6CAA6C,CAAC;AAG7E,MAAM,WAAW,GAA8C,EAAE,KAAK,EAAE,cAAc,EAAE,CAAC;AAEzF,qEAAqE;AACrE,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;AAEtD;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,EAAE,IAAI,EAAE,KAAK,EAAW;IACvD,MAAM,SAAS,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IACpC,IAAI,CAAC,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,GAAG,CAAC,CAAC;IAClE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,6 @@
1
+ /** A contact as given or stored: its kind (`email`…) and value. */
2
+ export interface Contact {
3
+ kind: string;
4
+ value: string;
5
+ }
6
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/contact-kind/type.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/contact-kind/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,16 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
4
+ /**
5
+ * The account a verified contact belongs to (written any way: it is normalized first), or undefined. Unverified
6
+ * contacts never count, so claiming someone's address proves nothing.
7
+ *
8
+ * @example
9
+ * const found = await findAccountByContact(database, { kind: "email", value: typed });
10
+ */
11
+ export declare function findAccountByContact(database: Kysely<AccountDatabase>, contact: Contact): Promise<{
12
+ accountId: string;
13
+ contactId: string;
14
+ signIn: boolean;
15
+ } | undefined>;
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-lookup/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAE3F,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,OAAO,EAAE,OAAO,GACf,OAAO,CAAC;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GAAG,SAAS,CAAC,CAUhF"}
@@ -0,0 +1,20 @@
1
+ import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
2
+ /**
3
+ * The account a verified contact belongs to (written any way: it is normalized first), or undefined. Unverified
4
+ * contacts never count, so claiming someone's address proves nothing.
5
+ *
6
+ * @example
7
+ * const found = await findAccountByContact(database, { kind: "email", value: typed });
8
+ */
9
+ export async function findAccountByContact(database, contact) {
10
+ const { kind, value } = normalizeContact(contact);
11
+ const row = await database
12
+ .selectFrom("contact")
13
+ .select(["id", "account_id", "sign_in"])
14
+ .where("kind", "=", kind)
15
+ .where("value", "=", value)
16
+ .where("verified_at", "is not", null)
17
+ .executeTakeFirst();
18
+ return row && { accountId: row.account_id, contactId: row.id, signIn: row.sign_in };
19
+ }
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-lookup/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAG9E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,QAAiC,EACjC,OAAgB;IAEhB,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAClD,MAAM,GAAG,GAAG,MAAM,QAAQ;SACvB,UAAU,CAAC,SAAS,CAAC;SACrB,MAAM,CAAC,CAAC,IAAI,EAAE,YAAY,EAAE,SAAS,CAAC,CAAC;SACvC,KAAK,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC;SACxB,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC;SAC1B,KAAK,CAAC,aAAa,EAAE,QAAQ,EAAE,IAAI,CAAC;SACpC,gBAAgB,EAAE,CAAC;IACtB,OAAO,GAAG,IAAI,EAAE,SAAS,EAAE,GAAG,CAAC,UAAU,EAAE,SAAS,EAAE,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC;AACtF,CAAC"}