diff options
Diffstat (limited to 'modules/rpc/jsonrpc.cpp')
-rw-r--r-- | modules/rpc/jsonrpc.cpp | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/modules/rpc/jsonrpc.cpp b/modules/rpc/jsonrpc.cpp index cb6835d47..8069ed8eb 100644 --- a/modules/rpc/jsonrpc.cpp +++ b/modules/rpc/jsonrpc.cpp @@ -99,7 +99,7 @@ public: auto *doc = yyjson_read_opts(const_cast<char *>(message.content.c_str()), message.content.length(), flags, nullptr, &error); if (!doc) { - SendError(reply, RPC::ERR_PARSE_ERROR, Anope::printf("JSON parse error #%u: %s", error.code, error.msg)); + SendError(reply, RPC::ERR_PARSE_ERROR, Anope::printf("JSON parse error #%u: %s", error.code, error.msg)); return true; } @@ -131,6 +131,16 @@ public: return true; } + if (!tokens.empty()) + { + auto it = message.headers.find("Authorization"); + if (it == message.headers.end() || !CanExecute(it->second, request.name)) + { + SendError(reply, RPC::ERR_METHOD_NOT_FOUND, "No authorization for method: " + request.name, id); + return true; + } + } + auto *params = yyjson_obj_get(root, "params"); size_t idx, max; yyjson_val *val; @@ -262,10 +272,24 @@ public: if (httpref) httpref->UnregisterPage(&jsonrpcinterface); - this->httpref = ServiceReference<HTTPProvider>("HTTPProvider", conf.GetModule(this).Get<const Anope::string>("server", "httpd/main")); + const auto &modconf = conf.GetModule(this); + this->httpref = ServiceReference<HTTPProvider>("HTTPProvider", modconf.Get<const Anope::string>("server", "httpd/main")); if (!httpref) throw ConfigException("Unable to find http reference, is httpd loaded?"); + jsonrpcinterface.tokens.clear(); + for (int i = 0; i < modconf.CountBlock("token"); ++i) + { + const auto &block = modconf.GetBlock("token", i); + const auto &token = block.Get<const Anope::string>("token"); + if (!token.empty()) + { + std::vector<Anope::string> methods; + spacesepstream(block.Get<const Anope::string>("methods")).GetTokens(methods); + jsonrpcinterface.tokens.emplace(token, methods); + } + } + httpref->RegisterPage(&jsonrpcinterface); } }; |