@sharadox/account-web-library 0.1.0-development.21 → 0.1.0-development.27

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 (232) hide show
  1. package/README.md +97 -21
  2. package/distribution/account-activity/index.d.ts +16 -0
  3. package/distribution/account-activity/index.d.ts.map +1 -0
  4. package/distribution/account-activity/index.js +18 -0
  5. package/distribution/account-activity/index.js.map +1 -0
  6. package/distribution/account-activity/type.d.ts +15 -0
  7. package/distribution/account-activity/type.d.ts.map +1 -0
  8. package/distribution/account-activity/type.js.map +1 -0
  9. package/distribution/account-database/type.d.ts +57 -4
  10. package/distribution/account-database/type.d.ts.map +1 -1
  11. package/distribution/account-deletion/index.d.ts +18 -0
  12. package/distribution/account-deletion/index.d.ts.map +1 -0
  13. package/distribution/account-deletion/index.js +28 -0
  14. package/distribution/account-deletion/index.js.map +1 -0
  15. package/distribution/account-export/index.d.ts +17 -0
  16. package/distribution/account-export/index.d.ts.map +1 -0
  17. package/distribution/account-export/index.js +87 -0
  18. package/distribution/account-export/index.js.map +1 -0
  19. package/distribution/account-export/type.d.ts +40 -0
  20. package/distribution/account-export/type.d.ts.map +1 -0
  21. package/distribution/{username-change → account-export}/type.js.map +1 -1
  22. package/distribution/account-migration/index.d.ts.map +1 -1
  23. package/distribution/account-migration/index.js +110 -0
  24. package/distribution/account-migration/index.js.map +1 -1
  25. package/distribution/account-name/index.d.ts +13 -0
  26. package/distribution/account-name/index.d.ts.map +1 -0
  27. package/distribution/account-name/index.js +22 -0
  28. package/distribution/account-name/index.js.map +1 -0
  29. package/distribution/account-name/type.d.ts +9 -0
  30. package/distribution/account-name/type.d.ts.map +1 -0
  31. package/distribution/account-name/type.js +2 -0
  32. package/distribution/account-name/type.js.map +1 -0
  33. package/distribution/account-name-change/index.d.ts +15 -0
  34. package/distribution/account-name-change/index.d.ts.map +1 -0
  35. package/distribution/account-name-change/index.js +26 -0
  36. package/distribution/account-name-change/index.js.map +1 -0
  37. package/distribution/account-name-change/type.d.ts +7 -0
  38. package/distribution/account-name-change/type.d.ts.map +1 -0
  39. package/distribution/account-name-change/type.js +2 -0
  40. package/distribution/account-name-change/type.js.map +1 -0
  41. package/distribution/account-picture-change/index.d.ts +30 -0
  42. package/distribution/account-picture-change/index.d.ts.map +1 -0
  43. package/distribution/account-picture-change/index.js +70 -0
  44. package/distribution/account-picture-change/index.js.map +1 -0
  45. package/distribution/account-picture-change/type.d.ts +11 -0
  46. package/distribution/account-picture-change/type.d.ts.map +1 -0
  47. package/distribution/account-picture-change/type.js +2 -0
  48. package/distribution/account-picture-change/type.js.map +1 -0
  49. package/distribution/account-server/index.d.ts +11 -1
  50. package/distribution/account-server/index.d.ts.map +1 -1
  51. package/distribution/account-server/index.js +12 -1
  52. package/distribution/account-server/index.js.map +1 -1
  53. package/distribution/account-server/type.d.ts +2 -0
  54. package/distribution/account-server/type.d.ts.map +1 -1
  55. package/distribution/account-session-end/index.d.ts +15 -0
  56. package/distribution/account-session-end/index.d.ts.map +1 -0
  57. package/distribution/account-session-end/index.js +21 -0
  58. package/distribution/account-session-end/index.js.map +1 -0
  59. package/distribution/account-session-list/index.d.ts +16 -0
  60. package/distribution/account-session-list/index.d.ts.map +1 -0
  61. package/distribution/account-session-list/index.js +33 -0
  62. package/distribution/account-session-list/index.js.map +1 -0
  63. package/distribution/account-session-list/type.d.ts +13 -0
  64. package/distribution/account-session-list/type.d.ts.map +1 -0
  65. package/distribution/account-session-list/type.js +2 -0
  66. package/distribution/account-session-list/type.js.map +1 -0
  67. package/distribution/account-sign-in/index.d.ts +1 -1
  68. package/distribution/account-sign-in/index.d.ts.map +1 -1
  69. package/distribution/account-sign-in/index.js +4 -2
  70. package/distribution/account-sign-in/index.js.map +1 -1
  71. package/distribution/account-sign-in/type.d.ts +2 -0
  72. package/distribution/account-sign-in/type.d.ts.map +1 -1
  73. package/distribution/audit-event/index.d.ts.map +1 -1
  74. package/distribution/audit-event/index.js +12 -1
  75. package/distribution/audit-event/index.js.map +1 -1
  76. package/distribution/audit-event/type.d.ts +1 -0
  77. package/distribution/audit-event/type.d.ts.map +1 -1
  78. package/distribution/browser-session-read/index.d.ts.map +1 -1
  79. package/distribution/browser-session-read/index.js +5 -1
  80. package/distribution/browser-session-read/index.js.map +1 -1
  81. package/distribution/browser-session-read/type.d.ts +5 -1
  82. package/distribution/browser-session-read/type.d.ts.map +1 -1
  83. package/distribution/code-request/index.js +1 -1
  84. package/distribution/code-request/type.d.ts +1 -1
  85. package/distribution/code-sending/index.d.ts +14 -0
  86. package/distribution/code-sending/index.d.ts.map +1 -0
  87. package/distribution/code-sending/index.js +65 -0
  88. package/distribution/code-sending/index.js.map +1 -0
  89. package/distribution/code-sending/type.d.ts +31 -0
  90. package/distribution/code-sending/type.d.ts.map +1 -0
  91. package/distribution/code-sending/type.js +2 -0
  92. package/distribution/code-sending/type.js.map +1 -0
  93. package/distribution/contact-addition-flow/index.d.ts +31 -0
  94. package/distribution/contact-addition-flow/index.d.ts.map +1 -0
  95. package/distribution/contact-addition-flow/index.js +85 -0
  96. package/distribution/contact-addition-flow/index.js.map +1 -0
  97. package/distribution/contact-addition-flow/type.d.ts +22 -0
  98. package/distribution/contact-addition-flow/type.d.ts.map +1 -0
  99. package/distribution/contact-addition-flow/type.js +2 -0
  100. package/distribution/contact-addition-flow/type.js.map +1 -0
  101. package/distribution/database-error/index.d.ts +1 -1
  102. package/distribution/database-error/index.js +1 -1
  103. package/distribution/email-channel/index.js +1 -1
  104. package/distribution/identifier/index.d.ts +23 -0
  105. package/distribution/identifier/index.d.ts.map +1 -0
  106. package/distribution/identifier/index.js +32 -0
  107. package/distribution/identifier/index.js.map +1 -0
  108. package/distribution/identifier/type.d.ts +10 -0
  109. package/distribution/identifier/type.d.ts.map +1 -0
  110. package/distribution/identifier/type.js +2 -0
  111. package/distribution/identifier/type.js.map +1 -0
  112. package/distribution/identifier-availability/index.d.ts +11 -0
  113. package/distribution/identifier-availability/index.d.ts.map +1 -0
  114. package/distribution/identifier-availability/index.js +21 -0
  115. package/distribution/identifier-availability/index.js.map +1 -0
  116. package/distribution/identifier-change/index.d.ts +15 -0
  117. package/distribution/identifier-change/index.d.ts.map +1 -0
  118. package/distribution/identifier-change/index.js +43 -0
  119. package/distribution/identifier-change/index.js.map +1 -0
  120. package/distribution/identifier-change/type.d.ts +7 -0
  121. package/distribution/identifier-change/type.d.ts.map +1 -0
  122. package/distribution/identifier-change/type.js +2 -0
  123. package/distribution/identifier-change/type.js.map +1 -0
  124. package/distribution/memory-picture-storage/index.d.ts +15 -0
  125. package/distribution/memory-picture-storage/index.d.ts.map +1 -0
  126. package/distribution/memory-picture-storage/index.js +16 -0
  127. package/distribution/memory-picture-storage/index.js.map +1 -0
  128. package/distribution/one-time-code/index.d.ts +2 -2
  129. package/distribution/one-time-code/index.js +3 -3
  130. package/distribution/person-account/index.d.ts +7 -3
  131. package/distribution/person-account/index.d.ts.map +1 -1
  132. package/distribution/person-account/index.js +21 -9
  133. package/distribution/person-account/index.js.map +1 -1
  134. package/distribution/person-account/type.d.ts +5 -2
  135. package/distribution/person-account/type.d.ts.map +1 -1
  136. package/distribution/picture-key/index.d.ts +8 -0
  137. package/distribution/picture-key/index.d.ts.map +1 -0
  138. package/distribution/picture-key/index.js +10 -0
  139. package/distribution/picture-key/index.js.map +1 -0
  140. package/distribution/picture-preparation/index.d.ts +18 -0
  141. package/distribution/picture-preparation/index.d.ts.map +1 -0
  142. package/distribution/picture-preparation/index.js +86 -0
  143. package/distribution/picture-preparation/index.js.map +1 -0
  144. package/distribution/picture-preparation/type.d.ts +18 -0
  145. package/distribution/picture-preparation/type.d.ts.map +1 -0
  146. package/distribution/picture-preparation/type.js +2 -0
  147. package/distribution/picture-preparation/type.js.map +1 -0
  148. package/distribution/picture-storage/type.d.ts +11 -0
  149. package/distribution/picture-storage/type.d.ts.map +1 -0
  150. package/distribution/picture-storage/type.js +2 -0
  151. package/distribution/picture-storage/type.js.map +1 -0
  152. package/distribution/recovery-code-regeneration/index.d.ts +19 -0
  153. package/distribution/recovery-code-regeneration/index.d.ts.map +1 -0
  154. package/distribution/recovery-code-regeneration/index.js +34 -0
  155. package/distribution/recovery-code-regeneration/index.js.map +1 -0
  156. package/distribution/recovery-code-store/index.d.ts +25 -0
  157. package/distribution/recovery-code-store/index.d.ts.map +1 -0
  158. package/distribution/recovery-code-store/index.js +34 -0
  159. package/distribution/recovery-code-store/index.js.map +1 -0
  160. package/distribution/second-factor-finish/index.d.ts +12 -0
  161. package/distribution/second-factor-finish/index.d.ts.map +1 -0
  162. package/distribution/second-factor-finish/index.js +78 -0
  163. package/distribution/second-factor-finish/index.js.map +1 -0
  164. package/distribution/second-factor-finish/type.d.ts +27 -0
  165. package/distribution/second-factor-finish/type.d.ts.map +1 -0
  166. package/distribution/second-factor-finish/type.js +2 -0
  167. package/distribution/second-factor-finish/type.js.map +1 -0
  168. package/distribution/secret-encryption/index.d.ts +17 -0
  169. package/distribution/secret-encryption/index.d.ts.map +1 -0
  170. package/distribution/secret-encryption/index.js +37 -0
  171. package/distribution/secret-encryption/index.js.map +1 -0
  172. package/distribution/sign-in-finish/index.d.ts +3 -3
  173. package/distribution/sign-in-finish/index.d.ts.map +1 -1
  174. package/distribution/sign-in-finish/index.js +22 -5
  175. package/distribution/sign-in-finish/index.js.map +1 -1
  176. package/distribution/sign-in-finish/type.d.ts +8 -1
  177. package/distribution/sign-in-finish/type.d.ts.map +1 -1
  178. package/distribution/sign-in-start/index.d.ts +4 -6
  179. package/distribution/sign-in-start/index.d.ts.map +1 -1
  180. package/distribution/sign-in-start/index.js +14 -58
  181. package/distribution/sign-in-start/index.js.map +1 -1
  182. package/distribution/sign-in-start/type.d.ts +7 -0
  183. package/distribution/sign-in-start/type.d.ts.map +1 -1
  184. package/distribution/sign-up-finish/index.d.ts +4 -4
  185. package/distribution/sign-up-finish/index.d.ts.map +1 -1
  186. package/distribution/sign-up-finish/index.js +11 -6
  187. package/distribution/sign-up-finish/index.js.map +1 -1
  188. package/distribution/sign-up-finish/type.d.ts +6 -1
  189. package/distribution/sign-up-finish/type.d.ts.map +1 -1
  190. package/distribution/signed-ticket/index.d.ts +1 -1
  191. package/distribution/signed-ticket/index.js +1 -1
  192. package/distribution/step-up/index.d.ts +48 -0
  193. package/distribution/step-up/index.d.ts.map +1 -0
  194. package/distribution/step-up/index.js +83 -0
  195. package/distribution/step-up/index.js.map +1 -0
  196. package/distribution/step-up/type.d.ts +15 -0
  197. package/distribution/step-up/type.d.ts.map +1 -0
  198. package/distribution/step-up/type.js +2 -0
  199. package/distribution/{username → step-up}/type.js.map +1 -1
  200. package/distribution/test-person/index.d.ts +14 -0
  201. package/distribution/test-person/index.d.ts.map +1 -0
  202. package/distribution/test-person/index.js +20 -0
  203. package/distribution/test-person/index.js.map +1 -0
  204. package/distribution/totp/type.d.ts +1 -1
  205. package/distribution/totp/type.d.ts.map +1 -1
  206. package/distribution/totp-setup/index.d.ts +50 -0
  207. package/distribution/totp-setup/index.d.ts.map +1 -0
  208. package/distribution/totp-setup/index.js +136 -0
  209. package/distribution/totp-setup/index.js.map +1 -0
  210. package/distribution/totp-setup/type.d.ts +24 -0
  211. package/distribution/totp-setup/type.d.ts.map +1 -0
  212. package/distribution/totp-setup/type.js +2 -0
  213. package/distribution/totp-setup/type.js.map +1 -0
  214. package/package.json +4 -3
  215. package/distribution/username/index.d.ts +0 -23
  216. package/distribution/username/index.d.ts.map +0 -1
  217. package/distribution/username/index.js +0 -32
  218. package/distribution/username/index.js.map +0 -1
  219. package/distribution/username/type.d.ts +0 -10
  220. package/distribution/username/type.d.ts.map +0 -1
  221. package/distribution/username-availability/index.d.ts +0 -11
  222. package/distribution/username-availability/index.d.ts.map +0 -1
  223. package/distribution/username-availability/index.js +0 -21
  224. package/distribution/username-availability/index.js.map +0 -1
  225. package/distribution/username-change/index.d.ts +0 -15
  226. package/distribution/username-change/index.d.ts.map +0 -1
  227. package/distribution/username-change/index.js +0 -39
  228. package/distribution/username-change/index.js.map +0 -1
  229. package/distribution/username-change/type.d.ts +0 -7
  230. package/distribution/username-change/type.d.ts.map +0 -1
  231. /package/distribution/{username-change → account-activity}/type.js +0 -0
  232. /package/distribution/{username → account-export}/type.js +0 -0
