@sharadox/account-web-library 0.1.0-development.15 → 0.1.0-development.21

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 (56) hide show
  1. package/README.md +39 -0
  2. package/distribution/account-database/type.d.ts +3 -0
  3. package/distribution/account-database/type.d.ts.map +1 -1
  4. package/distribution/account-server/index.d.ts +15 -0
  5. package/distribution/account-server/index.d.ts.map +1 -0
  6. package/distribution/account-server/index.js +23 -0
  7. package/distribution/account-server/index.js.map +1 -0
  8. package/distribution/account-server/type.d.ts +28 -0
  9. package/distribution/account-server/type.d.ts.map +1 -0
  10. package/distribution/account-server/type.js +2 -0
  11. package/distribution/account-server/type.js.map +1 -0
  12. package/distribution/challenge-issue/index.d.ts +13 -0
  13. package/distribution/challenge-issue/index.d.ts.map +1 -0
  14. package/distribution/challenge-issue/index.js +18 -0
  15. package/distribution/challenge-issue/index.js.map +1 -0
  16. package/distribution/secret-derivation/index.d.ts +10 -0
  17. package/distribution/secret-derivation/index.d.ts.map +1 -0
  18. package/distribution/secret-derivation/index.js +15 -0
  19. package/distribution/secret-derivation/index.js.map +1 -0
  20. package/distribution/sign-in-finish/index.d.ts +12 -0
  21. package/distribution/sign-in-finish/index.d.ts.map +1 -0
  22. package/distribution/sign-in-finish/index.js +47 -0
  23. package/distribution/sign-in-finish/index.js.map +1 -0
  24. package/distribution/sign-in-finish/type.d.ts +34 -0
  25. package/distribution/sign-in-finish/type.d.ts.map +1 -0
  26. package/distribution/sign-in-finish/type.js +2 -0
  27. package/distribution/sign-in-finish/type.js.map +1 -0
  28. package/distribution/sign-in-start/index.d.ts +19 -0
  29. package/distribution/sign-in-start/index.d.ts.map +1 -0
  30. package/distribution/sign-in-start/index.js +89 -0
  31. package/distribution/sign-in-start/index.js.map +1 -0
  32. package/distribution/sign-in-start/type.d.ts +27 -0
  33. package/distribution/sign-in-start/type.d.ts.map +1 -0
  34. package/distribution/sign-in-start/type.js +2 -0
  35. package/distribution/sign-in-start/type.js.map +1 -0
  36. package/distribution/sign-up-finish/index.d.ts +12 -0
  37. package/distribution/sign-up-finish/index.d.ts.map +1 -0
  38. package/distribution/sign-up-finish/index.js +27 -0
  39. package/distribution/sign-up-finish/index.js.map +1 -0
  40. package/distribution/sign-up-finish/type.d.ts +21 -0
  41. package/distribution/sign-up-finish/type.d.ts.map +1 -0
  42. package/distribution/sign-up-finish/type.js +2 -0
  43. package/distribution/sign-up-finish/type.js.map +1 -0
  44. package/distribution/signed-ticket/index.d.ts +19 -0
  45. package/distribution/signed-ticket/index.d.ts.map +1 -0
  46. package/distribution/signed-ticket/index.js +35 -0
  47. package/distribution/signed-ticket/index.js.map +1 -0
  48. package/distribution/signed-ticket/type.d.ts +16 -0
  49. package/distribution/signed-ticket/type.d.ts.map +1 -0
  50. package/distribution/signed-ticket/type.js +2 -0
  51. package/distribution/signed-ticket/type.js.map +1 -0
  52. package/distribution/test-server/index.d.ts +10 -0
  53. package/distribution/test-server/index.d.ts.map +1 -0
  54. package/distribution/test-server/index.js +16 -0
  55. package/distribution/test-server/index.js.map +1 -0
  56. package/package.json +4 -2
