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:
+26
-2
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user