parse-rust-auth 0.2.0

Parse Server compatible sessions, role graph expansion and bcrypt password hashing for parse-rust-server.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! An in-memory [`StorageAdapter`] for this crate's unit tests.
//!
//! Deliberately small and deliberately faithful on the two things the tests actually assert
//! about: it **applies `limit`, `skip` and the projection**, and it matches the constraint
//! vocabulary this crate emits. A fake that ignores `limit` would let the more-than-a-hundred
//! roles test pass while the real adapter truncated, which is the shape of green check that means
//! nothing.
//!
//! It panics on anything outside that vocabulary rather than returning an empty result, because a
//! silently unsupported constraint in a test double is how an authorization test comes to assert
//! against a query nobody ran.
//!
//! Rows are held in Parse form, which is what [`StorageAdapter`] deals in. There is no `_p_`
//! prefixing, no `_id` renaming and no BSON here: that is the Mongo adapter's job and it has its
//! own tests against a real server. What this fake is for is the logic in this crate.

use std::collections::HashMap;
use std::sync::Mutex;

use parse_rust_core::{deep_strict_eq, ClassLevelPermissions, ParseError, ParseValue};
use parse_rust_storage::{
    AddFieldOutcome, ClassSchema, Clause, Comparison, Constraint, FieldType, Query, QueryOptions,
    Row, SchemaIndex, SortDirection, StorageAdapter, Update, UpdateValue, WriteResult,
};

#[derive(Default)]
struct State {
    rows: HashMap<String, Vec<Row>>,
    schemas: HashMap<String, ClassSchema>,
    find_count: usize,
    last_find_limit: Option<Option<u32>>,
}

#[derive(Default)]
pub struct FakeStorage {
    state: Mutex<State>,
}

impl FakeStorage {
    pub fn new() -> Self {
        Self::default()
    }

    fn lock(&self) -> std::sync::MutexGuard<'_, State> {
        // Test code. A poisoned lock means another test thread panicked, and surfacing that is
        // more useful than swallowing it.
        self.state.lock().expect("fake storage lock")
    }

    /// Seed a row directly, bypassing the write path.
    pub fn insert_row(&self, class_name: &str, pairs: Vec<(&str, ParseValue)>) {
        let mut row = Row::new();
        for (k, v) in pairs {
            row.insert(k.to_string(), v);
        }
        self.lock()
            .rows
            .entry(class_name.to_string())
            .or_default()
            .push(row);
    }

    pub fn rows(&self, class_name: &str) -> Vec<Row> {
        self.lock()
            .rows
            .get(class_name)
            .cloned()
            .unwrap_or_default()
    }

    pub fn schema(&self, class_name: &str) -> Option<ClassSchema> {
        self.lock().schemas.get(class_name).cloned()
    }

    pub fn find_count(&self) -> usize {
        self.lock().find_count
    }

    pub fn reset_find_count(&self) {
        self.lock().find_count = 0;
    }

    /// The `limit` the most recent `find` was given, so a test can assert a query was bounded (or
    /// deliberately unbounded) rather than inferring it from the result.
    pub fn last_find_limit(&self) -> Option<Option<u32>> {
        self.lock().last_find_limit
    }
}

fn value_matches(value: Option<&ParseValue>, comparison: &Comparison) -> bool {
    match comparison {
        Comparison::Equal(want) | Comparison::EqualOperator(want) => {
            value.is_some_and(|v| deep_strict_eq(v, want))
        }
        // A missing field matches `$ne`, which is Mongo's behavior and the reason
        // `destroyDuplicatedSessions` can carry the guard unconditionally.
        Comparison::NotEqual(want) => !value.is_some_and(|v| deep_strict_eq(v, want)),
        Comparison::In(wants) => {
            value.is_some_and(|v| wants.iter().any(|want| deep_strict_eq(v, want)))
        }
        Comparison::NotIn(wants) => {
            !value.is_some_and(|v| wants.iter().any(|want| deep_strict_eq(v, want)))
        }
        Comparison::Exists(want) => value.is_some() == *want,
        other => panic!(
            "the fake storage adapter does not implement {other:?}; add it rather than letting \
             the constraint be dropped"
        ),
    }
}

fn constraint_matches(row: &Row, constraint: &Constraint) -> bool {
    value_matches(row.get(&constraint.field), &constraint.comparison)
}

fn query_matches(row: &Row, query: &Query) -> bool {
    query.clauses.iter().all(|clause| match clause {
        Clause::Field(c) => constraint_matches(row, c),
        Clause::Or(qs) => qs.iter().any(|q| query_matches(row, q)),
        Clause::And(qs) => qs.iter().all(|q| query_matches(row, q)),
        Clause::Nor(qs) => !qs.iter().any(|q| query_matches(row, q)),
    })
}

