@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,17 @@
1
+ import { createMemoryDatabase } from "@sharadox/database-web-library/memory-database";
2
+ import { migrateAccount } from "@sharadox/account-web-library/account-migration";
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 async function createTestAccountDatabase() {
13
+ const memory = await createMemoryDatabase();
14
+ await migrateAccount(memory.database);
15
+ return memory;
16
+ }
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/test-database/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,gDAAgD,CAAC;AAGtF,OAAO,EAAE,cAAc,EAAE,MAAM,iDAAiD,CAAC;AAEjF;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,yBAAyB;IAC7C,MAAM,MAAM,GAAG,MAAM,oBAAoB,EAAmB,CAAC;IAC7D,MAAM,cAAc,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACtC,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,35 @@
1
+ import type { TotpOptions, TotpUriOptions, TotpVerification, VerifyTotpOptions } from "@sharadox/account-web-library/totp/type";
2
+ /**
3
+ * The time-based one-time code (RFC 6238, SHA-1, as every authenticator app does it) for a moment in milliseconds.
4
+ *
5
+ * @example
6
+ * totpCode(secret, Date.now()); // "287082"
7
+ */
8
+ export declare function totpCode(secret: Uint8Array, now: number, { digits, period }?: TotpOptions): string;
9
+ /**
10
+ * Checks a code typed from an authenticator app. The current step and one either side are accepted, for clocks that
11
+ * drift. Comparison is constant-time. A step at or before `lastUsedStep` is refused, so a code someone saw can't be
12
+ * replayed. On success, store the returned `step`.
13
+ *
14
+ * @example
15
+ * const result = verifyTotp(secret, typed, Date.now(), { lastUsedStep: account.totpLastStep });
16
+ * if (result.valid) await saveLastStep(result.step);
17
+ */
18
+ export declare function verifyTotp(secret: Uint8Array, code: string, now: number, { digits, period, lastUsedStep }?: VerifyTotpOptions): TotpVerification;
19
+ /**
20
+ * A new two-factor secret: 20 random bytes (160 bits, as RFC 4226 recommends). Store it encrypted.
21
+ *
22
+ * @example
23
+ * const secret = createTotpSecret();
24
+ */
25
+ export declare function createTotpSecret(): Uint8Array;
26
+ /**
27
+ * The `otpauth://totp/…` address an authenticator app adds an account from. Show it as a QR code, and show the
28
+ * `secret` parameter as text for people who type it in.
29
+ *
30
+ * @example
31
+ * totpUri({ issuer: "Luis Pires Decor", account: "luis-pires", secret });
32
+ * // "otpauth://totp/Luis%20Pires%20Decor:luis-pires?secret=…&issuer=Luis%20Pires%20Decor&algorithm=SHA1&digits=6&period=30"
33
+ */
34
+ export declare function totpUri({ issuer, account, secret }: TotpUriOptions): string;
35
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/totp/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,yCAAyC,CAAC;AAYjD;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,MAAU,EAAE,MAAW,EAAE,GAAE,WAAgB,GAAG,MAAM,CAE/G;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CACxB,MAAM,EAAE,UAAU,EAClB,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,EACX,EAAE,MAAU,EAAE,MAAW,EAAE,YAAiB,EAAE,GAAE,iBAAsB,GACrE,gBAAgB,CASlB;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,IAAI,UAAU,CAE7C;AAED;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,cAAc,GAAG,MAAM,CAU3E"}
@@ -0,0 +1,70 @@
1
+ import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
2
+ import { encodeBase32 } from "@sharadox/account-web-library/base-32";
3
+ /** The code for one time step (RFC 4226 HOTP with SHA-1, dynamic truncation). */
4
+ function codeForStep(secret, step, digits) {
5
+ const counter = Buffer.alloc(8);
6
+ counter.writeBigUInt64BE(BigInt(step));
7
+ const hash = createHmac("sha1", secret).update(counter).digest();
8
+ const offset = hash[hash.length - 1] & 0xf;
9
+ const binary = hash.readUInt32BE(offset) & 0x7fffffff;
10
+ return String(binary % 10 ** digits).padStart(digits, "0");
11
+ }
12
+ /**
13
+ * The time-based one-time code (RFC 6238, SHA-1, as every authenticator app does it) for a moment in milliseconds.
14
+ *
15
+ * @example
16
+ * totpCode(secret, Date.now()); // "287082"
17
+ */
18
+ export function totpCode(secret, now, { digits = 6, period = 30 } = {}) {
19
+ return codeForStep(secret, Math.floor(now / (period * 1000)), digits);
20
+ }
21
+ /**
22
+ * Checks a code typed from an authenticator app. The current step and one either side are accepted, for clocks that
23
+ * drift. Comparison is constant-time. A step at or before `lastUsedStep` is refused, so a code someone saw can't be
24
+ * replayed. On success, store the returned `step`.
25
+ *
26
+ * @example
27
+ * const result = verifyTotp(secret, typed, Date.now(), { lastUsedStep: account.totpLastStep });
28
+ * if (result.valid) await saveLastStep(result.step);
29
+ */
30
+ export function verifyTotp(secret, code, now, { digits = 6, period = 30, lastUsedStep = -1 } = {}) {
31
+ if (!new RegExp(`^\\d{${digits}}$`).test(code))
32
+ return { valid: false };
33
+ const current = Math.floor(now / (period * 1000));
34
+ for (const step of [current - 1, current, current + 1]) {
35
+ if (step <= lastUsedStep)
36
+ continue;
37
+ if (timingSafeEqual(Buffer.from(codeForStep(secret, step, digits)), Buffer.from(code)))
38
+ return { valid: true, step };
39
+ }
40
+ return { valid: false };
41
+ }
42
+ /**
43
+ * A new two-factor secret: 20 random bytes (160 bits, as RFC 4226 recommends). Store it encrypted.
44
+ *
45
+ * @example
46
+ * const secret = createTotpSecret();
47
+ */
48
+ export function createTotpSecret() {
49
+ return new Uint8Array(randomBytes(20));
50
+ }
51
+ /**
52
+ * The `otpauth://totp/…` address an authenticator app adds an account from. Show it as a QR code, and show the
53
+ * `secret` parameter as text for people who type it in.
54
+ *
55
+ * @example
56
+ * totpUri({ issuer: "Luis Pires Decor", account: "luis-pires", secret });
57
+ * // "otpauth://totp/Luis%20Pires%20Decor:luis-pires?secret=…&issuer=Luis%20Pires%20Decor&algorithm=SHA1&digits=6&period=30"
58
+ */
59
+ export function totpUri({ issuer, account, secret }) {
60
+ const label = `${encodeURIComponent(issuer)}:${encodeURIComponent(account)}`;
61
+ const parameters = new URLSearchParams({
62
+ secret: encodeBase32(secret),
63
+ issuer,
64
+ algorithm: "SHA1",
65
+ digits: "6",
66
+ period: "30",
67
+ });
68
+ return `otpauth://totp/${label}?${parameters.toString().replace(/\+/g, "%20")}`;
69
+ }
70
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/totp/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,YAAY,EAAE,MAAM,uCAAuC,CAAC;AAQrE,iFAAiF;AACjF,SAAS,WAAW,CAAC,MAAkB,EAAE,IAAY,EAAE,MAAc;IACnE,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAChC,OAAO,CAAC,gBAAgB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;IACvC,MAAM,IAAI,GAAG,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC;IACjE,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAE,GAAG,GAAG,CAAC;IAC5C,MAAM,MAAM,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC;IACtD,OAAO,MAAM,CAAC,MAAM,GAAG,EAAE,IAAI,MAAM,CAAC,CAAC,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAC7D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,Q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;AACH,MAAM,UAAU,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAkB;IACjE,MAAM,KAAK,GAAG,GAAG,kBAAkB,CAAC,MAAM,CAAC,IAAI,kBAAkB,CAAC,OAAO,CAAC,EAAE,CAAC;IAC7E,MAAM,UAAU,GAAG,IAAI,eAAe,CAAC;QACrC,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC;QAC5B,MAAM;QACN,SAAS,EAAE,MAAM;QACjB,MAAM,EAAE,GAAG;QACX,MAAM,EAAE,IAAI;KACb,CAAC,CAAC;IACH,OAAO,kBAAkB,KAAK,IAAI,UAAU,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,CAAC;AAClF,CAAC"}
@@ -0,0 +1,25 @@
1
+ /** Code length (6, what authenticator apps show) and step length in seconds (30). */
2
+ export interface TotpOptions {
3
+ digits?: number;
4
+ period?: number;
5
+ }
6
+ /** Verification also takes the step of the last accepted code, so it can't be used twice. */
7
+ export interface VerifyTotpOptions extends TotpOptions {
8
+ lastUsedStep?: number;
9
+ }
10
+ /** A valid code (store `step` as the new last used step), or not. */
11
+ export type TotpVerification = {
12
+ valid: true;
13
+ step: number;
14
+ } | {
15
+ valid: false;
16
+ };
17
+ /** What the otpauth address shows in the authenticator app. */
18
+ export interface TotpUriOptions {
19
+ /** The company, shown above the code. */
20
+ issuer: string;
21
+ /** Which account: the username. */
22
+ account: string;
23
+ secret: Uint8Array;
24
+ }
25
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/totp/type.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,6FAA6F;AAC7F,MAAM,WAAW,iBAAkB,SAAQ,WAAW;IACpD,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,qEAAqE;AACrE,MAAM,MAAM,gBAAgB,GAAG;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC;AAEhF,+DAA+D;AAC/D,MAAM,WAAW,cAAc;IAC7B,yCAAyC;IACzC,MAAM,EAAE,MAAM,CAAC;IACf,mCAAmC;IACnC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,UAAU,CAAC;CACpB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/totp/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,23 @@
1
+ import type { UsernameCheck } from "@sharadox/account-web-library/username/type";
2
+ /** The longest username. */
3
+ export declare const USERNAME_MAXIMUM_LENGTH = 50;
4
+ /**
5
+ * Checks a username against the rules:
6
+ * - 1 to 50 characters;
7
+ * - only A–Z, a–z and 0–9, with `-`, `_` or `.` allowed only between letters or digits, never two in a row;
8
+ * - shown as written, unique without regard to case (see `usernameKey`).
9
+ *
10
+ * @example
11
+ * checkUsername("Luis-Pires"); // { valid: true, username: "Luis-Pires", key: "luis-pires" }
12
+ * checkUsername("a--b"); // { valid: false, reason: "characters" }
13
+ */
14
+ export declare function checkUsername(username: string): UsernameCheck;
15
+ /**
16
+ * The form a username is unique in: lowercase, so `abc` and `aBc` are the same username (the database's unique index
17
+ * is on this key).
18
+ *
19
+ * @example
20
+ * usernameKey("aBc"); // "abc"
21
+ */
22
+ export declare function usernameKey(username: string): string;
23
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/username/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6CAA6C,CAAC;AAKjF,4BAA4B;AAC5B,eAAO,MAAM,uBAAuB,KAAK,CAAC;AAE1C;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,aAAa,CAI7D;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEpD"}
@@ -0,0 +1,32 @@
1
+ /** Letters and digits, with single `-`, `_` or `.` only between them. */
2
+ const PATTERN = /^[A-Za-z0-9]+(?:[-_.][A-Za-z0-9]+)*$/;
3
+ /** The longest username. */
4
+ export const USERNAME_MAXIMUM_LENGTH = 50;
5
+ /**
6
+ * Checks a username against the rules:
7
+ * - 1 to 50 characters;
8
+ * - only A–Z, a–z and 0–9, with `-`, `_` or `.` allowed only between letters or digits, never two in a row;
9
+ * - shown as written, unique without regard to case (see `usernameKey`).
10
+ *
11
+ * @example
12
+ * checkUsername("Luis-Pires"); // { valid: true, username: "Luis-Pires", key: "luis-pires" }
13
+ * checkUsername("a--b"); // { valid: false, reason: "characters" }
14
+ */
15
+ export function checkUsername(username) {
16
+ if (username.length < 1 || username.length > USERNAME_MAXIMUM_LENGTH)
17
+ return { valid: false, reason: "length" };
18
+ if (!PATTERN.test(username))
19
+ return { valid: false, reason: "characters" };
20
+ return { valid: true, username, key: usernameKey(username) };
21
+ }
22
+ /**
23
+ * The form a username is unique in: lowercase, so `abc` and `aBc` are the same username (the database's unique index
24
+ * is on this key).
25
+ *
26
+ * @example
27
+ * usernameKey("aBc"); // "abc"
28
+ */
29
+ export function usernameKey(username) {
30
+ return username.toLowerCase();
31
+ }
32
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/username/index.ts"],"names":[],"mappings":"AAEA,yEAAyE;AACzE,MAAM,OAAO,GAAG,sCAAsC,CAAC;AAEvD,4BAA4B;AAC5B,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,CAAC;AAE1C;;;;;;;;;GASG;AACH,MAAM,UAAU,aAAa,CAAC,QAAgB;IAC5C,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,IAAI,QAAQ,CAAC,MAAM,GAAG,uBAAuB;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;IAChH,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;IAC3E,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC;AAC/D,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,QAAgB;IAC1C,OAAO,QAAQ,CAAC,WAAW,EAAE,CAAC;AAChC,CAAC"}
@@ -0,0 +1,10 @@
1
+ /** A username that follows the rules (with its `key` for uniqueness), or why not. */
2
+ export type UsernameCheck = {
3
+ valid: true;
4
+ username: string;
5
+ key: string;
6
+ } | {
7
+ valid: false;
8
+ reason: "length" | "characters";
9
+ };
10
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/username/type.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,MAAM,MAAM,aAAa,GACvB;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,QAAQ,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/username/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,11 @@
1
+ import { type Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ /**
4
+ * Whether a username can be taken: `available`, `taken` (in any case: `aBc` is taken when `abc` exists) or `invalid`
5
+ * (outside the rules). Rate limit callers: it tells whether an account exists.
6
+ *
7
+ * @example
8
+ * await usernameAvailability(database, "Luis-Pires"); // "available"
9
+ */
10
+ export declare function usernameAvailability(database: Kysely<AccountDatabase>, username: string): Promise<"available" | "taken" | "invalid">;
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/username-availability/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAG3F;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,WAAW,GAAG,OAAO,GAAG,SAAS,CAAC,CAS5C"}
@@ -0,0 +1,21 @@
1
+ import { sql } from "kysely";
2
+ import { checkUsername } from "@sharadox/account-web-library/username";
3
+ /**
4
+ * Whether a username can be taken: `available`, `taken` (in any case: `aBc` is taken when `abc` exists) or `invalid`
5
+ * (outside the rules). Rate limit callers: it tells whether an account exists.
6
+ *
7
+ * @example
8
+ * await usernameAvailability(database, "Luis-Pires"); // "available"
9
+ */
10
+ export async function usernameAvailability(database, username) {
11
+ const check = checkUsername(username);
12
+ if (!check.valid)
13
+ return "invalid";
14
+ const found = await database
15
+ .selectFrom("account")
16
+ .select("id")
17
+ .where(sql `lower(username)`, "=", check.key)
18
+ .executeTakeFirst();
19
+ return found ? "taken" : "available";
20
+ }
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/username-availability/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAe,MAAM,QAAQ,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AAEvE;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,QAAiC,EACjC,QAAgB;IAEhB,MAAM,KAAK,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;IACtC,IAAI,CAAC,KAAK,CAAC,KAAK;QAAE,OAAO,SAAS,CAAC;IACnC,MAAM,KAAK,GAAG,MAAM,QAAQ;SACzB,UAAU,CAAC,SAAS,CAAC;SACrB,MAAM,CAAC,IAAI,CAAC;SACZ,KAAK,CAAC,GAAG,CAAQ,iBAAiB,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC;SACnD,gBAAgB,EAAE,CAAC;IACtB,OAAO,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC;AACvC,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ import type { UsernameChangeResult } from "@sharadox/account-web-library/username-change/type";
4
+ /**
5
+ * Changes an account's username whenever its owner wants; the old one is free for anyone at once. Accounts are
6
+ * referenced by id, so nothing breaks; the audit log keeps the history. Changing only the case is allowed.
7
+ *
8
+ * @example
9
+ * await changeUsername(database, { accountId, username: "Luis-Pires" }); // { changed: true }
10
+ */
11
+ export declare function changeUsername(database: Kysely<AccountDatabase>, { accountId, username }: {
12
+ accountId: string;
13
+ username: string;
14
+ }): Promise<UsernameChangeResult>;
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/username-change/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oDAAoD,CAAC;AAE/F;;;;;;GAMG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC/D,OAAO,CAAC,oBAAoB,CAAC,CAwB/B"}
@@ -0,0 +1,39 @@
1
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
2
+ import { databaseError } from "@sharadox/account-web-library/database-error";
3
+ import { checkUsername } from "@sharadox/account-web-library/username";
4
+ /**
5
+ * Changes an account's username whenever its owner wants; the old one is free for anyone at once. Accounts are
6
+ * referenced by id, so nothing breaks; the audit log keeps the history. Changing only the case is allowed.
7
+ *
8
+ * @example
9
+ * await changeUsername(database, { accountId, username: "Luis-Pires" }); // { changed: true }
10
+ */
11
+ export async function changeUsername(database, { accountId, username }) {
12
+ if (!checkUsername(username).valid)
13
+ return { changed: false, reason: "username-invalid" };
14
+ try {
15
+ return await database.transaction().execute(async (transaction) => {
16
+ const current = await transaction
17
+ .selectFrom("account")
18
+ .select("username")
19
+ .where("id", "=", accountId)
20
+ .forUpdate()
21
+ .executeTakeFirst();
22
+ if (!current)
23
+ return { changed: false, reason: "no-account" };
24
+ await transaction.updateTable("account").set({ username }).where("id", "=", accountId).execute();
25
+ await recordAuditEvent(transaction, {
26
+ accountId,
27
+ action: "account.username-changed",
28
+ detail: { from: current.username, to: username },
29
+ });
30
+ return { changed: true };
31
+ });
32
+ }
33
+ catch (error) {
34
+ if (databaseError(error)?.constraint === "account_username_key")
35
+ return { changed: false, reason: "username-taken" };
36
+ throw error;
37
+ }
38
+ }
39
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/username-change/index.ts"],"names":[],"mapp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AC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,UAAU,KAAK,sBAAsB;YAC7D,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,CAAC;QACtD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,7 @@
1
+ export type UsernameChangeResult = {
2
+ changed: true;
3
+ } | {
4
+ changed: false;
5
+ reason: "username-invalid" | "username-taken" | "no-account";
6
+ };
7
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/username-change/type.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,oBAAoB,GAC9B;IAAE,OAAO,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,kBAAkB,GAAG,gBAAgB,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/username-change/type.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,93 @@
1
1
  {
2
2
  "name": "@sharadox/account-web-library",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0-code-request.8",
4
+ "description": "Passwordless accounts for web applications: one-time codes, two-factor, roles, bans and OpenID Connect for people and agents.",
5
+ "private": false,
6
+ "license": "SEE LICENSE IN LICENCA.md",
7
+ "author": "Sharadox <contact@sharadox.com>",
8
+ "type": "module",
9
+ "exports": {
10
+ "./*/type": {
11
+ "sharadox-source": "./source/*/type.ts",
12
+ "types": "./distribution/*/type.d.ts"
13
+ },
14
+ "./*": {
15
+ "sharadox-source": [
16
+ "./source/*/index.ts"
17
+ ],
18
+ "types": "./distribution/*/index.d.ts",
19
+ "default": "./distribution/*/index.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "distribution",
24
+ "LICENCA.md",
25
+ "LICENSE.md",
26
+ "NOTICE.md"
27
+ ],
28
+ "sideEffects": false,
29
+ "engines": {
30
+ "node": ">=26"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "lint-staged": {
36
+ "*.{ts,js,mjs,cjs}": [
37
+ "prettier --write",
38
+ "eslint --fix --max-warnings=0"
39
+ ],
40
+ "*.{json,md,yml,yaml}": "prettier --write"
41
+ },
42
+ "dependencies": {
43
+ "@sharadox/database-web-library": "0.1.0-development.6",
44
+ "@sharadox/web-library": "^0.11.1",
45
+ "kysely": "^0.28.20"
46
+ },
47
+ "devDependencies": {
48
+ "@electric-sql/pglite": "0.5.16",
49
+ "@sharadox/es-lint-configuration-web-library": "^0.1.3",
50
+ "@sharadox/library-build-web-library": "^0.2.1",
51
+ "@sharadox/prettier-configuration-web-library": "^0.1.2",
52
+ "@sharadox/turborepo-configuration-web-library": "^0.1.4",
53
+ "@sharadox/type-script-configuration-web-library": "^0.2.3",
54
+ "@sharadox/vitest-configuration-web-library": "^0.2.1",
55
+ "@types/node": "^26.6.6",
56
+ "@vitest/coverage-v8": "^5.1.1",
57
+ "eslint": "9.39.5",
58
+ "happy-dom": "^21.0.4",
59
+ "husky": "^9.1.7",
60
+ "lint-staged": "^17.6.2",
61
+ "prettier": "^3.10.1",
62
+ "turbo": "^2.11.13",
63
+ "typedoc": "^0.28.20",
64
+ "typescript": "6.0.3",
65
+ "vitest": "^5.1.1"
66
+ },
67
+ "repository": {
68
+ "type": "git",
69
+ "url": "git+https://github.com/Sharadox/account-web-library.git"
70
+ },
71
+ "homepage": "https://github.com/Sharadox/account-web-library#readme",
72
+ "keywords": [
73
+ "authentication",
74
+ "authorization",
75
+ "passwordless",
76
+ "one-time-code",
77
+ "totp",
78
+ "openid-connect",
79
+ "oauth",
80
+ "roles"
81
+ ],
82
+ "scripts": {
83
+ "build": "library-build",
84
+ "lint": "eslint --max-warnings=0 .",
85
+ "format": "prettier --write .",
86
+ "format:check": "prettier --check .",
87
+ "type-check": "tsc --noEmit",
88
+ "test:unit": "vitest run --coverage",
89
+ "test:benchmark": "vitest bench --run --passWithNoTests",
90
+ "documentation": "typedoc",
91
+ "verify": "turbo run format:check lint type-check test:unit test:benchmark build"
92
+ }
6
93
  }