@feltdb/core 0.13.0 → 0.15.0

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 (92) hide show
  1. package/dist/agent-runtime.d.ts +64 -2
  2. package/dist/agent-runtime.d.ts.map +1 -1
  3. package/dist/agent-runtime.js +121 -11
  4. package/dist/agent-state.d.ts +48 -0
  5. package/dist/agent-state.d.ts.map +1 -1
  6. package/dist/agent-state.js +174 -12
  7. package/dist/agent.d.ts +10 -1
  8. package/dist/agent.d.ts.map +1 -1
  9. package/dist/agent.js +10 -1
  10. package/dist/build-identity.json +4 -4
  11. package/dist/cli/commands.js +27 -6
  12. package/dist/collection.d.ts +3 -1
  13. package/dist/collection.d.ts.map +1 -1
  14. package/dist/collection.js +30 -8
  15. package/dist/create/build-identity.json +4 -4
  16. package/dist/create/cli.js +1 -1
  17. package/dist/create/create.js +4 -6
  18. package/dist/create/package-versions.js +1 -1
  19. package/dist/create/server-source/Cargo.lock +6 -6
  20. package/dist/create/server-source/Cargo.toml +1 -1
  21. package/dist/create/server-source/crates/feltdb/src/lib.rs +126 -35
  22. package/dist/create/server-source/crates/feltdb/src/state_contract.rs +162 -53
  23. package/dist/create/server-source/crates/feltdb/src/worker_mesh.rs +137 -1
  24. package/dist/create/server-source/crates/feltdb/src/workload.rs +1427 -91
  25. package/dist/create/server-source/crates/feltdb-client/Cargo.toml +1 -1
  26. package/dist/create/server-source/crates/feltdb-server/Cargo.toml +1 -1
  27. package/dist/create/server-source/crates/feltdb-server/README.md +6 -1
  28. package/dist/create/server-source/crates/feltdb-server/src/app_state.rs +9 -1
  29. package/dist/create/server-source/crates/feltdb-server/src/auth.rs +85 -25
  30. package/dist/create/server-source/crates/feltdb-server/src/backup.rs +4 -0
  31. package/dist/create/server-source/crates/feltdb-server/src/console_accounts.rs +1009 -0
  32. package/dist/create/server-source/crates/feltdb-server/src/control_authorization.rs +441 -0
  33. package/dist/create/server-source/crates/feltdb-server/src/identity.rs +68 -6
  34. package/dist/create/server-source/crates/feltdb-server/src/key_management.rs +355 -23
  35. package/dist/create/server-source/crates/feltdb-server/src/lib.rs +3 -0
  36. package/dist/create/server-source/crates/feltdb-server/src/main.rs +879 -84
  37. package/dist/create/server-source/crates/feltdb-server/src/redaction.rs +208 -0
  38. package/dist/create/server-source/crates/feltdb-server/src/tenancy.rs +198 -19
  39. package/dist/create/server-source/crates/feltdb-server/tests/transaction_precondition_contract.rs +403 -0
  40. package/dist/db.d.ts +64 -9
  41. package/dist/db.d.ts.map +1 -1
  42. package/dist/db.js +153 -27
  43. package/dist/deployment.d.ts +19 -0
  44. package/dist/deployment.d.ts.map +1 -1
  45. package/dist/deployment.js +38 -2
  46. package/dist/embedded-query.d.ts.map +1 -1
  47. package/dist/embedded-query.js +5 -4
  48. package/dist/embedded-transaction.d.ts +8 -4
  49. package/dist/embedded-transaction.d.ts.map +1 -1
  50. package/dist/embedded-transaction.js +55 -19
  51. package/dist/error-codes.d.ts +31 -2
  52. package/dist/error-codes.d.ts.map +1 -1
  53. package/dist/error-codes.js +41 -2
  54. package/dist/feltdb.d.ts +7 -0
  55. package/dist/feltdb.d.ts.map +1 -1
  56. package/dist/file-db.d.ts +27 -0
  57. package/dist/file-db.d.ts.map +1 -1
  58. package/dist/file-db.js +145 -7
  59. package/dist/http-client.d.ts +11 -3
  60. package/dist/http-client.d.ts.map +1 -1
  61. package/dist/http-client.js +27 -14
  62. package/dist/http-db.d.ts +27 -1
  63. package/dist/http-db.d.ts.map +1 -1
  64. package/dist/http-db.js +252 -54
  65. package/dist/index.js +1 -1
  66. package/dist/local-state-lock.d.ts +34 -0
  67. package/dist/local-state-lock.d.ts.map +1 -0
  68. package/dist/local-state-lock.js +225 -0
  69. package/dist/reactive-graph.d.ts +0 -3
  70. package/dist/reactive-graph.d.ts.map +1 -1
  71. package/dist/reactive-graph.js +11 -1
  72. package/dist/runtime-registry.d.ts +7 -2
  73. package/dist/runtime-registry.d.ts.map +1 -1
  74. package/dist/runtime-registry.js +2 -2
  75. package/dist/studio/components/index.js +1 -1
  76. package/dist/studio/{components-BSGmHfif.js → components-CJmzntBi.js} +2 -1
  77. package/dist/studio/index.js +1 -1
  78. package/dist/studio-app/assets/{feltdb_wasm-3hDngl1h.js → feltdb_wasm-BjM1ylL3.js} +1 -1
  79. package/dist/studio-app/assets/feltdb_wasm_bg-TseJn2Ax.wasm +0 -0
  80. package/dist/studio-app/assets/index-1wd0uKua.js +30 -0
  81. package/dist/studio-app/index.html +1 -1
  82. package/dist/transaction.d.ts +27 -11
  83. package/dist/transaction.d.ts.map +1 -1
  84. package/dist/transaction.js +20 -14
  85. package/dist/wasm/feltdb_wasm_bg.wasm +0 -0
  86. package/dist/wasm/package.json +1 -1
  87. package/dist/workload.d.ts +31 -3
  88. package/dist/workload.d.ts.map +1 -1
  89. package/dist/workload.js +28 -3
  90. package/package.json +1 -1
  91. package/dist/studio-app/assets/feltdb_wasm_bg-CFQT4gCy.wasm +0 -0
  92. package/dist/studio-app/assets/index-Dql1hS5m.js +0 -30
