parse-rust-server 0.2.1

A Rust-native, embeddable implementation of the Parse Server API.
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! The axum layer: extractors in, [`dispatch`] out.
//!
//! No behavior lives here. Each handler resolves the request context once, names the route it
//! matched, and dispatches. `/batch` reaches the same dispatcher with the same context, which is
//! what keeps a sub-request and a top-level request from being two implementations.
//!
//! **The method override is dispatched explicitly rather than rewritten by middleware.** The
//! JavaScript SDK sends every request as a `POST` with the real method in `_method`, and axum
//! matches on the transport method before a `Router::layer` runs. Verified by observation: a POST
//! carrying `_method: "PUT"` produced a 405 no matter where the layer was attached. So the
//! intended method travels in an extension and is read here. See `body_credentials`.

use std::collections::HashMap;

use axum::extract::{Path, Query, State};
use axum::response::{IntoResponse, Response};
use axum::Json;
use serde_json::Value as Json_;

use crate::auth::Authority;
use crate::body_credentials::MethodOverride;
use crate::params::Params;
use crate::response::{HttpError, ParseErrorResponse};
use crate::routes::dispatch::{self, Incoming, Route, RouteError};
use crate::state::AppState;

/// Resolve the context and run one route.
async fn run(state: &AppState, authority: &Authority, incoming: Incoming) -> Response {
    // **A session token attached to `/login` is discarded before it is resolved**
    // (`middlewares.js:267-268`). Upstream deletes it in `handleParseHeaders`, after the client-key
    // check and before any `Auth` is built, so the token is never looked up at all.
    //
    // Without this, a client holding an expired or revoked token cannot log back in: the generic
    // resolver validates whatever token arrived and answers `Invalid session token` before the
    // credentials in the body are ever read. That is the one situation where the client's only
    // recovery is the route being refused. SDKs keep sending the stored token until a login
    // succeeds, so the failure is self-sustaining rather than transient.
    //
    // Credentials are untouched: only the token is dropped. A master-key login is still a master
    // request, and `/loginAs`, which requires master, is a different route and unaffected.
    //
    // Keyed on the route rather than the path string, and applied here rather than inside the
    // login handler, so it holds for the SDK's `POST`-everything form as well. `/batch` is
    // deliberately not covered: upstream's middleware runs once on the outer HTTP request, so a
    // `/login` nested in a batch sees the outer request's token exactly as it does upstream.
    let authority = &match incoming.route {
        Route::Login => Authority {
            session_token: None,
            ..authority.clone()
        },
        _ => authority.clone(),
    };

    // One snapshot, one role expansion, per HTTP request.
    let rc = match state.request_context(authority).await {
        Ok(rc) => rc,
        Err(e) => return ParseErrorResponse(e).into_response(),
    };
    let outcome = dispatch::dispatch(state, &rc, authority, &incoming).await;
    match outcome {
        Ok(response) => (response.status, Json(response.body)).into_response(),
        Err(RouteError::Parse(e)) => ParseErrorResponse(e).into_response(),
        Err(RouteError::Http(e)) => e.into_response(),
        // Express answers a bare 404 for a path no router claims, with an HTML body no client
        // parses. The status is what matters and is what a client branches on; the body is the
        // `code`-less HTTP envelope, because inventing a Parse code for "this route does not
        // exist" would make an absent feature look like a rejected request.
        Err(RouteError::NotFound { method, path }) => HttpError {
            status: http::StatusCode::NOT_FOUND,
            message: format!("cannot route {method} {path}"),
        }
        .into_response(),
    }
}

/// The method a request is really asking for.
///
/// An override that names a method axum would have routed differently is honoured; anything
/// unparsable falls back to the transport method, which then fails to match a route arm and
/// reports that rather than silently doing something else.
fn effective_method(
    transport: http::Method,
    override_: Option<axum::Extension<MethodOverride>>,
) -> http::Method {
    match override_ {
        Some(axum::Extension(MethodOverride(m))) => m,
        None => transport,
    }
}

fn params(query: HashMap<String, String>) -> Params {
    Params::from_map(query)
}

// -------------------------------------------------------------------------------------------
// Handlers
// -------------------------------------------------------------------------------------------

