@sharadox/account-web-library 0.1.0-code-request.8 → 0.1.0-delivery-record.23

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 (103) hide show
  1. package/README.md +68 -0
  2. package/distribution/account-database/type.d.ts +24 -0
  3. package/distribution/account-database/type.d.ts.map +1 -1
  4. package/distribution/account-migration/index.d.ts.map +1 -1
  5. package/distribution/account-migration/index.js +24 -0
  6. package/distribution/account-migration/index.js.map +1 -1
  7. package/distribution/account-server/index.d.ts +15 -0
  8. package/distribution/account-server/index.d.ts.map +1 -0
  9. package/distribution/account-server/index.js +23 -0
  10. package/distribution/account-server/index.js.map +1 -0
  11. package/distribution/account-server/type.d.ts +28 -0
  12. package/distribution/account-server/type.d.ts.map +1 -0
  13. package/distribution/account-server/type.js +2 -0
  14. package/distribution/account-server/type.js.map +1 -0
  15. package/distribution/account-session-revocation/index.d.ts +14 -0
  16. package/distribution/account-session-revocation/index.d.ts.map +1 -0
  17. package/distribution/account-session-revocation/index.js +25 -0
  18. package/distribution/account-session-revocation/index.js.map +1 -0
  19. package/distribution/account-sign-in/index.d.ts +15 -0
  20. package/distribution/account-sign-in/index.d.ts.map +1 -0
  21. package/distribution/account-sign-in/index.js +76 -0
  22. package/distribution/account-sign-in/index.js.map +1 -0
  23. package/distribution/account-sign-in/type.d.ts +20 -0
  24. package/distribution/account-sign-in/type.d.ts.map +1 -0
  25. package/distribution/account-sign-in/type.js +2 -0
  26. package/distribution/account-sign-in/type.js.map +1 -0
  27. package/distribution/account-sign-out/index.d.ts +16 -0
  28. package/distribution/account-sign-out/index.d.ts.map +1 -0
  29. package/distribution/account-sign-out/index.js +34 -0
  30. package/distribution/account-sign-out/index.js.map +1 -0
  31. package/distribution/browser-session-read/index.d.ts +17 -0
  32. package/distribution/browser-session-read/index.d.ts.map +1 -0
  33. package/distribution/browser-session-read/index.js +48 -0
  34. package/distribution/browser-session-read/index.js.map +1 -0
  35. package/distribution/browser-session-read/type.d.ts +14 -0
  36. package/distribution/browser-session-read/type.d.ts.map +1 -0
  37. package/distribution/browser-session-read/type.js +2 -0
  38. package/distribution/browser-session-read/type.js.map +1 -0
  39. package/distribution/challenge-issue/index.d.ts +13 -0
  40. package/distribution/challenge-issue/index.d.ts.map +1 -0
  41. package/distribution/challenge-issue/index.js +18 -0
  42. package/distribution/challenge-issue/index.js.map +1 -0
  43. package/distribution/contact-channel/type.d.ts +20 -0
  44. package/distribution/contact-channel/type.d.ts.map +1 -0
  45. package/distribution/contact-channel/type.js +2 -0
  46. package/distribution/contact-channel/type.js.map +1 -0
  47. package/distribution/email-channel/index.d.ts +13 -0
  48. package/distribution/email-channel/index.d.ts.map +1 -0
  49. package/distribution/email-channel/index.js +90 -0
  50. package/distribution/email-channel/index.js.map +1 -0
  51. package/distribution/email-channel/type.d.ts +17 -0
  52. package/distribution/email-channel/type.d.ts.map +1 -0
  53. package/distribution/email-channel/type.js +2 -0
  54. package/distribution/email-channel/type.js.map +1 -0
  55. package/distribution/secret-derivation/index.d.ts +10 -0
  56. package/distribution/secret-derivation/index.d.ts.map +1 -0
  57. package/distribution/secret-derivation/index.js +15 -0
  58. package/distribution/secret-derivation/index.js.map +1 -0
  59. package/distribution/session-prune/index.d.ts +17 -0
  60. package/distribution/session-prune/index.d.ts.map +1 -0
  61. package/distribution/session-prune/index.js +30 -0
  62. package/distribution/session-prune/index.js.map +1 -0
  63. package/distribution/session-token/index.d.ts +19 -0
  64. package/distribution/session-token/index.d.ts.map +1 -0
  65. package/distribution/session-token/index.js +22 -0
  66. package/distribution/session-token/index.js.map +1 -0
  67. package/distribution/sign-in-finish/index.d.ts +12 -0
  68. package/distribution/sign-in-finish/index.d.ts.map +1 -0
  69. package/distribution/sign-in-finish/index.js +47 -0
  70. package/distribution/sign-in-finish/index.js.map +1 -0
  71. package/distribution/sign-in-finish/type.d.ts +34 -0
  72. package/distribution/sign-in-finish/type.d.ts.map +1 -0
  73. package/distribution/sign-in-finish/type.js +2 -0
  74. package/distribution/sign-in-finish/type.js.map +1 -0
  75. package/distribution/sign-in-start/index.d.ts +19 -0
  76. package/distribution/sign-in-start/index.d.ts.map +1 -0
  77. package/distribution/sign-in-start/index.js +93 -0
  78. package/distribution/sign-in-start/index.js.map +1 -0
  79. package/distribution/sign-in-start/type.d.ts +27 -0
  80. package/distribution/sign-in-start/type.d.ts.map +1 -0
  81. package/distribution/sign-in-start/type.js +2 -0
  82. package/distribution/sign-in-start/type.js.map +1 -0
  83. package/distribution/sign-up-finish/index.d.ts +12 -0
  84. package/distribution/sign-up-finish/index.d.ts.map +1 -0
  85. package/distribution/sign-up-finish/index.js +27 -0
  86. package/distribution/sign-up-finish/index.js.map +1 -0
  87. package/distribution/sign-up-finish/type.d.ts +21 -0
  88. package/distribution/sign-up-finish/type.d.ts.map +1 -0
  89. package/distribution/sign-up-finish/type.js +2 -0
  90. package/distribution/sign-up-finish/type.js.map +1 -0
  91. package/distribution/signed-ticket/index.d.ts +19 -0
  92. package/distribution/signed-ticket/index.d.ts.map +1 -0
  93. package/distribution/signed-ticket/index.js +35 -0
  94. package/distribution/signed-ticket/index.js.map +1 -0
  95. package/distribution/signed-ticket/type.d.ts +16 -0
  96. package/distribution/signed-ticket/type.d.ts.map +1 -0
  97. package/distribution/signed-ticket/type.js +2 -0
  98. package/distribution/signed-ticket/type.js.map +1 -0
  99. package/distribution/test-server/index.d.ts +10 -0
  100. package/distribution/test-server/index.d.ts.map +1 -0
  101. package/distribution/test-server/index.js +16 -0
  102. package/distribution/test-server/index.js.map +1 -0
  103. package/package.json +5 -2