package/README.md CHANGED
@@ -19,6 +19,45 @@ Sign-in codes, sessions and the OpenID Connect server come next (see `CHANGELOG.
19
19
  pnpm add @sharadox/account-web-library
20
20
  ```
21
21
 
22
+ ## The sign-in flow
23
+
24
+ Signing up and signing in are the same three steps, on one `AccountServer`:
25
+ `{ database, channels, secret, limits? }`. The database holds the account tables and protection-web-library's
26
+ (`migrateProtection` and `migrateAccount`). `secret` is `ACCOUNT_SECRET`: at least 32 random characters, server only.
27
+ Every key is derived from it with `deriveSecret`.
28
+
29
+ 1. `issueChallenge(server)` gives the page a proof-of-work challenge, which the browser solves in the background.
30
+ 2. `startSignIn(server, { contact, solution, address, language, now? })` checks, in this order:
31
+ - the proof of work (single use);
32
+ - the contact and its channel;
33
+ - rate limits per contact, per address group and for everyone;
34
+ - the channel's daily spending cap.
35
+
36
+ It answers `{ status: "sent", requestId, deliver }`, and you call `deliver` after answering (Next.js `after()`).
37
+ A failed send is recorded in the audit log as `code.delivery-failed`, with the error but never the code.
38
+ Otherwise it answers `refused` with `proof`, `contact`, `unavailable` (the cap is reached: sending pauses), or
39
+ `rate-limit` with `retryAfterSeconds`. The answer never depends on whether an account exists.
40
+
41
+ 3. `finishSignIn(server, { requestId, code, address, token, now? })` limits guesses per address group. It answers:
42
+ - `signed-in`, with `{ accountId, token }`: set `token` as the cookie;
43
+ - `sign-up`, with `{ ticket }`, for a contact with no account;
44
+ - `refused`, with the code's reason or `rate-limit`.
45
+ 4. `finishSignUp(server, { ticket, username, token, now? })` creates the account and signs it in. A refused
46
+ username can be tried again with the same ticket, which lasts 15 minutes.
47
+
48
+ `DEFAULT_SIGN_IN_LIMITS` (`account-server`):
49
+
50
+ | Limit | Default |
51
+ | ------------------------- | --------------------------------- |
52
+ | `perContact` | 3 codes, then 1 every 5 minutes |
53
+ | `perAddress` | 10, then 1 a minute |
54
+ | `global` | 200, then 1 a second |
55
+ | `verificationsPerAddress` | 20 typed codes, then 1 every 30 s |
56
+ | `dailySendsPerChannel` | 2,000 |
57
+ | `difficulty` | 14 bits |
58
+
59
+ `createTicket` and `readTicket` (`signed-ticket`) carry signed, short-lived data between steps.
60
+
22
61
  ## Storage
23
62
 
24
63
  Postgres through Kysely (`@sharadox/database-web-library`). Run `migrateAccount(database)` when the application
@@ -1,3 +1,4 @@
1
+ import type { ProtectionDatabase } from "@sharadox/protection-web-library/protection-database/type";
1
2
  import type { ColumnType, Generated } from "kysely";
2
3
  /** A timestamp the database fills in when not given. */
3
4
  type DefaultTime = ColumnType<Date, Date | undefined, Date>;
@@ -90,5 +91,7 @@ export interface AccountDatabase {
90
91
  browser_session: BrowserSessionTable;
91
92
  account_session: AccountSessionTable;
92
93
  }
94
+ /** The account tables and the protection tables (rate limits, spending caps, used challenges) the server needs. */
95
+ export type AccountServerDatabase = AccountDatabase & ProtectionDatabase;
93
96
  export {};
94
97
  //# sourceMappingURL=type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AE,MAAM,CAAC;IACX,kBAAkB,EAAE,MAAM,CAAC;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,IAAI,CAAC;IACjB,YAAY,EAAE,IAAI,CAAC;IACnB,UAAU,EAAE,IAAI,CAAC;IACjB,wEAAwE;IACxE,gBAAgB,EAAE,IAAI,GAAG,IAAI,CAAC;CAC/B;AAED,yEAAyE;AACzE,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,gBAAgB,CAAC;IAC9B,OAAO,EAAE,YAAY,CAAC;IACtB,OAAO,EAAE,YAAY,CAAC;IACtB,WAAW,EAAE,eAAe,CAAC;IAC7B,YAAY,EAAE,gBAAgB,CAAC;IAC/B,eAAe,EAAE,mBAAmB,CAAC;IACrC,eAAe,EAAE,mBAAmB,CAAC;CACtC;AAED,mHAAmH;AACnH,MAAM,MAAM,qBAAqB,GAAG,eAAe,GAAG,kBAAkB,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { AccountServer, SignInLimits } from "@sharadox/account-web-library/account-server/type";
2
+ /**
3
+ * The limits used when a company's configuration doesn't set them: 3 codes per contact then one per 5 minutes; 10
4
+ * per address group then one a minute; 200 for everyone then one a second; 20 typed codes per address group then
5
+ * one per 30 seconds; 2,000 sends per channel per UTC day; proof of work of 14 bits.
6
+ */
7
+ export declare const DEFAULT_SIGN_IN_LIMITS: SignInLimits;
8
+ /**
9
+ * A server's limits: its own where set, the defaults elsewhere.
10
+ *
11
+ * @example
12
+ * const { perContact } = signInLimits(server);
13
+ */
14
+ export declare function signInLimits(server: AccountServer): SignInLimits;
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-server/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,mDAAmD,CAAC;AAErG;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,EAAE,YAOpC,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,aAAa,GAAG,YAAY,CAEhE"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The limits used when a company's configuration doesn't set them: 3 codes per contact then one per 5 minutes; 10
3
+ * per address group then one a minute; 200 for everyone then one a second; 20 typed codes per address group then
4
+ * one per 30 seconds; 2,000 sends per channel per UTC day; proof of work of 14 bits.
5
+ */
6
+ export const DEFAULT_SIGN_IN_LIMITS = {
7
+ perContact: { capacity: 3, refillPerSecond: 1 / 300 },
8
+ perAddress: { capacity: 10, refillPerSecond: 1 / 60 },
9
+ global: { capacity: 200, refillPerSecond: 1 },
10
+ verificationsPerAddress: { capacity: 20, refillPerSecond: 1 / 30 },
11
+ dailySendsPerChannel: 2_000,
12
+ difficulty: 14,
13
+ };
14
+ /**
15
+ * A server's limits: its own where set, the defaults elsewhere.
16
+ *
17
+ * @example
18
+ * const { perContact } = signInLimits(server);
19
+ */
20
+ export function signInLimits(server) {
21
+ return { ...DEFAULT_SIGN_IN_LIMITS, ...server.limits };
22
+ }
23
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-server/index.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAiB;IAClD,UAAU,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,eAAe,EAAE,CAAC,GAAG,GAAG,EAAE;IACrD,UAAU,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,eAAe,EAAE,CAAC,GAAG,EAAE,EAAE;IACrD,MAAM,EAAE,EAAE,QAAQ,EAAE,GAAG,EAAE,eAAe,EAAE,CAAC,EAAE;IAC7C,uBAAuB,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,eAAe,EAAE,CAAC,GAAG,EAAE,EAAE;IAClE,oBAAoB,EAAE,KAAK;IAC3B,UAAU,EAAE,EAAE;CACf,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,MAAqB;IAChD,OAAO,EAAE,GAAG,sBAAsB,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;AACzD,CAAC"}
@@ -0,0 +1,28 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { TokenBucketRule } from "@sharadox/protection-web-library/token-bucket/type";
3
+ import type { AccountServerDatabase } from "@sharadox/account-web-library/account-database/type";
4
+ import type { ContactChannel } from "@sharadox/account-web-library/contact-channel/type";
5
+ /** How fast each kind of visitor may go. Every field has a default (see `DEFAULT_SIGN_IN_LIMITS`). */
6
+ export interface SignInLimits {
7
+ /** Codes sent to one contact. */
8
+ perContact: TokenBucketRule;
9
+ /** Codes requested from one address group (an IPv4 address, an IPv6 /64). */
10
+ perAddress: TokenBucketRule;
11
+ /** Codes requested by everyone together. */
12
+ global: TokenBucketRule;
13
+ /** Codes typed from one address group. */
14
+ verificationsPerAddress: TokenBucketRule;
15
+ /** The most codes each channel may send in a UTC day; past it, sending pauses (never spends). */
16
+ dailySendsPerChannel: number;
17
+ /** Leading zero bits of the proof of work. */
18
+ difficulty: number;
19
+ }
20
+ /** What every sign-in step needs: the database, the channels, and one root secret every key is derived from. */
21
+ export interface AccountServer {
22
+ database: Kysely<AccountServerDatabase>;
23
+ channels: ContactChannel[];
24
+ /** At least 32 random characters, server only (`ACCOUNT_SECRET`). */
25
+ secret: string;
26
+ limits?: Partial<SignInLimits>;
27
+ }
28
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-server/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oDAAoD,CAAC;AAC1F,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,qDAAqD,CAAC;AACjG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oDAAoD,CAAC;AAEzF,sGAAsG;AACtG,MAAM,WAAW,YAAY;IAC3B,iCAAiC;IACjC,UAAU,EAAE,eAAe,CAAC;IAC5B,6EAA6E;IAC7E,UAAU,EAAE,eAAe,CAAC;IAC5B,4CAA4C;IAC5C,MAAM,EAAE,eAAe,CAAC;IACxB,0CAA0C;IAC1C,uBAAuB,EAAE,eAAe,CAAC;IACzC,iGAAiG;IACjG,oBAAoB,EAAE,MAAM,CAAC;IAC7B,8CAA8C;IAC9C,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,gHAAgH;AAChH,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,MAAM,CAAC,qBAAqB,CAAC,CAAC;IACxC,QAAQ,EAAE,cAAc,EAAE,CAAC;IAC3B,qEAAqE;IACrE,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,OAAO,CAAC,YAAY,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/account-server/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,13 @@
1
+ import type { Challenge } from "@sharadox/protection-web-library/proof-of-work/type";
2
+ import type { AccountServer } from "@sharadox/account-web-library/account-server/type";
3
+ /**
4
+ * A proof-of-work challenge for the sign-in form, at the server's difficulty: the browser solves it in the
5
+ * background (protection-web-library's `solveChallenge`) and sends the solution with the contact.
6
+ *
7
+ * @example
8
+ * const challenge = issueChallenge(server); // give it to the page
9
+ */
10
+ export declare function issueChallenge(server: AccountServer, { now }?: {
11
+ now?: number;
12
+ }): Challenge;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/challenge-issue/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qDAAqD,CAAC;AAErF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAGvF;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,aAAa,EAAE,EAAE,GAAgB,EAAE,GAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,SAAS,CAM5G"}
@@ -0,0 +1,18 @@
1
+ import { createChallenge } from "@sharadox/protection-web-library/proof-of-work";
2
+ import { signInLimits } from "@sharadox/account-web-library/account-server";
3
+ import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
4
+ /**
5
+ * A proof-of-work challenge for the sign-in form, at the server's difficulty: the browser solves it in the
6
+ * background (protection-web-library's `solveChallenge`) and sends the solution with the contact.
7
+ *
8
+ * @example
9
+ * const challenge = issueChallenge(server); // give it to the page
10
+ */
11
+ export function issueChallenge(server, { now = Date.now() } = {}) {
12
+ return createChallenge({
13
+ secret: deriveSecret(server.secret, "proof-of-work"),
14
+ difficulty: signInLimits(server).difficulty,
15
+ now,
16
+ });
17
+ }
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/challenge-issue/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,gDAAgD,CAAC;AAEjF,OAAO,EAAE,YAAY,EAAE,MAAM,8CAA8C,CAAC;AAE5E,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAE/E;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,MAAqB,EAAE,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,KAAuB,EAAE;IAC/F,OAAO,eAAe,CAAC;QACrB,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC;QACpD,UAAU,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC,UAAU;QAC3C,GAAG;KACJ,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * A separate 256-bit key for one purpose (`one-time-code`, `rate-limit`, `proof-of-work`, `sign-up-ticket`…),
3
+ * derived from the account server's one root secret with HKDF-SHA-256: one secret to keep, and a key leaked from one
4
+ * use reveals nothing about the others.
5
+ *
6
+ * @example
7
+ * const pepper = deriveSecret(process.env.ACCOUNT_SECRET!, "one-time-code");
8
+ */
9
+ export declare function deriveSecret(root: string, purpose: string): string;
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/secret-derivation/index.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAGlE"}
@@ -0,0 +1,15 @@
1
+ import { hkdfSync } from "node:crypto";
2
+ /**
3
+ * A separate 256-bit key for one purpose (`one-time-code`, `rate-limit`, `proof-of-work`, `sign-up-ticket`…),
4
+ * derived from the account server's one root secret with HKDF-SHA-256: one secret to keep, and a key leaked from one
5
+ * use reveals nothing about the others.
6
+ *
7
+ * @example
8
+ * const pepper = deriveSecret(process.env.ACCOUNT_SECRET!, "one-time-code");
9
+ */
10
+ export function deriveSecret(root, purpose) {
11
+ if (root.length < 32)
12
+ throw new Error("the account secret must have at least 32 characters");
13
+ return Buffer.from(hkdfSync("sha256", root, "", `sharadox-account:${purpose}`, 32)).toString("base64url");
14
+ }
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/secret-derivation/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,IAAY,EAAE,OAAe;IACxD,IAAI,IAAI,CAAC,MAAM,GAAG,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;IAC7F,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,EAAE,oBAAoB,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;AAC5G,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { AccountServer } from "@sharadox/account-web-library/account-server/type";
2
+ import type { SignInFinish, SignInFinishResult } from "@sharadox/account-web-library/sign-in-finish/type";
3
+ /**
4
+ * The second step: the typed code. Guesses are limited per address group (and per request by `verifyCode`). With the
5
+ * right code, an account with that contact is signed in on the browser (the new token goes in its cookie); a contact
6
+ * with no account gets a sign-up ticket for choosing a username.
7
+ *
8
+ * @example
9
+ * const result = await finishSignIn(server, { requestId, code, address, token: cookie });
10
+ */
11
+ export declare function finishSignIn(server: AccountServer, { requestId, code, address, token, now }: SignInFinish): Promise<SignInFinishResult>;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/sign-in-finish/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAKvF,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,mDAAmD,CAAC;AAK1G;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,aAAa,EACrB,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,GAAgB,EAAE,EAAE,YAAY,GAClE,OAAO,CAAC,kBAAkB,CAAC,CA+B7B"}
@@ -0,0 +1,47 @@
1
+ import { addressGroup } from "@sharadox/protection-web-library/address-group";
2
+ import { limitRate } from "@sharadox/protection-web-library/rate-limit";
3
+ import { signIn } from "@sharadox/account-web-library/account-sign-in";
4
+ import { signInLimits } from "@sharadox/account-web-library/account-server";
5
+ import { verifyCode } from "@sharadox/account-web-library/code-verification";
6
+ import { findAccountByContact } from "@sharadox/account-web-library/contact-lookup";
7
+ import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
8
+ import { createTicket } from "@sharadox/account-web-library/signed-ticket";
9
+ /** How long a sign-up ticket lasts: time to choose a username. */
10
+ const SIGN_UP_SECONDS = 900;
11
+ /**
12
+ * The second step: the typed code. Guesses are limited per address group (and per request by `verifyCode`). With the
13
+ * right code, an account with that contact is signed in on the browser (the new token goes in its cookie); a contact
14
+ * with no account gets a sign-up ticket for choosing a username.
15
+ *
16
+ * @example
17
+ * const result = await finishSignIn(server, { requestId, code, address, token: cookie });
18
+ */
19
+ export async function finishSignIn(server, { requestId, code, address, token, now = Date.now() }) {
20
+ const rate = await limitRate(server.database, [{ key: `verification:${addressGroup(address)}`, rule: signInLimits(server).verificationsPerAddress }], { secret: deriveSecret(server.secret, "rate-limit"), now });
21
+ if (!rate.allowed)
22
+ return { status: "refused", reason: "rate-limit", retryAfterSeconds: rate.retryAfterSeconds };
23
+ const verification = await verifyCode(server.database, {
24
+ requestId,
25
+ code,
26
+ purpose: "sign-in",
27
+ pepper: deriveSecret(server.secret, "one-time-code"),
28
+ now,
29
+ });
30
+ if (!verification.valid)
31
+ return { status: "refused", reason: verification.reason };
32
+ const found = await findAccountByContact(server.database, verification.contact);
33
+ if (!found) {
34
+ const ticket = createTicket({ contact: verification.contact }, {
35
+ secret: deriveSecret(server.secret, "sign-up-ticket"),
36
+ purpose: "sign-up",
37
+ lifetimeSeconds: SIGN_UP_SECONDS,
38
+ now,
39
+ });
40
+ return { status: "sign-up", ticket };
41
+ }
42
+ if (!found.signIn)
43
+ return { status: "refused", reason: "unknown" };
44
+ const session = await signIn(server.database, { accountId: found.accountId, token, now });
45
+ return { status: "signed-in", accountId: found.accountId, token: session.token };
46
+ }
47
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/sign-in-finish/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAA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,eAAe,CAAC;QACpD,GAAG;KACJ,CAAC,CAAC;IACH,IAAI,CAAC,YAAY,CAAC,KAAK;QAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,CAAC,MAAM,EAAE,CAAC;IACnF,MAAM,KAAK,GAAG,MAAM,oBAAoB,CAAC,MAAM,CAAC,QAAQ,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC;IAChF,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,MAAM,GAAG,YAAY,CACzB,EAAE,OAAO,EAAE,YAAY,CAAC,OAAO,EAAE,EACjC;YACE,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC;YACrD,OAAO,EAAE,SAAS;YAClB,eAAe,EAAE,eAAe;YAChC,GAAG;SACJ,CACF,CAAC;QACF,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC;IACvC,CAAC;IACD,IAAI,CAAC,KAAK,CAAC,MAAM;QAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IACnE,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;IAC1F,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC;AACnF,CAAC"}
@@ -0,0 +1,34 @@
1
+ import type { CodeVerificationResult } from "@sharadox/account-web-library/code-verification/type";
2
+ export interface SignInFinish {
3
+ requestId: string;
4
+ /** As typed. */
5
+ code: string;
6
+ /** The client's IP address. */
7
+ address: string;
8
+ /** The browser's current session cookie, if any: the account joins its accounts. */
9
+ token: string | undefined;
10
+ /** Milliseconds since 1970 (now by default). */
11
+ now?: number;
12
+ }
13
+ type Refusal = Extract<CodeVerificationResult, {
14
+ valid: false;
15
+ }>["reason"];
16
+ export type SignInFinishResult = {
17
+ status: "signed-in";
18
+ accountId: string;
19
+ token: string;
20
+ }
21
+ /** No account has this contact yet: ask for a username, then `finishSignUp` with the ticket. */
22
+ | {
23
+ status: "sign-up";
24
+ ticket: string;
25
+ } | {
26
+ status: "refused";
27
+ reason: Refusal;
28
+ } | {
29
+ status: "refused";
30
+ reason: "rate-limit";
31
+ retryAfterSeconds: number;
32
+ };
33
+ export {};
34
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/sign-in-finish/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sDAAsD,CAAC;AAEnG,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,+BAA+B;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,oFAAoF;IACpF,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,KAAK,OAAO,GAAG,OAAO,CAAC,sBAAsB,EAAE;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC,CAAC,QAAQ,CAAC,CAAC;AAE3E,MAAM,MAAM,kBAAkB,GAC1B;IAAE,MAAM,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE;AAC3D,gGAAgG;GAC9F;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACrC;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GACtC;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,YAAY,CAAC;IAAC,iBAAiB,EAAE,MAAM,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/sign-in-finish/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,19 @@
1
+ import type { AccountServer } from "@sharadox/account-web-library/account-server/type";
2
+ import type { SignInStart, SignInStartResult } from "@sharadox/account-web-library/sign-in-start/type";
3
+ /**
4
+ * The first step of signing in (and of signing up: they are the same): a code to the contact. In order, each check
5
+ * cheaper than what it protects:
6
+ * 1. a single-use proof of work;
7
+ * 2. a valid contact with a channel;
8
+ * 3. rate limits per contact, per address group (an IPv6 /64 is one visitor) and for everyone;
9
+ * 4. the channel's daily spending cap (past it, sending pauses: it never spends);
10
+ * 5. a new code, sent by `deliver` after answering; a failed send is recorded in the audit log
11
+ * (`code.delivery-failed`, with the error, never the code) and thrown.
12
+ * The answer never depends on whether an account exists, so nobody can probe for accounts.
13
+ *
14
+ * @example
15
+ * const result = await startSignIn(server, { contact, solution, address: clientAddress(request), language });
16
+ * if (result.status === "sent") after(result.deliver);
17
+ */
18
+ export declare function startSignIn(server: AccountServer, { contact, solution, address, language, now }: SignInStart): Promise<SignInStartResult>;
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/sign-in-start/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAMvF,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,kDAAkD,CAAC;AAEvG;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,WAAW,CAC/B,MAAM,EAAE,aAAa,EACrB,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAgB,EAAE,EAAE,WAAW,GACtE,OAAO,CAAC,iBAAiB,CAAC,CAiE5B"}
@@ -0,0 +1,89 @@
1
+ import { addressGroup } from "@sharadox/protection-web-library/address-group";
2
+ import { limitRate } from "@sharadox/protection-web-library/rate-limit";
3
+ import { redeemSolution } from "@sharadox/protection-web-library/solution-redemption";
4
+ import { spendFromCap } from "@sharadox/protection-web-library/spending-cap";
5
+ import { signInLimits } from "@sharadox/account-web-library/account-server";
6
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
7
+ import { requestCode } from "@sharadox/account-web-library/code-request";
8
+ import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
9
+ import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
10
+ /**
11
+ * The first step of signing in (and of signing up: they are the same): a code to the contact. In order, each check
12
+ * cheaper than what it protects:
13
+ * 1. a single-use proof of work;
14
+ * 2. a valid contact with a channel;
15
+ * 3. rate limits per contact, per address group (an IPv6 /64 is one visitor) and for everyone;
16
+ * 4. the channel's daily spending cap (past it, sending pauses: it never spends);
17
+ * 5. a new code, sent by `deliver` after answering; a failed send is recorded in the audit log
18
+ * (`code.delivery-failed`, with the error, never the code) and thrown.
19
+ * The answer never depends on whether an account exists, so nobody can probe for accounts.
20
+ *
21
+ * @example
22
+ * const result = await startSignIn(server, { contact, solution, address: clientAddress(request), language });
23
+ * if (result.status === "sent") after(result.deliver);
24
+ */
25
+ export async function startSignIn(server, { contact, solution, address, language, now = Date.now() }) {
26
+ const limits = signInLimits(server);
27
+ const proof = await redeemSolution(server.database, solution, {
28
+ secret: deriveSecret(server.secret, "proof-of-work"),
29
+ now,
30
+ });
31
+ if (!proof.valid)
32
+ return { status: "refused", reason: "proof" };
33
+ let normalized;
34
+ try {
35
+ normalized = normalizeContact(contact);
36
+ }
37
+ catch {
38
+ return { status: "refused", reason: "contact" };
39
+ }
40
+ const channel = server.channels.find(({ kind }) => kind === normalized.kind);
41
+ if (!channel)
42
+ return { status: "refused", reason: "contact" };
43
+ const rate = await limitRate(server.database, [
44
+ { key: `contact:${normalized.kind}:${normalized.value}`, rule: limits.perContact },
45
+ { key: `address:${addressGroup(address)}`, rule: limits.perAddress },
46
+ { key: "global:sign-in", rule: limits.global },
47
+ ], { secret: deriveSecret(server.secret, "rate-limit"), now });
48
+ if (!rate.allowed)
49
+ return { status: "refused", reason: "rate-limit", retryAfterSeconds: rate.retryAfterSeconds };
50
+ const cap = await spendFromCap(server.database, { name: `${normalized.kind}-send`, limit: limits.dailySendsPerChannel, period: "day" }, { now });
51
+ if (!cap.allowed)
52
+ return { status: "refused", reason: "unavailable" };
53
+ const request = await requestCode(server.database, {
54
+ purpose: "sign-in",
55
+ contact: normalized,
56
+ pepper: deriveSecret(server.secret, "one-time-code"),
57
+ now,
58
+ });
59
+ return {
60
+ status: "sent",
61
+ requestId: request.requestId,
62
+ deliver: async () => {
63
+ try {
64
+ await channel.send({
65
+ contact: request.contact,
66
+ code: request.code,
67
+ purpose: "sign-in",
68
+ language,
69
+ expiresAt: request.expiresAt,
70
+ });
71
+ }
72
+ catch (error) {
73
+ // Nobody is waiting for the answer any more: the audit log is where a failed send shows (never the code).
74
+ const { name, message } = error;
75
+ await recordAuditEvent(server.database, {
76
+ action: "code.delivery-failed",
77
+ detail: {
78
+ kind: request.contact.kind,
79
+ purpose: "sign-in",
80
+ requestId: request.requestId,
81
+ error: `${name}: ${message}`.slice(0, 500),
82
+ },
83
+ });
84
+ throw error;
85
+ }
86
+ },
87
+ };
88
+ }
89
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/sign-in-start/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gDAAgD,CAAC;AAC9E,OAAO,EAAE,SAAS,EAAE,MAAM,6CAA6C,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAC;AACtF,OAAO,EAAE,YAAY,EAAE,MAAM,+CAA+C,CAAC;AAC7E,OAAO,EAAE,Y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K,IAAI,EAAE;YAClB,IAAI,CAAC;gBACH,MAAM,OAAO,CAAC,IAAI,CAAC;oBACjB,OAAO,EAAE,OAAO,CAAC,OAAO;oBACxB,IAAI,EAAE,OAAO,CAAC,IAAI;oBAClB,OAAO,EAAE,SAAS;oBAClB,QAAQ;oBACR,SAAS,EAAE,OAAO,CAAC,SAAS;iBAC7B,CAAC,CAAC;YACL,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,0GAA0G;gBAC1G,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,KAAc,CAAC;gBACzC,MAAM,gBAAgB,CAAC,MAAM,CAAC,QAAQ,EAAE;oBACtC,MAAM,EAAE,sBAAsB;oBAC9B,MAAM,EAAE;wBACN,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI;wBAC1B,OAAO,EAAE,SAAS;wBAClB,SAAS,EAAE,OAAO,CAAC,SAAS;wBAC5B,KAAK,EAAE,GAAG,IAAI,KAAK,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC;qBAC3C;iBACF,CAAC,CAAC;gBACH,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { Solution } from "@sharadox/protection-web-library/proof-of-work/type";
2
+ import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
3
+ export interface SignInStart {
4
+ contact: Contact;
5
+ /** The browser's answer to `issueChallenge`. */
6
+ solution: Solution;
7
+ /** The client's IP address (web-library's `clientAddress`). */
8
+ address: string;
9
+ /** For the code's message. */
10
+ language: string;
11
+ /** Milliseconds since 1970 (now by default). */
12
+ now?: number;
13
+ }
14
+ export type SignInStartResult = {
15
+ status: "sent";
16
+ requestId: string;
17
+ /** Sends the code: call it after answering (Next.js `after()`), so the answer's timing reveals nothing. */
18
+ deliver: () => Promise<void>;
19
+ } | {
20
+ status: "refused";
21
+ reason: "proof" | "contact" | "unavailable";
22
+ } | {
23
+ status: "refused";
24
+ reason: "rate-limit";
25
+ retryAfterSeconds: number;
26
+ };
27
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/sign-in-start/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qDAAqD,CAAC;AACpF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,OAAO,CAAC;IACjB,gDAAgD;IAChD,QAAQ,EAAE,QAAQ,CAAC;IACnB,+DAA+D;IAC/D,OAAO,EAAE,MAAM,CAAC;IAChB,8BAA8B;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,iBAAiB,GACzB;IACE,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,2GAA2G;IAC3G,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B,GACD;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,OAAO,GAAG,SAAS,GAAG,aAAa,CAAA;CAAE,GAClE;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,YAAY,CAAC;IAAC,iBAAiB,EAAE,MAAM,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/sign-in-start/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,12 @@
1
+ import type { AccountServer } from "@sharadox/account-web-library/account-server/type";
2
+ import type { SignUpFinish, SignUpFinishResult } from "@sharadox/account-web-library/sign-up-finish/type";
3
+ /**
4
+ * The last step for a new person: the username they chose, with the ticket proving their contact. Creates the
5
+ * account with that contact verified and signs it in. A taken or invalid username can be tried again with the same
6
+ * ticket while it lasts; a ticket can't make two accounts (the contact is then taken).
7
+ *
8
+ * @example
9
+ * const result = await finishSignUp(server, { ticket, username, token: cookie });
10
+ */
11
+ export declare function finishSignUp(server: AccountServer, { ticket, username, token, now }: SignUpFinish): Promise<SignUpFinishResult>;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/sign-up-finish/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mDAAmD,CAAC;AAKvF,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,mDAAmD,CAAC;AAE1G;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,aAAa,EACrB,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAgB,EAAE,EAAE,YAAY,GAC1D,OAAO,CAAC,kBAAkB,CAAC,CAW7B"}
@@ -0,0 +1,27 @@
1
+ import { signIn } from "@sharadox/account-web-library/account-sign-in";
2
+ import { createPersonAccount } from "@sharadox/account-web-library/person-account";
3
+ import { deriveSecret } from "@sharadox/account-web-library/secret-derivation";
4
+ import { readTicket } from "@sharadox/account-web-library/signed-ticket";
5
+ /**
6
+ * The last step for a new person: the username they chose, with the ticket proving their contact. Creates the
7
+ * account with that contact verified and signs it in. A taken or invalid username can be tried again with the same
8
+ * ticket while it lasts; a ticket can't make two accounts (the contact is then taken).
9
+ *
10
+ * @example
11
+ * const result = await finishSignUp(server, { ticket, username, token: cookie });
12
+ */
13
+ export async function finishSignUp(server, { ticket, username, token, now = Date.now() }) {
14
+ const reading = readTicket(ticket, {
15
+ secret: deriveSecret(server.secret, "sign-up-ticket"),
16
+ purpose: "sign-up",
17
+ now,
18
+ });
19
+ if (!reading.valid)
20
+ return { status: "refused", reason: "ticket" };
21
+ const created = await createPersonAccount(server.database, { username, contact: reading.data.contact, now });
22
+ if (!created.created)
23
+ return { status: "refused", reason: created.reason };
24
+ const session = await signIn(server.database, { accountId: created.accountId, token, now });
25
+ return { status: "signed-in", accountId: created.accountId, token: session.token };
26
+ }
27
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/sign-up-finish/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,+CAA+C,CAAC;AAGvE,OAAO,EAAE,mBAAmB,EAAE,MAAM,8CAA8C,CAAC;AACnF,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAC/E,OAAO,EAAE,UAAU,EAAE,MAAM,6CAA6C,CAAC;AAGzE;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,MAAqB,EACrB,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAgB;IAE3D,MAAM,OAAO,GAAG,UAAU,CAAuB,MAAM,EAAE;QACvD,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC;QACrD,OAAO,EAAE,SAAS;QAClB,GAAG;KACJ,CAAC,CAAC;IACH,IAAI,CAAC,OAAO,CAAC,KAAK;QAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;IACnE,MAAM,OAAO,GAAG,MAAM,mBAAmB,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC;IAC7G,IAAI,CAAC,OAAO,CAAC,OAAO;QAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC;IAC3E,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;IAC5F,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC;AACrF,CAAC"}
@@ -0,0 +1,21 @@
1
+ import type { PersonAccountResult } from "@sharadox/account-web-library/person-account/type";
2
+ export interface SignUpFinish {
3
+ /** From `finishSignIn`'s `sign-up` answer. */
4
+ ticket: string;
5
+ username: string;
6
+ /** The browser's current session cookie, if any. */
7
+ token: string | undefined;
8
+ /** Milliseconds since 1970 (now by default). */
9
+ now?: number;
10
+ }
11
+ export type SignUpFinishResult = {
12
+ status: "signed-in";
13
+ accountId: string;
14
+ token: string;
15
+ } | {
16
+ status: "refused";
17
+ reason: "ticket" | Extract<PersonAccountResult, {
18
+ created: false;
19
+ }>["reason"];
20
+ };
21
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/sign-up-finish/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mDAAmD,CAAC;AAE7F,MAAM,WAAW,YAAY;IAC3B,8CAA8C;IAC9C,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,oDAAoD;IACpD,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,kBAAkB,GAC1B;IAAE,MAAM,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACzD;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,QAAQ,GAAG,OAAO,CAAC,mBAAmB,EAAE;QAAE,OAAO,EAAE,KAAK,CAAA;KAAE,CAAC,CAAC,QAAQ,CAAC,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/sign-up-finish/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,19 @@
1
+ import type { TicketOptions, TicketReading } from "@sharadox/account-web-library/signed-ticket/type";
2
+ /**
3
+ * A short-lived, signed ticket carrying data between two steps (a proved contact from the code step to the
4
+ * username step) without storing anything: the data can be read, but not changed or used for another purpose.
5
+ *
6
+ * @example
7
+ * const ticket = createTicket({ contact }, { secret, purpose: "sign-up", lifetimeSeconds: 900 });
8
+ */
9
+ export declare function createTicket(data: unknown, { secret, purpose, lifetimeSeconds, now }: TicketOptions & {
10
+ lifetimeSeconds: number;
11
+ }): string;
12
+ /**
13
+ * The data of a ticket made by `createTicket`, if it is intact, for this purpose, and not expired.
14
+ *
15
+ * @example
16
+ * const reading = readTicket<{ contact: Contact }>(ticket, { secret, purpose: "sign-up" });
17
+ */
18
+ export declare function readTicket<Data>(ticket: string, { secret, purpose, now }: TicketOptions): TicketReading<Data>;
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/signed-ticket/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,kDAAkD,CAAC;AAIrG;;;;;;GAMG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,OAAO,EACb,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,GAAgB,EAAE,EAAE,aAAa,GAAG;IAAE,eAAe,EAAE,MAAM,CAAA;CAAE,GAClG,MAAM,CAKR;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAC7B,MAAM,EAAE,MAAM,EACd,EAAE,MAAM,EAAE,OAAO,EAAE,GAAgB,EAAE,EAAE,aAAa,GACnD,aAAa,CAAC,IAAI,CAAC,CAerB"}
@@ -0,0 +1,35 @@
1
+ import { createHmac, timingSafeEqual } from "node:crypto";
2
+ const sign = (secret, body) => createHmac("sha256", secret).update(body).digest("base64url");
3
+ /**
4
+ * A short-lived, signed ticket carrying data between two steps (a proved contact from the code step to the
5
+ * username step) without storing anything: the data can be read, but not changed or used for another purpose.
6
+ *
7
+ * @example
8
+ * const ticket = createTicket({ contact }, { secret, purpose: "sign-up", lifetimeSeconds: 900 });
9
+ */
10
+ export function createTicket(data, { secret, purpose, lifetimeSeconds, now = Date.now() }) {
11
+ const body = Buffer.from(JSON.stringify({ data, purpose, expiresAt: now + lifetimeSeconds * 1000 })).toString("base64url");
12
+ return `${body}.${sign(secret, body)}`;
13
+ }
14
+ /**
15
+ * The data of a ticket made by `createTicket`, if it is intact, for this purpose, and not expired.
16
+ *
17
+ * @example
18
+ * const reading = readTicket<{ contact: Contact }>(ticket, { secret, purpose: "sign-up" });
19
+ */
20
+ export function readTicket(ticket, { secret, purpose, now = Date.now() }) {
21
+ const [body, signature, ...rest] = ticket.split(".");
22
+ if (!body || !signature || rest.length > 0)
23
+ return { valid: false, reason: "signature" };
24
+ const expected = Buffer.from(sign(secret, body));
25
+ const given = Buffer.from(signature);
26
+ if (expected.length !== given.length || !timingSafeEqual(expected, given))
27
+ return { valid: false, reason: "signature" };
28
+ const content = JSON.parse(Buffer.from(body, "base64url").toString());
29
+ if (content.purpose !== purpose)
30
+ return { valid: false, reason: "signature" };
31
+ if (content.expiresAt <= now)
32
+ return { valid: false, reason: "expired" };
33
+ return { valid: true, data: content.data };
34
+ }
35
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/signed-ticket/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAA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Ae,CAAC,QAAQ,EAAE,KAAK,CAAC;QACvE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC;IAC/C,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC,QAAQ,EAAE,CAInE,CAAC;IACF,IAAI,OAAO,CAAC,OAAO,KAAK,OAAO;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC;IAC9E,IAAI,OAAO,CAAC,SAAS,IAAI,GAAG;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IACzE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC;AAC7C,CAAC"}
@@ -0,0 +1,16 @@
1
+ export interface TicketOptions {
2
+ /** At least 32 characters, server only. */
3
+ secret: string;
4
+ /** What the ticket is for: a ticket for one purpose is refused for any other. */
5
+ purpose: string;
6
+ /** Milliseconds since 1970 (now by default). */
7
+ now?: number;
8
+ }
9
+ export type TicketReading<Data> = {
10
+ valid: true;
11
+ data: Data;
12
+ } | {
13
+ valid: false;
14
+ reason: "signature" | "expired";
15
+ };
16
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/signed-ticket/type.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,2CAA2C;IAC3C,MAAM,EAAE,MAAM,CAAC;IACf,iFAAiF;IACjF,OAAO,EAAE,MAAM,CAAC;IAChB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,aAAa,CAAC,IAAI,IAAI;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,WAAW,GAAG,SAAS,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/signed-ticket/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,10 @@
1
+ import type { MemoryDatabase } from "@sharadox/database-web-library/memory-database/type";
2
+ import type { AccountServerDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ /**
4
+ * A migrated account server database (account and protection tables) in memory, for tests of the sign-in flow.
5
+ *
6
+ * @example
7
+ * const memory = await createTestServerDatabase();
8
+ */
9
+ export declare function createTestServerDatabase(): Promise<MemoryDatabase<AccountServerDatabase>>;
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/test-server/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qDAAqD,CAAC;AAE1F,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,qDAAqD,CAAC;AAGjG;;;;;GAKG;AACH,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,cAAc,CAAC,qBAAqB,CAAC,CAAC,CAK/F"}
@@ -0,0 +1,16 @@
1
+ import { createMemoryDatabase } from "@sharadox/database-web-library/memory-database";
2
+ import { migrateProtection } from "@sharadox/protection-web-library/protection-migration";
3
+ import { migrateAccount } from "@sharadox/account-web-library/account-migration";
4
+ /**
5
+ * A migrated account server database (account and protection tables) in memory, for tests of the sign-in flow.
6
+ *
7
+ * @example
8
+ * const memory = await createTestServerDatabase();
9
+ */
10
+ export async function createTestServerDatabase() {
11
+ const memory = await createMemoryDatabase();
12
+ await migrateProtection(memory.database);
13
+ await migrateAccount(memory.database);
14
+ return memory;
15
+ }
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/test-server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,gDAAgD,CAAC;AAEtF,OAAO,EAAE,iBAAiB,EAAE,MAAM,uDAAuD,CAAC;AAE1F,OAAO,EAAE,cAAc,EAAE,MAAM,iDAAiD,CAAC;AAEjF;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB;IAC5C,MAAM,MAAM,GAAG,MAAM,oBAAoB,EAAyB,CAAC;IACnE,MAAM,iBAAiB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACzC,MAAM,cAAc,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACtC,OAAO,MAAM,CAAC;AAChB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sharadox/account-web-library",
3
- "version": "0.1.0-development.15",
3
+ "version": "0.1.0-development.21",
4
4
  "description": "Passwordless accounts for web applications: one-time codes, two-factor, roles, bans and OpenID Connect for people and agents.",
5
5
  "private": false,
6
6
  "license": "SEE LICENSE IN LICENCA.md",
@@ -42,8 +42,10 @@
42
42
  "dependencies": {
43
43
  "@sharadox/database-web-library": "0.1.0-development.6",
44
44
  "@sharadox/email-web-library": "^0.7.1",
45
+ "@sharadox/protection-web-library": "0.1.0-development.9",
45
46
  "@sharadox/web-library": "^0.11.1",
46
- "kysely": "^0.28.20"
47
+ "kysely": "^0.28.20",
48
+ "zod": "^4.6.7"
47
49
  },
48
50
  "devDependencies": {
49
51
  "@electric-sql/pglite": "0.5.16",