@sharadox/account-web-library 0.1.0-development.9 → 0.1.0-email-channel.14

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 (51) hide show
  1. package/README.md +28 -0
  2. package/distribution/account-database/type.d.ts +21 -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-session-revocation/index.d.ts +14 -0
  8. package/distribution/account-session-revocation/index.d.ts.map +1 -0
  9. package/distribution/account-session-revocation/index.js +25 -0
  10. package/distribution/account-session-revocation/index.js.map +1 -0
  11. package/distribution/account-sign-in/index.d.ts +15 -0
  12. package/distribution/account-sign-in/index.d.ts.map +1 -0
  13. package/distribution/account-sign-in/index.js +76 -0
  14. package/distribution/account-sign-in/index.js.map +1 -0
  15. package/distribution/account-sign-in/type.d.ts +20 -0
  16. package/distribution/account-sign-in/type.d.ts.map +1 -0
  17. package/distribution/account-sign-in/type.js +2 -0
  18. package/distribution/account-sign-in/type.js.map +1 -0
  19. package/distribution/account-sign-out/index.d.ts +16 -0
  20. package/distribution/account-sign-out/index.d.ts.map +1 -0
  21. package/distribution/account-sign-out/index.js +34 -0
  22. package/distribution/account-sign-out/index.js.map +1 -0
  23. package/distribution/browser-session-read/index.d.ts +17 -0
  24. package/distribution/browser-session-read/index.d.ts.map +1 -0
  25. package/distribution/browser-session-read/index.js +48 -0
  26. package/distribution/browser-session-read/index.js.map +1 -0
  27. package/distribution/browser-session-read/type.d.ts +14 -0
  28. package/distribution/browser-session-read/type.d.ts.map +1 -0
  29. package/distribution/browser-session-read/type.js +2 -0
  30. package/distribution/browser-session-read/type.js.map +1 -0
  31. package/distribution/contact-channel/type.d.ts +20 -0
  32. package/distribution/contact-channel/type.d.ts.map +1 -0
  33. package/distribution/contact-channel/type.js +2 -0
  34. package/distribution/contact-channel/type.js.map +1 -0
  35. package/distribution/email-channel/index.d.ts +13 -0
  36. package/distribution/email-channel/index.d.ts.map +1 -0
  37. package/distribution/email-channel/index.js +90 -0
  38. package/distribution/email-channel/index.js.map +1 -0
  39. package/distribution/email-channel/type.d.ts +17 -0
  40. package/distribution/email-channel/type.d.ts.map +1 -0
  41. package/distribution/email-channel/type.js +2 -0
  42. package/distribution/email-channel/type.js.map +1 -0
  43. package/distribution/session-prune/index.d.ts +17 -0
  44. package/distribution/session-prune/index.d.ts.map +1 -0
  45. package/distribution/session-prune/index.js +30 -0
  46. package/distribution/session-prune/index.js.map +1 -0
  47. package/distribution/session-token/index.d.ts +19 -0
  48. package/distribution/session-token/index.d.ts.map +1 -0
  49. package/distribution/session-token/index.js +22 -0
  50. package/distribution/session-token/index.js.map +1 -0
  51. package/package.json +2 -1
package/README.md CHANGED
@@ -151,6 +151,34 @@ pass the limit; after the last attempt even the right code is refused.
151
151
 
152
152
  ### `code-request-prune`: `pruneCodeRequests(database, { before })` → deleted count
153
153
 