pub async fn health(State(state): State<AppState>) -> Response {
    // Credential-free upstream, and the endpoint every bring-up script polls, so it does not go
    // through the dispatcher's context resolution: a health check must answer while the database
    // is unreachable, which is the state a caller most wants to distinguish.
    let _ = state;
    Json(crate::routes::health::body()).into_response()
}

pub async fn server_info(State(state): State<AppState>, authority: Authority) -> Response {
    // The one route that needs no request context: it reads config and nothing else, so it stays
    // answerable when the database is down.
    if !authority.is_master() {
        return HttpError::master_key_required(state.config().error_detail()).into_response();
    }
    Json(crate::routes::features::server_info_body(state.config())).into_response()
}

pub async fn users_collection(
    State(state): State<AppState>,
    authority: Authority,
    method: Option<axum::Extension<MethodOverride>>,
    body: Option<Json<Json_>>,
) -> Response {
    let method = effective_method(http::Method::POST, method);
    run(
        &state,
        &authority,
        Incoming {
            method,
            route: Route::Users,
            params: Params::default(),
            body: body.map(|b| b.0),
            path: "/users".to_string(),
        },
    )
    .await
}

pub async fn users_me(
    State(state): State<AppState>,
    authority: Authority,
    method: Option<axum::Extension<MethodOverride>>,
    transport: http::Method,
) -> Response {
    // The SDK reaches this as a POST carrying `_method: "GET"`.
    let method = effective_method(transport, method);
    run(
        &state,
        &authority,
        Incoming {
            method,
            route: Route::UsersMe,
            params: Params::default(),
            body: None,
            path: "/users/me".to_string(),
        },
    )
    .await
}

pub async fn login(
    State(state): State<AppState>,
    authority: Authority,
    body: Option<Json<Json_>>,
) -> Response {
    run(
        &state,
        &authority,
        Incoming {
            method: http::Method::POST,
            route: Route::Login,
            params: Params::default(),
            body: body.map(|b| b.0),
            path: "/login".to_string(),
        },
    )
    .await
}

pub async fn logout(State(state): State<AppState>, authority: Authority) -> Response {
    run(
        &state,
        &authority,
        Incoming {
            method: http::Method::POST,
            route: Route::Logout,
            params: Params::default(),
            body: None,
            path: "/logout".to_string(),
        },
    )
    .await
}

pub async fn classes_collection(
    State(state): State<AppState>,
    authority: Authority,
    Path(class_name): Path<String>,
    Query(query): Query<HashMap<String, String>>,
    method: Option<axum::Extension<MethodOverride>>,
    transport: http::Method,
    body: Option<Json<Json_>>,
) -> Response {
    let method = effective_method(transport, method);
    let path = format!("/classes/{class_name}");
    run(
        &state,
        &authority,
        Incoming {
            method,
            route: Route::Classes { class_name },
            params: params(query),
            body: body.map(|b| b.0),
            path,
        },
    )
    .await
}

pub async fn classes_object(
    State(state): State<AppState>,
    authority: Authority,
    Path((class_name, object_id)): Path<(String, String)>,
    Query(query): Query<HashMap<String, String>>,
    method: Option<axum::Extension<MethodOverride>>,
    transport: http::Method,
    body: Option<Json<Json_>>,
) -> Response {
    // There is no POST verb on an object route. A bare POST with no override used to fall through
    // to `update`, so an unrelated request could mutate a row; an override-free POST now reaches
    // the dispatcher as POST and finds no arm.
    let method = effective_method(transport, method);
    let path = format!("/classes/{class_name}/{object_id}");
    run(
        &state,
        &authority,
        Incoming {
            method,
            route: Route::ClassObject {
                class_name,
                object_id,
            },
            params: params(query),
            body: body.map(|b| b.0),
            path,
        },
    )
    .await
}

pub async fn roles_collection(
    State(state): State<AppState>,
    authority: Authority,
    Query(query): Query<HashMap<String, String>>,
    method: Option<axum::Extension<MethodOverride>>,
    transport: http::Method,
    body: Option<Json<Json_>>,
) -> Response {
    let method = effective_method(transport, method);
    run(
        &state,
        &authority,
        Incoming {
            method,
            route: Route::Roles,
            params: params(query),
            body: body.map(|b| b.0),
            path: "/roles".to_string(),
        },
    )
    .await
}