package/README.md CHANGED
@@ -19,6 +19,46 @@ 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
+ Each send is recorded in the audit log as `code.sent`, or as `code.delivery-failed` with the error, never the
38
+ code.
39
+ Otherwise it answers `refused` with `proof`, `contact`, `unavailable` (the cap is reached: sending pauses), or
40
+ `rate-limit` with `retryAfterSeconds`. The answer never depends on whether an account exists.
41
+
42
+ 3. `finishSignIn(server, { requestId, code, address, token, now? })` limits guesses per address group. It answers:
43
+ - `signed-in`, with `{ accountId, token }`: set `token` as the cookie;
44
+ - `sign-up`, with `{ ticket }`, for a contact with no account;
45
+ - `refused`, with the code's reason or `rate-limit`.
46
+ 4. `finishSignUp(server, { ticket, username, token, now? })` creates the account and signs it in. A refused
47
+ username can be tried again with the same ticket, which lasts 15 minutes.
48
+
49
+ `DEFAULT_SIGN_IN_LIMITS` (`account-server`):
50
+
51
+ | Limit | Default |
52
+ | ------------------------- | --------------------------------- |
53
+ | `perContact` | 3 codes, then 1 every 5 minutes |
54
+ | `perAddress` | 10, then 1 a minute |
55
+ | `global` | 200, then 1 a second |
56
+ | `verificationsPerAddress` | 20 typed codes, then 1 every 30 s |
57
+ | `dailySendsPerChannel` | 2,000 |
58
+ | `difficulty` | 14 bits |
59
+
60
+ `createTicket` and `readTicket` (`signed-ticket`) carry signed, short-lived data between steps.
61
+
22
62
  ## Storage