154
+ ### Sessions and the account selector
155
+
156
+ A browser holds one cookie with an opaque token (only its SHA-256 is stored). On the server, that browser session
157
+ points to any number of account sessions, so the selector has no limit and the cookie never grows. Use a
158
+ `__Host-` cookie: HttpOnly, Secure, SameSite=Lax, path `/`.
159
+
160
+ - `signIn(database, { accountId, token?, secondFactor?, accountSessionSeconds = 30 days, browserSessionSeconds = 400 days, now? })`
161
+ → `{ token, browserSessionId, accountSessionId }`. Adds the account to the browser's accounts (or starts a
162
+ browser session). The browser gets a **new token at every sign-in**: set it as the cookie.
163
+ - `readBrowserSession(database, { token, idleSeconds = 7 days, now? })` → `{ browserSessionId, accounts }` or
164
+ undefined. `accounts` are `{ accountSessionId, accountId, username, signedInAt, secondFactorAt }`, in sign-in
165
+ order, without ended or idle sessions. It marks them as seen.
166
+ - `signOut(database, { token, accountId? })` → `{ signedOut }`: one account, or all and the browser session.
167
+ - `revokeAccountSessions(database, { accountId, actorAccountId? })` → count: signed out on every browser.
168
+ - `pruneSessions(database, { idleSeconds?, now? })` → `{ accountSessions, browserSessions }`, for a daily job.
169
+ - `createSessionToken()`, `hashSessionToken(token)` (`session-token`).
170
+
171
+ ### `email-channel`: `createEmailChannel({ sender, from, design, logos?, now? })` → `ContactChannel`
172
+
173
+ Sends codes by email through email-web-library's sender, in the shared email layout.
174
+
175
+ - English, or European Portuguese for any `pt` reader.
176
+ - The code is in the body only, never in the subject (lock screens show subjects).
177
+ - No link to click: nothing for a phishing email to imitate.
178
+
179
+ A `ContactChannel` (`contact-channel/type`) is `{ kind, send({ contact, code, purpose, language, expiresAt }) }`:
180
+ phone, WhatsApp, Messenger and Instagram channels will have the same shape.
181
+
154
182
  ### `contact-kind`: `normalizeContact({ kind, value })`, `CONTACT_KINDS`
155
183
 
156
184
  `email` in this version; unknown kinds and invalid values throw.
@@ -61,6 +61,25 @@ export interface CodeRequestTable {
61
61
  /** When a newer request for the same contact and purpose replaced it. */
62
62
  voided_at: Date | null;
63
63
  }
64
+ /** One browser (the account site's cookie holds its token; only the token's hash is stored). */
65
+ export interface BrowserSessionTable {
66
+ id: string;
67
+ token_hash: string;
68
+ created_at: Date;
69
+ last_seen_at: Date;
70
+ expires_at: Date;
71
+ }
72
+ /** One account signed in on one browser: a browser can hold any number of them (the account selector). */
73
+ export interface AccountSessionTable {
74
+ id: string;
75
+ browser_session_id: string;
76
+ account_id: string;
77
+ created_at: Date;
78
+ last_seen_at: Date;
79
+ expires_at: Date;
80
+ /** When two-factor was last passed in this session; null without it. */
81
+ second_factor_at: Date | null;
82
+ }
64
83
  /** The tables `migrateAccount` creates, for typing a Kysely instance. */