fn sort_key(row: &Row, key: &str) -> String {
    match row.get(key) {
        Some(ParseValue::String(s)) => s.clone(),
        Some(ParseValue::Number(n)) => format!("{n:020.6}"),
        Some(other) => other.to_json(),
        None => String::new(),
    }
}

impl StorageAdapter for FakeStorage {
    async fn all_schemas(&self) -> Result<Vec<ClassSchema>, ParseError> {
        Ok(self.lock().schemas.values().cloned().collect())
    }

    async fn insert_schema(&self, schema: &ClassSchema) -> Result<(), ParseError> {
        let mut state = self.lock();
        if state.schemas.contains_key(&schema.class_name) {
            return Err(ParseError::new(
                parse_rust_core::ErrorCode::DuplicateValue,
                "Class already exists.",
            ));
        }
        state
            .schemas
            .insert(schema.class_name.clone(), schema.clone());
        Ok(())
    }

    async fn upsert_schema(&self, schema: &ClassSchema) -> Result<(), ParseError> {
        let mut state = self.lock();
        let entry = state
            .schemas
            .entry(schema.class_name.clone())
            .or_insert_with(|| ClassSchema::new(&schema.class_name));
        for (name, ty) in &schema.fields {
            entry.fields.insert(name.clone(), ty.clone());
        }
        // Matching the real adapter: metadata is only touched when it was supplied.
        if schema.clp.is_some() {
            entry.clp = schema.clp.clone();
        }
        Ok(())
    }

    async fn reserve_field(
        &self,
        class_name: &str,
        field_name: &str,
        field_type: &FieldType,
        _options: Option<&parse_rust_core::ParseMap>,
    ) -> Result<AddFieldOutcome, ParseError> {
        let mut state = self.lock();
        let entry = state
            .schemas
            .entry(class_name.to_string())
            .or_insert_with(|| ClassSchema::new(class_name));
        match entry.fields.get(field_name) {
            None => {
                entry
                    .fields
                    .insert(field_name.to_string(), field_type.clone());
                Ok(AddFieldOutcome::Added)
            }
            Some(existing) if existing == field_type => Ok(AddFieldOutcome::AlreadyPresentSameType),
            Some(existing) => Ok(AddFieldOutcome::Conflict {
                existing: existing.clone(),
            }),
        }
    }

    /// Nothing in this crate reaches the schema API, so these are inert here.
    async fn set_field_options(
        &self,
        _class_name: &str,
        _field_name: &str,
        _options: &parse_rust_core::ParseMap,
    ) -> Result<(), ParseError> {
        Ok(())
    }

    async fn set_indexes(
        &self,
        _class_name: &str,
        _indexes: &parse_rust_core::ParseMap,
    ) -> Result<(), ParseError> {
        Ok(())
    }

    async fn set_class_permissions(
        &self,
        class_name: &str,
        clp: Option<&ClassLevelPermissions>,
    ) -> Result<(), ParseError> {
        let mut state = self.lock();
        if let Some(schema) = state.schemas.get_mut(class_name) {
            schema.clp = clp.cloned();
        }
        Ok(())
    }

    async fn delete_class(&self, schema: &ClassSchema) -> Result<(), ParseError> {
        let mut state = self.lock();
        state.rows.remove(&schema.class_name);
        state.schemas.remove(&schema.class_name);
        Ok(())
    }

    async fn delete_fields(
        &self,
        schema: &ClassSchema,
        fields: &[String],
    ) -> Result<(), ParseError> {
        let mut state = self.lock();
        if let Some(stored) = state.schemas.get_mut(&schema.class_name) {
            for f in fields {
                stored.fields.shift_remove(f);
            }
        }
        if let Some(rows) = state.rows.get_mut(&schema.class_name) {
            for row in rows.iter_mut() {
                for f in fields {
                    row.shift_remove(f);
                }
            }
        }
        Ok(())
    }

    async fn create(&self, schema: &ClassSchema, row: &Row) -> Result<WriteResult, ParseError> {
        let object_id = match row.get("objectId") {
            Some(ParseValue::String(id)) => id.clone(),
            _ => String::new(),
        };
        self.lock()
            .rows
            .entry(schema.class_name.clone())
            .or_default()
            .push(row.clone());
        Ok(WriteResult { object_id })
    }

    async fn upsert_one(
        &self,
        schema: &ClassSchema,
        query: &Query,
        row: &Row,
    ) -> Result<(), ParseError> {
        let mut state = self.lock();
        let rows = state.rows.entry(schema.class_name.clone()).or_default();
        if !rows.iter().any(|r| query_matches(r, query)) {
            rows.push(row.clone());
        }
        Ok(())
    }

