@sharadox/account-web-library 0.1.0-development.0 → 0.1.0-development.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +58 -3
  2. package/distribution/account-database/type.d.ts +54 -0
  3. package/distribution/account-database/type.d.ts.map +1 -0
  4. package/distribution/account-database/type.js +2 -0
  5. package/distribution/account-database/type.js.map +1 -0
  6. package/distribution/account-migration/index.d.ts +18 -0
  7. package/distribution/account-migration/index.d.ts.map +1 -0
  8. package/distribution/account-migration/index.js +102 -0
  9. package/distribution/account-migration/index.js.map +1 -0
  10. package/distribution/audit-event/index.d.ts +12 -0
  11. package/distribution/audit-event/index.d.ts.map +1 -0
  12. package/distribution/audit-event/index.js +25 -0
  13. package/distribution/audit-event/index.js.map +1 -0
  14. package/distribution/audit-event/type.d.ts +15 -0
  15. package/distribution/audit-event/type.d.ts.map +1 -0
  16. package/distribution/audit-event/type.js +2 -0
  17. package/distribution/audit-event/type.js.map +1 -0
  18. package/distribution/contact-addition/index.d.ts +13 -0
  19. package/distribution/contact-addition/index.d.ts.map +1 -0
  20. package/distribution/contact-addition/index.js +48 -0
  21. package/distribution/contact-addition/index.js.map +1 -0
  22. package/distribution/contact-addition/type.d.ts +15 -0
  23. package/distribution/contact-addition/type.d.ts.map +1 -0
  24. package/distribution/contact-addition/type.js +2 -0
  25. package/distribution/contact-addition/type.js.map +1 -0
  26. package/distribution/contact-kind/index.d.ts +12 -0
  27. package/distribution/contact-kind/index.d.ts.map +1 -0
  28. package/distribution/contact-kind/index.js +18 -0
  29. package/distribution/contact-kind/index.js.map +1 -0
  30. package/distribution/contact-kind/type.d.ts +6 -0
  31. package/distribution/contact-kind/type.d.ts.map +1 -0
  32. package/distribution/contact-kind/type.js +2 -0
  33. package/distribution/contact-kind/type.js.map +1 -0
  34. package/distribution/contact-lookup/index.d.ts +16 -0
  35. package/distribution/contact-lookup/index.d.ts.map +1 -0
  36. package/distribution/contact-lookup/index.js +20 -0
  37. package/distribution/contact-lookup/index.js.map +1 -0
  38. package/distribution/contact-removal/index.d.ts +19 -0
  39. package/distribution/contact-removal/index.d.ts.map +1 -0
  40. package/distribution/contact-removal/index.js +35 -0
  41. package/distribution/contact-removal/index.js.map +1 -0
  42. package/distribution/contact-verification/index.d.ts +20 -0
  43. package/distribution/contact-verification/index.d.ts.map +1 -0
  44. package/distribution/contact-verification/index.js +37 -0
  45. package/distribution/contact-verification/index.js.map +1 -0
  46. package/distribution/database-error/index.d.ts +12 -0
  47. package/distribution/database-error/index.d.ts.map +1 -0
  48. package/distribution/database-error/index.js +14 -0
  49. package/distribution/database-error/index.js.map +1 -0
  50. package/distribution/person-account/index.d.ts +14 -0
  51. package/distribution/person-account/index.d.ts.map +1 -0
  52. package/distribution/person-account/index.js +45 -0
  53. package/distribution/person-account/index.js.map +1 -0
  54. package/distribution/person-account/type.d.ts +17 -0
  55. package/distribution/person-account/type.d.ts.map +1 -0
  56. package/distribution/person-account/type.js +2 -0
  57. package/distribution/person-account/type.js.map +1 -0
  58. package/distribution/test-database/index.d.ts +13 -0
  59. package/distribution/test-database/index.d.ts.map +1 -0
  60. package/distribution/test-database/index.js +17 -0
  61. package/distribution/test-database/index.js.map +1 -0
  62. package/distribution/username-availability/index.d.ts +11 -0
  63. package/distribution/username-availability/index.d.ts.map +1 -0
  64. package/distribution/username-availability/index.js +21 -0
  65. package/distribution/username-availability/index.js.map +1 -0
  66. package/distribution/username-change/index.d.ts +15 -0
  67. package/distribution/username-change/index.d.ts.map +1 -0
  68. package/distribution/username-change/index.js +39 -0
  69. package/distribution/username-change/index.js.map +1 -0
  70. package/distribution/username-change/type.d.ts +7 -0
  71. package/distribution/username-change/type.d.ts.map +1 -0
  72. package/distribution/username-change/type.js +2 -0
  73. package/distribution/username-change/type.js.map +1 -0
  74. package/package.json +18 -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, building blocks.** Usernames, one-time codes, TOTP, recovery codes, email contacts, effective