@@ -0,0 +1,1009 @@
1
+ //! Console (Studio / www.feltdb.com) accounts.
2
+ //!
3
+ //! Before 0.15.0 the website signed a console session for any e-mail address
4
+ //! with any password, and derived the user id from a prefix of the identity,
5
+ //! so two identities could share one platform user. This store is the account
6
+ //! authority the website now has to ask: it verifies passwords (Argon2), binds
7
+ //! GitHub sign-in to the numeric GitHub id, and hands out random user ids.
8
+ //!
9
+ //! Only the website may call the account routes. Every request is signed with
10
+ //! HMAC-SHA256 under `FELTDB_SESSION_SECRET`, the secret the website and the
11
+ //! control plane already share for console session cookies
12
+ //! (see [`verify_console_request`]).
13
+ //!
14
+ //! Persisted in its own JSON file beside the data (`<data>.console-accounts.json`),
15
+ //! written to a temporary file and renamed, owner-only (0600).
16
+
17
+ use crate::tenancy::TenancyStore;
18
+ use argon2::{Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
19
+ use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
20
+ use hmac::{Hmac, Mac};
21
+ use password_hash::{rand_core::OsRng, SaltString};
22
+ use rand::{distributions::Alphanumeric, Rng};
23
+ use serde::{Deserialize, Serialize};
24
+ use sha2::Sha256;
25
+ use std::{
26
+ fs,
27
+ io::Write,
28
+ path::PathBuf,
29
+ sync::{Arc, OnceLock, RwLock},
30
+ time::{SystemTime, UNIX_EPOCH},
31
+ };
32
+
33
+ /// Minimum password length for console password accounts.
34
+ pub const MINIMUM_PASSWORD_LENGTH: usize = 12;
35
+ /// Maximum accepted clock skew between the website and the control plane.
36
+ pub const MAXIMUM_SIGNATURE_SKEW_SECONDS: u64 = 300;
37
+ pub const SIGNATURE_HEADER: &str = "x-feltdb-console-signature";
38
+ pub const TIMESTAMP_HEADER: &str = "x-feltdb-console-timestamp";
39
+ const DEVELOPMENT_SECRETS: &[&str] = &[
40
+ "feltdb-development-session-secret-change-me",
41
+ "change-me-in-production",
42
+ ];
43
+
44
+ #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
45
+ #[serde(rename_all = "snake_case")]
46
+ pub enum ConsoleProvider {
47
+ Password,
48
+ Github,
49
+ }
50
+
51
+ #[derive(Debug, Clone, Serialize, Deserialize)]
52
+ pub struct ConsoleAccount {
53
+ /// Normalized e-mail address, or `github:<numeric id>`.
54
+ pub identity: String,
55
+ pub user_id: String,
56
+ #[serde(default, skip_serializing_if = "Option::is_none")]
57
+ pub password_hash: Option<String>,
58
+ pub provider: ConsoleProvider,
59
+ pub created_at: u64,
60
+ /// The identity the console shows and signs into the session cookie: the
61
+ /// e-mail address, or `@<login>` (the most recent GitHub login seen).
62
+ pub display_identity: String,
63
+ /// How the account came to own its user id: `new`, `legacy_github_login`
64
+ /// (continuity binding of a pre-0.15.0 GitHub user) or `operator_claim`.
65
+ #[serde(default = "default_origin")]
66
+ pub origin: String,
67
+ }
68
+ fn default_origin() -> String {
69
+ "new".into()
70
+ }
71
+
72
+ #[derive(Debug, Default, Serialize, Deserialize)]
73
+ struct Records {
74
+ accounts: Vec<ConsoleAccount>,
75
+ }
76
+
77
+ /// A failed account operation, with the wire code the website shows.
78
+ #[derive(Debug, Clone, PartialEq, Eq)]
79
+ pub struct AccountError {
80
+ pub status: u16,
81
+ pub code: &'static str,
82
+ pub message: String,
83
+ }
84
+ impl AccountError {
85
+ fn new(status: u16, code: &'static str, message: impl Into<String>) -> Self {
86
+ Self {
87
+ status,
88
+ code,
89
+ message: message.into(),
90
+ }
91
+ }
92
+ fn internal(message: impl Into<String>) -> Self {
93
+ Self::new(500, "EXECUTION_FAILED", message)
94
+ }
95
+ }
96
+
97
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
98
+ pub struct ResolvedAccount {
99
+ pub user_id: String,
100
+ pub identity: String,
101
+ pub display_identity: String,
102
+ }
103
+ impl From<&ConsoleAccount> for ResolvedAccount {
104
+ fn from(account: &ConsoleAccount) -> Self {
105
+ Self {
106
+ user_id: account.user_id.clone(),
107
+ identity: account.identity.clone(),
108
+ display_identity: account.display_identity.clone(),
109
+ }
110
+ }
111
+ }
112
+
113
+ #[derive(Clone)]
114
+ pub struct ConsoleAccountStore {
115
+ path: PathBuf,
116
+ records: Arc<RwLock<Records>>,
117
+ /// Serializes read-modify-write operations that span the Argon2 work.
118
+ writer: Arc<std::sync::Mutex<()>>,
119
+ /// Modification time of the file as last loaded or written, so a change
120
+ /// made by another process (the operator `console-accounts claim` CLI) is
121
+ /// picked up instead of being overwritten.
122
+ loaded: Arc<std::sync::Mutex<Option<SystemTime>>>,
123
+ secret: Option<Arc<[u8]>>,
124
+ }
125
+
126
+ fn now() -> u64 {
127
+ SystemTime::now()
128
+ .duration_since(UNIX_EPOCH)
129
+ .unwrap_or_default()
130
+ .as_secs()
131
+ }
132
+
133
+ /// A new console user id: `user_` and 24 random alphanumerics (about 142 bits).
134
+ pub fn new_user_id() -> String {
135
+ let random: String = rand::thread_rng()
136
+ .sample_iter(&Alphanumeric)
137
+ .take(24)
138
+ .map(char::from)
139
+ .collect();
140
+ format!("user_{random}")
141
+ }
142
+
143
+ /// The user id the pre-0.15.0 website derived from an identity:
144
+ /// `'user_' + base64url(lowercase(identity)).slice(0, 18)`.
145
+ pub fn legacy_user_id(identity: &str) -> String {
146
+ let normalized = identity.trim().to_lowercase();
147
+ format!(
148
+ "user_{}",
149
+ URL_SAFE_NO_PAD
150
+ .encode(normalized)
151
+ .chars()
152
+ .take(18)
153
+ .collect::<String>()
154
+ )
155
+ }
156
+
157
+ pub fn normalize_email(email: &str) -> Result<String, AccountError> {
158
+ let email = email.trim().to_lowercase();
159
+ let valid = email.len() <= 254
160
+ && !email.chars().any(char::is_whitespace)
161
+ && email.split_once('@').is_some_and(|(local, domain)| {
162
+ !local.is_empty()
163
+ && !domain.contains('@')
164
+ && domain
165
+ .split_once('.')
166
+ .is_some_and(|(left, right)| !left.is_empty() && !right.is_empty())
167
+ });
168
+ if valid {
169
+ Ok(email)
170
+ } else {
171
+ Err(AccountError::new(
172
+ 400,
173
+ "VALIDATION_ERROR",
174
+ "enter a valid email address",
175
+ ))
176
+ }
177
+ }
178
+
179
+ fn validate_password(password: &str) -> Result<(), AccountError> {
180
+ if password.chars().count() < MINIMUM_PASSWORD_LENGTH {
181
+ return Err(AccountError::new(
182
+ 400,
183
+ "PASSWORD_TOO_SHORT",
184
+ format!("password must be at least {MINIMUM_PASSWORD_LENGTH} characters"),
185
+ ));
186
+ }
187
+ if password.len() > 1024 {
188
+ return Err(AccountError::new(
189
+ 400,
190
+ "VALIDATION_ERROR",
191
+ "password must be at most 1024 bytes",
192
+ ));
193
+ }
194
+ Ok(())
195
+ }
196
+
197
+ fn hash_password(password: &str) -> Result<String, AccountError> {
198
+ Argon2::default()
199
+ .hash_password(password.as_bytes(), &SaltString::generate(&mut OsRng))
200
+ .map(|hash| hash.to_string())
201
+ .map_err(|error| AccountError::internal(error.to_string()))
202
+ }
203
+
204
+ fn verify_password(password: &str, hash: &str) -> bool {
205
+ PasswordHash::new(hash).is_ok_and(|parsed| {
206
+ Argon2::default()
207
+ .verify_password(password.as_bytes(), &parsed)
208
+ .is_ok()
209
+ })
210
+ }
211
+
212
+ /// A fixed hash with the same parameters as real ones, verified when the
213
+ /// e-mail is unknown so that "no such account" costs what "wrong password" does.
214
+ fn dummy_hash() -> &'static str {
215
+ static DUMMY: OnceLock<String> = OnceLock::new();
216
+ DUMMY.get_or_init(|| {
217
+ let salt = SaltString::from_b64("ZmVsdGRiY29uc29sZWR1bW15").expect("static salt");
218
+ Argon2::default()
219
+ .hash_password(b"feltdb-console-dummy-password", &salt)
220
+ .expect("dummy hash")
221
+ .to_string()
222
+ })
223
+ }
224
+
225
+ fn invalid_credentials() -> AccountError {
226
+ AccountError::new(401, "INVALID_CREDENTIALS", "invalid email or password")
227
+ }
228
+
229
+ /// The secret the account routes are keyed by, if the deployment configured
230
+ /// one: `FELTDB_SESSION_SECRET`, at least 32 bytes, not a development value.
231
+ pub fn configured_secret(value: Option<String>) -> Option<Vec<u8>> {
232
+ value
233
+ .filter(|secret| secret.len() >= 32 && !DEVELOPMENT_SECRETS.contains(&secret.as_str()))
234
+ .map(String::into_bytes)
235
+ }
236
+
237
+ /// The bytes the website signs: `METHOD\nPATH\nTIMESTAMP\nBODY`.
238
+ pub fn signing_input(method: &str, path: &str, timestamp: &str, body: &[u8]) -> Vec<u8> {
239
+ let mut input = format!("{}\n{path}\n{timestamp}\n", method.to_uppercase()).into_bytes();
240
+ input.extend_from_slice(body);
241
+ input
242
+ }
243
+
244
+ pub fn sign_console_request(
245
+ secret: &[u8],
246
+ method: &str,
247
+ path: &str,
248
+ timestamp: &str,
249
+ body: &[u8],
250
+ ) -> String {
251
+ let mut mac = Hmac::<Sha256>::new_from_slice(secret).expect("HMAC accepts any key length");
252
+ mac.update(&signing_input(method, path, timestamp, body));
253
+ mac.finalize()
254
+ .into_bytes()
255
+ .iter()
256
+ .map(|byte| format!("{byte:02x}"))
257
+ .collect()
258
+ }
259
+
260
+ fn decode_hex(value: &str) -> Option<Vec<u8>> {
261
+ if value.len() % 2 != 0 {
262
+ return None;
263
+ }
264
+ (0..value.len())
265
+ .step_by(2)
266
+ .map(|index| u8::from_str_radix(value.get(index..index + 2)?, 16).ok())
267
+ .collect()
268
+ }
269
+
270
+ /// Verify a website request to the account routes. `timestamp` is Unix
271
+ /// seconds; requests more than five minutes away from `now` are refused so a
272
+ /// captured request cannot be replayed indefinitely.
273
+ pub fn verify_console_request(
274
+ secret: &[u8],
275
+ method: &str,
276
+ path: &str,
277
+ timestamp: Option<&str>,
278
+ signature: Option<&str>,
279
+ body: &[u8],
280
+ now: u64,
281
+ ) -> Result<(), AccountError> {
282
+ let refused = |message: &str| AccountError::new(401, "CONSOLE_SIGNATURE_INVALID", message);
283
+ let timestamp = timestamp.ok_or_else(|| refused("console request timestamp required"))?;
284
+ let signature = signature.ok_or_else(|| refused("console request signature required"))?;
285
+ let seconds: u64 = timestamp
286
+ .trim()
287
+ .parse()
288
+ .map_err(|_| refused("console request timestamp is not Unix seconds"))?;
289
+ if seconds.abs_diff(now) > MAXIMUM_SIGNATURE_SKEW_SECONDS {
290
+ return Err(refused(
291
+ "console request timestamp is outside the allowed skew",
292
+ ));
293
+ }
294
+ let supplied = decode_hex(signature.trim().to_lowercase().as_str())
295
+ .ok_or_else(|| refused("console request signature is not hex"))?;
296
+ let mut mac = Hmac::<Sha256>::new_from_slice(secret).expect("HMAC accepts any key length");
297
+ mac.update(&signing_input(method, path, timestamp, body));
298
+ mac.verify_slice(&supplied)
299
+ .map_err(|_| refused("console request signature does not verify"))
300
+ }
301
+
302
+ impl ConsoleAccountStore {
303
+ pub fn load(path: impl Into<PathBuf>, secret: Option<Vec<u8>>) -> Result<Self, String> {
304
+ let path = path.into();
305
+ let records = if path.exists() {
306
+ serde_json::from_slice(&fs::read(&path).map_err(|error| error.to_string())?)
307
+ .map_err(|error| format!("{}: {error}", path.display()))?
308
+ } else {
309
+ Records::default()
310
+ };
311
+ let loaded = fs::metadata(&path).and_then(|meta| meta.modified()).ok();
312
+ Ok(Self {
313
+ path,
314
+ records: Arc::new(RwLock::new(records)),
315
+ writer: Arc::new(std::sync::Mutex::new(())),
316
+ loaded: Arc::new(std::sync::Mutex::new(loaded)),
317
+ secret: secret.map(Into::into),
318
+ })
319
+ }
320
+
321
+ /// Re-read the file if another process changed it since we last saw it.
322
+ fn refresh(&self) -> Result<(), AccountError> {
323
+ let poisoned = || AccountError::internal("console account lock poisoned");
324
+ let modified = fs::metadata(&self.path)
325
+ .and_then(|meta| meta.modified())
326
+ .ok();
327
+ if modified.is_none() || modified == *self.loaded.lock().map_err(|_| poisoned())? {
328
+ return Ok(());
329
+ }
330
+ let bytes = fs::read(&self.path)
331
+ .map_err(|error| AccountError::internal(format!("console account store: {error}")))?;
332
+ let records: Records = serde_json::from_slice(&bytes)
333
+ .map_err(|error| AccountError::internal(format!("console account store: {error}")))?;
334
+ // Lock order everywhere: records, then loaded (as in `persist`).
335
+ let mut current = self.records.write().map_err(|_| poisoned())?;
336
+ *current = records;
337
+ *self.loaded.lock().map_err(|_| poisoned())? = modified;
338
+ Ok(())
339
+ }
340
+
341
+ fn find(&self, matches: impl Fn(&ConsoleAccount) -> bool) -> Option<ConsoleAccount> {
342
+ let lookup = |matches: &dyn Fn(&ConsoleAccount) -> bool| {
343
+ self.read()
344
+ .ok()?
345
+ .accounts
346
+ .iter()
347
+ .find(|account| matches(account))
348
+ .cloned()
349
+ };
350
+ lookup(&matches).or_else(|| {
351
+ self.refresh().ok()?;
352
+ lookup(&matches)
353
+ })
354
+ }
355
+
356
+ /// The signing secret, or `None` when the routes must answer 503.
357
+ pub fn secret(&self) -> Option<&[u8]> {
358
+ self.secret.as_deref()
359
+ }
360
+
361
+ fn persist(&self, records: &Records) -> Result<(), AccountError> {
362
+ let write = || -> std::io::Result<()> {
363
+ if let Some(parent) = self.path.parent() {
364
+ fs::create_dir_all(parent)?;
365
+ }
366
+ let temporary = self.path.with_extension("json.tmp");
367
+ let _ = fs::remove_file(&temporary);
368
+ let mut options = fs::OpenOptions::new();
369
+ options.write(true).create_new(true);
370
+ #[cfg(unix)]
371
+ {
372
+ use std::os::unix::fs::OpenOptionsExt;
373
+ options.mode(0o600);
374
+ }
375
+ let mut file = options.open(&temporary)?;
376
+ file.write_all(&serde_json::to_vec_pretty(records)?)?;
377
+ file.sync_all()?;
378
+ fs::rename(&temporary, &self.path)?;
379
+ if let Ok(mut loaded) = self.loaded.lock() {
380
+ *loaded = fs::metadata(&self.path)
381
+ .and_then(|meta| meta.modified())
382
+ .ok();
383
+ }
384
+ Ok(())
385
+ };
386
+ write().map_err(|error| AccountError::internal(format!("console account store: {error}")))
387
+ }
388
+
389
+ fn read(&self) -> Result<std::sync::RwLockReadGuard<'_, Records>, AccountError> {
390
+ self.records
391
+ .read()
392
+ .map_err(|_| AccountError::internal("console account lock poisoned"))
393
+ }
394
+
395
+ fn insert(&self, account: ConsoleAccount) -> Result<ConsoleAccount, AccountError> {
396
+ let mut records = self
397
+ .records
398
+ .write()
399
+ .map_err(|_| AccountError::internal("console account lock poisoned"))?;
400
+ records.accounts.push(account.clone());
401
+ if let Err(error) = self.persist(&records) {
402
+ records.accounts.pop();
403
+ return Err(error);
404
+ }
405
+ Ok(account)
406
+ }
407
+
408
+ pub fn account_for_user(&self, user_id: &str) -> Option<ConsoleAccount> {
409
+ self.find(|account| account.user_id == user_id)
410
+ }
411
+
412
+ pub fn account_for_identity(&self, identity: &str) -> Option<ConsoleAccount> {
413
+ self.find(|account| account.identity == identity)
414
+ }
415
+
416
+ pub fn list(&self) -> Vec<ConsoleAccount> {
417
+ let _ = self.refresh();
418
+ self.read()
419
+ .map(|records| records.accounts.clone())
420
+ .unwrap_or_default()
421
+ }
422
+
423
+ fn register_platform_user(
424
+ tenancy: &TenancyStore,
425
+ account: &ConsoleAccount,
426
+ ) -> Result<(), AccountError> {
427
+ tenancy
428
+ .register_platform_user(&account.user_id, &account.display_identity)
429
+ .map_err(AccountError::internal)
430
+ }
431
+
432
+ /// Create a password account. A pre-0.15.0 platform user registered under
433
+ /// this e-mail (unverified, but possibly owning tenants) is never handed
434
+ /// to whoever signs up first: it has to be claimed by an operator.
435
+ pub fn password_signup(
436
+ &self,
437
+ tenancy: &TenancyStore,
438
+ email: &str,
439
+ password: &str,
440
+ ) -> Result<ResolvedAccount, AccountError> {
441
+ let email = normalize_email(email)?;
442
+ validate_password(password)?;
443
+ let _writer = self
444
+ .writer
445
+ .lock()
446
+ .map_err(|_| AccountError::internal("console account writer poisoned"))?;
447
+ self.refresh()?;
448
+ if self.account_for_identity(&email).is_some() {
449
+ return Err(AccountError::new(
450
+ 409,
451
+ "ACCOUNT_EXISTS",
452
+ "an account already exists for this email; sign in instead",
453
+ ));
454
+ }
455
+ if !self.unclaimed_legacy_users(tenancy, &email).is_empty() {
456
+ return Err(AccountError::new(
457
+ 409,
458
+ "ACCOUNT_CLAIM_REQUIRED",
459
+ "this email has a console account created before password verification; \
460
+ an operator must claim it",
461
+ ));
462
+ }
463
+ let mut user_id = new_user_id();
464
+ while self.account_for_user(&user_id).is_some() || tenancy.platform_user_exists(&user_id) {
465
+ user_id = new_user_id();
466
+ }
467
+ let account = self.insert(ConsoleAccount {
468
+ identity: email.clone(),
469
+ user_id,
470
+ password_hash: Some(hash_password(password)?),
471
+ provider: ConsoleProvider::Password,
472
+ created_at: now(),
473
+ display_identity: email,
474
+ origin: default_origin(),
475
+ })?;
476
+ Self::register_platform_user(tenancy, &account)?;
477
+ Ok((&account).into())
478
+ }
479
+
480
+ pub fn password_signin(
481
+ &self,
482
+ email: &str,
483
+ password: &str,
484
+ ) -> Result<ResolvedAccount, AccountError> {
485
+ let account = normalize_email(email)
486
+ .ok()
487
+ .and_then(|email| self.account_for_identity(&email))
488
+ .filter(|account| account.provider == ConsoleProvider::Password);
489
+ let hash = account
490
+ .as_ref()
491
+ .and_then(|account| account.password_hash.as_deref())
492
+ .unwrap_or_else(|| dummy_hash());
493
+ let verified = verify_password(password, hash);
494
+ match account {
495
+ Some(account) if verified => Ok((&account).into()),
496
+ _ => Err(invalid_credentials()),
497
+ }
498
+ }
499
+
500
+ /// Platform users the old website could have created for `email` that no
501
+ /// console account owns yet.
502
+ fn unclaimed_legacy_users(&self, tenancy: &TenancyStore, email: &str) -> Vec<String> {
503
+ tenancy
504
+ .platform_users_with_identity(email)
505
+ .into_iter()
506
+ .filter(|user_id| user_id.starts_with("user_"))
507
+ .filter(|user_id| self.account_for_user(user_id).is_none())
508
+ .collect()
509
+ }
510
+
511
+ /// Resolve the console user id for a GitHub sign-in.
512
+ ///
513
+ /// An account bound to `github:<id>` wins. Otherwise a pre-0.15.0 user
514
+ /// whose derived id came from `@<login>` and whose recorded identity is
515
+ /// exactly `@<login>` keeps its data (continuity), unless another console
516
+ /// account already owns that id. Everything else gets a new random id.
517
+ /// The binding is by numeric GitHub id, so a later login rename (or a new
518
+ /// GitHub user taking an old login) never moves it.
519
+ pub fn github_resolve(
520
+ &self,
521
+ tenancy: &TenancyStore,
522
+ github_id: &str,
523
+ login: &str,
524
+ ) -> Result<ResolvedAccount, AccountError> {
525
+ let github_id = github_id.trim();
526
+ if github_id.is_empty()
527
+ || github_id.len() > 20
528
+ || !github_id
529
+ .chars()
530
+ .all(|character| character.is_ascii_digit())
531
+ {
532
+ return Err(AccountError::new(
533
+ 400,
534
+ "VALIDATION_ERROR",
535
+ "github_id must be the numeric GitHub user id",
536
+ ));
537
+ }
538
+ let login = login.trim().to_lowercase();
539
+ if login.is_empty()
540
+ || login.len() > 39
541
+ || !login
542
+ .chars()
543
+ .all(|character| character.is_ascii_alphanumeric() || character == '-')
544
+ {
545
+ return Err(AccountError::new(
546
+ 400,
547
+ "VALIDATION_ERROR",
548
+ "login must be a GitHub login",
549
+ ));
550
+ }
551
+ let identity = format!("github:{github_id}");
552
+ let display_identity = format!("@{login}");
553
+ let _writer = self
554
+ .writer
555
+ .lock()
556
+ .map_err(|_| AccountError::internal("console account writer poisoned"))?;
557
+ self.refresh()?;
558
+ {
559
+ let mut records = self
560
+ .records
561
+ .write()
562
+ .map_err(|_| AccountError::internal("console account lock poisoned"))?;
563
+ if let Some(index) = records
564
+ .accounts
565
+ .iter()
566
+ .position(|account| account.identity == identity)
567
+ {
568
+ if records.accounts[index].display_identity != display_identity {
569
+ records.accounts[index].display_identity = display_identity.clone();
570
+ self.persist(&records)?;
571
+ }
572
+ return Ok((&records.accounts[index]).into());
573
+ }
574
+ }
575
+ let legacy = legacy_user_id(&display_identity);
576
+ let continuity = tenancy.platform_user_identity(&legacy).as_deref()
577
+ == Some(display_identity.as_str())
578
+ && self.account_for_user(&legacy).is_none();
579
+ let (user_id, origin) = if continuity {
580
+ (legacy, "legacy_github_login".to_string())
581
+ } else {
582
+ let mut user_id = new_user_id();
583
+ while self.account_for_user(&user_id).is_some()
584
+ || tenancy.platform_user_exists(&user_id)
585
+ {
586
+ user_id = new_user_id();
587
+ }
588
+ (user_id, default_origin())
589
+ };
590
+ let account = self.insert(ConsoleAccount {
591
+ identity,
592
+ user_id,
593
+ password_hash: None,
594
+ provider: ConsoleProvider::Github,
595
+ created_at: now(),
596
+ display_identity,
597
+ origin,
598
+ })?;
599
+ if !continuity {
600
+ Self::register_platform_user(tenancy, &account)?;
601
+ }
602
+ Ok((&account).into())
603
+ }
604
+
605
+ /// Operator claim: bind a password account for `email` to the existing
606
+ /// pre-0.15.0 platform user registered under that e-mail.
607
+ pub fn claim_password(
608
+ &self,
609
+ tenancy: &TenancyStore,
610
+ email: &str,
611
+ password: &str,
612
+ ) -> Result<ResolvedAccount, AccountError> {
613
+ let email = normalize_email(email)?;
614
+ validate_password(password)?;
615
+ let _writer = self
616
+ .writer
617
+ .lock()
618
+ .map_err(|_| AccountError::internal("console account writer poisoned"))?;
619
+ self.refresh()?;
620
+ if self.account_for_identity(&email).is_some() {
621
+ return Err(AccountError::new(
622
+ 409,
623
+ "ACCOUNT_EXISTS",
624
+ "a console account already exists for this email",
625
+ ));
626
+ }
627
+ let candidates = self.unclaimed_legacy_users(tenancy, &email);
628
+ let user_id = match candidates.as_slice() {
629
+ [user_id] => user_id.clone(),
630
+ [] => {
631
+ return Err(AccountError::new(
632
+ 404,
633
+ "NOT_FOUND",
634
+ "no unclaimed legacy platform user is registered under this email",
635
+ ))
636
+ }
637
+ _ => {
638
+ return Err(AccountError::new(
639
+ 409,
640
+ "CONFLICT",
641
+ format!(
642
+ "several unclaimed platform users are registered under this email: {}",
643
+ candidates.join(", ")
644
+ ),
645
+ ))
646
+ }
647
+ };
648
+ let account = self.insert(ConsoleAccount {
649
+ identity: email.clone(),
650
+ user_id,
651
+ password_hash: Some(hash_password(password)?),
652
+ provider: ConsoleProvider::Password,
653
+ created_at: now(),
654
+ display_identity: email,
655
+ origin: "operator_claim".into(),
656
+ })?;
657
+ Ok((&account).into())
658
+ }
659
+ }
660
+
661
+ #[cfg(test)]
662
+ mod tests {
663
+ use super::*;
664
+
665
+ fn stores() -> (ConsoleAccountStore, TenancyStore, PathBuf) {
666
+ let dir = std::env::temp_dir().join(format!("feltdb-console-accounts-{}", new_user_id()));
667
+ fs::create_dir_all(&dir).unwrap();
668
+ let accounts =
669
+ ConsoleAccountStore::load(dir.join("state.console-accounts.json"), None).unwrap();
670
+ let tenancy = TenancyStore::load(dir.join("state.platform.json")).unwrap();
671
+ (accounts, tenancy, dir)
672
+ }
673
+
674
+ #[test]
675
+ fn signup_then_signin_verifies_the_password() {
676
+ let (accounts, tenancy, dir) = stores();
677
+ let created = accounts
678
+ .password_signup(&tenancy, " Alice@Example.com ", "correct horse battery")
679
+ .unwrap();
680
+ assert_eq!(created.identity, "alice@example.com");
681
+ assert!(created.user_id.starts_with("user_") && created.user_id.len() >= 27);
682
+ assert_eq!(
683
+ tenancy.platform_user_identity(&created.user_id).as_deref(),
684
+ Some("alice@example.com")
685
+ );
686
+ let signed_in = accounts
687
+ .password_signin("alice@example.com", "correct horse battery")
688
+ .unwrap();
689
+ assert_eq!(signed_in.user_id, created.user_id);
690
+ assert_eq!(
691
+ accounts
692
+ .password_signin("alice@example.com", "wrong horse battery")
693
+ .unwrap_err()
694
+ .code,
695
+ "INVALID_CREDENTIALS"
696
+ );
697
+ assert_eq!(
698
+ accounts
699
+ .password_signin("nobody@example.com", "correct horse battery")
700
+ .unwrap_err()
701
+ .code,
702
+ "INVALID_CREDENTIALS"
703
+ );
704
+ assert_eq!(
705
+ accounts
706
+ .password_signup(&tenancy, "alice@example.com", "another long password")
707
+ .unwrap_err()
708
+ .code,
709
+ "ACCOUNT_EXISTS"
710
+ );
711
+ // Persisted, owner-only, and reloadable.
712
+ let path = dir.join("state.console-accounts.json");
713
+ #[cfg(unix)]
714
+ {
715
+ use std::os::unix::fs::PermissionsExt;
716
+ assert_eq!(
717
+ fs::metadata(&path).unwrap().permissions().mode() & 0o777,
718
+ 0o600
719
+ );
720
+ }
721
+ let reloaded = ConsoleAccountStore::load(&path, None).unwrap();
722
+ assert_eq!(
723
+ reloaded
724
+ .password_signin("alice@example.com", "correct horse battery")
725
+ .unwrap()
726
+ .user_id,
727
+ created.user_id
728
+ );
729
+ fs::remove_dir_all(dir).ok();
730
+ }
731
+
732
+ #[test]
733
+ fn prefix_sharing_emails_get_distinct_user_ids() {
734
+ let (accounts, tenancy, dir) = stores();
735
+ let one = accounts
736
+ .password_signup(
737
+ &tenancy,
738
+ "abcdefghijklm-one@example.com",
739
+ "password-one-long",
740
+ )
741
+ .unwrap();
742
+ let two = accounts
743
+ .password_signup(
744
+ &tenancy,
745
+ "abcdefghijklm-two@example.com",
746
+ "password-two-long",
747
+ )
748
+ .unwrap();
749
+ assert_eq!(
750
+ legacy_user_id("abcdefghijklm-one@example.com"),
751
+ legacy_user_id("abcdefghijklm-two@example.com")
752
+ );
753
+ assert_ne!(one.user_id, two.user_id);
754
+ fs::remove_dir_all(dir).ok();
755
+ }
756
+
757
+ #[test]
758
+ fn password_policy_and_email_shape_are_enforced() {
759
+ let (accounts, tenancy, dir) = stores();
760
+ assert_eq!(
761
+ accounts
762
+ .password_signup(&tenancy, "short@example.com", "elevenchars")
763
+ .unwrap_err()
764
+ .code,
765
+ "PASSWORD_TOO_SHORT"
766
+ );
767
+ assert_eq!(
768
+ accounts
769
+ .password_signup(&tenancy, "not-an-email", "a long enough password")
770
+ .unwrap_err()
771
+ .code,
772
+ "VALIDATION_ERROR"
773
+ );
774
+ fs::remove_dir_all(dir).ok();
775
+ }
776
+
777
+ #[test]
778
+ fn a_legacy_password_user_must_be_claimed_by_an_operator() {
779
+ let (accounts, tenancy, dir) = stores();
780
+ let legacy = legacy_user_id("owner@example.com");
781
+ tenancy
782
+ .register_platform_user(&legacy, "owner@example.com")
783
+ .unwrap();
784
+ assert_eq!(
785
+ accounts
786
+ .password_signup(&tenancy, "owner@example.com", "attacker password")
787
+ .unwrap_err()
788
+ .code,
789
+ "ACCOUNT_CLAIM_REQUIRED"
790
+ );
791
+ let claimed = accounts
792
+ .claim_password(&tenancy, "owner@example.com", "the owner's password")
793
+ .unwrap();
794
+ assert_eq!(claimed.user_id, legacy);
795
+ assert_eq!(
796
+ accounts
797
+ .password_signin("owner@example.com", "the owner's password")
798
+ .unwrap()
799
+ .user_id,
800
+ legacy
801
+ );
802
+ assert_eq!(
803
+ accounts
804
+ .claim_password(&tenancy, "owner@example.com", "the owner's password")
805
+ .unwrap_err()
806
+ .code,
807
+ "ACCOUNT_EXISTS"
808
+ );
809
+ assert_eq!(
810
+ accounts
811
+ .claim_password(&tenancy, "stranger@example.com", "a long enough password")
812
+ .unwrap_err()
813
+ .status,
814
+ 404
815
+ );
816
+ fs::remove_dir_all(dir).ok();
817
+ }
818
+
819
+ #[test]
820
+ fn github_resolve_keeps_the_genuine_legacy_owner_and_isolates_colliders() {
821
+ let (accounts, tenancy, dir) = stores();
822
+ // Two logins whose legacy ids collide (shared 13-character prefix).
823
+ let victim = "averylongname-victim";
824
+ let attacker = "averylongname-attack";
825
+ let legacy = legacy_user_id(&format!("@{victim}"));
826
+ assert_eq!(legacy, legacy_user_id(&format!("@{attacker}")));
827
+ tenancy
828
+ .register_platform_user(&legacy, &format!("@{victim}"))
829
+ .unwrap();
830
+
831
+ let colliding = accounts.github_resolve(&tenancy, "999", attacker).unwrap();
832
+ assert_ne!(colliding.user_id, legacy);
833
+ let genuine = accounts.github_resolve(&tenancy, "123", victim).unwrap();
834
+ assert_eq!(genuine.user_id, legacy);
835
+ assert_eq!(genuine.identity, "github:123");
836
+ // Stable across sign-ins and a login rename.
837
+ assert_eq!(
838
+ accounts
839
+ .github_resolve(&tenancy, "123", "renamed-victim")
840
+ .unwrap()
841
+ .user_id,
842
+ legacy
843
+ );
844
+ // A new GitHub user taking the old login cannot re-bind the legacy id.
845
+ let squatter = accounts.github_resolve(&tenancy, "555", victim).unwrap();
846
+ assert_ne!(squatter.user_id, legacy);
847
+ assert_eq!(
848
+ accounts
849
+ .github_resolve(&tenancy, "12a", victim)
850
+ .unwrap_err()
851
+ .code,
852
+ "VALIDATION_ERROR"
853
+ );
854
+ fs::remove_dir_all(dir).ok();
855
+ }
856
+
857
+ #[test]
858
+ fn signed_requests_verify_and_forgeries_do_not() {
859
+ let secret = b"0123456789abcdef0123456789abcdef";
860
+ let body = br#"{"email":"a@example.com","password":"x"}"#;
861
+ let now = 1_800_000_000;
862
+ let timestamp = now.to_string();
863
+ let path = "/v1/console/accounts/password/signin";
864
+ let signature = sign_console_request(secret, "POST", path, &timestamp, body);
865
+ assert!(verify_console_request(
866
+ secret,
867
+ "POST",
868
+ path,
869
+ Some(&timestamp),
870
+ Some(&signature),
871
+ body,
872
+ now
873
+ )
874
+ .is_ok());
875
+ let refused = |method: &str,
876
+ path: &str,
877
+ timestamp: Option<&str>,
878
+ signature: Option<&str>,
879
+ body: &[u8],
880
+ now: u64| {
881
+ verify_console_request(secret, method, path, timestamp, signature, body, now)
882
+ .unwrap_err()
883
+ .code
884
+ };
885
+ assert_eq!(
886
+ refused("POST", path, Some(&timestamp), None, body, now),
887
+ "CONSOLE_SIGNATURE_INVALID"
888
+ );
889
+ assert_eq!(
890
+ refused("POST", path, None, Some(&signature), body, now),
891
+ "CONSOLE_SIGNATURE_INVALID"
892
+ );
893
+ // Body, path, method and timestamp are all covered by the MAC.
894
+ assert!(verify_console_request(
895
+ secret,
896
+ "POST",
897
+ path,
898
+ Some(&timestamp),
899
+ Some(&signature),
900
+ b"{}",
901
+ now
902
+ )
903
+ .is_err());
904
+ assert!(verify_console_request(
905
+ secret,
906
+ "POST",
907
+ "/v1/console/accounts/password/signup",
908
+ Some(&timestamp),
909
+ Some(&signature),
910
+ body,
911
+ now
912
+ )
913
+ .is_err());
914
+ assert!(verify_console_request(
915
+ secret,
916
+ "PUT",
917
+ path,
918
+ Some(&timestamp),
919
+ Some(&signature),
920
+ body,
921
+ now
922
+ )
923
+ .is_err());
924
+ let other = (now + 1).to_string();
925
+ assert!(verify_console_request(
926
+ secret,
927
+ "POST",
928
+ path,
929
+ Some(&other),
930
+ Some(&signature),
931
+ body,
932
+ now
933
+ )
934
+ .is_err());
935
+ // Another key does not verify.
936
+ let forged = sign_console_request(
937
+ b"another-secret-another-secret-xx",
938
+ "POST",
939
+ path,
940
+ &timestamp,
941
+ body,
942
+ );
943
+ assert!(verify_console_request(
944
+ secret,
945
+ "POST",
946
+ path,
947
+ Some(&timestamp),
948
+ Some(&forged),
949
+ body,
950
+ now
951
+ )
952
+ .is_err());
953
+ // Stale and future timestamps are refused even when correctly signed.
954
+ let stale = (now - MAXIMUM_SIGNATURE_SKEW_SECONDS - 1).to_string();
955
+ let stale_signature = sign_console_request(secret, "POST", path, &stale, body);
956
+ assert!(verify_console_request(
957
+ secret,
958
+ "POST",
959
+ path,
960
+ Some(&stale),
961
+ Some(&stale_signature),
962
+ body,
963
+ now
964
+ )
965
+ .is_err());
966
+ let future = (now + MAXIMUM_SIGNATURE_SKEW_SECONDS + 1).to_string();
967
+ let future_signature = sign_console_request(secret, "POST", path, &future, body);
968
+ assert!(verify_console_request(
969
+ secret,
970
+ "POST",
971
+ path,
972
+ Some(&future),
973
+ Some(&future_signature),
974
+ body,
975
+ now
976
+ )
977
+ .is_err());
978
+ assert!(verify_console_request(
979
+ secret,
980
+ "POST",
981
+ path,
982
+ Some(&timestamp),
983
+ Some("zz"),
984
+ body,
985
+ now
986
+ )
987
+ .is_err());
988
+ }
989
+
990
+ #[test]
991
+ fn only_a_real_secret_enables_the_routes() {
992
+ assert!(configured_secret(None).is_none());
993
+ assert!(configured_secret(Some("short".into())).is_none());
994
+ assert!(
995
+ configured_secret(Some("feltdb-development-session-secret-change-me".into())).is_none()
996
+ );
997
+ assert!(configured_secret(Some("x".repeat(32))).is_some());
998
+ }
999
+
1000
+ #[test]
1001
+ fn legacy_user_id_matches_the_old_website_formula() {
1002
+ // 'user_' + Buffer.from('@octocat').toString('base64url').slice(0, 18)
1003
+ assert_eq!(legacy_user_id("@OctoCat"), "user_QG9jdG9jYXQ");
1004
+ assert_eq!(
1005
+ legacy_user_id("alice@example.com"),
1006
+ "user_YWxpY2VAZXhhbXBsZS"
1007
+ );
1008
+ }
1009
+ }