23
63
 
24
64
  Postgres through Kysely (`@sharadox/database-web-library`). Run `migrateAccount(database)` when the application
@@ -151,6 +191,34 @@ pass the limit; after the last attempt even the right code is refused.
151
191
 
152
192
  ### `code-request-prune`: `pruneCodeRequests(database, { before })` → deleted count
153
193
 
194
+ ### Sessions and the account selector
195
+
196
+ A browser holds one cookie with an opaque token (only its SHA-256 is stored). On the server, that browser session
197
+ points to any number of account sessions, so the selector has no limit and the cookie never grows. Use a
198
+ `__Host-` cookie: HttpOnly, Secure, SameSite=Lax, path `/`.
199
+
200
+ - `signIn(database, { accountId, token?, secondFactor?, accountSessionSeconds = 30 days, browserSessionSeconds = 400 days, now? })`
201
+ → `{ token, browserSessionId, accountSessionId }`. Adds the account to the browser's accounts (or starts a
202
+ browser session). The browser gets a **new token at every sign-in**: set it as the cookie.
203
+ - `readBrowserSession(database, { token, idleSeconds = 7 days, now? })` → `{ browserSessionId, accounts }` or
204
+ undefined. `accounts` are `{ accountSessionId, accountId, username, signedInAt, secondFactorAt }`, in sign-in
205
+ order, without ended or idle sessions. It marks them as seen.
206
+ - `signOut(database, { token, accountId? })` → `{ signedOut }`: one account, or all and the browser session.
207
+ - `revokeAccountSessions(database, { accountId, actorAccountId? })` → count: signed out on every browser.
208
+ - `pruneSessions(database, { idleSeconds?, now? })` → `{ accountSessions, browserSessions }`, for a daily job.
209
+ - `createSessionToken()`, `hashSessionToken(token)` (`session-token`).
210
+
211
+ ### `email-channel`: `createEmailChannel({ sender, from, design, logos?, now? })` → `ContactChannel`
212
+
213
+ Sends codes by email through email-web-library's sender, in the shared email layout.
214
+
215
+ - English, or European Portuguese for any `pt` reader.
216
+ - The code is in the body only, never in the subject (lock screens show subjects).
217
+ - No link to click: nothing for a phishing email to imitate.
218
+
219
+ A `ContactChannel` (`contact-channel/type`) is `{ kind, send({ contact, code, purpose, language, expiresAt }) }`:
220
+ phone, WhatsApp, Messenger and Instagram channels will have the same shape.
221
+
154
222
  ### `contact-kind`: `normalizeContact({ kind, value })`, `CONTACT_KINDS`
155
223
 
156
224
  `email` in this version; unknown kinds and invalid values throw.
@@ -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>;
@@ -61,6 +62,25 @@ export interface CodeRequestTable {
61
62
  /** When a newer request for the same contact and purpose replaced it. */
62
63
  voided_at: Date | null;
63
64
  }
65
+ /** One browser (the account site's cookie holds its token; only the token's hash is stored). */
66
+ export interface BrowserSessionTable {
67
+ id: string;
68
+ token_hash: string;
69
+ created_at: Date;
70
+ last_seen_at: Date;
71
+ expires_at: Date;
72
+ }
73
+ /** One account signed in on one browser: a browser can hold any number of them (the account selector). */
74
+ export interface AccountSessionTable {
75
+ id: string;
76
+ browser_session_id: string;
77
+ account_id: string;
78
+ created_at: Date;
79
+ last_seen_at: Date;
80
+ expires_at: Date;
81
+ /** When two-factor was last passed in this session; null without it. */
82
+ second_factor_at: Date | null;
83
+ }
64
84
  /** The tables `migrateAccount` creates, for typing a Kysely instance. */
