2026-10-25 09:14:26 +01:00
|
|
|
#include "../GoogleDemos.hpp"
|
|
|
|
|
|
2026-10-25 15:03:45 +01:00
|
|
|
#include <map>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
|
|
|
2026-10-25 09:14:26 +01:00
|
|
|
#include "../../fw/Router.hpp"
|
2026-10-25 15:03:45 +01:00
|
|
|
#include "../../fw/Templates.hpp"
|
|
|
|
|
#include "google/credentials.h"
|
|
|
|
|
#include "google/workspace/calls.h"
|
|
|
|
|
#include "google/workspace/caps.h"
|
|
|
|
|
#include "google/workspace/workspace_client.h"
|
2026-10-25 09:14:26 +01:00
|
|
|
|
|
|
|
|
namespace molo::example::app {
|
|
|
|
|
|
2026-10-25 15:03:45 +01:00
|
|
|
// Google Workspace (Gmail, Drive, Calendar, Sheets, Docs, Slides, Forms, Tasks,
|
|
|
|
|
// Chat, Meet, People, Keep, Classroom, YouTube Data / Analytics / Reporting,
|
|
|
|
|
// Admin SDK Directory and Reports, Groups Settings, Vault) over the real
|
|
|
|
|
// src/google/workspace part. Every Discovery method is reachable through
|
|
|
|
|
// POST /google/workspace/call. GET routes need no credential. Reads answer 503
|
|
|
|
|
// google_not_configured without one, 422 on bad input (validated first).
|
|
|
|
|
// Writes, deletes and sends are dry runs unless the body says "confirm":true;
|
|
|
|
|
// Workspace data belongs to humans, so the page never defaults it.
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
namespace ws = google::workspace;
|
|
|
|
|
using Json = clients::body::Json;
|
|
|
|
|
|
|
|
|
|
constexpr std::size_t kMaxBody = 512 * 1024;
|
|
|
|
|
constexpr std::size_t kMaxCallsShown = 300;
|
|
|
|
|
constexpr std::size_t kMaxClients = 40;
|
|
|
|
|
|
|
|
|
|
const char kPage[] = R"WORKSPACE({% extends "base.html" %}
|
|
|
|
|
{% block content %}
|
|
|
|
|
<h1>Google Workspace</h1>
|
|
|
|
|
<p class="hint">Twenty Workspace APIs, 714 calls from Google's Discovery documents.
|
|
|
|
|
Reads run at once; writes, deletes and sends are dry runs until you tick confirm.</p>
|
|
|
|
|
<p id="ws-config" class="hint">Checking credentials...</p>
|
|
|
|
|
<section class="ws">
|
|
|
|
|
<label>API <select id="ws-api"></select></label>
|
|
|
|
|
<label>Kind <select id="ws-kind">
|
|
|
|
|
<option value="">all</option><option value="r">read</option><option value="w">write</option>
|
|
|
|
|
<option value="s">send</option><option value="d">destroy</option></select></label>
|
|
|
|
|
<label>Call <select id="ws-call"></select></label>
|
|
|
|
|
<p id="ws-sig" class="hint"></p>
|
|
|
|
|
<label>Parameters (JSON object) <textarea id="ws-params" rows="3">{}</textarea></label>
|
|
|
|
|
<label>Body (JSON object, optional) <textarea id="ws-body" rows="3"></textarea></label>
|
|
|
|
|
<label class="ws-check"><input id="ws-confirm" type="checkbox"> confirm (only for write, delete, send)</label>
|
|
|
|
|
<div class="ws-actions">
|
|
|
|
|
<button id="ws-run" type="button" class="primary">Run</button>
|
|
|
|
|
<button id="ws-list" type="button">Run as list (pages)</button>
|
|
|
|
|
</div>
|
|
|
|
|
<p id="ws-error" class="error" role="alert"></p>
|
|
|
|
|
<pre id="ws-out" class="ws-out"></pre>
|
|
|
|
|
</section>
|
|
|
|
|
<style>
|
|
|
|
|
.ws label{display:block;margin:.5rem 0}
|
|
|
|
|
.ws select,.ws textarea{width:100%;box-sizing:border-box}
|
|
|
|
|
.ws-check input{width:auto}
|
|
|
|
|
.ws-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin:.5rem 0}
|
|
|
|
|
.ws-out{overflow:auto;max-height:24rem;white-space:pre-wrap;word-break:break-word}
|
|
|
|
|
</style>
|
|
|
|
|
<script src="/embedded/google_workspace.js"></script>
|
|
|
|
|
{% endblock %}
|
|
|
|
|
)WORKSPACE";
|
|
|
|
|
|
|
|
|
|
fw::Response json_out(const Json& j, int status = 200) {
|
|
|
|
|
fw::Response res = fw::JsonResponse(j.dump(), status);
|
|
|
|
|
res.headers.set("Cache-Control", "no-store");
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); }
|
|
|
|
|
|
|
|
|
|
[[noreturn]] void rethrow(const google::GoogleError& e) {
|
|
|
|
|
if (e.http_status() == 503 && e.status() == "NOT_CONFIGURED")
|
|
|
|
|
throw fw::HttpError(503, "google_not_configured", e.message());
|
|
|
|
|
if (e.status() == "INVALID_ARGUMENT" && e.http_status() == 400 && e.service().empty())
|
|
|
|
|
throw fw::HttpError(422, "validation_error", e.message());
|
|
|
|
|
throw fw::HttpError(502, "google_error",
|
|
|
|
|
std::to_string(e.http_status()) + " " + e.status() + ": " + e.message());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
|
auto guarded(F&& f) -> decltype(f()) {
|
|
|
|
|
try {
|
|
|
|
|
return f();
|
|
|
|
|
} catch (const google::GoogleError& e) {
|
|
|
|
|
rethrow(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json parse_body(const fw::Request& req) {
|
|
|
|
|
if (req.body.size() > kMaxBody) throw fw::HttpError(413, "payload_too_large", "body too large");
|
|
|
|
|
if (req.body.empty()) return Json::object();
|
|
|
|
|
try {
|
|
|
|
|
Json j = Json::parse(req.body);
|
|
|
|
|
if (!j.is_object()) bad("body must be a JSON object");
|
|
|
|
|
return j;
|
|
|
|
|
} catch (const clients::body::JsonError&) {
|
|
|
|
|
bad("body is not valid JSON");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string text(const Json& body, const char* key, bool required) {
|
|
|
|
|
const Json* v = body.find(key);
|
|
|
|
|
if (!v || v->is_null()) {
|
|
|
|
|
if (required) bad(std::string(key) + " is required");
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
if (!v->is_string()) bad(std::string(key) + " must be a string");
|
|
|
|
|
return v->as_string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool flag(const Json& body, const char* key) {
|
|
|
|
|
const Json* v = body.find(key);
|
|
|
|
|
if (!v || v->is_null()) return false;
|
|
|
|
|
if (!v->is_bool()) bad(std::string(key) + " must be true or false");
|
|
|
|
|
return v->as_bool();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json object_field(const Json& body, const char* key) {
|
|
|
|
|
const Json* v = body.find(key);
|
|
|
|
|
if (!v || v->is_null()) return Json::object();
|
|
|
|
|
if (!v->is_object()) bad(std::string(key) + " must be a JSON object");
|
|
|
|
|
return *v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> split_csv(const std::string& s) {
|
|
|
|
|
std::vector<std::string> out;
|
|
|
|
|
std::string cur;
|
|
|
|
|
for (char c : s) {
|
|
|
|
|
if (c == ',') {
|
|
|
|
|
if (!cur.empty()) out.push_back(cur);
|
|
|
|
|
cur.clear();
|
|
|
|
|
} else {
|
|
|
|
|
cur += c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!cur.empty()) out.push_back(cur);
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* kind_word(ws::Kind k) {
|
|
|
|
|
switch (k) {
|
|
|
|
|
case ws::Kind::kRead: return "read";
|
|
|
|
|
case ws::Kind::kWrite: return "write";
|
|
|
|
|
case ws::Kind::kDestroy: return "destroy";
|
|
|
|
|
case ws::Kind::kSend: return "send";
|
|
|
|
|
}
|
|
|
|
|
return "?";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// One object per route family: owns the client policy. An injected client
|
|
|
|
|
// (services.google) is used as is; otherwise a client is built per API and
|
|
|
|
|
// access level from the environment, because Workspace needs per-API scopes.
|
|
|
|
|
class WorkspaceRoutes {
|
|
|
|
|
public:
|
|
|
|
|
explicit WorkspaceRoutes(std::shared_ptr<google::GoogleClient> injected)
|
|
|
|
|
: injected_(std::move(injected)) {}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<google::GoogleClient> client_for(const std::string& api, bool readonly) {
|
|
|
|
|
if (injected_) return injected_;
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
std::string key = api + (readonly ? ":ro" : ":rw");
|
|
|
|
|
auto it = clients_.find(key);
|
|
|
|
|
if (it != clients_.end()) return it->second;
|
|
|
|
|
if (clients_.size() >= kMaxClients) clients_.clear();
|
|
|
|
|
ws::WorkspaceOptions o;
|
|
|
|
|
o.apis = {api};
|
|
|
|
|
o.readonly = readonly;
|
|
|
|
|
auto c = ws::make_workspace_client(o);
|
|
|
|
|
clients_[key] = c;
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string mode() const {
|
|
|
|
|
if (injected_) return injected_->configured() ? "injected" : "none";
|
|
|
|
|
return ws::WorkspaceAuth::mode_from_env(ws::WorkspaceOptions{});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ws::CallSpec& call_of(const Json& body) const {
|
|
|
|
|
std::string api = text(body, "api", true), id = text(body, "id", true);
|
|
|
|
|
if (!ws::find_api(api)) bad("unknown api " + api);
|
|
|
|
|
const ws::CallSpec* spec = ws::find_call_in(api, id);
|
|
|
|
|
if (!spec) bad("unknown call " + id + " in " + api);
|
|
|
|
|
return *spec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fw::Response call(const fw::Request& req) {
|
|
|
|
|
Json body = parse_body(req);
|
|
|
|
|
const ws::CallSpec& spec = call_of(body);
|
|
|
|
|
Json params = object_field(body, "params");
|
|
|
|
|
const Json* payload = body.find("body");
|
|
|
|
|
if (payload && payload->is_null()) payload = nullptr;
|
|
|
|
|
bool confirm = flag(body, "confirm");
|
|
|
|
|
ws::Kind kind = ws::kind_of(spec);
|
|
|
|
|
std::string api = spec.api;
|
|
|
|
|
return guarded([&] {
|
|
|
|
|
// Validate with a client-free API object first: 422 before any credential check.
|
|
|
|
|
auto probe = ws::WorkspaceApi(client_for(api, true), api);
|
|
|
|
|
ws::PreparedCall prepared = probe.prepare(spec.id, params, payload);
|
|
|
|
|
if (kind != ws::Kind::kRead && !confirm) {
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("dry_run", Json(true));
|
|
|
|
|
o.set("kind", Json(kind_word(kind)));
|
|
|
|
|
o.set("http", Json(spec.http));
|
|
|
|
|
o.set("path", Json(prepared.path));
|
|
|
|
|
o.set("hint", Json("send confirm:true to run it"));
|
|
|
|
|
return json_out(o);
|
|
|
|
|
}
|
|
|
|
|
auto client = client_for(api, kind == ws::Kind::kRead);
|
|
|
|
|
ws::WorkspaceApi real(client, api);
|
|
|
|
|
switch (kind) {
|
|
|
|
|
case ws::Kind::kRead: return json_out(real.read(spec.id, params, payload));
|
|
|
|
|
case ws::Kind::kWrite: return json_out(real.write(spec.id, params, payload, true));
|
|
|
|
|
case ws::Kind::kDestroy: return json_out(real.destroy(spec.id, params, true));
|
|
|
|
|
case ws::Kind::kSend: return json_out(real.send(spec.id, params, payload, true));
|
|
|
|
|
}
|
|
|
|
|
bad("unknown kind");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fw::Response list(const fw::Request& req) {
|
|
|
|
|
Json body = parse_body(req);
|
|
|
|
|
const ws::CallSpec& spec = call_of(body);
|
|
|
|
|
Json params = object_field(body, "params");
|
|
|
|
|
int pages = 1;
|
|
|
|
|
if (const Json* p = body.find("maxPages")) {
|
|
|
|
|
if (!p->is_number()) bad("maxPages must be a number");
|
|
|
|
|
pages = static_cast<int>(p->as_int());
|
|
|
|
|
if (pages < 1 || pages > 5) bad("maxPages must be 1..5");
|
|
|
|
|
}
|
|
|
|
|
std::string api = spec.api;
|
|
|
|
|
return guarded([&] {
|
|
|
|
|
ws::WorkspaceApi real(client_for(api, true), api);
|
|
|
|
|
real.prepare(spec.id, params);
|
|
|
|
|
if (ws::kind_of(spec) != ws::Kind::kRead) bad(std::string(spec.id) + " is not a read call");
|
|
|
|
|
if (!*spec.items_key) bad(std::string(spec.id) + " has no single items array");
|
|
|
|
|
ws::Listing l = real.list(spec.id, params, pages);
|
|
|
|
|
Json items = Json::array();
|
|
|
|
|
for (const Json& i : l.items) items.push(i);
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("items", items);
|
|
|
|
|
o.set("count", Json(static_cast<int>(l.items.size())));
|
|
|
|
|
o.set("pages", Json(l.pages));
|
|
|
|
|
o.set("truncated", Json(l.truncated));
|
|
|
|
|
if (!l.next_page_token.empty()) o.set("nextPageToken", Json(l.next_page_token));
|
|
|
|
|
return json_out(o);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::shared_ptr<google::GoogleClient> injected_;
|
|
|
|
|
std::mutex mutex_;
|
|
|
|
|
std::map<std::string, std::shared_ptr<google::GoogleClient>> clients_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2026-10-25 09:14:26 +01:00
|
|
|
void register_google_workspace_demo(fw::Router& router, Services& services) {
|
2026-10-25 15:03:45 +01:00
|
|
|
auto routes = std::make_shared<WorkspaceRoutes>(services.google);
|
|
|
|
|
fw::Environment* env = services.templates.get();
|
|
|
|
|
services.templates->add_template("google_workspace.html", kPage);
|
|
|
|
|
services.add_feature("google-workspace", "/google/workspace");
|
|
|
|
|
|
|
|
|
|
router.get("/google/workspace", "google-workspace-page", [env](fw::Request req) {
|
|
|
|
|
(void)req;
|
|
|
|
|
fw::TemplateCtx ctx;
|
|
|
|
|
ctx["title"] = std::string("Google Workspace");
|
|
|
|
|
return fw::Response(200, env->render("google_workspace.html", ctx), "text/html; charset=utf-8");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.get("/google/workspace/status", "google-workspace-status", [routes](fw::Request req) {
|
|
|
|
|
(void)req;
|
|
|
|
|
std::string mode = routes->mode();
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("configured", Json(mode != "none"));
|
|
|
|
|
o.set("mode", Json(mode));
|
|
|
|
|
o.set("apis", Json(static_cast<int>(ws::kApiTableSize)));
|
|
|
|
|
o.set("calls", Json(static_cast<int>(ws::kCallTableSize)));
|
|
|
|
|
o.set("subject_set",
|
|
|
|
|
Json(!google::Credentials::env("GOOGLE_WORKSPACE_SUBJECT").empty()));
|
|
|
|
|
o.set("max_pages", Json(5));
|
|
|
|
|
return json_out(o);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.get("/google/workspace/apis", "google-workspace-apis", [](fw::Request req) {
|
|
|
|
|
(void)req;
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (std::size_t i = 0; i < ws::kApiTableSize; ++i) {
|
|
|
|
|
const ws::ApiSpec& a = ws::kApiTable[i];
|
|
|
|
|
Json row = Json::object();
|
|
|
|
|
row.set("name", Json(a.name));
|
|
|
|
|
row.set("discovery", Json(a.discovery_id));
|
|
|
|
|
row.set("revision", Json(a.revision));
|
|
|
|
|
row.set("base_url", Json(a.base_url));
|
|
|
|
|
row.set("service", Json(a.service));
|
|
|
|
|
row.set("calls", Json(static_cast<int>(ws::calls_of(a.name).size())));
|
|
|
|
|
row.set("scopes", Json(static_cast<int>(ws::Scopes::all_of(a.name).size())));
|
|
|
|
|
row.set("readonly_scopes", Json(static_cast<int>(ws::Scopes::readonly_of(a.name).size())));
|
|
|
|
|
arr.push(row);
|
|
|
|
|
}
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("apis", arr);
|
|
|
|
|
return json_out(o);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// The discovery-checked call table, filtered: ?api=gmail&kind=r
|
|
|
|
|
router.get("/google/workspace/calls", "google-workspace-calls", [](fw::Request req) {
|
|
|
|
|
std::string api = req.query_or("api", ""), kind = req.query_or("kind", "");
|
|
|
|
|
if (!api.empty() && !ws::find_api(api)) bad("unknown api " + api);
|
|
|
|
|
if (kind.size() > 1 || (!kind.empty() && std::string("rwds").find(kind[0]) == std::string::npos))
|
|
|
|
|
bad("kind must be one of r w d s");
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
bool more = false;
|
|
|
|
|
for (std::size_t i = 0; i < ws::kCallTableSize; ++i) {
|
|
|
|
|
const ws::CallSpec& c = ws::kCallTable[i];
|
|
|
|
|
if (!api.empty() && api != c.api) continue;
|
|
|
|
|
if (!kind.empty() && kind[0] != c.kind[0]) continue;
|
|
|
|
|
if (arr.as_array().size() >= kMaxCallsShown) {
|
|
|
|
|
more = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Json row = Json::object();
|
|
|
|
|
row.set("id", Json(c.id));
|
|
|
|
|
row.set("api", Json(c.api));
|
|
|
|
|
row.set("http", Json(c.http));
|
|
|
|
|
row.set("path", Json(c.path));
|
|
|
|
|
row.set("kind", Json(kind_word(ws::kind_of(c))));
|
|
|
|
|
row.set("params", Json(c.params));
|
|
|
|
|
row.set("body", Json(c.body));
|
|
|
|
|
arr.push(row);
|
|
|
|
|
}
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("calls", arr);
|
|
|
|
|
o.set("truncated", Json(more));
|
|
|
|
|
return json_out(o);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Scopes of the named APIs: ?apis=gmail,drive&readonly=true
|
|
|
|
|
router.get("/google/workspace/scopes", "google-workspace-scopes", [](fw::Request req) {
|
|
|
|
|
std::string ro = req.query_or("readonly", "true");
|
|
|
|
|
if (ro != "true" && ro != "false") bad("readonly must be true or false");
|
|
|
|
|
return guarded([&] {
|
|
|
|
|
Json arr = Json::array();
|
|
|
|
|
for (const auto& s : ws::Scopes::for_apis(split_csv(req.query_or("apis", "")), ro == "true"))
|
|
|
|
|
arr.push(Json(s));
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("scopes", arr);
|
|
|
|
|
o.set("readonly", Json(ro == "true"));
|
|
|
|
|
return json_out(o);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Domain-wide delegation: validates the subject and scope set locally,
|
|
|
|
|
// nothing is sent to Google.
|
|
|
|
|
router.post("/google/workspace/delegation", "google-workspace-delegation", [](fw::Request req) {
|
|
|
|
|
Json body = parse_body(req);
|
|
|
|
|
std::string subject = text(body, "subject", false);
|
|
|
|
|
std::string apis = text(body, "apis", true);
|
|
|
|
|
bool readonly = body.find("readonly") ? flag(body, "readonly") : true;
|
|
|
|
|
return guarded([&] {
|
|
|
|
|
ws::Delegation d{subject};
|
|
|
|
|
d.validate();
|
|
|
|
|
Json scopes = Json::array();
|
|
|
|
|
for (const auto& s : ws::Scopes::for_apis(split_csv(apis), readonly)) scopes.push(Json(s));
|
|
|
|
|
Json o = Json::object();
|
|
|
|
|
o.set("subject", Json(subject));
|
|
|
|
|
o.set("impersonating", Json(d.active()));
|
|
|
|
|
o.set("scopes", scopes);
|
|
|
|
|
o.set("grant_at", Json("Admin console > Security > API controls > Domain-wide delegation"));
|
|
|
|
|
return json_out(o);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post("/google/workspace/call", "google-workspace-call",
|
|
|
|
|
[routes](fw::Request req) { return routes->call(req); });
|
|
|
|
|
router.post("/google/workspace/list", "google-workspace-list",
|
|
|
|
|
[routes](fw::Request req) { return routes->list(req); });
|
2026-10-25 09:14:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace molo::example::app
|