65
84
  export interface AccountDatabase {
66
85
  application: ApplicationTable;
@@ -68,6 +87,8 @@ export interface AccountDatabase {
68
87
  contact: ContactTable;
69
88
  audit_event: AuditEventTable;
70
89
  code_request: CodeRequestTable;
90
+ browser_session: BrowserSessionTable;
91
+ account_session: AccountSessionTable;
71
92
  }
72
93
  export {};
73
94
  //# sourceMappingURL=type.d.ts.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AM,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,EAuGxC,CAAC;AAEF;;;;;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,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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U,EAAE,IAAI,IAAI,CAAC,GAAG,GAAG,qBAAqB,GAAG,IAAI,CAAC;YACxD,gBAAgB,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;;;;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EAAE,SAAS,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE,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;;;;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AAC,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC,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":""}
@@ -0,0 +1,20 @@
1
+ import type { CodePurpose } from "@sharadox/account-web-library/code-request/type";
2
+ import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
3
+ /** A one-time code to deliver to a contact. */
4
+ export interface CodeDelivery {
5
+ contact: Contact;
6
+ code: string;
7
+ purpose: CodePurpose;
8
+ /** The reader's language (`en`, `pt-PT`…); channels fall back to English. */
9
+ language: string;
10
+ expiresAt: Date;
11
+ }
12
+ /**
13
+ * How codes reach one kind of contact (email now; phone, WhatsApp, Messenger, Instagram later). Cap the sends of paid
14
+ * channels before calling `send` (protection-web-library's `spendFromCap`).
15
+ */
16
+ export interface ContactChannel {
17
+ kind: string;
18
+ send(delivery: CodeDelivery): Promise<void>;
19
+ }
20
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/contact-channel/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iDAAiD,CAAC;AACnF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,+CAA+C;AAC/C,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,WAAW,CAAC;IACrB,6EAA6E;IAC7E,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,QAAQ,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7C"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/contact-channel/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,13 @@
1
+ import type { ContactChannel } from "@sharadox/account-web-library/contact-channel/type";
2
+ import type { EmailChannelOptions } from "@sharadox/account-web-library/email-channel/type";
3
+ /**
4
+ * Sends one-time codes by email, in the shared Sharadox email layout (email-web-library), in English or European
5
+ * Portuguese (any `pt` reader). The code is in the body only, never the subject (lock screens show subjects), with
6
+ * no link to click: nothing to imitate in a phishing email.
7
+ *
8
+ * @example
9
+ * const channel = createEmailChannel({ sender: createAmazonSesSender(configuration), from, design });
10
+ * await channel.send({ contact, code, purpose: "sign-in", language: "pt-PT", expiresAt });
11
+ */
12
+ export declare function createEmailChannel({ sender, from, design, logos, now, }: EmailChannelOptions): ContactChannel;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/email-channel/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAgB,cAAc,EAAE,MAAM,oDAAoD,CAAC;AACvG,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kDAAkD,CAAC;AAsD5F;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,EACjC,MAAM,EACN,IAAI,EACJ,MAAM,EACN,KAAU,EACV,GAAc,GACf,EAAE,mBAAmB,GAAG,cAAc,CA2CtC"}
@@ -0,0 +1,90 @@
1
+ import { emailLayout } from "@sharadox/email-web-library/email-layout";
2
+ const ENGLISH = {
3
+ language: "en",
4
+ subject: {
5
+ "sign-in": (company) => `Your ${company} sign-in code`,
6
+ "contact-verification": (company) => `Confirm your address for ${company}`,
7
+ "step-up": (company) => `Your ${company} confirmation code`,
8
+ },
9
+ eyebrow: { "sign-in": "Sign in", "contact-verification": "Confirm address", "step-up": "Confirmation" },
10
+ heading: {
11
+ "sign-in": "Your sign-in code",
12
+ "contact-verification": "Confirm this address",
13
+ "step-up": "Confirm it's you",
14
+ },
15
+ label: "Code",
16
+ lifetime: (minutes) => `It works once, for ${minutes} ${minutes === 1 ? "minute" : "minutes"}.`,
17
+ ignore: "If you didn't ask for it, you can ignore this email: without the code, nobody gets in.",
18
+ never: "We will never ask you for this code.",
19
+ closing: "Best regards,",
20
+ };
21
+ const PORTUGUESE = {
22
+ language: "pt-PT",
23
+ subject: {
24
+ "sign-in": (company) => `O seu código para entrar em ${company}`,
25
+ "contact-verification": (company) => `Confirme o seu endereço em ${company}`,
26
+ "step-up": (company) => `O seu código de confirmação de ${company}`,
27
+ },
28
+ eyebrow: { "sign-in": "Entrar", "contact-verification": "Confirmar endereço", "step-up": "Confirmação" },
29
+ heading: {
30
+ "sign-in": "O seu código para entrar",
31
+ "contact-verification": "Confirme este endereço",
32
+ "step-up": "Confirme que é você",
33
+ },
34
+ label: "Código",
35
+ lifetime: (minutes) => `Funciona uma vez, durante ${minutes} ${minutes === 1 ? "minuto" : "minutos"}.`,
36
+ ignore: "Se não o pediu, pode ignorar este email: sem o código, ninguém entra.",
37
+ never: "Nunca lhe pediremos este código.",
38
+ closing: "Com os melhores cumprimentos,",
39
+ };
40
+ /**
41
+ * Sends one-time codes by email, in the shared Sharadox email layout (email-web-library), in English or European
42
+ * Portuguese (any `pt` reader). The code is in the body only, never the subject (lock screens show subjects), with
43
+ * no link to click: nothing to imitate in a phishing email.
44
+ *
45
+ * @example
46
+ * const channel = createEmailChannel({ sender: createAmazonSesSender(configuration), from, design });
47
+ * await channel.send({ contact, code, purpose: "sign-in", language: "pt-PT", expiresAt });
48
+ */
49
+ export function createEmailChannel({ sender, from, design, logos = [], now = Date.now, }) {
50
+ const carried = { light: logos.some(({ id }) => id === "logo"), dark: logos.some(({ id }) => id === "logo-dark") };
51
+ return {
52
+ kind: "email",
53
+ async send({ contact, code, purpose, language, expiresAt }) {
54
+ if (contact.kind !== "email")
55
+ throw new Error(`the email channel can't send to a "${contact.kind}" contact`);
56
+ const words = language.toLowerCase().startsWith("pt") ? PORTUGUESE : ENGLISH;
57
+ const shown = code.replace(/(.{4})(?=.)/g, "$1 ");
58
+ const minutes = Math.max(1, Math.ceil((expiresAt.getTime() - now()) / 60_000));
59
+ const paragraphs = [words.lifetime(minutes), words.ignore, words.never];
60
+ const html = emailLayout({
61
+ language: words.language,
62
+ preview: words.heading[purpose],
63
+ eyebrow: words.eyebrow[purpose],
64
+ heading: words.heading[purpose],
65
+ rows: [{ label: words.label, value: shown }],
66
+ paragraphs,
67
+ closing: words.closing,
68
+ }, design, carried);
69
+ const text = [
70
+ words.heading[purpose],
71
+ "",
72
+ `${words.label}: ${shown}`,
73
+ "",
74
+ ...paragraphs,
75
+ "",
76
+ words.closing,
77
+ design.name,
78
+ ].join("\n");
79
+ await sender.send({
80
+ from,
81
+ to: contact.value,
82
+ subject: words.subject[purpose](design.name),
83
+ html,
84
+ text,
85
+ ...(logos.length > 0 ? { inlineImages: logos } : {}),
86
+ });
87
+ },
88
+ };
89
+ }
90
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/email-channel/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0CAA0C,CAAC;AAiBvE,MAAM,OAAO,GAAU;IACrB,QAAQ,EAAE,IAAI;IACd,OAAO,EAAE;QACP,SAAS,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,QAAQ,OAAO,eAAe;QACtD,sBAAsB,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,4BAA4B,OAAO,EAAE;QAC1E,SAAS,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,QAAQ,OAAO,oBAAoB;KAC5D;IACD,OAAO,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,SAAS,EAAE,cAAc,EAAE;IACvG,OAAO,EAAE;QACP,SAAS,EAAE,mBAAmB;QAC9B,sBAAsB,EAAE,sBAAs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E,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;gBAC5C,UAAU;gBACV,OAAO,EAAE,KAAK,CAAC,OAAO;aACvB,EACD,MAAM,EACN,OAAO,CACR,CAAC;YACF,MAAM,IAAI,GAAG;gBACX,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;gBACtB,EAAE;gBACF,GAAG,KAAK,CAAC,KAAK,KAAK,KAAK,EAAE;gBAC1B,EAAE;gBACF,GAAG,UAAU;gBACb,EAAE;gBACF,KAAK,CAAC,OAAO;gBACb,MAAM,CAAC,IAAI;aACZ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACb,MAAM,MAAM,CAAC,IAAI,CAAC;gBAChB,IAAI;gBACJ,EAAE,EAAE,OAAO,CAAC,KAAK;gBACjB,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC;gBAC5C,IAAI;gBACJ,IAAI;gBACJ,GAAG,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aACrD,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { EmailAddress } from "@sharadox/email-web-library/email-address/type";
2
+ import type { EmailDesign } from "@sharadox/email-web-library/email-layout/type";
3
+ import type { InlineImage } from "@sharadox/email-web-library/email-message/type";
4
+ import type { EmailSender } from "@sharadox/email-web-library/email-sender/type";
5
+ export interface EmailChannelOptions {
6
+ /** From email-web-library (`createAmazonSesSender`…). */
7
+ sender: EmailSender;
8
+ /** The sending address, for example `"Luis Pires Decor" <account@web.luispiresdecor.com>`. */
9
+ from: EmailAddress;
10
+ /** The company's name, logo size and signature (email-web-library's layout). */
11
+ design: EmailDesign;
12
+ /** The inline images "logo" and "logo-dark", when the design shows a logo. */
13
+ logos?: InlineImage[];
14
+ /** The clock, for how long the code still works (Date.now by default). */
15
+ now?: () => number;
16
+ }
17
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/email-channel/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gDAAgD,CAAC;AACnF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+CAA+C,CAAC;AACjF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gDAAgD,CAAC;AAClF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+CAA+C,CAAC;AAEjF,MAAM,WAAW,mBAAmB;IAClC,yDAAyD;IACzD,MAAM,EAAE,WAAW,CAAC;IACpB,8FAA8F;IAC9F,IAAI,EAAE,YAAY,CAAC;IACnB,gFAAgF;IAChF,MAAM,EAAE,WAAW,CAAC;IACpB,8EAA8E;IAC9E,KAAK,CAAC,EAAE,WAAW,EAAE,CAAC;IACtB,0EAA0E;IAC1E,GAAG,CAAC,EAAE,MAAM,MAAM,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/email-channel/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,17 @@
1
+ import { type Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ /**
4
+ * Deletes account sessions past their end or idle longer than `idleSeconds` (7 days by default, as
5
+ * `readBrowserSession`), then browser sessions that ended or hold no account; from a daily job. Returns the counts.
6
+ *
7
+ * @example
8
+ * await pruneSessions(database);
9
+ */
10
+ export declare function pruneSessions(database: Kysely<AccountDatabase>, { idleSeconds, now }?: {
11
+ idleSeconds?: number;
12
+ now?: number;
13
+ }): Promise<{
14
+ accountSessions: number;
15
+ browserSessions: number;
16
+ }>;
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/session-prune/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAE3F;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,WAAwB,EAAE,GAAgB,EAAE,GAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAO,GAC1F,OAAO,CAAC;IAAE,eAAe,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,MAAM,CAAA;CAAE,CAAC,CA4B/D"}
@@ -0,0 +1,30 @@
1
+ import { sql } from "kysely";
2
+ /**
3
+ * Deletes account sessions past their end or idle longer than `idleSeconds` (7 days by default, as
4
+ * `readBrowserSession`), then browser sessions that ended or hold no account; from a daily job. Returns the counts.
5
+ *
6
+ * @example
7
+ * await pruneSessions(database);
8
+ */
9
+ export async function pruneSessions(database, { idleSeconds = 7 * 86_400, now = Date.now() } = {}) {
10
+ const at = new Date(now);
11
+ const accountSessions = await database
12
+ .deleteFrom("account_session")
13
+ .where((where) => where.or([where("expires_at", "<=", at), where("last_seen_at", "<=", new Date(now - idleSeconds * 1000))]))
14
+ .executeTakeFirst();
15
+ const browserSessions = await database
16
+ .deleteFrom("browser_session")
17
+ .where((where) => where.or([
18
+ where("expires_at", "<=", at),
19
+ where.not(where.exists(where
20
+ .selectFrom("account_session")
21
+ .select(sql `1`.as("one"))
22
+ .whereRef("account_session.browser_session_id", "=", "browser_session.id"))),
23
+ ]))
24
+ .executeTakeFirst();
25
+ return {
26
+ accountSessions: Number(accountSessions.numDeletedRows),
27
+ browserSessions: Number(browserSessions.numDeletedRows),
28
+ };
29
+ }
30
+ //# sourceMapping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,CAAC,cAAc,CAAC;QACvD,eAAe,EAAE,MAAM,CAAC,eAAe,CAAC,cAAc,CAAC;KACxD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * A new session token (256 random bits, base64url) for a cookie, and the hash to store instead: a leaked table gives
3
+ * no sessions. A plain SHA-256 is enough for tokens this random.
4
+ *
5
+ * @example
6
+ * const { token, hash } = createSessionToken();
7
+ */
8
+ export declare function createSessionToken(): {
9
+ token: string;
10
+ hash: string;
11
+ };
12
+ /**
13
+ * The stored form of a session token.
14
+ *
15
+ * @example
16
+ * hashSessionToken(cookie); // compare with browser_session.token_hash
17
+ */
18
+ export declare function hashSessionToken(token: string): string;
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/session-token/index.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,kBAAkB,IAAI;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAGpE;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEtD"}
@@ -0,0 +1,22 @@
1
+ import { createHash, randomBytes } from "node:crypto";
2
+ /**
3
+ * A new session token (256 random bits, base64url) for a cookie, and the hash to store instead: a leaked table gives
4
+ * no sessions. A plain SHA-256 is enough for tokens this random.
5
+ *
6
+ * @example
7
+ * const { token, hash } = createSessionToken();
8
+ */
9
+ export function createSessionToken() {
10
+ const token = randomBytes(32).toString("base64url");
11
+ return { token, hash: hashSessionToken(token) };
12
+ }
13
+ /**
14
+ * The stored form of a session token.
15
+ *
16
+ * @example
17
+ * hashSessionToken(cookie); // compare with browser_session.token_hash
18
+ */
19
+ export function hashSessionToken(token) {
20
+ return createHash("sha256").update(token).digest("base64url");
21
+ }
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/session-token/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEtD;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB;IAChC,MAAM,KAAK,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IACpD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;AAClD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AAChE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sharadox/account-web-library",
3
- "version": "0.1.0-development.9",
3
+ "version": "0.1.0-email-channel.14",
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",
@@ -41,6 +41,7 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@sharadox/database-web-library": "0.1.0-development.6",
44
+ "@sharadox/email-web-library": "^0.7.1",
44
45
  "@sharadox/web-library": "^0.11.1",
45
46
  "kysely": "^0.28.20"
46
47
  },