65
85
  export interface AccountDatabase {
66
86
  application: ApplicationTable;
@@ -68,6 +88,10 @@ export interface AccountDatabase {
68
88
  contact: ContactTable;
69
89
  audit_event: AuditEventTable;
70
90
  code_request: CodeRequestTable;
91
+ browser_session: BrowserSessionTable;
92
+ account_session: AccountSessionTable;
71
93
  }
94
+ /** The account tables and the protection tables (rate limits, spending caps, used challenges) the server needs. */
95
+ export type AccountServerDatabase = AccountDatabase & ProtectionDatabase;
72
96
  export {};
73
97
  //# sourceMappingURL=type.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-database/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAEpD,wDAAwD;AACxD,KAAK,WAAW,GAAG,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,SAAS,EAAE,IAAI,CAAC,CAAC;AAE5D,sFAAsF;AACtF,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,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;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAElF"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-migration/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAE/E,iHAAiH;AACjH,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAE3D;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,SAAS,EA+HxC,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAElF"}
@@ -109,6 +109,30 @@ export const accountMigrations = [
109
109
  create index code_request_expires_at on code_request (expires_at);
110
110
  `,
111
111
  },
112
+ {
113
+ name: "0003-session",
114
+ sql: `
115
+ create table browser_session (
116
+ id uuid primary key check (uuid_extract_version(id) = 7),
117
+ token_hash text not null unique,
118
+ created_at timestamptz not null,
119
+ last_seen_at timestamptz not null,
120
+ expires_at timestamptz not null
121
+ );
122
+ create index browser_session_expires_at on browser_session (expires_at);
123
+ create table account_session (
124
+ id uuid primary key check (uuid_extract_version(id) = 7),
125
+ browser_session_id uuid not null references browser_session (id) on delete cascade,
126
+ account_id uuid not null references account (id) on delete cascade,
127
+ created_at timestamptz not null,
128
+ last_seen_at timestamptz not null,
129
+ expires_at timestamptz not null,
130
+ second_factor_at timestamptz,
131
+ unique (browser_session_id, account_id)
132
+ );
133
+ create index account_session_account on account_session (account_id);
134
+ `,
135
+ },
112
136
  ];
113
137
  /**
114
138
  * Creates or updates the account tables, tracked in their own migration table. Returns the names applied.
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,0CAA0C,CAAC;AAGnE,iHAAiH;AACjH,MAAM,CAAC,MAAM,uBAAuB,GAAG,mBAAmB,CAAC;AAE3D;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAgB;IAC5C;QACE,IAAI,EAAE,wCAAwC;QAC9C,GAAG,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA8EJ;KACF;IACD;QACE,IAAI,EAAE,mBAAmB;QACzB,GAAG,EAAE;;;;;;;;;;;;;;;;KAgBJ;KACF;CACF,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAS,QAAwB;IAC7D,OAAO,OAAO,CAAC,QAAQ,EAAE,iBAAiB,EAAE,EAAE,KAAK,EAAE,uBAAuB,EAAE,CAAC,CAAC;AAClF,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,0CAA0C,CAAC;AAGnE,iHAAiH;AACjH,MAAM,CAAC,MAAM,uBAAuB,GAAG,mBAAmB,CAAC;AAE3D;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAgB;IAC5C;QACE,IAAI,EAAE,wCAAwC;QAC9C,GAAG,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA8EJ;KACF;IACD;QACE,IAAI,EAAE,mBAAmB;QACzB,GAAG,EAAE;;;;;;;;;;;;;;;;KAgBJ;KACF;IACD;QACE,IAAI,EAAE,cAAc;QACpB,GAAG,EAAE;;;;;;;;;;;;;;;;;;;;KAoBJ;KACF;CACF,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAS,QAAwB;IAC7D,OAAO,OAAO,CAAC,QAAQ,EAAE,iBAAiB,EAAE,EAAE,KAAK,EAAE,uBAAuB,EAAE,CAAC,CAAC;AAClF,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { AccountServer, 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,14 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ /**
4
+ * Signs an account out on every browser at once: after a ban, a lost device, a recovery, or when its owner asks.
5
+ * Records who did it. Returns how many sessions ended.
6
+ *
7
+ * @example
8
+ * await revokeAccountSessions(database, { accountId, actorAccountId: moderatorId });
9
+ */
10
+ export declare function revokeAccountSessions(database: Kysely<AccountDatabase>, { accountId, actorAccountId }: {
11
+ accountId: string;
12
+ actorAccountId?: string | null;
13
+ }): Promise<number>;
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-session-revocation/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAG3F;;;;;;GAMG;AACH,wBAAsB,qBAAqB,CACzC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,cAAqB,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GAC1F,OAAO,CAAC,MAAM,CAAC,CAejB"}
@@ -0,0 +1,25 @@
1
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
2
+ /**
3
+ * Signs an account out on every browser at once: after a ban, a lost device, a recovery, or when its owner asks.
4
+ * Records who did it. Returns how many sessions ended.
5
+ *
6
+ * @example
7
+ * await revokeAccountSessions(database, { accountId, actorAccountId: moderatorId });
8
+ */
9
+ export async function revokeAccountSessions(database, { accountId, actorAccountId = null }) {
10
+ return database.transaction().execute(async (transaction) => {
11
+ const removed = await transaction
12
+ .deleteFrom("account_session")
13
+ .where("account_id", "=", accountId)
14
+ .returning("id")
15
+ .execute();
16
+ await recordAuditEvent(transaction, {
17
+ accountId,
18
+ actorAccountId,
19
+ action: "session.revoked",
20
+ detail: { sessions: removed.length },
21
+ });
22
+ return removed.length;
23
+ });
24
+ }
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-session-revocation/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAE7E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,QAAiC,EACjC,EAAE,SAAS,EAAE,cAAc,GAAG,IAAI,EAAyD;IAE3F,OAAO,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;QAC1D,MAAM,OAAO,GAAG,MAAM,WAAW;aAC9B,UAAU,CAAC,iBAAiB,CAAC;aAC7B,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,SAAS,CAAC;aACnC,SAAS,CAAC,IAAI,CAAC;aACf,OAAO,EAAE,CAAC;QACb,MAAM,gBAAgB,CAAC,WAAW,EAAE;YAClC,SAAS;YACT,cAAc;YACd,MAAM,EAAE,iBAAiB;YACzB,MAAM,EAAE,EAAE,QAAQ,EAAE,OAAO,CAAC,MAAM,EAAE;SACrC,CAAC,CAAC;QACH,OAAO,OAAO,CAAC,MAAM,CAAC;IACxB,CAAC,CAAC,CAAC;AACL,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 { SignInOptions, SignInResult } from "@sharadox/account-web-library/account-sign-in/type";
4
+ /**
5
+ * Signs an account in on a browser, after its code (and two-factor, when it has it) passed: the account joins the
6
+ * accounts already signed in there (the account selector has no limit), or a new browser session starts. The browser
7
+ * gets a new token every time, so a token planted before sign-in is worthless. Signing in again with the same account
8
+ * replaces its session there.
9
+ *
10
+ * @example
11
+ * const { token } = await signIn(database, { accountId, token: cookies.get("__Host-account")?.value });
12
+ * cookies.set("__Host-account", token, { httpOnly: true, secure: true, sameSite: "lax", path: "/" });
13
+ */
14
+ export declare function signIn(database: Kysely<AccountDatabase>, { accountId, token, secondFactor, accountSessionSeconds, browserSessionSeconds, now, }: SignInOptions): Promise<SignInResult>;
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-sign-in/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,aAAa,EAAE,YAAY,EAAE,MAAM,oDAAoD,CAAC;AAMtG;;;;;;;;;GASG;AACH,wBAAsB,MAAM,CAC1B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EACE,SAAS,EACT,KAAK,EACL,YAAoB,EACpB,qBAAgC,EAChC,qBAAiC,EACjC,GAAgB,GACjB,EAAE,aAAa,GACf,OAAO,CAAC,YAAY,CAAC,CA2DvB"}
@@ -0,0 +1,76 @@
1
+ import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
2
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
3
+ import { createSessionToken, hashSessionToken } from "@sharadox/account-web-library/session-token";
4
+ const DAY = 86_400;
5
+ /**
6
+ * Signs an account in on a browser, after its code (and two-factor, when it has it) passed: the account joins the
7
+ * accounts already signed in there (the account selector has no limit), or a new browser session starts. The browser
8
+ * gets a new token every time, so a token planted before sign-in is worthless. Signing in again with the same account
9
+ * replaces its session there.
10
+ *
11
+ * @example
12
+ * const { token } = await signIn(database, { accountId, token: cookies.get("__Host-account")?.value });
13
+ * cookies.set("__Host-account", token, { httpOnly: true, secure: true, sameSite: "lax", path: "/" });
14
+ */
15
+ export async function signIn(database, { accountId, token, secondFactor = false, accountSessionSeconds = 30 * DAY, browserSessionSeconds = 400 * DAY, now = Date.now(), }) {
16
+ const at = new Date(now);
17
+ const fresh = createSessionToken();
18
+ return database.transaction().execute(async (transaction) => {
19
+ const browser = token
20
+ ? await transaction
21
+ .selectFrom("browser_session")
22
+ .select("id")
23
+ .where("token_hash", "=", hashSessionToken(token))
24
+ .where("expires_at", ">", at)
25
+ .forUpdate()
26
+ .executeTakeFirst()
27
+ : undefined;
28
+ const browserSessionId = browser?.id ?? uuidVersion7({ now });
29
+ if (browser) {
30
+ await transaction
31
+ .updateTable("browser_session")
32
+ .set({ token_hash: fresh.hash, last_seen_at: at })
33
+ .where("id", "=", browserSessionId)
34
+ .execute();
35
+ }
36
+ else {
37
+ await transaction
38
+ .insertInto("browser_session")
39
+ .values({
40
+ id: browserSessionId,
41
+ token_hash: fresh.hash,
42
+ created_at: at,
43
+ last_seen_at: at,
44
+ expires_at: new Date(now + browserSessionSeconds * 1000),
45
+ })
46
+ .execute();
47
+ }
48
+ await transaction
49
+ .deleteFrom("account_session")
50
+ .where("browser_session_id", "=", browserSessionId)
51
+ .where("account_id", "=", accountId)
52
+ .execute();
53
+ const accountSessionId = uuidVersion7({ now });
54
+ await transaction
55
+ .insertInto("account_session")
56
+ .values({
57
+ id: accountSessionId,
58
+ browser_session_id: browserSessionId,
59
+ account_id: accountId,
60
+ created_at: at,
61
+ last_seen_at: at,
62
+ expires_at: new Date(now + accountSessionSeconds * 1000),
63
+ second_factor_at: secondFactor ? at : null,
64
+ })
65
+ .execute();
66
+ await transaction.updateTable("account").set({ last_active_at: at }).where("id", "=", accountId).execute();
67
+ await recordAuditEvent(transaction, {
68
+ accountId,
69
+ action: "session.signed-in",
70
+ detail: { accountSessionId, secondFactor },
71
+ now,
72
+ });
73
+ return { token: fresh.token, browserSessionId, accountSessionId };
74
+ });
75
+ }
76
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-sign-in/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAG/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,6CAA6C,CAAC;AAEnG,MAAM,GAAG,GAAG,MAAM,CAAC;AAEnB;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAC1B,QAAiC,EACjC,EACE,SAAS,EACT,KAAK,EACL,YAAY,GAAG,KAAK,EACpB,qBAAqB,GAAG,EAAE,GAAG,GAAG,EAChC,qBAAqB,GAAG,GAAG,GAAG,GAAG,EACjC,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,GACF;IAEhB,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,MAAM,KAAK,GAAG,kBAAkB,EAAE,CAAC;IACnC,OAAO,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;QAC1D,MAAM,OAAO,GAAG,KAAK;YACnB,CAAC,CAAC,MAAM,WAAW;iBACd,UAAU,CAAC,iBAAiB,CAAC;iBAC7B,MAAM,CAAC,IAAI,CAAC;iBACZ,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,gBAAgB,CAAC,KAAK,CAAC,CAAC;iBACjD,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,EAAE,CAAC;iBAC5B,SAAS,EAAE;iBACX,gBAAgB,EAAE;YACvB,CA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AgB,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI;SAC3C,CAAC;aACD,OAAO,EAAE,CAAC;QACb,MAAM,WAAW,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,EAAE,cAAc,EAAE,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC,CAAC,OAAO,EAAE,CAAC;QAC3G,MAAM,gBAAgB,CAAC,WAAW,EAAE;YAClC,SAAS;YACT,MAAM,EAAE,mBAAmB;YAC3B,MAAM,EAAE,EAAE,gBAAgB,EAAE,YAAY,EAAE;YAC1C,GAAG;SACJ,CAAC,CAAC;QACH,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,CAAC;IACpE,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,20 @@
1
+ export interface SignInOptions {
2
+ accountId: string;
3
+ /** The browser's current token (its cookie), if it has one: the account joins that browser's accounts. */
4
+ token?: string;
5
+ /** Whether two-factor was passed for this sign-in. */
6
+ secondFactor?: boolean;
7
+ /** 30 days by default. */
8
+ accountSessionSeconds?: number;
9
+ /** 400 days by default (the longest a browser keeps a cookie). */
10
+ browserSessionSeconds?: number;
11
+ /** Milliseconds since 1970 (now by default). */
12
+ now?: number;
13
+ }
14
+ export interface SignInResult {
15
+ /** The browser's new token: set it as the cookie (it replaces the old one at every sign-in). */
16
+ token: string;
17
+ browserSessionId: string;
18
+ accountSessionId: string;
19
+ }
20
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-sign-in/type.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,0GAA0G;IAC1G,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,sDAAsD;IACtD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,0BAA0B;IAC1B,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,kEAAkE;IAClE,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,YAAY;IAC3B,gGAAgG;IAChG,KAAK,EAAE,MAAM,CAAC;IACd,gBAAgB,EAAE,MAAM,CAAC;IACzB,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/account-sign-in/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
+ /**
4
+ * Signs one account out of a browser (`accountId`), or every account and the browser session with it. Returns the
5
+ * accounts signed out; tell their applications (back-channel logout) so they end their own sessions too.
6
+ *
7
+ * @example
8
+ * await signOut(database, { token: cookies.get("__Host-account")?.value, accountId });
9
+ */
10
+ export declare function signOut(database: Kysely<AccountDatabase>, { token, accountId }: {
11
+ token: string | undefined;
12
+ accountId?: string;
13
+ }): Promise<{
14
+ signedOut: string[];
15
+ }>;
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-sign-out/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F;;;;;;GAMG;AACH,wBAAsB,OAAO,CAC3B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE;IAAE,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,GACtE,OAAO,CAAC;IAAE,SAAS,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC,CAmBlC"}
@@ -0,0 +1,34 @@
1
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
2
+ import { hashSessionToken } from "@sharadox/account-web-library/session-token";
3
+ /**
4
+ * Signs one account out of a browser (`accountId`), or every account and the browser session with it. Returns the
5
+ * accounts signed out; tell their applications (back-channel logout) so they end their own sessions too.
6
+ *
7
+ * @example
8
+ * await signOut(database, { token: cookies.get("__Host-account")?.value, accountId });
9
+ */
10
+ export async function signOut(database, { token, accountId }) {
11
+ if (!token)
12
+ return { signedOut: [] };
13
+ return database.transaction().execute(async (transaction) => {
14
+ const browser = await transaction
15
+ .selectFrom("browser_session")
16
+ .select("id")
17
+ .where("token_hash", "=", hashSessionToken(token))
18
+ .forUpdate()
19
+ .executeTakeFirst();
20
+ if (!browser)
21
+ return { signedOut: [] };
22
+ let removal = transaction.deleteFrom("account_session").where("browser_session_id", "=", browser.id);
23
+ if (accountId)
24
+ removal = removal.where("account_id", "=", accountId);
25
+ const removed = await removal.returning("account_id").execute();
26
+ if (!accountId)
27
+ await transaction.deleteFrom("browser_session").where("id", "=", browser.id).execute();
28
+ for (const { account_id } of removed) {
29
+ await recordAuditEvent(transaction, { accountId: account_id, action: "session.signed-out" });
30
+ }
31
+ return { signedOut: removed.map(({ account_id }) => account_id) };
32
+ });
33
+ }
34
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-sign-out/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,gBAAgB,EAAE,MAAM,6CAA6C,CAAC;AAE/E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,OAAO,CAC3B,QAAiC,EACjC,EAAE,KAAK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AE,CAAC,UAAU,CAAC,EAAE,CAAC;IACpE,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ import type { BrowserSession } from "@sharadox/account-web-library/browser-session-read/type";
4
+ /**
5
+ * The accounts signed in on a browser, from its cookie's token, leaving out sessions past their end or idle longer
6
+ * than `idleSeconds` (7 days by default); marks the browser and those sessions as seen now. Undefined for a missing,
7
+ * unknown or expired token.
8
+ *
9
+ * @example
10
+ * const session = await readBrowserSession(database, { token: cookies.get("__Host-account")?.value });
11
+ */
12
+ export declare function readBrowserSession(database: Kysely<AccountDatabase>, { token, idleSeconds, now, }: {
13
+ token: string | undefined;
14
+ idleSeconds?: number;
15
+ now?: number;
16
+ }): Promise<BrowserSession | undefined>;
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/browser-session-read/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAC3F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yDAAyD,CAAC;AAG9F;;;;;;;GAOG;AACH,wBAAsB,kBAAkB,CACtC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EACE,KAAK,EACL,WAAwB,EACxB,GAAgB,GACjB,EAAE;IAAE,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GACnE,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CAuCrC"}
@@ -0,0 +1,48 @@
1
+ import { hashSessionToken } from "@sharadox/account-web-library/session-token";
2
+ /**
3
+ * The accounts signed in on a browser, from its cookie's token, leaving out sessions past their end or idle longer
4
+ * than `idleSeconds` (7 days by default); marks the browser and those sessions as seen now. Undefined for a missing,
5
+ * unknown or expired token.
6
+ *
7
+ * @example
8
+ * const session = await readBrowserSession(database, { token: cookies.get("__Host-account")?.value });
9
+ */
10
+ export async function readBrowserSession(database, { token, idleSeconds = 7 * 86_400, now = Date.now(), }) {
11
+ if (!token)
12
+ return undefined;
13
+ const at = new Date(now);
14
+ const browser = await database
15
+ .selectFrom("browser_session")
16
+ .select("id")
17
+ .where("token_hash", "=", hashSessionToken(token))
18
+ .where("expires_at", ">", at)
19
+ .executeTakeFirst();
20
+ if (!browser)
21
+ return undefined;
22
+ const rows = await database
23
+ .selectFrom("account_session")
24
+ .innerJoin("account", "account.id", "account_session.account_id")
25
+ .select([
26
+ "account_session.id as accountSessionId",
27
+ "account.id as accountId",
28
+ "account.username as username",
29
+ "account_session.created_at as signedInAt",
30
+ "account_session.second_factor_at as secondFactorAt",
31
+ ])
32
+ .where("account_session.browser_session_id", "=", browser.id)
33
+ .where("account_session.expires_at", ">", at)
34
+ .where("account_session.last_seen_at", ">", new Date(now - idleSeconds * 1000))
35
+ .orderBy("account_session.created_at")
36
+ .orderBy("account_session.id")
37
+ .execute();
38
+ await database.updateTable("browser_session").set({ last_seen_at: at }).where("id", "=", browser.id).execute();
39
+ if (rows.length > 0) {
40
+ await database
41
+ .updateTable("account_session")
42
+ .set({ last_seen_at: at })
43
+ .where("id", "in", rows.map(({ accountSessionId }) => accountSessionId))
44
+ .execute();
45
+ }
46
+ return { browserSessionId: browser.id, accounts: rows };
47
+ }
48
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/browser-session-read/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,6CAA6C,CAAC;AAE/E;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,QAAiC,EACjC,EACE,KAA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C,OAAO,EAAE,CAAC;IAC/G,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACpB,MAAM,QAAQ;aACX,WAAW,CAAC,iBAAiB,CAAC;aAC9B,GAAG,CAAC,EAAE,YAAY,EAAE,EAAE,EAAE,CAAC;aACzB,KAAK,CACJ,IAAI,EACJ,IAAI,EACJ,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,gBAAgB,EAAE,EAAE,EAAE,CAAC,gBAAgB,CAAC,CACrD;aACA,OAAO,EAAE,CAAC;IACf,CAAC;IACD,OAAO,EAAE,gBAAgB,EAAE,OAAO,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;AAC1D,CAAC"}
@@ -0,0 +1,14 @@
1
+ /** An account signed in on the browser, as the account selector shows it. */
2
+ export interface SignedInAccount {
3
+ accountSessionId: string;
4
+ accountId: string;
5
+ username: string;
6
+ signedInAt: Date;
7
+ secondFactorAt: Date | null;
8
+ }
9
+ export interface BrowserSession {
10
+ browserSessionId: string;
11
+ /** In the order they signed in. */
12
+ accounts: SignedInAccount[];
13
+ }
14
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/browser-session-read/type.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,MAAM,WAAW,eAAe;IAC9B,gBAAgB,EAAE,MAAM,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,IAAI,CAAC;IACjB,cAAc,EAAE,IAAI,GAAG,IAAI,CAAC;CAC7B;AAED,MAAM,WAAW,cAAc;IAC7B,gBAAgB,EAAE,MAAM,CAAC;IACzB,mCAAmC;IACnC,QAAQ,EAAE,eAAe,EAAE,CAAC;CAC7B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/browser-session-read/type.ts"],"names":[],"mappings":""}