@sharadox/account-web-library 0.1.0-development.0 → 0.1.0-development.15
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 +103 -3
- package/distribution/account-database/type.d.ts +94 -0
- package/distribution/account-database/type.d.ts.map +1 -0
- package/distribution/account-database/type.js +2 -0
- package/distribution/account-database/type.js.map +1 -0
- package/distribution/account-migration/index.d.ts +18 -0
- package/distribution/account-migration/index.d.ts.map +1 -0
- package/distribution/account-migration/index.js +146 -0
- package/distribution/account-migration/index.js.map +1 -0
- 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/audit-event/index.d.ts +12 -0
- package/distribution/audit-event/index.d.ts.map +1 -0
- package/distribution/audit-event/index.js +25 -0
- package/distribution/audit-event/index.js.map +1 -0
- package/distribution/audit-event/type.d.ts +15 -0
- package/distribution/audit-event/type.d.ts.map +1 -0
- package/distribution/audit-event/type.js +2 -0
- package/distribution/audit-event/type.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-addition/index.d.ts +13 -0
- package/distribution/contact-addition/index.d.ts.map +1 -0
- package/distribution/contact-addition/index.js +48 -0
- package/distribution/contact-addition/index.js.map +1 -0
- package/distribution/contact-addition/type.d.ts +15 -0
- package/distribution/contact-addition/type.d.ts.map +1 -0
- package/distribution/contact-addition/type.js +2 -0
- package/distribution/contact-addition/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/contact-kind/index.d.ts +12 -0
- package/distribution/contact-kind/index.d.ts.map +1 -0
- package/distribution/contact-kind/index.js +18 -0
- package/distribution/contact-kind/index.js.map +1 -0
- package/distribution/contact-kind/type.d.ts +6 -0
- package/distribution/contact-kind/type.d.ts.map +1 -0
- package/distribution/contact-kind/type.js +2 -0
- package/distribution/contact-kind/type.js.map +1 -0
- package/distribution/contact-lookup/index.d.ts +16 -0
- package/distribution/contact-lookup/index.d.ts.map +1 -0
- package/distribution/contact-lookup/index.js +20 -0
- package/distribution/contact-lookup/index.js.map +1 -0
- package/distribution/contact-removal/index.d.ts +19 -0
- package/distribution/contact-removal/index.d.ts.map +1 -0
- package/distribution/contact-removal/index.js +35 -0
- package/distribution/contact-removal/index.js.map +1 -0
- package/distribution/contact-verification/index.d.ts +20 -0
- package/distribution/contact-verification/index.d.ts.map +1 -0
- package/distribution/contact-verification/index.js +37 -0
- package/distribution/contact-verification/index.js.map +1 -0
- package/distribution/database-error/index.d.ts +12 -0
- package/distribution/database-error/index.d.ts.map +1 -0
- package/distribution/database-error/index.js +14 -0
- package/distribution/database-error/index.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/person-account/index.d.ts +14 -0
- package/distribution/person-account/index.d.ts.map +1 -0
- package/distribution/person-account/index.js +45 -0
- package/distribution/person-account/index.js.map +1 -0
- package/distribution/person-account/type.d.ts +17 -0
- package/distribution/person-account/type.d.ts.map +1 -0
- package/distribution/person-account/type.js +2 -0
- package/distribution/person-account/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/distribution/test-database/index.d.ts +13 -0
- package/distribution/test-database/index.d.ts.map +1 -0
- package/distribution/test-database/index.js +17 -0
- package/distribution/test-database/index.js.map +1 -0
- package/distribution/username-availability/index.d.ts +11 -0
- package/distribution/username-availability/index.d.ts.map +1 -0
- package/distribution/username-availability/index.js +21 -0
- package/distribution/username-availability/index.js.map +1 -0
- package/distribution/username-change/index.d.ts +15 -0
- package/distribution/username-change/index.d.ts.map +1 -0
- package/distribution/username-change/index.js +39 -0
- package/distribution/username-change/index.js.map +1 -0
- package/distribution/username-change/type.d.ts +7 -0
- package/distribution/username-change/type.d.ts.map +1 -0
- package/distribution/username-change/type.js +2 -0
- package/distribution/username-change/type.js.map +1 -0
- package/package.json +19 -17
package/README.md
CHANGED
|
@@ -9,9 +9,9 @@ Passwordless accounts for web applications, built on open standards, with no aut
|
|
|
9
9
|
|
|
10
10
|
One deployment per company. Everything company-specific is configuration.
|
|
11
11
|
|
|
12
|
-
**Status: 0.1.0,
|
|
13
|
-
permissions
|
|
14
|
-
`CHANGELOG.md`).
|
|
12
|
+
**Status: 0.1.0, in progress.** Building blocks (usernames, one-time codes, TOTP, recovery codes, email contacts,
|
|
13
|
+
effective permissions, ban lengths) and account storage in Postgres (accounts, usernames, contacts, audit log).
|
|
14
|
+
Sign-in codes, sessions and the OpenID Connect server come next (see `CHANGELOG.md`).
|
|
15
15
|
|
|
16
16
|
## Install
|
|
17
17
|
|
|
@@ -19,6 +19,22 @@ permissions and ban lengths. Accounts, contacts, sessions, storage and the OpenI
|
|
|
19
19
|
pnpm add @sharadox/account-web-library
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
+
## Storage
|
|
23
|
+
|
|
24
|
+
Postgres through Kysely (`@sharadox/database-web-library`). Run `migrateAccount(database)` when the application
|
|
25
|
+
migrates: the tables are tracked in their own table (`account_migration`). Type Kysely with `AccountDatabase`
|
|
26
|
+
(`account-database/type`). The database enforces the rules itself, as the last line behind the code:
|
|
27
|
+
|
|
28
|
+
- ids are UUIDv7;
|
|
29
|
+
- usernames follow the rules and are unique without regard to case;
|
|
30
|
+
- a verified contact belongs to one account only;
|
|
31
|
+
- every person keeps at least one verified sign-in contact, checked when a change commits;
|
|
32
|
+
- agents have an owner (and go with them);
|
|
33
|
+
- the audit log is never changed.
|
|
34
|
+
|
|
35
|
+
Tests: `createTestAccountDatabase()` (`test-database`) gives a migrated Postgres in memory (PGlite, a development
|
|
36
|
+
dependency).
|
|
37
|
+
|
|
22
38
|
## API
|
|
23
39
|
|
|
24
40
|
Each export has its own entry point (`@sharadox/account-web-library/<name>`).
|
|
@@ -89,6 +105,90 @@ Codes like `7KQM-W3HD-9TXA` (about 59 bits each), from characters that can't be
|
|
|
89
105
|
- `normalizeEmail` trims and lowercases, and throws for text that isn't an email address.
|
|
90
106
|
- `emailLedgerForm` also drops `+tags`, for the banned-contact ledger.
|
|
91
107
|
|
|
108
|
+
### `person-account`: `createPersonAccount(database, { username, contact, now? })`
|
|
109
|
+
|
|
110
|
+
Creates a person with the contact their one-time code just proved, stored verified, and records `account.created`.
|
|
111
|
+
Returns `{ created: true, accountId, contactId }`, or `{ created: false, reason }` with `username-invalid`,
|
|
112
|
+
`username-taken` or `contact-taken`.
|
|
113
|
+
|
|
114
|
+
### `username-availability`: `usernameAvailability(database, username)` → `available` | `taken` | `invalid`
|
|
115
|
+
|
|
116
|
+
Rate limit it: it tells whether an account exists.
|
|
117
|
+
|
|
118
|
+
### `username-change`: `changeUsername(database, { accountId, username })`
|
|
119
|
+
|
|
120
|
+
Any time, and the old name is free at once; the audit log keeps `{ from, to }`. Reasons: `username-invalid`,
|
|
121
|
+
`username-taken`, `no-account`.
|
|
122
|
+
|
|
123
|
+
### `contact-lookup`: `findAccountByContact(database, contact)` → `{ accountId, contactId, signIn }` or undefined
|
|
124
|
+
|
|
125
|
+
Verified contacts only, normalized first.
|
|
126
|
+
|
|
127
|
+
### `contact-addition`, `contact-verification`, `contact-removal`
|
|
128
|
+
|
|
129
|
+
- `addContact(database, { accountId, contact, now? })` adds it unverified. Reasons: `already-added`,
|
|
130
|
+
`contact-taken`, `no-account`.
|
|
131
|
+
- `verifyContact(database, { accountId, contactId, now? })` once its code came back. Reasons: `no-contact`,
|
|
132
|
+
`contact-taken` (another account verified it first).
|
|
133
|
+
- `removeContact(database, { accountId, contactId })`. Reasons: `no-contact`, `last-sign-in-contact`.
|
|
134
|
+
|
|
135
|
+
Each is recorded in the audit log. Ask for a fresh code first (step-up) and tell the other contacts.
|
|
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
|
+
|
|
182
|
+
### `contact-kind`: `normalizeContact({ kind, value })`, `CONTACT_KINDS`
|
|
183
|
+
|
|
184
|
+
`email` in this version; unknown kinds and invalid values throw.
|
|
185
|
+
|
|
186
|
+
### `audit-event`: `recordAuditEvent(database, { action, accountId?, actorAccountId?, application?, detail?, now? })` → id
|
|
187
|
+
|
|
188
|
+
Pass the transaction of the change it describes. `detail` holds ids and kinds, never contact values or secrets.
|
|
189
|
+
|
|
190
|
+
### `database-error`: `databaseError(error)` → `{ code, constraint }` or undefined
|
|
191
|
+
|
|
92
192
|
## License
|
|
93
193
|
|
|
94
194
|
Sharadox Open Source License 0.1.0 (see `LICENSE.md`).
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { ColumnType, Generated } from "kysely";
|
|
2
|
+
/** A timestamp the database fills in when not given. */
|
|
3
|
+
type DefaultTime = ColumnType<Date, Date | undefined, Date>;
|
|
4
|
+
/** An application the accounts sign in to (`account` itself, `platform`, `shop`…). */
|
|
5
|
+
export interface ApplicationTable {
|
|
6
|
+
id: string;
|
|
7
|
+
name: string;
|
|
8
|
+
created_at: DefaultTime;
|
|
9
|
+
}
|
|
10
|
+
/** A person, or an agent owned by a person. */
|
|
11
|
+
export interface AccountTable {
|
|
12
|
+
id: string;
|
|
13
|
+
kind: "person" | "agent";
|
|
14
|
+
/** Shown as written; unique without regard to case. */
|
|
15
|
+
username: string;
|
|
16
|
+
/** The person who owns an agent; null for people. */
|
|
17
|
+
owner_id: string | null;
|
|
18
|
+
created_at: DefaultTime;
|
|
19
|
+
last_active_at: DefaultTime;
|
|
20
|
+
}
|
|
21
|
+
/** A way to reach an account (email now; phone and messaging later). */
|
|
22
|
+
export interface ContactTable {
|
|
23
|
+
id: string;
|
|
24
|
+
account_id: string;
|
|
25
|
+
kind: string;
|
|
26
|
+
/** Normalized (see `normalizeContact`). */
|
|
27
|
+
value: string;
|
|
28
|
+
/** When a one-time code proved it; null until then. A verified contact belongs to one account only. */
|
|
29
|
+
verified_at: Date | null;
|
|
30
|
+
/** Whether codes for signing in may be sent to it. */
|
|
31
|
+
sign_in: Generated<boolean>;
|
|
32
|
+
created_at: DefaultTime;
|
|
33
|
+
}
|
|
34
|
+
/** One line of the append-only audit log. */
|
|
35
|
+
export interface AuditEventTable {
|
|
36
|
+
id: string;
|
|
37
|
+
at: DefaultTime;
|
|
38
|
+
/** The account it is about (kept after the account is deleted, as a bare id). */
|
|
39
|
+
account_id: string | null;
|
|
40
|
+
/** Who did it, when not the account itself (an administrator, an agent). */
|
|
41
|
+
actor_account_id: string | null;
|
|
42
|
+
application: string | null;
|
|
43
|
+
action: string;
|
|
44
|
+
detail: ColumnType<Record<string, unknown>, Record<string, unknown> | undefined, never>;
|
|
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
|
+
}
|
|
83
|
+
/** The tables `migrateAccount` creates, for typing a Kysely instance. */
|
|
84
|
+
export interface AccountDatabase {
|
|
85
|
+
application: ApplicationTable;
|
|
86
|
+
account: AccountTable;
|
|
87
|
+
contact: ContactTable;
|
|
88
|
+
audit_event: AuditEventTable;
|
|
89
|
+
code_request: CodeRequestTable;
|
|
90
|
+
browser_session: BrowserSessionTable;
|
|
91
|
+
account_session: AccountSessionTable;
|
|
92
|
+
}
|
|
93
|
+
export {};
|
|
94
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-database/type.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/account-database/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { Migration } from "@sharadox/database-web-library/migration/type";
|
|
3
|
+
/** The table that records which account migrations a database has applied (apart from the application's own). */
|
|
4
|
+
export declare const ACCOUNT_MIGRATION_TABLE = "account_migration";
|
|
5
|
+
/**
|
|
6
|
+
* The account tables, forward only. The database enforces the rules itself, as the last line behind the code:
|
|
7
|
+
* UUIDv7 ids, username rules and case-insensitive uniqueness, one account per verified contact, agents owned by a
|
|
8
|
+
* person, every person keeping a verified sign-in contact (checked at commit), and an audit log that is never changed.
|
|
9
|
+
*/
|
|
10
|
+
export declare const accountMigrations: Migration[];
|
|
11
|
+
/**
|
|
12
|
+
* Creates or updates the account tables, tracked in their own migration table. Returns the names applied.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* await migrateAccount(database);
|
|
16
|
+
*/
|
|
17
|
+
export declare function migrateAccount<Schema>(database: Kysely<Schema>): Promise<string[]>;
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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,EA+HxC,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAElF"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { migrate } from "@sharadox/database-web-library/migration";
|
|
2
|
+
/** The table that records which account migrations a database has applied (apart from the application's own). */
|
|
3
|
+
export const ACCOUNT_MIGRATION_TABLE = "account_migration";
|
|
4
|
+
/**
|
|
5
|
+
* The account tables, forward only. The database enforces the rules itself, as the last line behind the code:
|
|
6
|
+
* UUIDv7 ids, username rules and case-insensitive uniqueness, one account per verified contact, agents owned by a
|
|
7
|
+
* person, every person keeping a verified sign-in contact (checked at commit), and an audit log that is never changed.
|
|
8
|
+
*/
|
|
9
|
+
export const accountMigrations = [
|
|
10
|
+
{
|
|
11
|
+
name: "0001-application-account-contact-audit",
|
|
12
|
+
sql: `
|
|
13
|
+
create table application (
|
|
14
|
+
id text primary key check (id ~ '^[a-z][a-z0-9]*(-[a-z0-9]+)*$' and length(id) <= 50),
|
|
15
|
+
name text not null,
|
|
16
|
+
created_at timestamptz not null default now()
|
|
17
|
+
);
|
|
18
|
+
insert into application (id, name) values ('account', 'Account');
|
|
19
|
+
|
|
20
|
+
create table account (
|
|
21
|
+
id uuid primary key check (uuid_extract_version(id) = 7),
|
|
22
|
+
kind text not null check (kind in ('person', 'agent')),
|
|
23
|
+
username text not null
|
|
24
|
+
check (length(username) between 1 and 50 and username ~ '^[A-Za-z0-9]+([-_.][A-Za-z0-9]+)*$'),
|
|
25
|
+
owner_id uuid references account (id) on delete cascade,
|
|
26
|
+
created_at timestamptz not null default now(),
|
|
27
|
+
last_active_at timestamptz not null default now(),
|
|
28
|
+
constraint account_owner_check check ((kind = 'agent') = (owner_id is not null))
|
|
29
|
+
);
|
|
30
|
+
create unique index account_username_key on account (lower(username));
|
|
31
|
+
create index account_owner on account (owner_id) where owner_id is not null;
|
|
32
|
+
create index account_last_active_at on account (last_active_at);
|
|
33
|
+
|
|
34
|
+
create table contact (
|
|
35
|
+
id uuid primary key check (uuid_extract_version(id) = 7),
|
|
36
|
+
account_id uuid not null references account (id) on delete cascade,
|
|
37
|
+
kind text not null check (kind ~ '^[a-z][a-z0-9]*(-[a-z0-9]+)*$'),
|
|
38
|
+
value text not null check (length(value) between 1 and 320),
|
|
39
|
+
verified_at timestamptz,
|
|
40
|
+
sign_in boolean not null default true,
|
|
41
|
+
created_at timestamptz not null default now()
|
|
42
|
+
);
|
|
43
|
+
create unique index contact_verified on contact (kind, value) where verified_at is not null;
|
|
44
|
+
create unique index contact_per_account on contact (account_id, kind, value);
|
|
45
|
+
|
|
46
|
+
create function account_require_sign_in_contact(target uuid) returns void
|
|
47
|
+
language plpgsql set search_path = public as $$
|
|
48
|
+
begin
|
|
49
|
+
if exists (select 1 from account where id = target and kind = 'person')
|
|
50
|
+
and not exists (
|
|
51
|
+
select 1 from contact where account_id = target and verified_at is not null and sign_in
|
|
52
|
+
) then
|
|
53
|
+
raise exception 'a person account needs at least one verified sign-in contact' using errcode = '23514';
|
|
54
|
+
end if;
|
|
55
|
+
end $$;
|
|
56
|
+
create function account_after_insert() returns trigger
|
|
57
|
+
language plpgsql set search_path = public as $$
|
|
58
|
+
begin
|
|
59
|
+
perform account_require_sign_in_contact(new.id);
|
|
60
|
+
return null;
|
|
61
|
+
end $$;
|
|
62
|
+
create function contact_after_change() returns trigger
|
|
63
|
+
language plpgsql set search_path = public as $$
|
|
64
|
+
begin
|
|
65
|
+
perform account_require_sign_in_contact(old.account_id);
|
|
66
|
+
return null;
|
|
67
|
+
end $$;
|
|
68
|
+
create constraint trigger account_sign_in_contact after insert on account
|
|
69
|
+
deferrable initially deferred for each row execute function account_after_insert();
|
|
70
|
+
create constraint trigger contact_sign_in_contact after update or delete on contact
|
|
71
|
+
deferrable initially deferred for each row execute function contact_after_change();
|
|
72
|
+
|
|
73
|
+
create table audit_event (
|
|
74
|
+
id uuid primary key check (uuid_extract_version(id) = 7),
|
|
75
|
+
at timestamptz not null default now(),
|
|
76
|
+
account_id uuid,
|
|
77
|
+
actor_account_id uuid,
|
|
78
|
+
application text,
|
|
79
|
+
action text not null check (length(action) between 1 and 100),
|
|
80
|
+
detail jsonb not null default '{}'
|
|
81
|
+
);
|
|
82
|
+
create index audit_event_account on audit_event (account_id, at);
|
|
83
|
+
create function audit_event_unchangeable() returns trigger
|
|
84
|
+
language plpgsql as $$
|
|
85
|
+
begin
|
|
86
|
+
raise exception 'audit events are never changed';
|
|
87
|
+
end $$;
|
|
88
|
+
create trigger audit_event_unchangeable before update on audit_event
|
|
89
|
+
for each row execute function audit_event_unchangeable();
|
|
90
|
+
`,
|
|
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
|
+
},
|
|
136
|
+
];
|
|
137
|
+
/**
|
|
138
|
+
* Creates or updates the account tables, tracked in their own migration table. Returns the names applied.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* await migrateAccount(database);
|
|
142
|
+
*/
|
|
143
|
+
export function migrateAccount(database) {
|
|
144
|
+
return migrate(database, accountMigrations, { table: ACCOUNT_MIGRATION_TABLE });
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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;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":"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"}
|
|
@@ -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"}
|