|
@@ -26,17 +26,42 @@ struct Target {
|
|
|
CollectionMeta meta;
|
|
CollectionMeta meta;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-// Authorize + verify the collection exists.
|
|
|
|
|
-Target scoped(ServerDeps* d, const httplib::Request& req, int projIdx, int collIdx, KeyOp op) {
|
|
|
|
|
|
|
+// Authorize only - everything `scoped()` does BEFORE the resource has to exist.
|
|
|
|
|
+//
|
|
|
|
|
+// Split out so a route can refuse a REQUEST-level fault BETWEEN authorization
|
|
|
|
|
+// and the 404, which is the one ordering rule this service holds everywhere:
|
|
|
|
|
+// authorize, then the server's own capability (501), then the REQUEST (422),
|
|
|
|
|
+// then the resource (404). A request-level fault that no amount of creating the
|
|
|
|
|
+// collection would fix must not hide behind the collection's absence.
|
|
|
|
|
+//
|
|
|
|
|
+// It exists because PUT's `ttl_seconds` refusal was on the wrong side of that
|
|
|
|
|
+// line and the defect was visible from outside: the SAME refused parameter
|
|
|
|
|
+// answered 422 on a collection that existed and 404 on one that did not, so a
|
|
|
|
|
+// caller who misspelled the collection never learned the parameter is
|
|
|
|
|
+// unsupported - they fixed the name, retried, and only then met the 422.
|
|
|
|
|
+Target authorized(ServerDeps* d, const httplib::Request& req, int projIdx, int collIdx, KeyOp op) {
|
|
|
Target t;
|
|
Target t;
|
|
|
t.key = requireKey(*d, req);
|
|
t.key = requireKey(*d, req);
|
|
|
t.project = req.matches[projIdx];
|
|
t.project = req.matches[projIdx];
|
|
|
t.name = req.matches[collIdx];
|
|
t.name = req.matches[collIdx];
|
|
|
requireCapability(*d, t.key, req, t.project, t.name, op);
|
|
requireCapability(*d, t.key, req, t.project, t.name, op);
|
|
|
|
|
+ t.qualified = qualify(t.project, t.name);
|
|
|
|
|
+ return t;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// Fill in the collection metadata, 404 if there is no such collection.
|
|
|
|
|
+void requireCollection(ServerDeps* d, Target& t) {
|
|
|
auto meta = d->registry.get(t.project, t.name);
|
|
auto meta = d->registry.get(t.project, t.name);
|
|
|
if (!meta) throw ApiError(ErrCode::NotFound, "not_found", "no such collection");
|
|
if (!meta) throw ApiError(ErrCode::NotFound, "not_found", "no such collection");
|
|
|
t.meta = *meta;
|
|
t.meta = *meta;
|
|
|
- t.qualified = qualify(t.project, t.name);
|
|
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// Authorize + verify the collection exists. The composition of the two above,
|
|
|
|
|
+// so there is ONE implementation of each step rather than a second copy in the
|
|
|
|
|
+// one route that needs them apart.
|
|
|
|
|
+Target scoped(ServerDeps* d, const httplib::Request& req, int projIdx, int collIdx, KeyOp op) {
|
|
|
|
|
+ Target t = authorized(d, req, projIdx, collIdx, op);
|
|
|
|
|
+ requireCollection(d, t);
|
|
|
return t;
|
|
return t;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -284,7 +309,11 @@ void registerDocumentRoutes(ApiServer& s) {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
svr.Put(R"(/api/v1/projects/([^/]+)/collections/([^/]+)/documents/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) {
|
|
svr.Put(R"(/api/v1/projects/([^/]+)/collections/([^/]+)/documents/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) {
|
|
|
- Target t = scoped(d, req, 1, 2, KeyOp::Update);
|
|
|
|
|
|
|
+ // AUTHORIZE first, then the REQUEST, then the RESOURCE - see
|
|
|
|
|
+ // `authorized()` above. The parameter refusal below must come before
|
|
|
|
|
+ // the collection's 404, or the same refused parameter answers 422 on a
|
|
|
|
|
+ // collection that exists and 404 on one that does not.
|
|
|
|
|
+ Target t = authorized(d, req, 1, 2, KeyOp::Update);
|
|
|
std::string id = req.matches[3];
|
|
std::string id = req.matches[3];
|
|
|
|
|
|
|
|
// REFUSE `?ttl_seconds=` HERE, because this route CANNOT honour it and
|
|
// REFUSE `?ttl_seconds=` HERE, because this route CANNOT honour it and
|
|
@@ -317,6 +346,9 @@ void registerDocumentRoutes(ApiServer& s) {
|
|
|
"no TTL. POST with an id to create a row WITH one, or PATCH to set or clear it on a "
|
|
"no TTL. POST with an id to create a row WITH one, or PATCH to set or clear it on a "
|
|
|
"row that already exists");
|
|
"row that already exists");
|
|
|
|
|
|
|
|
|
|
+ // The resource LAST of the three.
|
|
|
|
|
+ requireCollection(d, t);
|
|
|
|
|
+
|
|
|
const auto ifVersion = ifVersionParam(req);
|
|
const auto ifVersion = ifVersionParam(req);
|
|
|
nlohmann::json data = documentBody(bodyJson(req));
|
|
nlohmann::json data = documentBody(bodyJson(req));
|
|
|
|
|
|