return false;
}
-static void apiWrapper(WebServer::HandlerFunction handler, HttpRequest* req, HttpResponse* resp) {
- const string& api_key = arg()["api-key"];
-
+static void apiWrapper(WebServer::HandlerFunction handler, HttpRequest* req, HttpResponse* resp, const string &apikey) {
if (optionsHandler(req, resp)) return;
resp->headers["access-control-allow-origin"] = "*";
- if (api_key.empty()) {
+ if (apikey.empty()) {
g_log<<Logger::Error<<"HTTP API Request \"" << req->url.path << "\": Authentication failed, API Key missing in config" << endl;
throw HttpUnauthorizedException("X-API-Key");
}
- bool auth_ok = req->compareHeader("x-api-key", api_key) || req->getvars["api-key"]==api_key;
+ bool auth_ok = req->compareHeader("x-api-key", apikey) || req->getvars["api-key"] == apikey;
if (!auth_ok) {
g_log<<Logger::Error<<"HTTP Request \"" << req->url.path << "\": Authentication by API Key failed" << endl;
}
void WebServer::registerApiHandler(const string& url, HandlerFunction handler) {
- HandlerFunction f = boost::bind(&apiWrapper, handler, _1, _2);
+ HandlerFunction f = boost::bind(&apiWrapper, handler, _1, _2, d_apikey);
registerBareHandler(url, f);
+ d_registerApiHandlerCalled = true;
}
static void webWrapper(WebServer::HandlerFunction handler, HttpRequest* req, HttpResponse* resp) {
public:
WebServer(const string &listenaddress, int port);
virtual ~WebServer() { };
+
+ void setApiKey(const string &apikey) {
+ if (d_registerApiHandlerCalled) {
+ throw PDNSException("registerApiHandler has been called, can not change apikey");
+ }
+ d_apikey = apikey;
+ }
+
void bind();
void go();
int d_port;
string d_password;
std::shared_ptr<Server> d_server;
+
+ std::string d_apikey;
+ bool d_registerApiHandlerCalled{false};
};
#endif /* WEBSERVER_HH */