resp->headers["access-control-allow-origin"] = "*";
if (api_key.empty()) {
- L<<Logger::Debug<<"HTTP API Request \"" << req->url.path << "\": Authentication failed, API Key missing in config" << endl;
- throw HttpUnauthorizedException();
+ L<<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;
if (!auth_ok) {
- L<<Logger::Debug<<"HTTP Request \"" << req->url.path << "\": Authentication by API Key failed" << endl;
- throw HttpBadRequestException();
+ L<<Logger::Error<<"HTTP Request \"" << req->url.path << "\": Authentication by API Key failed" << endl;
+ throw HttpUnauthorizedException("X-API-Key");
}
resp->headers["Content-Type"] = "application/json";
bool auth_ok = req->compareAuthorization(web_password);
if (!auth_ok) {
L<<Logger::Debug<<"HTTP Request \"" << req->url.path << "\": Web Authentication failed" << endl;
- throw HttpUnauthorizedException();
+ throw HttpUnauthorizedException("Basic");
}
}
class HttpUnauthorizedException : public HttpException {
public:
- HttpUnauthorizedException() : HttpException(401)
+ HttpUnauthorizedException(string const &scheme) : HttpException(401)
{
- d_response.headers["WWW-Authenticate"] = "Basic realm=\"PowerDNS\"";
+ d_response.headers["WWW-Authenticate"] = scheme + " realm=\"PowerDNS\"";
}
};
+class HttpForbiddenException : public HttpException {
+public:
+ HttpForbiddenException() : HttpException(403) { };
+};
+
class HttpNotFoundException : public HttpException {
public:
HttpNotFoundException() : HttpException(404) { };
def test_unauth(self):
r = requests.get(self.url("/servers/localhost"))
- self.assertEquals(r.status_code, requests.codes.bad_request)
+ self.assertEquals(r.status_code, requests.codes.unauthorized)
def test_split_request(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)