1
0
mirror of https://github.com/anope/anope.git synced 2026-07-04 12:53:14 +02:00

Add support for bearer tokens for authorising with RPC.

This commit is contained in:
Sadie Powell
2025-05-04 14:14:19 +01:00
parent 0b2b00b37d
commit 4b854d3935
5 changed files with 124 additions and 10 deletions
+26 -2
View File
@@ -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);
}
};