@@ -133,6 +133,116 @@ export const accountMigrations = [
133
133
  create index account_session_account on account_session (account_id);
134
134
  `,
135
135
  },
136
+ {
137
+ name: "0004-identifier-name-picture-second-factor",
138
+ sql: `
139
+ alter table application rename constraint application_id_check to application_key_check;
140
+ alter table application rename column id to key;
141
+ alter table application drop constraint application_pkey;
142
+ alter table application add constraint application_key_key unique (key);
143
+ alter table application
144
+ add column id uuid primary key default uuidv7() check (uuid_extract_version(id) = 7);
145
+
146
+ alter table audit_event add column application_id uuid
147
+ references application (id) on update cascade on delete restrict;
148
+ alter table audit_event disable trigger audit_event_unchangeable;
149
+ update audit_event set application_id = application.id from application
150
+ where audit_event.application = application.key;
151
+ alter table audit_event enable trigger audit_event_unchangeable;
152
+ alter table audit_event drop column application;
153
+
154
+ create table identifier (
155
+ id uuid primary key check (uuid_extract_version(id) = 7),
156
+ value text not null
157
+ check (length(value) between 1 and 50 and value ~ '^[A-Za-z0-9]+([-_.][A-Za-z0-9]+)*$'),
158
+ account_id uuid unique references account (id) on update cascade on delete cascade,
159
+ created_at timestamptz not null default now(),
160
+ constraint identifier_owner_check check (num_nonnulls(account_id) = 1)
161
+ );
162
+ create unique index identifier_value_key on identifier (lower(value));
163
+ insert into identifier (id, value, account_id) select uuidv7(), username, id from account;
164
+
165
+ alter table account add column name text
166
+ check (length(name) between 1 and 50 and name = btrim(name) and name !~ '[[:cntrl:]]');
167
+ update account set name = username;
168
+ alter table account alter column name set not null;
169
+ alter table account drop column username;
170
+
171
+ create function account_require_identifier(target uuid) returns void
172
+ language plpgsql set search_path = public as $$
173
+ begin
174
+ if exists (select 1 from account where id = target)
175
+ and not exists (select 1 from identifier where account_id = target) then
176
+ raise exception 'an account needs an identifier' using errcode = '23514';
177
+ end if;
178
+ end $$;
179
+ create function account_identifier_after_insert() returns trigger
180
+ language plpgsql set search_path = public as $$
181
+ begin
182
+ perform account_require_identifier(new.id);
183
+ return null;
184
+ end $$;
185
+ create function identifier_after_change() returns trigger
186
+ language plpgsql set search_path = public as $$
187
+ begin
188
+ if old.account_id is not null then
189
+ perform account_require_identifier(old.account_id);
190
+ end if;
191
+ return null;
192
+ end $$;
193
+ create constraint trigger account_identifier after insert on account
194
+ deferrable initially deferred for each row execute function account_identifier_after_insert();
195
+ create constraint trigger identifier_owner after update or delete on identifier
196
+ deferrable initially deferred for each row execute function identifier_after_change();
197
+
198
+ alter table account drop constraint account_owner_id_fkey, add constraint account_owner_id_fkey
199
+ foreign key (owner_id) references account (id) on update cascade on delete cascade;
200
+ alter table contact drop constraint contact_account_id_fkey, add constraint contact_account_id_fkey
201
+ foreign key (account_id) references account (id) on update cascade on delete cascade;
202
+ alter table account_session drop constraint account_session_browser_session_id_fkey,
203
+ add constraint account_session_browser_session_id_fkey foreign key (browser_session_id)
204
+ references browser_session (id) on update cascade on delete cascade;
205
+ alter table account_session drop constraint account_session_account_id_fkey,
206
+ add constraint account_session_account_id_fkey
207
+ foreign key (account_id) references account (id) on update cascade on delete cascade;
208
+ delete from code_request where account_id is not null
209
+ and not exists (select 1 from account where account.id = code_request.account_id);
210
+ alter table code_request add constraint code_request_account_id_fkey
211
+ foreign key (account_id) references account (id) on update cascade on delete cascade;
212
+
213
+ create table account_picture (
214
+ id uuid primary key check (uuid_extract_version(id) = 7),
215
+ account_id uuid not null unique references account (id) on update cascade on delete cascade,
216
+ content_type text not null check (content_type in
217
+ ('image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/svg+xml')),
218
+ byte_size integer not null check (byte_size between 1 and 5242880),
219
+ width integer not null check (width between 1 and 4096),
220
+ height integer not null check (height between 1 and 4096),
221
+ created_at timestamptz not null default now()
222
+ );
223
+
224
+ create table totp_credential (
225
+ id uuid primary key check (uuid_extract_version(id) = 7),
226
+ account_id uuid not null unique references account (id) on update cascade on delete cascade,
227
+ secret_ciphertext text not null,
228
+ last_used_step bigint,
229
+ created_at timestamptz not null default now(),
230
+ confirmed_at timestamptz
231
+ );
232
+
233
+ create table recovery_code (
234
+ id uuid primary key check (uuid_extract_version(id) = 7),
235
+ account_id uuid not null references account (id) on update cascade on delete cascade,
236
+ code_hash text not null,
237
+ created_at timestamptz not null default now(),
238
+ used_at timestamptz,
239
+ unique (account_id, code_hash)
240
+ );
241
+
242
+ alter table browser_session add column user_agent text check (length(user_agent) <= 500);
243
+ alter table account_session add column step_up_at timestamptz;
244
+ `,
245
+ },
136
246
  ];
137
247
  /**
138
248
  * Creates or updates the account tables, tracked in their own migration table. Returns the names applied.
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,0CAA0C,CAAC;AAGnE,iHAAiH;AACjH,MAAM,CAAC,MAAM,uBAAuB,GAAG,mBAAmB,CAAC;AAE3D;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAgB;IAC5C;QACE,IAAI,EAAE,wCAAwC;QAC9C,GAAG,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA8EJ;KACF;IACD;QACE,IAAI,EAAE,mBAAmB;QACzB,GAAG,EAAE;;;;;;;;;;;;;;;;KAgBJ;KACF;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"}
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;IACD;QACE,IAAI,EAAE,4CAA4C;QAClD,GAAG,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA0GJ;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,13 @@
1
+ import type { AccountNameCheck } from "@sharadox/account-web-library/account-name/type";
2
+ /** The longest name, in characters (an emoji counts once). */
3
+ export declare const ACCOUNT_NAME_MAXIMUM_LENGTH = 50;
4
+ /**
5
+ * Checks the name an account shows (`Luis Pires`): any letters and symbols, 1 to 50 characters once trimmed and with
6
+ * runs of spaces made single, and no control characters (new lines, tabs, nulls).
7
+ *
8
+ * @example
9
+ * checkAccountName(" Luís Pires "); // { valid: true, name: "Luís Pires" }
10
+ * checkAccountName(""); // { valid: false, reason: "length" }
11
+ */
12
+ export declare function checkAccountName(name: string): AccountNameCheck;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-name/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iDAAiD,CAAC;AAExF,8DAA8D;AAC9D,eAAO,MAAM,2BAA2B,KAAK,CAAC;AAK9C;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,CAM/D"}
@@ -0,0 +1,22 @@
1
+ /** The longest name, in characters (an emoji counts once). */
2
+ export const ACCOUNT_NAME_MAXIMUM_LENGTH = 50;
3
+ // eslint-disable-next-line no-control-regex
4
+ const CONTROL = /[\u0000-\u001f\u007f-\u009f]/u;
5
+ /**
6
+ * Checks the name an account shows (`Luis Pires`): any letters and symbols, 1 to 50 characters once trimmed and with
7
+ * runs of spaces made single, and no control characters (new lines, tabs, nulls).
8
+ *
9
+ * @example
10
+ * checkAccountName(" Luís Pires "); // { valid: true, name: "Luís Pires" }
11
+ * checkAccountName(""); // { valid: false, reason: "length" }
12
+ */
13
+ export function checkAccountName(name) {
14
+ if (CONTROL.test(name))
15
+ return { valid: false, reason: "characters" };
16
+ const tidy = name.trim().replace(/ {2,}/g, " ");
17
+ const length = [...tidy].length;
18
+ if (length < 1 || length > ACCOUNT_NAME_MAXIMUM_LENGTH)
19
+ return { valid: false, reason: "length" };
20
+ return { valid: true, name: tidy };
21
+ }
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-name/index.ts"],"names":[],"mappings":"AAEA,8DAA8D;AAC9D,MAAM,CAAC,MAAM,2BAA2B,GAAG,EAAE,CAAC;AAE9C,4CAA4C;AAC5C,MAAM,OAAO,GAAG,+BAA+B,CAAC;AAEhD;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;IACtE,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;IAChD,MAAM,MAAM,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,MAAM,CAAC;IAChC,IAAI,MAAM,GAAG,CAAC,IAAI,MAAM,GAAG,2BAA2B;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;IAClG,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;AACrC,CAAC"}
@@ -0,0 +1,9 @@
1
+ /** A name that follows the rules (trimmed, single spaces), or why not. */
2
+ export type AccountNameCheck = {
3
+ valid: true;
4
+ name: string;
5
+ } | {
6
+ valid: false;
7
+ reason: "length" | "characters";
8
+ };
9
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-name/type.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,MAAM,MAAM,gBAAgB,GAAG;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,QAAQ,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/account-name/type.ts"],"names":[],"mappings":""}
@@ -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 { AccountNameChangeResult } from "@sharadox/account-web-library/account-name-change/type";
4
+ /**
5
+ * Changes the name an account shows (tidied by `checkAccountName`). The audit log records that it changed, not the
6
+ * names, which are personal data.
7
+ *
8
+ * @example
9
+ * await changeAccountName(database, { accountId, name: "Luis Pires" }); // { changed: true }
10
+ */
11
+ export declare function changeAccountName(database: Kysely<AccountDatabase>, { accountId, name }: {
12
+ accountId: string;
13
+ name: string;
14
+ }): Promise<AccountNameChangeResult>;
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-name-change/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,uBAAuB,EAAE,MAAM,wDAAwD,CAAC;AAGtG;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACvD,OAAO,CAAC,uBAAuB,CAAC,CAalC"}
@@ -0,0 +1,26 @@
1
+ import { checkAccountName } from "@sharadox/account-web-library/account-name";
2
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
3
+ /**
4
+ * Changes the name an account shows (tidied by `checkAccountName`). The audit log records that it changed, not the
5
+ * names, which are personal data.
6
+ *
7
+ * @example
8
+ * await changeAccountName(database, { accountId, name: "Luis Pires" }); // { changed: true }
9
+ */
10
+ export async function changeAccountName(database, { accountId, name }) {
11
+ const check = checkAccountName(name);
12
+ if (!check.valid)
13
+ return { changed: false, reason: "name-invalid" };
14
+ return database.transaction().execute(async (transaction) => {
15
+ const updated = await transaction
16
+ .updateTable("account")
17
+ .set({ name: check.name })
18
+ .where("id", "=", accountId)
19
+ .executeTakeFirst();
20
+ if (updated.numUpdatedRows === 0n)
21
+ return { changed: false, reason: "no-account" };
22
+ await recordAuditEvent(transaction, { accountId, action: "account.name-changed" });
23
+ return { changed: true };
24
+ });
25
+ }
26
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-name-change/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAE9E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAE7E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,QAAiC,EACjC,EAAE,SAAS,EAAE,IAAI,EAAuC;IAExD,MAAM,KAAK,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;IACrC,IAAI,CAAC,KAAK,CAAC,KAAK;QAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,CAAC;IACpE,OAAO,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAoC,EAAE;QAC5F,MAAM,OAAO,GAAG,MAAM,WAAW;aAC9B,WAAW,CAAC,SAAS,CAAC;aACtB,GAAG,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC;aACzB,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;aAC3B,gBAAgB,EAAE,CAAC;QACtB,IAAI,OAAO,CAAC,cAAc,KAAK,EAAE;YAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;QACnF,MAAM,gBAAgB,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,sBAAsB,EAAE,CAAC,CAAC;QACnF,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC3B,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,7 @@
1
+ export type AccountNameChangeResult = {
2
+ changed: true;
3
+ } | {
4
+ changed: false;
5
+ reason: "name-invalid" | "no-account";
6
+ };
7
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-name-change/type.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,uBAAuB,GAAG;IAAE,OAAO,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,cAAc,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/account-name-change/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,30 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
3
+ import type { AccountPictureChangeResult } from "@sharadox/account-web-library/account-picture-change/type";
4
+ import type { PictureStorage } from "@sharadox/account-web-library/picture-storage/type";
5
+ /**
6
+ * Sets an account's picture from an uploaded file (checked and made safe by `preparePicture`): the new file is
7
+ * stored first, then the database points to it, then the old file is deleted, so the account is never left without
8
+ * a working picture and replaced files never stay behind.
9
+ *
10
+ * @example
11
+ * const result = await setAccountPicture(database, storage, { accountId, bytes });
12
+ * if (result.changed) revalidate(result.pictureId);
13
+ */
14
+ export declare function setAccountPicture(database: Kysely<AccountDatabase>, storage: PictureStorage, { accountId, bytes, now }: {
15
+ accountId: string;
16
+ bytes: Uint8Array;
17
+ now?: number;
18
+ }): Promise<AccountPictureChangeResult>;
19
+ /**
20
+ * Removes an account's picture and deletes its file.
21
+ *
22
+ * @example
23
+ * await removeAccountPicture(database, storage, { accountId }); // { removed: true }
24
+ */
25
+ export declare function removeAccountPicture(database: Kysely<AccountDatabase>, storage: PictureStorage, { accountId }: {
26
+ accountId: string;
27
+ }): Promise<{
28
+ removed: boolean;
29
+ }>;
30
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-picture-change/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,0BAA0B,EAAE,MAAM,2DAA2D,CAAC;AAI5G,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oDAAoD,CAAC;AAEzF;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,OAAO,EAAE,cAAc,EACvB,EAAE,SAAS,EAAE,KAAK,EAAE,GAAgB,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,UAAU,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GAC7F,OAAO,CAAC,0BAA0B,CAAC,CA8BrC;AAED;;;;;GAKG;AACH,wBAAsB,oBAAoB,CACxC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,OAAO,EAAE,cAAc,EACvB,EAAE,SAAS,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,GACnC,OAAO,CAAC;IAAE,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,CAa/B"}
@@ -0,0 +1,70 @@
1
+ import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
2
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
3
+ import { pictureKey } from "@sharadox/account-web-library/picture-key";
4
+ import { preparePicture } from "@sharadox/account-web-library/picture-preparation";
5
+ /**
6
+ * Sets an account's picture from an uploaded file (checked and made safe by `preparePicture`): the new file is
7
+ * stored first, then the database points to it, then the old file is deleted, so the account is never left without
8
+ * a working picture and replaced files never stay behind.
9
+ *
10
+ * @example
11
+ * const result = await setAccountPicture(database, storage, { accountId, bytes });
12
+ * if (result.changed) revalidate(result.pictureId);
13
+ */
14
+ export async function setAccountPicture(database, storage, { accountId, bytes, now = Date.now() }) {
15
+ const prepared = await preparePicture(bytes);
16
+ if (!prepared.valid)
17
+ return { changed: false, reason: prepared.reason };
18
+ const account = await database.selectFrom("account").select("id").where("id", "=", accountId).executeTakeFirst();
19
+ if (!account)
20
+ return { changed: false, reason: "no-account" };
21
+ const pictureId = uuidVersion7({ now });
22
+ await storage.put(pictureKey(pictureId), prepared.bytes, "image/webp");
23
+ const old = await database.transaction().execute(async (transaction) => {
24
+ const previous = await transaction
25
+ .deleteFrom("account_picture")
26
+ .where("account_id", "=", accountId)
27
+ .returning("id")
28
+ .executeTakeFirst();
29
+ await transaction
30
+ .insertInto("account_picture")
31
+ .values({
32
+ id: pictureId,
33
+ account_id: accountId,
34
+ content_type: "image/webp",
35
+ byte_size: prepared.bytes.length,
36
+ width: prepared.width,
37
+ height: prepared.height,
38
+ created_at: new Date(now),
39
+ })
40
+ .execute();
41
+ await recordAuditEvent(transaction, { accountId, action: "account.picture-changed", detail: { pictureId }, now });
42
+ return previous;
43
+ });
44
+ if (old)
45
+ await storage.remove(pictureKey(old.id));
46
+ return { changed: true, pictureId };
47
+ }
48
+ /**
49
+ * Removes an account's picture and deletes its file.
50
+ *
51
+ * @example
52
+ * await removeAccountPicture(database, storage, { accountId }); // { removed: true }
53
+ */
54
+ export async function removeAccountPicture(database, storage, { accountId }) {
55
+ const removed = await database.transaction().execute(async (transaction) => {
56
+ const picture = await transaction
57
+ .deleteFrom("account_picture")
58
+ .where("account_id", "=", accountId)
59
+ .returning("id")
60
+ .executeTakeFirst();
61
+ if (picture)
62
+ await recordAuditEvent(transaction, { accountId, action: "account.picture-removed" });
63
+ return picture;
64
+ });
65
+ if (!removed)
66
+ return { removed: false };
67
+ await storage.remove(pictureKey(removed.id));
68
+ return { removed: true };
69
+ }
70
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-picture-change/index.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,11 @@
1
+ import type { PreparedPicture } from "@sharadox/account-web-library/picture-preparation/type";
2
+ export type AccountPictureChangeResult = {
3
+ changed: true;
4
+ pictureId: string;
5
+ } | {
6
+ changed: false;
7
+ reason: Extract<PreparedPicture, {
8
+ valid: false;
9
+ }>["reason"] | "no-account";
10
+ };
11
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-picture-change/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wDAAwD,CAAC;AAE9F,MAAM,MAAM,0BAA0B,GAClC;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GACpC;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,OAAO,CAAC,eAAe,EAAE;QAAE,KAAK,EAAE,KAAK,CAAA;KAAE,CAAC,CAAC,QAAQ,CAAC,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/account-picture-change/type.ts"],"names":[],"mappings":""}
@@ -1,8 +1,10 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
1
3
  import type { AccountServer, SignInLimits } from "@sharadox/account-web-library/account-server/type";
2
4
  /**
3
5
  * The limits used when a company's configuration doesn't set them: 3 codes per contact then one per 5 minutes; 10
4
6
  * per address group then one a minute; 200 for everyone then one a second; 20 typed codes per address group then
5
- * one per 30 seconds; 2,000 sends per channel per UTC day; proof of work of 14 bits.
7
+ * one per 30 seconds; 5 two-factor codes per account then one a minute; 2,000 sends per channel per UTC day; proof of work of 14 bits.
6
8
  */
7
9
  export declare const DEFAULT_SIGN_IN_LIMITS: SignInLimits;
8
10
  /**
@@ -12,4 +14,12 @@ export declare const DEFAULT_SIGN_IN_LIMITS: SignInLimits;
12
14
  * const { perContact } = signInLimits(server);
13
15
  */
14
16
  export declare function signInLimits(server: AccountServer): SignInLimits;
17
+ /**
18
+ * The server's database seen through the account tables alone, for helpers that take `Kysely<AccountDatabase>`
19
+ * (its transactions then have that type too).
20
+ *
21
+ * @example
22
+ * await accountTables(server).transaction().execute((transaction) => removeTotp(transaction, { accountId }));
23
+ */
24
+ export declare function accountTables(server: AccountServer): Kysely<AccountDatabase>;
15
25
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-server/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,mDAAmD,CAAC;AAErG;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,EAAE,YAOpC,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,aAAa,GAAG,YAAY,CAEhE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-server/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAC3F,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,mDAAmD,CAAC;AAErG;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,EAAE,YAQpC,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,aAAa,GAAG,YAAY,CAEhE;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,MAAM,CAAC,eAAe,CAAC,CAE5E"}
@@ -1,13 +1,14 @@
1
1
  /**
2
2
  * The limits used when a company's configuration doesn't set them: 3 codes per contact then one per 5 minutes; 10
3
3
  * per address group then one a minute; 200 for everyone then one a second; 20 typed codes per address group then
4
- * one per 30 seconds; 2,000 sends per channel per UTC day; proof of work of 14 bits.
4
+ * one per 30 seconds; 5 two-factor codes per account then one a minute; 2,000 sends per channel per UTC day; proof of work of 14 bits.
5
5
  */
6
6
  export const DEFAULT_SIGN_IN_LIMITS = {
7
7
  perContact: { capacity: 3, refillPerSecond: 1 / 300 },
8
8
  perAddress: { capacity: 10, refillPerSecond: 1 / 60 },
9
9
  global: { capacity: 200, refillPerSecond: 1 },
10
10
  verificationsPerAddress: { capacity: 20, refillPerSecond: 1 / 30 },
11
+ secondFactorPerAccount: { capacity: 5, refillPerSecond: 1 / 60 },
11
12
  dailySendsPerChannel: 2_000,
12
13
  difficulty: 14,
13
14
  };
@@ -20,4 +21,14 @@ export const DEFAULT_SIGN_IN_LIMITS = {
20
21
  export function signInLimits(server) {
21
22
  return { ...DEFAULT_SIGN_IN_LIMITS, ...server.limits };
22
23
  }
24
+ /**
25
+ * The server's database seen through the account tables alone, for helpers that take `Kysely<AccountDatabase>`
26
+ * (its transactions then have that type too).
27
+ *
28
+ * @example
29
+ * await accountTables(server).transaction().execute((transaction) => removeTotp(transaction, { accountId }));
30
+ */
31
+ export function accountTables(server) {
32
+ return server.database;
33
+ }
23
34
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-server/index.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAiB;IAClD,UAAU,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,eAAe,EAAE,CAAC,GAAG,GAAG,EAAE;IACrD,UAAU,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,eAAe,EAAE,CAAC,GAAG,EAAE,EAAE;IACrD,MAAM,EAAE,EAAE,QAAQ,EAAE,GAAG,EAAE,eAAe,EAAE,CAAC,EAAE;IAC7C,uBAAuB,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,eAAe,EAAE,CAAC,GAAG,EAAE,EAAE;IAClE,oBAAoB,EAAE,KAAK;IAC3B,UAAU,EAAE,EAAE;CACf,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,MAAqB;IAChD,OAAO,EAAE,GAAG,sBAAsB,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;AACzD,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-server/index.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAiB;IAClD,UAAU,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,eAAe,EAAE,CAAC,GAAG,GAAG,EAAE;IACrD,UAAU,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,eAAe,EAAE,CAAC,GAAG,EAAE,EAAE;IACrD,MAAM,EAAE,EAAE,QAAQ,EAAE,GAAG,EAAE,eAAe,EAAE,CAAC,EAAE;IAC7C,uBAAuB,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,eAAe,EAAE,CAAC,GAAG,EAAE,EAAE;IAClE,sBAAsB,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,eAAe,EAAE,CAAC,GAAG,EAAE,EAAE;IAChE,oBAAoB,EAAE,KAAK;IAC3B,UAAU,EAAE,EAAE;CACf,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,MAAqB;IAChD,OAAO,EAAE,GAAG,sBAAsB,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;AACzD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,MAAqB;IACjD,OAAO,MAAM,CAAC,QAAQ,CAAC;AACzB,CAAC"}
@@ -12,6 +12,8 @@ export interface SignInLimits {
12
12
  global: TokenBucketRule;
13
13
  /** Codes typed from one address group. */
14
14
  verificationsPerAddress: TokenBucketRule;
15
+ /** Two-factor codes typed for one account. */
16
+ secondFactorPerAccount: TokenBucketRule;
15
17
  /** The most codes each channel may send in a UTC day; past it, sending pauses (never spends). */
16
18
  dailySendsPerChannel: number;
17
19
  /** Leading zero bits of the proof of work. */
@@ -1 +1 @@
1
- {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-server/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oDAAoD,CAAC;AAC1F,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,qDAAqD,CAAC;AACjG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oDAAoD,CAAC;AAEzF,sGAAsG;AACtG,MAAM,WAAW,YAAY;IAC3B,iCAAiC;IACjC,UAAU,EAAE,eAAe,CAAC;IAC5B,6EAA6E;IAC7E,UAAU,EAAE,eAAe,CAAC;IAC5B,4CAA4C;IAC5C,MAAM,EAAE,eAAe,CAAC;IACxB,0CAA0C;IAC1C,uBAAuB,EAAE,eAAe,CAAC;IACzC,iGAAiG;IACjG,oBAAoB,EAAE,MAAM,CAAC;IAC7B,8CAA8C;IAC9C,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,gHAAgH;AAChH,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,MAAM,CAAC,qBAAqB,CAAC,CAAC;IACxC,QAAQ,EAAE,cAAc,EAAE,CAAC;IAC3B,qEAAqE;IACrE,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;CAChC"}
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-server/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oDAAoD,CAAC;AAC1F,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,qDAAqD,CAAC;AACjG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oDAAoD,CAAC;AAEzF,sGAAsG;AACtG,MAAM,WAAW,YAAY;IAC3B,iCAAiC;IACjC,UAAU,EAAE,eAAe,CAAC;IAC5B,6EAA6E;IAC7E,UAAU,EAAE,eAAe,CAAC;IAC5B,4CAA4C;IAC5C,MAAM,EAAE,eAAe,CAAC;IACxB,0CAA0C;IAC1C,uBAAuB,EAAE,eAAe,CAAC;IACzC,8CAA8C;IAC9C,sBAAsB,EAAE,eAAe,CAAC;IACxC,iGAAiG;IACjG,oBAAoB,EAAE,MAAM,CAAC;IAC7B,8CAA8C;IAC9C,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,gHAAgH;AAChH,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,MAAM,CAAC,qBAAqB,CAAC,CAAC;IACxC,QAAQ,EAAE,cAAc,EAAE,CAAC;IAC3B,qEAAqE;IACrE,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;CAChC"}
@@ -0,0 +1,15 @@
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 one device (from its list of devices); only its own sessions can be ended.
5
+ *
6
+ * @example
7
+ * await endAccountSession(database, { accountId, accountSessionId }); // { ended: true }
8
+ */
9
+ export declare function endAccountSession(database: Kysely<AccountDatabase>, { accountId, accountSessionId }: {
10
+ accountId: string;
11
+ accountSessionId: string;
12
+ }): Promise<{
13
+ ended: boolean;
14
+ }>;
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-session-end/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAG3F;;;;;GAKG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,gBAAgB,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,gBAAgB,EAAE,MAAM,CAAA;CAAE,GAC/E,OAAO,CAAC;IAAE,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC,CAW7B"}
@@ -0,0 +1,21 @@
1
+ import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
2
+ /**
3
+ * Signs an account out on one device (from its list of devices); only its own sessions can be ended.
4
+ *
5
+ * @example
6
+ * await endAccountSession(database, { accountId, accountSessionId }); // { ended: true }
7
+ */
8
+ export async function endAccountSession(database, { accountId, accountSessionId }) {
9
+ return database.transaction().execute(async (transaction) => {
10
+ const ended = await transaction
11
+ .deleteFrom("account_session")
12
+ .where("id", "=", accountSessionId)
13
+ .where("account_id", "=", accountId)
14
+ .executeTakeFirst();
15
+ if (ended.numDeletedRows === 0n)
16
+ return { ended: false };
17
+ await recordAuditEvent(transaction, { accountId, action: "session.ended", detail: { accountSessionId } });
18
+ return { ended: true };
19
+ });
20
+ }
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-session-end/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAE7E;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,QAAiC,EACjC,EAAE,SAAS,EAAE,gBAAgB,EAAmD;IAEhF,OAAO,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;QAC1D,MAAM,KAAK,GAAG,MAAM,WAAW;aAC5B,UAAU,CAAC,iBAAiB,CAAC;aAC7B,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,gBAAgB,CAAC;aAClC,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,SAAS,CAAC;aACnC,gBAAgB,EAAE,CAAC;QACtB,IAAI,KAAK,CAAC,cAAc,KAAK,EAAE;YAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;QACzD,MAAM,gBAAgB,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,EAAE,gBAAgB,EAAE,EAAE,CAAC,CAAC;QAC1G,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;IACzB,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -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 { AccountDevice } from "@sharadox/account-web-library/account-session-list/type";
4
+ /**
5
+ * The devices an account is signed in on, most recently seen first, without sessions that ended; the browser whose
6
+ * token is given is marked `current`.
7
+ *
8
+ * @example
9
+ * const devices = await listAccountSessions(database, { accountId, token: cookies.get("__Host-account")?.value });
10
+ */
11
+ export declare function listAccountSessions(database: Kysely<AccountDatabase>, { accountId, token, now }: {
12
+ accountId: string;
13
+ token: string | undefined;
14
+ now?: number;
15
+ }): Promise<AccountDevice[]>;
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-session-list/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAC3F,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yDAAyD,CAAC;AAG7F;;;;;;GAMG;AACH,wBAAsB,mBAAmB,CACvC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,KAAK,EAAE,GAAgB,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GACrG,OAAO,CAAC,aAAa,EAAE,CAAC,CAuB1B"}
@@ -0,0 +1,33 @@
1
+ import { hashSessionToken } from "@sharadox/account-web-library/session-token";
2
+ /**
3
+ * The devices an account is signed in on, most recently seen first, without sessions that ended; the browser whose
4
+ * token is given is marked `current`.
5
+ *
6
+ * @example
7
+ * const devices = await listAccountSessions(database, { accountId, token: cookies.get("__Host-account")?.value });
8
+ */
9
+ export async function listAccountSessions(database, { accountId, token, now = Date.now() }) {
10
+ const asking = token ? hashSessionToken(token) : "";
11
+ const rows = await database
12
+ .selectFrom("account_session")
13
+ .innerJoin("browser_session", "browser_session.id", "account_session.browser_session_id")
14
+ .select([
15
+ "account_session.id as accountSessionId",
16
+ "browser_session.user_agent as userAgent",
17
+ "account_session.created_at as signedInAt",
18
+ "account_session.last_seen_at as lastSeenAt",
19
+ "account_session.second_factor_at as secondFactorAt",
20
+ "browser_session.token_hash as tokenHash",
21
+ ])
22
+ .where("account_session.account_id", "=", accountId)
23
+ .where("account_session.expires_at", ">", new Date(now))
24
+ .orderBy("account_session.last_seen_at", "desc")
25
+ .orderBy("account_session.id", "desc")
26
+ .execute();
27
+ return rows.map(({ secondFactorAt, tokenHash, ...device }) => ({
28
+ ...device,
29
+ secondFactor: secondFactorAt !== null,
30
+ current: tokenHash === asking,
31
+ }));
32
+ }
33
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-session-list/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,6CAA6C,CAAC;AAE/E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,QAAiC,EACjC,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAkE;IAEtG,MAAM,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IACpD,MAAM,IAAI,GAAG,MAAM,QAAQ;SACxB,UAAU,CAAC,iBAAiB,CAAC;SAC7B,SAAS,CAAC,iBAAiB,EAAE,oBAAoB,EAAE,oCAAoC,CAAC;SACxF,MAAM,CAAC;QACN,wCAAwC;QACxC,yCAAyC;QACzC,0CAA0C;QAC1C,4CAA4C;QAC5C,oDAAoD;QACpD,yCAAyC;KAC1C,CAAC;SACD,KAAK,CAAC,4BAA4B,EAAE,GAAG,EAAE,SAAS,CAAC;SACnD,KAAK,CAAC,4BAA4B,EAAE,GAAG,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;SACvD,OAAO,CAAC,8BAA8B,EAAE,MAAM,CAAC;SAC/C,OAAO,CAAC,oBAAoB,EAAE,MAAM,CAAC;SACrC,OAAO,EAAE,CAAC;IACb,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,cAAc,EAAE,SAAS,EAAE,GAAG,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;QAC7D,GAAG,MAAM;QACT,YAAY,EAAE,cAAc,KAAK,IAAI;QACrC,OAAO,EAAE,SAAS,KAAK,MAAM;KAC9B,CAAC,CAAC,CAAC;AACN,CAAC"}
@@ -0,0 +1,13 @@
1
+ /** One device (browser) the account is signed in on. */
2
+ export interface AccountDevice {
3
+ accountSessionId: string;
4
+ /** The browser's description when it signed in; null when unknown. */
5
+ userAgent: string | null;
6
+ signedInAt: Date;
7
+ lastSeenAt: Date;
8
+ /** Whether two-factor was passed in this session. */
9
+ secondFactor: boolean;
10
+ /** Whether it is the browser asking. */
11
+ current: boolean;
12
+ }
13
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-session-list/type.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,MAAM,WAAW,aAAa;IAC5B,gBAAgB,EAAE,MAAM,CAAC;IACzB,sEAAsE;IACtE,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,UAAU,EAAE,IAAI,CAAC;IACjB,UAAU,EAAE,IAAI,CAAC;IACjB,qDAAqD;IACrD,YAAY,EAAE,OAAO,CAAC;IACtB,wCAAwC;IACxC,OAAO,EAAE,OAAO,CAAC;CAClB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type.js.map