    async fn find(
        &self,
        schema: &ClassSchema,
        query: &Query,
        options: &QueryOptions,
    ) -> Result<Vec<Row>, ParseError> {
        let mut state = self.lock();
        state.find_count += 1;
        state.last_find_limit = Some(options.limit);

        let mut rows: Vec<Row> = state
            .rows
            .get(&schema.class_name)
            .map(|rows| {
                rows.iter()
                    .filter(|row| query_matches(row, query))
                    .cloned()
                    .collect()
            })
            .unwrap_or_default();

        if let Some((key, direction)) = options.order.first() {
            rows.sort_by(|a, b| {
                let ord = sort_key(a, key).cmp(&sort_key(b, key));
                match direction {
                    SortDirection::Ascending => ord,
                    SortDirection::Descending => ord.reverse(),
                }
            });
        }
        if let Some(skip) = options.skip {
            rows = rows.into_iter().skip(skip as usize).collect();
        }
        if let Some(limit) = options.limit {
            rows.truncate(limit as usize);
        }
        if let Some(keys) = &options.keys {
            // The Mongo adapter projects the requested keys, and Mongo returns `_id` regardless
            // of an inclusion projection, so `objectId` and the timestamps survive too.
            rows = rows
                .into_iter()
                .map(|row| {
                    row.into_iter()
                        .filter(|(k, _)| {
                            keys.contains(k)
                                || matches!(k.as_str(), "objectId" | "createdAt" | "updatedAt")
                        })
                        .collect()
                })
                .collect();
        }
        Ok(rows)
    }

    async fn count(&self, schema: &ClassSchema, query: &Query) -> Result<u64, ParseError> {
        let state = self.lock();
        Ok(state
            .rows
            .get(&schema.class_name)
            .map(|rows| rows.iter().filter(|r| query_matches(r, query)).count() as u64)
            .unwrap_or(0))
    }

    async fn update(
        &self,
        schema: &ClassSchema,
        query: &Query,
        update: &Update,
    ) -> Result<u64, ParseError> {
        let mut state = self.lock();
        let mut matched = 0u64;
        if let Some(rows) = state.rows.get_mut(&schema.class_name) {
            for row in rows.iter_mut().filter(|r| query_matches(r, query)) {
                matched += 1;
                apply_update(row, update);
            }
        }
        Ok(matched)
    }

    async fn update_one_returning(
        &self,
        schema: &ClassSchema,
        query: &Query,
        update: &Update,
    ) -> Result<Option<Row>, ParseError> {
        let mut state = self.lock();
        let Some(rows) = state.rows.get_mut(&schema.class_name) else {
            return Ok(None);
        };
        let Some(row) = rows.iter_mut().find(|r| query_matches(r, query)) else {
            return Ok(None);
        };
        apply_update(row, update);
        Ok(Some(row.clone()))
    }

    async fn delete(&self, schema: &ClassSchema, query: &Query) -> Result<u64, ParseError> {
        let mut state = self.lock();
        let Some(rows) = state.rows.get_mut(&schema.class_name) else {
            return Ok(0);
        };
        let before = rows.len();
        rows.retain(|row| !query_matches(row, query));
        Ok((before - rows.len()) as u64)
    }

    async fn ensure_index(
        &self,
        _class_name: &str,
        _fields: &[&str],
        _name: Option<&str>,
        _unique: bool,
        _case_insensitive: bool,
    ) -> Result<(), ParseError> {
        Ok(())
    }

    /// Nothing in this crate reaches the schema API, so these are inert here.
    async fn create_indexes(
        &self,
        _class_name: &str,
        _indexes: &[SchemaIndex],
    ) -> Result<(), ParseError> {
        Ok(())
    }

    async fn drop_index(&self, _class_name: &str, _name: &str) -> Result<(), ParseError> {
        Ok(())
    }
}

fn apply_update(row: &mut Row, update: &Update) {
    for (key, value) in update {
        match value {
            UpdateValue::Set(v) => {
                row.insert(key.clone(), v.clone());
            }
            UpdateValue::Unset => {
                row.shift_remove(key);
            }
            // These fakes never insert through `update`, so `$setOnInsert` is always a no-op
            // here. Spelled out rather than folded into a catch-all so a future upsert path
            // cannot silently inherit the wrong behavior.
            UpdateValue::SetOnInsert(_) => {}
            UpdateValue::Increment(by) => {
                let current = match row.get(key) {
                    Some(ParseValue::Number(n)) => *n,
                    _ => 0.0,
                };
                row.insert(key.clone(), ParseValue::Number(current + by));
            }
            other => panic!(
                "the fake storage adapter does not implement {other:?}; add it rather than \
                 letting the update be dropped"
            ),
        }
    }
}