pub async fn roles_object(
    State(state): State<AppState>,
    authority: Authority,
    Path(object_id): Path<String>,
    Query(query): Query<HashMap<String, String>>,
    method: Option<axum::Extension<MethodOverride>>,
    transport: http::Method,
    body: Option<Json<Json_>>,
) -> Response {
    let method = effective_method(transport, method);
    let path = format!("/roles/{object_id}");
    run(
        &state,
        &authority,
        Incoming {
            method,
            route: Route::RoleObject { object_id },
            params: params(query),
            body: body.map(|b| b.0),
            path,
        },
    )
    .await
}

pub async fn sessions_collection(
    State(state): State<AppState>,
    authority: Authority,
    Query(query): Query<HashMap<String, String>>,
    method: Option<axum::Extension<MethodOverride>>,
    transport: http::Method,
) -> Response {
    let method = effective_method(transport, method);
    run(
        &state,
        &authority,
        Incoming {
            method,
            route: Route::Sessions,
            params: params(query),
            body: None,
            path: "/sessions".to_string(),
        },
    )
    .await
}

pub async fn sessions_me(
    State(state): State<AppState>,
    authority: Authority,
    method: Option<axum::Extension<MethodOverride>>,
    transport: http::Method,
) -> Response {
    let method = effective_method(transport, method);
    run(
        &state,
        &authority,
        Incoming {
            method,
            route: Route::SessionsMe,
            params: Params::default(),
            body: None,
            path: "/sessions/me".to_string(),
        },
    )
    .await
}

pub async fn sessions_object(
    State(state): State<AppState>,
    authority: Authority,
    Path(object_id): Path<String>,
    Query(query): Query<HashMap<String, String>>,
    method: Option<axum::Extension<MethodOverride>>,
    transport: http::Method,
) -> Response {
    let method = effective_method(transport, method);
    let path = format!("/sessions/{object_id}");
    run(
        &state,
        &authority,
        Incoming {
            method,
            route: Route::SessionObject { object_id },
            params: params(query),
            body: None,
            path,
        },
    )
    .await
}

pub async fn schemas_collection(
    State(state): State<AppState>,
    authority: Authority,
    method: Option<axum::Extension<MethodOverride>>,
    transport: http::Method,
    body: Option<Json<Json_>>,
) -> Response {
    let method = effective_method(transport, method);
    run(
        &state,
        &authority,
        Incoming {
            method,
            route: Route::Schemas,
            params: Params::default(),
            body: body.map(|b| b.0),
            path: "/schemas".to_string(),
        },
    )
    .await
}

pub async fn schemas_class(
    State(state): State<AppState>,
    authority: Authority,
    Path(class_name): Path<String>,
    method: Option<axum::Extension<MethodOverride>>,
    transport: http::Method,
    body: Option<Json<Json_>>,
) -> Response {
    let method = effective_method(transport, method);
    let path = format!("/schemas/{class_name}");
    run(
        &state,
        &authority,
        Incoming {
            method,
            route: Route::SchemaClass { class_name },
            params: Params::default(),
            body: body.map(|b| b.0),
            path,
        },
    )
    .await
}

pub async fn purge(
    State(state): State<AppState>,
    authority: Authority,
    Path(class_name): Path<String>,
    method: Option<axum::Extension<MethodOverride>>,
    transport: http::Method,
) -> Response {
    let method = effective_method(transport, method);
    let path = format!("/purge/{class_name}");
    run(
        &state,
        &authority,
        Incoming {
            method,
            route: Route::Purge { class_name },
            params: Params::default(),
            body: None,
            path,
        },
    )
    .await
}

/// `POST /batch`.
///
/// Not routed through [`dispatch`], because a batch is the thing that *calls* the dispatcher. The
/// context is resolved here, once, and shared by every sub-request.
pub async fn batch(
    State(state): State<AppState>,
    authority: Authority,
    body: Option<Json<Json_>>,
) -> Response {
    let rc = match state.request_context(&authority).await {
        Ok(rc) => rc,
        Err(e) => return ParseErrorResponse(e).into_response(),
    };
    let body = body.map(|b| b.0);
    let mount_path = state.config().mount_path.clone();
    match crate::routes::batch::handle(&state, &rc, &authority, &mount_path, body.as_ref()).await {
        Ok(results) => Json(results).into_response(),
        Err(e) => ParseErrorResponse(e).into_response(),
    }
}