@sharadox/account-web-library 0.1.0-development.6 → 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.
- package/README.md +45 -0
- package/distribution/account-database/type.d.ts +40 -0
- package/distribution/account-database/type.d.ts.map +1 -1
- package/distribution/account-migration/index.d.ts.map +1 -1
- package/distribution/account-migration/index.js +44 -0
- package/distribution/account-migration/index.js.map +1 -1
- package/distribution/account-session-revocation/index.d.ts +14 -0
- package/distribution/account-session-revocation/index.d.ts.map +1 -0
- package/distribution/account-session-revocation/index.js +25 -0
- package/distribution/account-session-revocation/index.js.map +1 -0
- package/distribution/account-sign-in/index.d.ts +15 -0
- package/distribution/account-sign-in/index.d.ts.map +1 -0
- package/distribution/account-sign-in/index.js +76 -0
- package/distribution/account-sign-in/index.js.map +1 -0
- package/distribution/account-sign-in/type.d.ts +20 -0
- package/distribution/account-sign-in/type.d.ts.map +1 -0
- package/distribution/account-sign-in/type.js +2 -0
- package/distribution/account-sign-in/type.js.map +1 -0
- package/distribution/account-sign-out/index.d.ts +16 -0
- package/distribution/account-sign-out/index.d.ts.map +1 -0
- package/distribution/account-sign-out/index.js +34 -0
- package/distribution/account-sign-out/index.js.map +1 -0
- package/distribution/browser-session-read/index.d.ts +17 -0
- package/distribution/browser-session-read/index.d.ts.map +1 -0
- package/distribution/browser-session-read/index.js +48 -0
- package/distribution/browser-session-read/index.js.map +1 -0
- package/distribution/browser-session-read/type.d.ts +14 -0
- package/distribution/browser-session-read/type.d.ts.map +1 -0
- package/distribution/browser-session-read/type.js +2 -0
- package/distribution/browser-session-read/type.js.map +1 -0
- package/distribution/code-request/index.d.ts +16 -0
- package/distribution/code-request/index.d.ts.map +1 -0
- package/distribution/code-request/index.js +52 -0
- package/distribution/code-request/index.js.map +1 -0
- package/distribution/code-request/type.d.ts +25 -0
- package/distribution/code-request/type.d.ts.map +1 -0
- package/distribution/code-request/type.js +2 -0
- package/distribution/code-request/type.js.map +1 -0
- package/distribution/code-request-prune/index.d.ts +13 -0
- package/distribution/code-request-prune/index.d.ts.map +1 -0
- package/distribution/code-request-prune/index.js +12 -0
- package/distribution/code-request-prune/index.js.map +1 -0
- package/distribution/code-verification/index.d.ts +14 -0
- package/distribution/code-verification/index.d.ts.map +1 -0
- package/distribution/code-verification/index.js +43 -0
- package/distribution/code-verification/index.js.map +1 -0
- package/distribution/code-verification/type.d.ts +22 -0
- package/distribution/code-verification/type.d.ts.map +1 -0
- package/distribution/code-verification/type.js +2 -0
- package/distribution/code-verification/type.js.map +1 -0
- package/distribution/contact-channel/type.d.ts +20 -0
- package/distribution/contact-channel/type.d.ts.map +1 -0
- package/distribution/contact-channel/type.js +2 -0
- package/distribution/contact-channel/type.js.map +1 -0
- package/distribution/email-channel/index.d.ts +13 -0
- package/distribution/email-channel/index.d.ts.map +1 -0
- package/distribution/email-channel/index.js +90 -0
- package/distribution/email-channel/index.js.map +1 -0
- package/distribution/email-channel/type.d.ts +17 -0
- package/distribution/email-channel/type.d.ts.map +1 -0
- package/distribution/email-channel/type.js +2 -0
- package/distribution/email-channel/type.js.map +1 -0
- package/distribution/session-prune/index.d.ts +17 -0
- package/distribution/session-prune/index.d.ts.map +1 -0
- package/distribution/session-prune/index.js +30 -0
- package/distribution/session-prune/index.js.map +1 -0
- package/distribution/session-token/index.d.ts +19 -0
- package/distribution/session-token/index.d.ts.map +1 -0
- package/distribution/session-token/index.js +22 -0
- package/distribution/session-token/index.js.map +1 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -134,6 +134,51 @@ Verified contacts only, normalized first.
|
|
|
134
134
|
|
|
135
135
|
Each is recorded in the audit log. Ask for a fresh code first (step-up) and tell the other contacts.
|
|
136
136
|
|
|
137
|
+
### `code-request`: `requestCode(database, { purpose, contact, accountId?, pepper, length = 8, lifetimeSeconds = 600, now? })` → `{ requestId, code, contact, expiresAt }`
|
|
138
|
+
|
|
139
|
+
Starts a one-time code for `sign-in`, `contact-verification` or `step-up`.
|
|
140
|
+
|
|
141
|
+
- Only an HMAC bound to the purpose and request is stored.
|
|
142
|
+
- A new request replaces the open one for the same contact and purpose.
|
|
143
|
+
- Send the code after answering the request, so timing reveals nothing. Rate limit and cap the sends first
|
|
144
|
+
(`limitRate`, `spendFromCap` from protection-web-library).
|
|
145
|
+
|
|
146
|
+
### `code-verification`: `verifyCode(database, { requestId, code, purpose, pepper, maximumAttempts = 5, now? })`
|
|
147
|
+
|
|
148
|
+
Returns `{ valid: true, contact, accountId }` (the account that asked, null for sign-in), or `{ valid: false, reason }`
|
|
149
|
+
with `unknown`, `used`, `replaced`, `expired`, `attempts` or `wrong`. The row is locked, so concurrent guesses can't
|
|
150
|
+
pass the limit; after the last attempt even the right code is refused.
|
|
151
|
+
|
|
152
|
+
### `code-request-prune`: `pruneCodeRequests(database, { before })` → deleted count
|
|
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
|
+
|
|
137
182
|
### `contact-kind`: `normalizeContact({ kind, value })`, `CONTACT_KINDS`
|
|
138
183
|
|
|
139
184
|
`email` in this version; unknown kinds and invalid values throw.
|
|
@@ -43,12 +43,52 @@ export interface AuditEventTable {
|
|
|
43
43
|
action: string;
|
|
44
44
|
detail: ColumnType<Record<string, unknown>, Record<string, unknown> | undefined, never>;
|
|
45
45
|
}
|
|
46
|
+
/** A one-time code sent to a contact, stored only as an HMAC bound to its purpose and request. */
|
|
47
|
+
export interface CodeRequestTable {
|
|
48
|
+
id: string;
|
|
49
|
+
/** `sign-in`, `contact-verification` or `step-up`. */
|
|
50
|
+
purpose: string;
|
|
51
|
+
/** The account asking (step-up, contact verification); null when signing in. */
|
|
52
|
+
account_id: string | null;
|
|
53
|
+
kind: string;
|
|
54
|
+
value: string;
|
|
55
|
+
code_hash: string;
|
|
56
|
+
created_at: Date;
|
|
57
|
+
expires_at: Date;
|
|
58
|
+
attempts: Generated<number>;
|
|
59
|
+
/** When the right code came back. */
|
|
60
|
+
used_at: Date | null;
|
|
61
|
+
/** When a newer request for the same contact and purpose replaced it. */
|
|
62
|
+
voided_at: Date | null;
|
|
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
|
+
}
|
|
46
83
|
/** The tables `migrateAccount` creates, for typing a Kysely instance. */
|
|
47
84
|
export interface AccountDatabase {
|
|
48
85
|
application: ApplicationTable;
|
|
49
86
|
account: AccountTable;
|
|
50
87
|
contact: ContactTable;
|
|
51
88
|
audit_event: AuditEventTable;
|
|
89
|
+
code_request: CodeRequestTable;
|
|
90
|
+
browser_session: BrowserSessionTable;
|
|
91
|
+
account_session: AccountSessionTable;
|
|
52
92
|
}
|
|
53
93
|
export {};
|
|
54
94
|
//# 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,MAAM,CAAC;IACb,UAAU,EAA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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"}
|
|
@@ -1 +1 @@
|
|
|
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,
|
|
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"}
|
|
@@ -89,6 +89,50 @@ export const accountMigrations = [
|
|
|
89
89
|
for each row execute function audit_event_unchangeable();
|
|
90
90
|
`,
|
|
91
91
|
},
|
|
92
|
+
{
|
|
93
|
+
name: "0002-code-request",
|
|
94
|
+
sql: `
|
|
95
|
+
create table code_request (
|
|
96
|
+
id uuid primary key check (uuid_extract_version(id) = 7),
|
|
97
|
+
purpose text not null check (purpose in ('sign-in', 'contact-verification', 'step-up')),
|
|
98
|
+
account_id uuid,
|
|
99
|
+
kind text not null,
|
|
100
|
+
value text not null,
|
|
101
|
+
code_hash text not null,
|
|
102
|
+
created_at timestamptz not null,
|
|
103
|
+
expires_at timestamptz not null,
|
|
104
|
+
attempts integer not null default 0 check (attempts >= 0),
|
|
105
|
+
used_at timestamptz,
|
|
106
|
+
voided_at timestamptz
|
|
107
|
+
);
|
|
108
|
+
create index code_request_open on code_request (kind, value, purpose) where used_at is null and voided_at is null;
|
|
109
|
+
create index code_request_expires_at on code_request (expires_at);
|
|
110
|
+
`,
|
|
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
|
+
},
|
|
92
136
|
];
|
|
93
137
|
/**
|
|
94
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,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AClF,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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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 @@
|
|
|
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;;;;;;GA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,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;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,QAAiC,EACjC,EACE,KAAK,EACL,WAAW,GAAG,CAAC,GAAG,MAAM,EACxB,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,GACkD;IAEpE,IAAI,CAAC,KAAK;QAAE,OAAO,SAAS,CAAC;IAC7B,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,MAAM,OAAO,GAAG,MAAM,QAAQ;SAC3B,UAAU,CAAC,iBAAiB,CAAC;SAC7B,MAAM,CAAC,IAAI,CAAC;SACZ,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,gBAAgB,CAAC,KAAK,CAAC,CAAC;SACjD,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,EAAE,CAAC;SAC5B,gBAAgB,EAAE,CAAC;IACtB,IAAI,CAAC,OAAO;QAAE,OAAO,SAAS,CAAC;IAC/B,MAAM,IAAI,GAAG,MAAM,QAAQ;SACxB,UAAU,CAAC,iBAAiB,CAAC;SAC7B,SAAS,CAAC,SAAS,EAAE,YAAY,EAAE,4BAA4B,CAAC;SAChE,MAAM,CAAC;QACN,wCAAwC;QACxC,yBAAyB;QACzB,8BAA8B;QAC9B,0CAA0C;QAC1C,oDAAoD;KACrD,CAAC;SACD,KAAK,CAAC,oCAAoC,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,CAAC;SAC5D,KAAK,CAAC,4BAA4B,EAAE,GAAG,EAAE,EAAE,CAAC;SAC5C,KAAK,CAAC,8BAA8B,EAAE,GAAG,EAAE,IAAI,IAAI,CAAC,GAAG,GAAG,WAAW,GAAG,IAAI,CAAC,CAAC;SAC9E,OAAO,CAAC,4BAA4B,CAAC;SACrC,OAAO,CAAC,oBAAoB,CAAC;SAC7B,OAAO,EAAE,CAAC;IACb,MAAM,QAAQ,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,EAAE,EAAE,CAAC,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 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/browser-session-read/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
import type { CodePurpose, CodeRequest, CodeRequestOptions } from "@sharadox/account-web-library/code-request/type";
|
|
4
|
+
/** The purposes a code can have. */
|
|
5
|
+
export declare const CODE_PURPOSES: readonly CodePurpose[];
|
|
6
|
+
/**
|
|
7
|
+
* Starts a one-time code for a contact: a new code (8 digits, 10 minutes by default), stored only as an HMAC bound
|
|
8
|
+
* to its purpose and request, replacing any open request for the same contact and purpose. Returns the code to send
|
|
9
|
+
* through the contact's channel. Rate limit and cap the sends before calling it (protection-web-library).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const { requestId, code } = await requestCode(database, { purpose: "sign-in", contact, pepper });
|
|
13
|
+
* await channel.send(contact, code); // after answering the request, so timing reveals nothing
|
|
14
|
+
*/
|
|
15
|
+
export declare function requestCode(database: Kysely<AccountDatabase>, { purpose, contact, accountId, pepper, length, lifetimeSeconds, now }: CodeRequestOptions): Promise<CodeRequest>;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/code-request/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAC3F,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,iDAAiD,CAAC;AAIpH,oCAAoC;AACpC,eAAO,MAAM,aAAa,EAAE,SAAS,WAAW,EAAmD,CAAC;AAEpG;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAC/B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAU,EAAE,eAAqB,EAAE,GAAgB,EAAE,EAAE,kBAAkB,GAC/G,OAAO,CAAC,WAAW,CAAC,CAmCtB"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
|
|
2
|
+
import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
|
|
3
|
+
import { createOneTimeCode, hashOneTimeCode } from "@sharadox/account-web-library/one-time-code";
|
|
4
|
+
/** The purposes a code can have. */
|
|
5
|
+
export const CODE_PURPOSES = ["sign-in", "contact-verification", "step-up"];
|
|
6
|
+
/**
|
|
7
|
+
* Starts a one-time code for a contact: a new code (8 digits, 10 minutes by default), stored only as an HMAC bound
|
|
8
|
+
* to its purpose and request, replacing any open request for the same contact and purpose. Returns the code to send
|
|
9
|
+
* through the contact's channel. Rate limit and cap the sends before calling it (protection-web-library).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const { requestId, code } = await requestCode(database, { purpose: "sign-in", contact, pepper });
|
|
13
|
+
* await channel.send(contact, code); // after answering the request, so timing reveals nothing
|
|
14
|
+
*/
|
|
15
|
+
export async function requestCode(database, { purpose, contact, accountId, pepper, length = 8, lifetimeSeconds = 600, now = Date.now() }) {
|
|
16
|
+
if (!CODE_PURPOSES.includes(purpose))
|
|
17
|
+
throw new Error(`unknown code purpose "${purpose}"`);
|
|
18
|
+
const { kind, value } = normalizeContact(contact);
|
|
19
|
+
const requestId = uuidVersion7({ now });
|
|
20
|
+
const code = createOneTimeCode(length);
|
|
21
|
+
const codeHash = hashOneTimeCode(code, { pepper, purpose: `${purpose}:${requestId}` });
|
|
22
|
+
const createdAt = new Date(now);
|
|
23
|
+
const expiresAt = new Date(now + lifetimeSeconds * 1000);
|
|
24
|
+
await database.transaction().execute(async (transaction) => {
|
|
25
|
+
await transaction
|
|
26
|
+
.updateTable("code_request")
|
|
27
|
+
.set({ voided_at: createdAt })
|
|
28
|
+
.where("kind", "=", kind)
|
|
29
|
+
.where("value", "=", value)
|
|
30
|
+
.where("purpose", "=", purpose)
|
|
31
|
+
.where("used_at", "is", null)
|
|
32
|
+
.where("voided_at", "is", null)
|
|
33
|
+
.execute();
|
|
34
|
+
await transaction
|
|
35
|
+
.insertInto("code_request")
|
|
36
|
+
.values({
|
|
37
|
+
id: requestId,
|
|
38
|
+
purpose,
|
|
39
|
+
account_id: accountId ?? null,
|
|
40
|
+
kind,
|
|
41
|
+
value,
|
|
42
|
+
code_hash: codeHash,
|
|
43
|
+
created_at: createdAt,
|
|
44
|
+
expires_at: expiresAt,
|
|
45
|
+
used_at: null,
|
|
46
|
+
voided_at: null,
|
|
47
|
+
})
|
|
48
|
+
.execute();
|
|
49
|
+
});
|
|
50
|
+
return { requestId, code, contact: { kind, value }, expiresAt };
|
|
51
|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D,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;QACzD,MAAM,WAAW;aACd,WAAW,CAAC,cAAc,CAAC;aAC3B,GAAG,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;aAC7B,KAAK,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC;aACxB,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC;aAC1B,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,OAAO,CAAC;aAC9B,KAAK,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC;aAC5B,KAAK,CAAC,WAAW,EAAE,IAAI,EAAE,IAAI,CAAC;aAC9B,OAAO,EAAE,CAAC;QACb,MAAM,WAAW;aACd,UAAU,CAAC,cAAc,CAAC;aAC1B,MAAM,CAAC;YACN,EAAE,EAAE,SAAS;YACb,OAAO;YACP,UAAU,EAAE,SAAS,IAAI,IAAI;YAC7B,IAAI;YACJ,KAAK;YACL,SAAS,EAAE,QAAQ;YACnB,UAAU,EAAE,SAAS;YACrB,UAAU,EAAE,SAAS;YACrB,OAAO,EAAE,IAAI;YACb,SAAS,EAAE,IAAI;SAChB,CAAC;aACD,OAAO,EAAE,CAAC;IACf,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
|
|
2
|
+
/** What a one-time code is for; a code works only for its own purpose and request. */
|
|
3
|
+
export type CodePurpose = "sign-in" | "contact-verification" | "step-up";
|
|
4
|
+
export interface CodeRequestOptions {
|
|
5
|
+
purpose: CodePurpose;
|
|
6
|
+
contact: Contact;
|
|
7
|
+
/** The account asking (step-up, contact verification). */
|
|
8
|
+
accountId?: string;
|
|
9
|
+
/** At least 32 characters, kept outside the database. */
|
|
10
|
+
pepper: string;
|
|
11
|
+
/** Digits (8 by default). */
|
|
12
|
+
length?: number;
|
|
13
|
+
/** 600 (10 minutes) by default. */
|
|
14
|
+
lifetimeSeconds?: number;
|
|
15
|
+
/** Milliseconds since 1970 (now by default). */
|
|
16
|
+
now?: number;
|
|
17
|
+
}
|
|
18
|
+
export interface CodeRequest {
|
|
19
|
+
requestId: string;
|
|
20
|
+
/** Send it, then forget it: only its hash is stored. */
|
|
21
|
+
code: string;
|
|
22
|
+
contact: Contact;
|
|
23
|
+
expiresAt: Date;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/code-request/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,sFAAsF;AACtF,MAAM,MAAM,WAAW,GAAG,SAAS,GAAG,sBAAsB,GAAG,SAAS,CAAC;AAEzE,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,WAAW,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,0DAA0D;IAC1D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,yDAAyD;IACzD,MAAM,EAAE,MAAM,CAAC;IACf,6BAA6B;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mCAAmC;IACnC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,WAAW;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,IAAI,CAAC;CACjB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/code-request/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
/**
|
|
4
|
+
* Deletes code requests that expired before `before`, with the contacts they hold, from a daily job. Returns how
|
|
5
|
+
* many were deleted.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await pruneCodeRequests(database, { before: new Date(Date.now() - 86_400_000) });
|
|
9
|
+
*/
|
|
10
|
+
export declare function pruneCodeRequests(database: Kysely<AccountDatabase>, { before }: {
|
|
11
|
+
before: Date;
|
|
12
|
+
}): Promise<number>;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/code-request-prune/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAE3F;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,MAAM,EAAE,EAAE;IAAE,MAAM,EAAE,IAAI,CAAA;CAAE,GAC3B,OAAO,CAAC,MAAM,CAAC,CAGjB"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deletes code requests that expired before `before`, with the contacts they hold, from a daily job. Returns how
|
|
3
|
+
* many were deleted.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* await pruneCodeRequests(database, { before: new Date(Date.now() - 86_400_000) });
|
|
7
|
+
*/
|
|
8
|
+
export async function pruneCodeRequests(database, { before }) {
|
|
9
|
+
const result = await database.deleteFrom("code_request").where("expires_at", "<", before).executeTakeFirst();
|
|
10
|
+
return Number(result.numDeletedRows);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/code-request-prune/index.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,QAAiC,EACjC,EAAE,MAAM,EAAoB;IAE5B,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,UAAU,CAAC,cAAc,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC,gBAAgB,EAAE,CAAC;IAC7G,OAAO,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
import type { CodeVerificationOptions, CodeVerificationResult } from "@sharadox/account-web-library/code-verification/type";
|
|
4
|
+
/**
|
|
5
|
+
* Checks a typed one-time code against its request, with the row locked so concurrent guesses can't pass the
|
|
6
|
+
* attempt limit. The right code works once, before it expires, unless a newer request replaced it or 5 wrong codes
|
|
7
|
+
* came first. Returns the proved contact and the account that asked (null for sign-in).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const result = await verifyCode(database, { requestId, code: typed, purpose: "sign-in", pepper });
|
|
11
|
+
* if (result.valid) signIn(await findAccountByContact(database, result.contact));
|
|
12
|
+
*/
|
|
13
|
+
export declare function verifyCode(database: Kysely<AccountDatabase>, { requestId, code, purpose, pepper, maximumAttempts, now }: CodeVerificationOptions): Promise<CodeVerificationResult>;
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/code-verification/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAC3F,OAAO,KAAK,EACV,uBAAuB,EACvB,sBAAsB,EACvB,MAAM,sDAAsD,CAAC;AAK9D;;;;;;;;GAQG;AACH,wBAAsB,UAAU,CAC9B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,eAAmB,EAAE,GAAgB,EAAE,EAAE,uBAAuB,GACnG,OAAO,CAAC,sBAAsB,CAAC,CAwBjC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { matchesOneTimeCode } from "@sharadox/account-web-library/one-time-code";
|
|
2
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
3
|
+
/**
|
|
4
|
+
* Checks a typed one-time code against its request, with the row locked so concurrent guesses can't pass the
|
|
5
|
+
* attempt limit. The right code works once, before it expires, unless a newer request replaced it or 5 wrong codes
|
|
6
|
+
* came first. Returns the proved contact and the account that asked (null for sign-in).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const result = await verifyCode(database, { requestId, code: typed, purpose: "sign-in", pepper });
|
|
10
|
+
* if (result.valid) signIn(await findAccountByContact(database, result.contact));
|
|
11
|
+
*/
|
|
12
|
+
export async function verifyCode(database, { requestId, code, purpose, pepper, maximumAttempts = 5, now = Date.now() }) {
|
|
13
|
+
if (!UUID.test(requestId))
|
|
14
|
+
return { valid: false, reason: "unknown" };
|
|
15
|
+
return database.transaction().execute(async (transaction) => {
|
|
16
|
+
const request = await transaction
|
|
17
|
+
.selectFrom("code_request")
|
|
18
|
+
.selectAll()
|
|
19
|
+
.where("id", "=", requestId)
|
|
20
|
+
.where("purpose", "=", purpose)
|
|
21
|
+
.forUpdate()
|
|
22
|
+
.executeTakeFirst();
|
|
23
|
+
if (!request)
|
|
24
|
+
return { valid: false, reason: "unknown" };
|
|
25
|
+
if (request.used_at)
|
|
26
|
+
return { valid: false, reason: "used" };
|
|
27
|
+
if (request.voided_at)
|
|
28
|
+
return { valid: false, reason: "replaced" };
|
|
29
|
+
if (request.expires_at.getTime() <= now)
|
|
30
|
+
return { valid: false, reason: "expired" };
|
|
31
|
+
if (request.attempts >= maximumAttempts)
|
|
32
|
+
return { valid: false, reason: "attempts" };
|
|
33
|
+
const at = transaction.updateTable("code_request").where("id", "=", requestId);
|
|
34
|
+
if (matchesOneTimeCode(code, request.code_hash, { pepper, purpose: `${purpose}:${requestId}` })) {
|
|
35
|
+
await at.set({ used_at: new Date(now) }).execute();
|
|
36
|
+
return { valid: true, contact: { kind: request.kind, value: request.value }, accountId: request.account_id };
|
|
37
|
+
}
|
|
38
|
+
const attempts = request.attempts + 1;
|
|
39
|
+
await at.set({ attempts }).execute();
|
|
40
|
+
return { valid: false, reason: attempts >= maximumAttempts ? "attempts" : "wrong" };
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/code-verification/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,kBAAkB,EAAE,MAAM,6CAA6C,CAAC;AAEjF,MAAM,IAAI,GAAG,iEAAiE,CAAC;AAE/E;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,QAAiC,EACjC,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,eAAe,GAAG,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAA2B;IAEpG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IACt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CAAC,CAAC,OAAO,EAAE,CAAC;YACnD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,EAAE,SAAS,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC;QAC/G,CAAC;QACD,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;QACtC,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;QACrC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,IAAI,eAAe,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;IACtF,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CodePurpose } from "@sharadox/account-web-library/code-request/type";
|
|
2
|
+
import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
|
|
3
|
+
export interface CodeVerificationOptions {
|
|
4
|
+
requestId: string;
|
|
5
|
+
/** As typed: spaces are ignored. */
|
|
6
|
+
code: string;
|
|
7
|
+
purpose: CodePurpose;
|
|
8
|
+
pepper: string;
|
|
9
|
+
/** Wrong codes allowed before the request is dead (5 by default). */
|
|
10
|
+
maximumAttempts?: number;
|
|
11
|
+
/** Milliseconds since 1970 (now by default). */
|
|
12
|
+
now?: number;
|
|
13
|
+
}
|
|
14
|
+
export type CodeVerificationResult = {
|
|
15
|
+
valid: true;
|
|
16
|
+
contact: Contact;
|
|
17
|
+
accountId: string | null;
|
|
18
|
+
} | {
|
|
19
|
+
valid: false;
|
|
20
|
+
reason: "unknown" | "used" | "replaced" | "expired" | "attempts" | "wrong";
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/code-verification/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iDAAiD,CAAC;AACnF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,MAAM,WAAW,uBAAuB;IACtC,SAAS,EAAE,MAAM,CAAC;IAClB,oCAAoC;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,WAAW,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,qEAAqE;IACrE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,sBAAsB,GAC9B;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,OAAO,EAAE,OAAO,CAAC;IAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GAC3D;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,SAAS,GAAG,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,UAAU,GAAG,OAAO,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/code-verification/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 @@
|
|
|
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,EA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ress } 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 @@
|
|
|
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
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/session-prune/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAe,MAAM,QAAQ,CAAC;AAG1C;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAiC,EACjC,EAAE,WAAW,GAAG,CAAC,GAAG,MAAM,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,KAA6C,EAAE;IAE3F,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,MAAM,eAAe,GAAG,MAAM,QAAQ;SACnC,UAAU,CAAC,iBAAiB,CAAC;SAC7B,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CACf,KAAK,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,KAAK,CAAC,cAAc,EAAE,IAAI,EAAE,IAAI,IAAI,CAAC,GAAG,GAAG,WAAW,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAC3G;SACA,gBAAgB,EAAE,CAAC;IACtB,MAAM,eAAe,GAAG,MAAM,QAAQ;SACnC,UAAU,CAAC,iBAAiB,CAAC;SAC7B,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CACf,KAAK,CAAC,EAAE,CAAC;QACP,KAAK,CAAC,YAAY,EAAE,IAAI,EAAE,EAAE,CAAC;QAC7B,KAAK,CAAC,GAAG,CACP,KAAK,CAAC,MAAM,CACV,KAAK;aACF,UAAU,CAAC,iBAAiB,CAAC;aAC7B,MAAM,CAAC,GAAG,CAAA,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;aACxB,QAAQ,CAAC,oCAAoC,EAAE,GAAG,EAAE,oBAAoB,CAAC,CAC7E,CACF;KACF,CAAC,CACH;SACA,gBAAgB,EAAE,CAAC;IACtB,OAAO;QACL,eAAe,EAAE,MAAM,CAAC,eAAe,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-
|
|
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
|
},
|