13
- permissions and ban lengths. Accounts, contacts, sessions, storage and the OpenID Connect server come next (see
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,45 @@ 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
+ ### `contact-kind`: `normalizeContact({ kind, value })`, `CONTACT_KINDS`
138
+
139
+ `email` in this version; unknown kinds and invalid values throw.
140
+
141
+ ### `audit-event`: `recordAuditEvent(database, { action, accountId?, actorAccountId?, application?, detail?, now? })` → id
142
+
143
+ Pass the transaction of the change it describes. `detail` holds ids and kinds, never contact values or secrets.
144
+
145
+ ### `database-error`: `databaseError(error)` → `{ code, constraint }` or undefined
146
+
92
147
  ## License
93
148
 
94
149
  Sharadox Open Source License 0.1.0 (see `LICENSE.md`).
@@ -0,0 +1,54 @@
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
+ /** The tables `migrateAccount` creates, for typing a Kysely instance. */
47
+ export interface AccountDatabase {
48
+ application: ApplicationTable;
49
+ account: AccountTable;
50
+ contact: ContactTable;
51
+ audit_event: AuditEventTable;
52
+ }
53
+ export {};
54
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +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,EAAE,WAAW,CAAC;CACzB;AAED,+CAA+C;AAC/C,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC;IACzB,uDAAuD;IACvD,QAAQ,EAAE,MAAM,CAAC;IACjB,qDAAqD;IACrD,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,UAAU,EAAE,WAAW,CAAC;IACxB,cAAc,EAAE,WAAW,CAAC;CAC7B;AAED,wEAAwE;AACxE,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,2CAA2C;IAC3C,KAAK,EAAE,MAAM,CAAC;IACd,uGAAuG;IACvG,WAAW,EAAE,IAAI,GAAG,IAAI,CAAC;IACzB,sDAAsD;IACtD,OAAO,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC;IAC5B,UAAU,EAAE,WAAW,CAAC;CACzB;AAED,6CAA6C;AAC7C,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,WAAW,CAAC;IAChB,iFAAiF;IACjF,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,4EAA4E;IAC5E,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,UAAU,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EAAE,KAAK,CAAC,CAAC;CACzF;AAED,yEAAyE;AACzE,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,gBAAgB,CAAC;IAC9B,OAAO,EAAE,YAAY,CAAC;IACtB,OAAO,EAAE,YAAY,CAAC;IACtB,WAAW,EAAE,eAAe,CAAC;CAC9B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -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,EAmFxC,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,102 @@
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
+ /**
94
+ * Creates or updates the account tables, tracked in their own migration table. Returns the names applied.
95
+ *
96
+ * @example
97
+ * await migrateAccount(database);
98
+ */
99
+ export function migrateAccount(database) {
100
+ return migrate(database, accountMigrations, { table: ACCOUNT_MIGRATION_TABLE });
101
+ }
102
+ //# 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;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,12 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ import type { AuditEvent } from "@sharadox/account-web-library/audit-event/type";
4
+ /**
5
+ * Adds a line to the append-only audit log (the database refuses changes to it). Pass a transaction to record it
6
+ * together with the change it describes. Returns the event's UUIDv7.
7
+ *
8
+ * @example
9
+ * await recordAuditEvent(transaction, { accountId, action: "contact.removed", detail: { contactId } });
10
+ */
11
+ export declare function recordAuditEvent(database: Kysely<AccountDatabase>, { accountId, actorAccountId, application, action, detail, now }: AuditEvent): Promise<string>;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/audit-event/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,UAAU,EAAE,MAAM,gDAAgD,CAAC;AAEjF;;;;;;GAMG;AACH,wBAAsB,gBAAgB,CACpC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAgB,EAAE,cAAqB,EAAE,WAAkB,EAAE,MAAM,EAAE,MAAW,EAAE,GAAgB,EAAE,EAAE,UAAU,GACjH,OAAO,CAAC,MAAM,CAAC,CAejB"}
@@ -0,0 +1,25 @@
1
+ import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
2
+ /**
3
+ * Adds a line to the append-only audit log (the database refuses changes to it). Pass a transaction to record it
4
+ * together with the change it describes. Returns the event's UUIDv7.
5
+ *
6
+ * @example
7
+ * await recordAuditEvent(transaction, { accountId, action: "contact.removed", detail: { contactId } });
8
+ */
9
+ export async function recordAuditEvent(database, { accountId = null, actorAccountId = null, application = null, action, detail = {}, now = Date.now() }) {
10
+ const id = uuidVersion7({ now });
11
+ await database
12
+ .insertInto("audit_event")
13
+ .values({
14
+ id,
15
+ at: new Date(now),
16
+ account_id: accountId,
17
+ actor_account_id: actorAccountId,
18
+ application,
19
+ action,
20
+ detail,
21
+ })
22
+ .execute();
23
+ return id;
24
+ }
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/audit-event/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAI/E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,QAAiC,EACjC,EAAE,SAAS,GAAG,IAAI,EAAE,cAAc,GAAG,IAAI,EAAE,WAAW,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,EAAE,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAc;IAElH,MAAM,EAAE,GAAG,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IACjC,MAAM,QAAQ;SACX,UAAU,CAAC,aAAa,CAAC;SACzB,MAAM,CAAC;QACN,EAAE;QACF,EAAE,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC;QACjB,UAAU,EAAE,SAAS;QACrB,gBAAgB,EAAE,cAAc;QAChC,WAAW;QACX,MAAM;QACN,MAAM;KACP,CAAC;SACD,OAAO,EAAE,CAAC;IACb,OAAO,EAAE,CAAC;AACZ,CAAC"}
@@ -0,0 +1,15 @@
1
+ /** What happened, to which account, done by whom, in which application. */
2
+ export interface AuditEvent {
3
+ /** The account it is about. */
4
+ accountId?: string | null;
5
+ /** Who did it, when not the account itself. */
6
+ actorAccountId?: string | null;
7
+ application?: string | null;
8
+ /** `<subject>.<verb>`: `account.created`, `contact.removed`, `role.granted`… */
9
+ action: string;
10
+ /** Ids and kinds, never contact values or secrets. */
11
+ detail?: Record<string, unknown>;
12
+ /** Milliseconds since 1970 (now by default). */
13
+ now?: number;
14
+ }
15
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/audit-event/type.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,MAAM,WAAW,UAAU;IACzB,+BAA+B;IAC/B,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,+CAA+C;IAC/C,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,gFAAgF;IAChF,MAAM,EAAE,MAAM,CAAC;IACf,sDAAsD;IACtD,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/audit-event/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
+ import type { ContactAdditionRequest, ContactAdditionResult } from "@sharadox/account-web-library/contact-addition/type";
4
+ /**
5
+ * Adds a contact to an account, unverified until a one-time code sent to it comes back (`verifyContact`). Refuses
6
+ * one the account already has, or one another account has verified. Ask for a fresh code first (step-up) and tell
7
+ * the other contacts.
8
+ *
9
+ * @example
10
+ * const result = await addContact(database, { accountId, contact: { kind: "email", value } });
11
+ */
12
+ export declare function addContact(database: Kysely<AccountDatabase>, { accountId, contact, now }: ContactAdditionRequest): Promise<ContactAdditionResult>;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-addition/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAE3F,OAAO,KAAK,EACV,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,qDAAqD,CAAC;AAI7D;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAC9B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,OAAO,EAAE,GAAgB,EAAE,EAAE,sBAAsB,GAC/D,OAAO,CAAC,qBAAqB,CAAC,CA8BhC"}
@@ -0,0 +1,48 @@
1
+ import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
2
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
3
+ import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
4
+ import { databaseError } from "@sharadox/account-web-library/database-error";
5
+ /**
6
+ * Adds a contact to an account, unverified until a one-time code sent to it comes back (`verifyContact`). Refuses
7
+ * one the account already has, or one another account has verified. Ask for a fresh code first (step-up) and tell
8
+ * the other contacts.
9
+ *
10
+ * @example
11
+ * const result = await addContact(database, { accountId, contact: { kind: "email", value } });
12
+ */
13
+ export async function addContact(database, { accountId, contact, now = Date.now() }) {
14
+ const { kind, value } = normalizeContact(contact);
15
+ const contactId = uuidVersion7({ now });
16
+ try {
17
+ return await database.transaction().execute(async (transaction) => {
18
+ const account = await transaction
19
+ .selectFrom("account")
20
+ .select("id")
21
+ .where("id", "=", accountId)
22
+ .executeTakeFirst();
23
+ if (!account)
24
+ return { added: false, reason: "no-account" };
25
+ const owner = await transaction
26
+ .selectFrom("contact")
27
+ .select("account_id")
28
+ .where("kind", "=", kind)
29
+ .where("value", "=", value)
30
+ .where("verified_at", "is not", null)
31
+ .executeTakeFirst();
32
+ if (owner && owner.account_id !== accountId)
33
+ return { added: false, reason: "contact-taken" };
34
+ await transaction
35
+ .insertInto("contact")
36
+ .values({ id: contactId, account_id: accountId, kind, value, verified_at: null, created_at: new Date(now) })
37
+ .execute();
38
+ await recordAuditEvent(transaction, { accountId, action: "contact.added", detail: { contactId, kind }, now });
39
+ return { added: true, contactId };
40
+ });
41
+ }
42
+ catch (error) {
43
+ if (databaseError(error)?.constraint === "contact_per_account")
44
+ return { added: false, reason: "already-added" };
45
+ throw error;
46
+ }
47
+ }
48
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-addition/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAE/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAK7E,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAE7E;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,QAAiC,EACjC,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAA0B;IAEhE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAClD,MAAM,SAAS,GAAG,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IACxC,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAkC,EAAE;YAChG,MAAM,OAAO,GAAG,MAAM,WAAW;iBAC9B,UAAU,CAAC,SAAS,CAAC;iBACrB,MAAM,CAAC,IAAI,CAAC;iBACZ,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;iBAC3B,gBAAgB,EAAE,CAAC;YACtB,IAAI,CAAC,OAAO;gBAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;YAC5D,MAAM,KAAK,GAAG,MAAM,WAAW;iBAC5B,UAAU,CAAC,SAAS,CAAC;iBACrB,MAAM,CAAC,YAAY,CAAC;iBACpB,KAAK,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC;iBACxB,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC;iBAC1B,KAAK,CAAC,aAAa,EAAE,QAAQ,EAAE,IAAI,CAAC;iBACpC,gBAAgB,EAAE,CAAC;YACtB,IAAI,KAAK,IAAI,KAAK,CAAC,UAAU,KAAK,SAAS;gBAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC;YAC9F,MAAM,WAAW;iBACd,UAAU,CAAC,SAAS,CAAC;iBACrB,MAAM,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;iBAC3G,OAAO,EAAE,CAAC;YACb,MAAM,gBAAgB,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;YAC9G,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;QACpC,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,UAAU,KAAK,qBAAqB;YAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC;QACjH,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
2
+ export interface ContactAdditionRequest {
3
+ accountId: string;
4
+ contact: Contact;
5
+ /** Milliseconds since 1970 (now by default). */
6
+ now?: number;
7
+ }
8
+ export type ContactAdditionResult = {
9
+ added: true;
10
+ contactId: string;
11
+ } | {
12
+ added: false;
13
+ reason: "already-added" | "contact-taken" | "no-account";
14
+ };
15
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/contact-addition/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,MAAM,WAAW,sBAAsB;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;IACjB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,qBAAqB,GAC/B;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,eAAe,GAAG,eAAe,GAAG,YAAY,CAAA;CAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/contact-addition/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,12 @@
1
+ import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
2
+ /** The kinds of contact this version can store and send codes to. */
3
+ export declare const CONTACT_KINDS: string[];
4
+ /**
5
+ * A contact in the one form it is stored and compared in (an email trimmed and lowercase…). Throws for a kind this
6
+ * version doesn't know, or a value that isn't valid for its kind.
7
+ *
8
+ * @example
9
+ * normalizeContact({ kind: "email", value: " Luis@Example.com " }); // { kind: "email", value: "luis@example.com" }
10
+ */
11
+ export declare function normalizeContact({ kind, value }: Contact): Contact;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-kind/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAI/E,qEAAqE;AACrE,eAAO,MAAM,aAAa,UAA2B,CAAC;AAEtD;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,OAAO,GAAG,OAAO,CAIlE"}
@@ -0,0 +1,18 @@
1
+ import { normalizeEmail } from "@sharadox/account-web-library/email-contact";
2
+ const NORMALIZERS = { email: normalizeEmail };
3
+ /** The kinds of contact this version can store and send codes to. */
4
+ export const CONTACT_KINDS = Object.keys(NORMALIZERS);
5
+ /**
6
+ * A contact in the one form it is stored and compared in (an email trimmed and lowercase…). Throws for a kind this
7
+ * version doesn't know, or a value that isn't valid for its kind.
8
+ *
9
+ * @example
10
+ * normalizeContact({ kind: "email", value: " Luis@Example.com " }); // { kind: "email", value: "luis@example.com" }
11
+ */
12
+ export function normalizeContact({ kind, value }) {
13
+ const normalize = NORMALIZERS[kind];
14
+ if (!normalize)
15
+ throw new Error(`unknown contact kind "${kind}"`);
16
+ return { kind, value: normalize(value) };
17
+ }
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-kind/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,6CAA6C,CAAC;AAG7E,MAAM,WAAW,GAA8C,EAAE,KAAK,EAAE,cAAc,EAAE,CAAC;AAEzF,qEAAqE;AACrE,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;AAEtD;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,EAAE,IAAI,EAAE,KAAK,EAAW;IACvD,MAAM,SAAS,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IACpC,IAAI,CAAC,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,GAAG,CAAC,CAAC;IAClE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,6 @@
1
+ /** A contact as given or stored: its kind (`email`…) and value. */
2
+ export interface Contact {
3
+ kind: string;
4
+ value: string;
5
+ }
6
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/contact-kind/type.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/contact-kind/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 { Contact } from "@sharadox/account-web-library/contact-kind/type";
4
+ /**
5
+ * The account a verified contact belongs to (written any way: it is normalized first), or undefined. Unverified
6
+ * contacts never count, so claiming someone's address proves nothing.
7
+ *
8
+ * @example
9
+ * const found = await findAccountByContact(database, { kind: "email", value: typed });
10
+ */
11
+ export declare function findAccountByContact(database: Kysely<AccountDatabase>, contact: Contact): Promise<{
12
+ accountId: string;
13
+ contactId: string;
14
+ signIn: boolean;
15
+ } | undefined>;
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-lookup/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAE3F,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,OAAO,EAAE,OAAO,GACf,OAAO,CAAC;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GAAG,SAAS,CAAC,CAUhF"}
@@ -0,0 +1,20 @@
1
+ import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
2
+ /**
3
+ * The account a verified contact belongs to (written any way: it is normalized first), or undefined. Unverified
4
+ * contacts never count, so claiming someone's address proves nothing.
5
+ *
6
+ * @example
7
+ * const found = await findAccountByContact(database, { kind: "email", value: typed });
8
+ */
9
+ export async function findAccountByContact(database, contact) {
10
+ const { kind, value } = normalizeContact(contact);
11
+ const row = await database
12
+ .selectFrom("contact")
13
+ .select(["id", "account_id", "sign_in"])
14
+ .where("kind", "=", kind)
15
+ .where("value", "=", value)
16
+ .where("verified_at", "is not", null)
17
+ .executeTakeFirst();
18
+ return row && { accountId: row.account_id, contactId: row.id, signIn: row.sign_in };
19
+ }
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-lookup/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAG9E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,QAAiC,EACjC,OAAgB;IAEhB,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAClD,MAAM,GAAG,GAAG,MAAM,QAAQ;SACvB,UAAU,CAAC,SAAS,CAAC;SACrB,MAAM,CAAC,CAAC,IAAI,EAAE,YAAY,EAAE,SAAS,CAAC,CAAC;SACvC,KAAK,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC;SACxB,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC;SAC1B,KAAK,CAAC,aAAa,EAAE,QAAQ,EAAE,IAAI,CAAC;SACpC,gBAAgB,EAAE,CAAC;IACtB,OAAO,GAAG,IAAI,EAAE,SAAS,EAAE,GAAG,CAAC,UAAU,EAAE,SAAS,EAAE,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC;AACtF,CAAC"}
@@ -0,0 +1,19 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ /**
4
+ * Removes a contact from an account. Refuses to remove the last verified sign-in contact of a person (the database
5
+ * checks it when the change commits). Ask for a fresh code first (step-up) and tell the other contacts.
6
+ *
7
+ * @example
8
+ * const result = await removeContact(database, { accountId, contactId });
9
+ */
10
+ export declare function removeContact(database: Kysely<AccountDatabase>, { accountId, contactId }: {
11
+ accountId: string;
12
+ contactId: string;
13
+ }): Promise<{
14
+ removed: true;
15
+ } | {
16
+ removed: false;
17
+ reason: "no-contact" | "last-sign-in-contact";
18
+ }>;
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-removal/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,aAAa,CACjC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GACjE,OAAO,CAAC;IAAE,OAAO,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,YAAY,GAAG,sBAAsB,CAAA;CAAE,CAAC,CAqBhG"}
@@ -0,0 +1,35 @@
1
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
2
+ import { databaseError } from "@sharadox/account-web-library/database-error";
3
+ /**
4
+ * Removes a contact from an account. Refuses to remove the last verified sign-in contact of a person (the database
5
+ * checks it when the change commits). Ask for a fresh code first (step-up) and tell the other contacts.
6
+ *
7
+ * @example
8
+ * const result = await removeContact(database, { accountId, contactId });
9
+ */
10
+ export async function removeContact(database, { accountId, contactId }) {
11
+ try {
12
+ return await database.transaction().execute(async (transaction) => {
13
+ const removed = await transaction
14
+ .deleteFrom("contact")
15
+ .where("id", "=", contactId)
16
+ .where("account_id", "=", accountId)
17
+ .returning("kind")
18
+ .executeTakeFirst();
19
+ if (!removed)
20
+ return { removed: false, reason: "no-contact" };
21
+ await recordAuditEvent(transaction, {
22
+ accountId,
23
+ action: "contact.removed",
24
+ detail: { contactId, kind: removed.kind },
25
+ });
26
+ return { removed: true };
27
+ });
28
+ }
29
+ catch (error) {
30
+ if (databaseError(error)?.code === "23514")
31
+ return { removed: false, reason: "last-sign-in-contact" };
32
+ throw error;
33
+ }
34
+ }
35
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-removal/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAE7E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAiC,EACjC,EAAE,SAAS,EAAE,SAAS,EAA4C;IAElE,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;YAChE,MAAM,OAAO,GAAG,MAAM,WAAW;iBAC9B,UAAU,CAAC,SAAS,CAAC;iBACrB,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;iBAC3B,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,SAAS,CAAC;iBACnC,SAAS,CAAC,MAAM,CAAC;iBACjB,gBAAgB,EAAE,CAAC;YACtB,IAAI,CAAC,OAAO;gBAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAW,CAAC;YACvE,MAAM,gBAAgB,CAAC,WAAW,EAAE;gBAClC,SAAS;gBACT,MAAM,EAAE,iBAAiB;gBACzB,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE;aAC1C,CAAC,CAAC;YACH,OAAO,EAAE,OAAO,EAAE,IAAI,EAAW,CAAC;QACpC,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,IAAI,KAAK,OAAO;YAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,sBAAsB,EAAE,CAAC;QACtG,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,20 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ /**
4
+ * Marks an account's contact verified, once the one-time code sent to it came back. Refuses when another account
5
+ * verified the same contact first (a verified contact belongs to one account only).
6
+ *
7
+ * @example
8
+ * await verifyContact(database, { accountId, contactId }); // { verified: true }
9
+ */
10
+ export declare function verifyContact(database: Kysely<AccountDatabase>, { accountId, contactId, now }: {
11
+ accountId: string;
12
+ contactId: string;
13
+ now?: number;
14
+ }): Promise<{
15
+ verified: true;
16
+ } | {
17
+ verified: false;
18
+ reason: "no-contact" | "contact-taken";
19
+ }>;
20
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/contact-verification/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,aAAa,CACjC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,SAAS,EAAE,GAAgB,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GACjG,OAAO,CAAC;IAAE,QAAQ,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,QAAQ,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,YAAY,GAAG,eAAe,CAAA;CAAE,CAAC,CAuB3F"}
@@ -0,0 +1,37 @@
1
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
2
+ import { databaseError } from "@sharadox/account-web-library/database-error";
3
+ /**
4
+ * Marks an account's contact verified, once the one-time code sent to it came back. Refuses when another account
5
+ * verified the same contact first (a verified contact belongs to one account only).
6
+ *
7
+ * @example
8
+ * await verifyContact(database, { accountId, contactId }); // { verified: true }
9
+ */
10
+ export async function verifyContact(database, { accountId, contactId, now = Date.now() }) {
11
+ try {
12
+ return await database.transaction().execute(async (transaction) => {
13
+ const updated = await transaction
14
+ .updateTable("contact")
15
+ .set({ verified_at: new Date(now) })
16
+ .where("id", "=", contactId)
17
+ .where("account_id", "=", accountId)
18
+ .returning("kind")
19
+ .executeTakeFirst();
20
+ if (!updated)
21
+ return { verified: false, reason: "no-contact" };
22
+ await recordAuditEvent(transaction, {
23
+ accountId,
24
+ action: "contact.verified",
25
+ detail: { contactId, kind: updated.kind },
26
+ now,
27
+ });
28
+ return { verified: true };
29
+ });
30
+ }
31
+ catch (error) {
32
+ if (databaseError(error)?.constraint === "contact_verified")
33
+ return { verified: false, reason: "contact-taken" };
34
+ throw error;
35
+ }
36
+ }
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/contact-verification/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAE7E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAiC,EACjC,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAA0D;IAElG,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;YAChE,MAAM,OAAO,GAAG,MAAM,WAAW;iBAC9B,WAAW,CAAC,SAAS,CAAC;iBACtB,GAAG,CAAC,EAAE,WAAW,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;iBACnC,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;iBAC3B,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,SAAS,CAAC;iBACnC,SAAS,CAAC,MAAM,CAAC;iBACjB,gBAAgB,EAAE,CAAC;YACtB,IAAI,CAAC,OAAO;gBAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAW,CAAC;YACxE,MAAM,gBAAgB,CAAC,WAAW,EAAE;gBAClC,SAAS;gBACT,MAAM,EAAE,kBAAkB;gBAC1B,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE;gBACzC,GAAG;aACJ,CAAC,CAAC;YACH,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAW,CAAC;QACrC,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,UAAU,KAAK,kBAAkB;YAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC;QACjH,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Postgres's error code (`23505` unique, `23514` check…) and the constraint that failed, from an error thrown by the
3
+ * driver (Neon's or PGlite's); undefined for any other error.
4
+ *
5
+ * @example
6
+ * if (databaseError(error)?.constraint === "account_username_key") return { reason: "username-taken" };
7
+ */
8
+ export declare function databaseError(error: unknown): {
9
+ code: string;
10
+ constraint: string | undefined;
11
+ } | undefined;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/database-error/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,GAAG,SAAS,CAK1G"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Postgres's error code (`23505` unique, `23514` check…) and the constraint that failed, from an error thrown by the
3
+ * driver (Neon's or PGlite's); undefined for any other error.
4
+ *
5
+ * @example
6
+ * if (databaseError(error)?.constraint === "account_username_key") return { reason: "username-taken" };
7
+ */
8
+ export function databaseError(error) {
9
+ if (typeof error !== "object" || error === null || typeof error.code !== "string")
10
+ return undefined;
11
+ const { code, constraint } = error;
12
+ return { code, constraint };
13
+ }
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/database-error/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc;IAC1C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,OAAQ,KAA4B,CAAC,IAAI,KAAK,QAAQ;QACvG,OAAO,SAAS,CAAC;IACnB,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,KAA8C,CAAC;IAC5E,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;AAC9B,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 { PersonAccountRequest, PersonAccountResult } from "@sharadox/account-web-library/person-account/type";
4
+ /**
5
+ * Creates a person's account with the contact their one-time code just proved, as its verified sign-in contact, and
6
+ * records it — all or nothing. Refuses a username outside the rules or taken in any case, and a contact another
7
+ * account has verified.
8
+ *
9
+ * @example
10
+ * const result = await createPersonAccount(database, { username: "Luis-Pires", contact: { kind: "email", value } });
11
+ * if (!result.created) return explain(result.reason);
12
+ */
13
+ export declare function createPersonAccount(database: Kysely<AccountDatabase>, { username, contact, now }: PersonAccountRequest): Promise<PersonAccountResult>;
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/person-account/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,mDAAmD,CAAC;AAGnH;;;;;;;;GAQG;AACH,wBAAsB,mBAAmB,CACvC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAgB,EAAE,EAAE,oBAAoB,GAC5D,OAAO,CAAC,mBAAmB,CAAC,CAyB9B"}
@@ -0,0 +1,45 @@
1
+ import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
2
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
3
+ import { normalizeContact } from "@sharadox/account-web-library/contact-kind";
4
+ import { databaseError } from "@sharadox/account-web-library/database-error";
5
+ import { checkUsername } from "@sharadox/account-web-library/username";
6
+ /**
7
+ * Creates a person's account with the contact their one-time code just proved, as its verified sign-in contact, and
8
+ * records it — all or nothing. Refuses a username outside the rules or taken in any case, and a contact another
9
+ * account has verified.
10
+ *
11
+ * @example
12
+ * const result = await createPersonAccount(database, { username: "Luis-Pires", contact: { kind: "email", value } });
13
+ * if (!result.created) return explain(result.reason);
14
+ */
15
+ export async function createPersonAccount(database, { username, contact, now = Date.now() }) {
16
+ if (!checkUsername(username).valid)
17
+ return { created: false, reason: "username-invalid" };
18
+ const { kind, value } = normalizeContact(contact);
19
+ const at = new Date(now);
20
+ const accountId = uuidVersion7({ now });
21
+ const contactId = uuidVersion7({ now });
22
+ try {
23
+ await database.transaction().execute(async (transaction) => {
24
+ await transaction
25
+ .insertInto("account")
26
+ .values({ id: accountId, kind: "person", username, owner_id: null, created_at: at, last_active_at: at })
27
+ .execute();
28
+ await transaction
29
+ .insertInto("contact")
30
+ .values({ id: contactId, account_id: accountId, kind, value, verified_at: at, created_at: at })
31
+ .execute();
32
+ await recordAuditEvent(transaction, { accountId, action: "account.created", detail: { contactId }, now });
33
+ });
34
+ }
35
+ catch (error) {
36
+ const constraint = databaseError(error)?.constraint;
37
+ if (constraint === "account_username_key")
38
+ return { created: false, reason: "username-taken" };
39
+ if (constraint === "contact_verified")
40
+ return { created: false, reason: "contact-taken" };
41
+ throw error;
42
+ }
43
+ return { created: true, accountId, contactId };
44
+ }
45
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/person-account/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAE/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAE7E,OAAO,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AAEvE;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,QAAiC,EACjC,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAwB;IAE7D,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,KAAK;QAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,kBAAkB,EAAE,CAAC;IAC1F,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAClD,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,MAAM,SAAS,GAAG,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IACxC,MAAM,SAAS,GAAG,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IACxC,IAAI,CAAC;QACH,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;YACzD,MAAM,WAAW;iBACd,UAAU,CAAC,SAAS,CAAC;iBACrB,MAAM,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,cAAc,EAAE,EAAE,EAAE,CAAC;iBACvG,OAAO,EAAE,CAAC;YACb,MAAM,WAAW;iBACd,UAAU,CAAC,SAAS,CAAC;iBACrB,MAAM,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;iBAC9F,OAAO,EAAE,CAAC;YACb,MAAM,gBAAgB,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;QAC5G,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,UAAU,GAAG,aAAa,CAAC,KAAK,CAAC,EAAE,UAAU,CAAC;QACpD,IAAI,UAAU,KAAK,sBAAsB;YAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,CAAC;QAC/F,IAAI,UAAU,KAAK,kBAAkB;YAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC;QAC1F,MAAM,KAAK,CAAC;IACd,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;AACjD,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { Contact } from "@sharadox/account-web-library/contact-kind/type";
2
+ export interface PersonAccountRequest {
3
+ username: string;
4
+ /** Already proved by a one-time code: it is stored verified. */
5
+ contact: Contact;
6
+ /** Milliseconds since 1970 (now by default). */
7
+ now?: number;
8
+ }
9
+ export type PersonAccountResult = {
10
+ created: true;
11
+ accountId: string;
12
+ contactId: string;
13
+ } | {
14
+ created: false;
15
+ reason: "username-invalid" | "username-taken" | "contact-taken";
16
+ };
17
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/person-account/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iDAAiD,CAAC;AAE/E,MAAM,WAAW,oBAAoB;IACnC,QAAQ,EAAE,MAAM,CAAC;IACjB,gEAAgE;IAChE,OAAO,EAAE,OAAO,CAAC;IACjB,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,mBAAmB,GAC3B;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GACvD;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,kBAAkB,GAAG,gBAAgB,GAAG,eAAe,CAAA;CAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/person-account/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,13 @@
1
+ import type { MemoryDatabase } from "@sharadox/database-web-library/memory-database/type";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ /**
4
+ * A migrated account database in memory, for this library's tests (and applications' own): Postgres 18 through
5
+ * PGlite, so every constraint and trigger runs as in production.
6
+ *
7
+ * @example
8
+ * const memory = await createTestAccountDatabase();
9
+ * // … test …
10
+ * await memory.close();
11
+ */
12
+ export declare function createTestAccountDatabase(): Promise<MemoryDatabase<AccountDatabase>>;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/test-database/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qDAAqD,CAAC;AAC1F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAG3F;;;;;;;;GAQG;AACH,wBAAsB,yBAAyB,IAAI,OAAO,CAAC,cAAc,CAAC,eAAe,CAAC,CAAC,CAI1F"}
@@ -0,0 +1,17 @@
1
+ import { createMemoryDatabase } from "@sharadox/database-web-library/memory-database";
2
+ import { migrateAccount } from "@sharadox/account-web-library/account-migration";
3
+ /**
4
+ * A migrated account database in memory, for this library's tests (and applications' own): Postgres 18 through
5
+ * PGlite, so every constraint and trigger runs as in production.
6
+ *
7
+ * @example
8
+ * const memory = await createTestAccountDatabase();
9
+ * // … test …
10
+ * await memory.close();
11
+ */
12
+ export async function createTestAccountDatabase() {
13
+ const memory = await createMemoryDatabase();
14
+ await migrateAccount(memory.database);
15
+ return memory;
16
+ }
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/test-database/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,gDAAgD,CAAC;AAGtF,OAAO,EAAE,cAAc,EAAE,MAAM,iDAAiD,CAAC;AAEjF;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,yBAAyB;IAC7C,MAAM,MAAM,GAAG,MAAM,oBAAoB,EAAmB,CAAC;IAC7D,MAAM,cAAc,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACtC,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { type Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ /**
4
+ * Whether a username can be taken: `available`, `taken` (in any case: `aBc` is taken when `abc` exists) or `invalid`
5
+ * (outside the rules). Rate limit callers: it tells whether an account exists.
6
+ *
7
+ * @example
8
+ * await usernameAvailability(database, "Luis-Pires"); // "available"
9
+ */
10
+ export declare function usernameAvailability(database: Kysely<AccountDatabase>, username: string): Promise<"available" | "taken" | "invalid">;
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/username-availability/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAG3F;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,WAAW,GAAG,OAAO,GAAG,SAAS,CAAC,CAS5C"}
@@ -0,0 +1,21 @@
1
+ import { sql } from "kysely";
2
+ import { checkUsername } from "@sharadox/account-web-library/username";
3
+ /**
4
+ * Whether a username can be taken: `available`, `taken` (in any case: `aBc` is taken when `abc` exists) or `invalid`
5
+ * (outside the rules). Rate limit callers: it tells whether an account exists.
6
+ *
7
+ * @example
8
+ * await usernameAvailability(database, "Luis-Pires"); // "available"
9
+ */
10
+ export async function usernameAvailability(database, username) {
11
+ const check = checkUsername(username);
12
+ if (!check.valid)
13
+ return "invalid";
14
+ const found = await database
15
+ .selectFrom("account")
16
+ .select("id")
17
+ .where(sql `lower(username)`, "=", check.key)
18
+ .executeTakeFirst();
19
+ return found ? "taken" : "available";
20
+ }
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/username-availability/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAe,MAAM,QAAQ,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AAEvE;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,QAAiC,EACjC,QAAgB;IAEhB,MAAM,KAAK,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;IACtC,IAAI,CAAC,KAAK,CAAC,KAAK;QAAE,OAAO,SAAS,CAAC;IACnC,MAAM,KAAK,GAAG,MAAM,QAAQ;SACzB,UAAU,CAAC,SAAS,CAAC;SACrB,MAAM,CAAC,IAAI,CAAC;SACZ,KAAK,CAAC,GAAG,CAAQ,iBAAiB,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC;SACnD,gBAAgB,EAAE,CAAC;IACtB,OAAO,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC;AACvC,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 { UsernameChangeResult } from "@sharadox/account-web-library/username-change/type";
4
+ /**
5
+ * Changes an account's username whenever its owner wants; the old one is free for anyone at once. Accounts are
6
+ * referenced by id, so nothing breaks; the audit log keeps the history. Changing only the case is allowed.
7
+ *
8
+ * @example
9
+ * await changeUsername(database, { accountId, username: "Luis-Pires" }); // { changed: true }
10
+ */
11
+ export declare function changeUsername(database: Kysely<AccountDatabase>, { accountId, username }: {
12
+ accountId: string;
13
+ username: string;
14
+ }): Promise<UsernameChangeResult>;
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/username-change/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oDAAoD,CAAC;AAE/F;;;;;;GAMG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC/D,OAAO,CAAC,oBAAoB,CAAC,CAwB/B"}
@@ -0,0 +1,39 @@
1
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
2
+ import { databaseError } from "@sharadox/account-web-library/database-error";
3
+ import { checkUsername } from "@sharadox/account-web-library/username";
4
+ /**
5
+ * Changes an account's username whenever its owner wants; the old one is free for anyone at once. Accounts are
6
+ * referenced by id, so nothing breaks; the audit log keeps the history. Changing only the case is allowed.
7
+ *
8
+ * @example
9
+ * await changeUsername(database, { accountId, username: "Luis-Pires" }); // { changed: true }
10
+ */
11
+ export async function changeUsername(database, { accountId, username }) {
12
+ if (!checkUsername(username).valid)
13
+ return { changed: false, reason: "username-invalid" };
14
+ try {
15
+ return await database.transaction().execute(async (transaction) => {
16
+ const current = await transaction
17
+ .selectFrom("account")
18
+ .select("username")
19
+ .where("id", "=", accountId)
20
+ .forUpdate()
21
+ .executeTakeFirst();
22
+ if (!current)
23
+ return { changed: false, reason: "no-account" };
24
+ await transaction.updateTable("account").set({ username }).where("id", "=", accountId).execute();
25
+ await recordAuditEvent(transaction, {
26
+ accountId,
27
+ action: "account.username-changed",
28
+ detail: { from: current.username, to: username },
29
+ });
30
+ return { changed: true };
31
+ });
32
+ }
33
+ catch (error) {
34
+ if (databaseError(error)?.constraint === "account_username_key")
35
+ return { changed: false, reason: "username-taken" };
36
+ throw error;
37
+ }
38
+ }
39
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/username-change/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,8CAA8C,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AAGvE;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,QAAiC,EACjC,EAAE,SAAS,EAAE,QAAQ,EAA2C;IAEhE,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,KAAK;QAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,kBAAkB,EAAE,CAAC;IAC1F,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAiC,EAAE;YAC/F,MAAM,OAAO,GAAG,MAAM,WAAW;iBAC9B,UAAU,CAAC,SAAS,CAAC;iBACrB,MAAM,CAAC,UAAU,CAAC;iBAClB,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;iBAC3B,SAAS,EAAE;iBACX,gBAAgB,EAAE,CAAC;YACtB,IAAI,CAAC,OAAO;gBAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;YAC9D,MAAM,WAAW,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC,CAAC,OAAO,EAAE,CAAC;YACjG,MAAM,gBAAgB,CAAC,WAAW,EAAE;gBAClC,SAAS;gBACT,MAAM,EAAE,0BAA0B;gBAClC,MAAM,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE;aACjD,CAAC,CAAC;YACH,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;QAC3B,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,UAAU,KAAK,sBAAsB;YAC7D,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,CAAC;QACtD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,7 @@
1
+ export type UsernameChangeResult = {
2
+ changed: true;
3
+ } | {
4
+ changed: false;
5
+ reason: "username-invalid" | "username-taken" | "no-account";
6
+ };
7
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/username-change/type.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,oBAAoB,GAC9B;IAAE,OAAO,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,kBAAkB,GAAG,gBAAgB,GAAG,YAAY,CAAA;CAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/username-change/type.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sharadox/account-web-library",
3
- "version": "0.1.0-development.0",
3
+ "version": "0.1.0-development.6",
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",
@@ -29,22 +29,9 @@
29
29
  "engines": {
30
30
  "node": ">=26"
31
31
  },
32
- "packageManager": "pnpm@10.34.9",
33
32
  "publishConfig": {
34
33
  "access": "public"
35
34
  },
36
- "scripts": {
37
- "build": "library-build",
38
- "lint": "eslint --max-warnings=0 .",
39
- "format": "prettier --write .",
40
- "format:check": "prettier --check .",
41
- "type-check": "tsc --noEmit",
42
- "test:unit": "vitest run --coverage",
43
- "test:benchmark": "vitest bench --run --passWithNoTests",
44
- "documentation": "typedoc",
45
- "verify": "turbo run format:check lint type-check test:unit test:benchmark build",
46
- "prepare": "turborepo-configuration library && (husky || true)"
47
- },
48
35
  "lint-staged": {
49
36
  "*.{ts,js,mjs,cjs}": [
50
37
  "prettier --write",
@@ -53,9 +40,12 @@
53
40
  "*.{json,md,yml,yaml}": "prettier --write"
54
41
  },
55
42
  "dependencies": {
56
- "@sharadox/web-library": "^0.11.1"
43
+ "@sharadox/database-web-library": "0.1.0-development.6",
44
+ "@sharadox/web-library": "^0.11.1",
45
+ "kysely": "^0.28.20"
57
46
  },
58
47
  "devDependencies": {
48
+ "@electric-sql/pglite": "0.5.16",
59
49
  "@sharadox/es-lint-configuration-web-library": "^0.1.3",
60
50
  "@sharadox/library-build-web-library": "^0.2.1",
61
51
  "@sharadox/prettier-configuration-web-library": "^0.1.2",
@@ -88,5 +78,16 @@
88
78
  "openid-connect",
89
79
  "oauth",
90
80
  "roles"
91
- ]
92
- }
81
+ ],
82
+ "scripts": {
83
+ "build": "library-build",
84
+ "lint": "eslint --max-warnings=0 .",
85
+ "format": "prettier --write .",
86
+ "format:check": "prettier --check .",
87
+ "type-check": "tsc --noEmit",
88
+ "test:unit": "vitest run --coverage",
89
+ "test:benchmark": "vitest bench --run --passWithNoTests",
90
+ "documentation": "typedoc",
91
+ "verify": "turbo run format:check lint type-check test:unit test:benchmark build"
92
+ }
93
+ }