| 298 | } |
| 299 | |
| 300 | static inline |
| 301 | bool EnsureAuthenticatedUser( |
| 302 | const HttpApiRequest& request, |
| 303 | HttpApiResponse& response, |
| 304 | boost::asio::yield_context& yc |
| 305 | ) |
| 306 | { |
| 307 | namespace http = boost::beast::http; |
| 308 | |
| 309 | if (!request.User()) { |
| 310 | Log(LogWarning, "HttpServerConnection") |
| 311 | << "Unauthorized request: " << request.method_string() << ' ' << request.target(); |
| 312 | |
| 313 | response.result(http::status::unauthorized); |
| 314 | response.set(http::field::www_authenticate, "Basic realm=\"Icinga 2\""); |
| 315 | response.set(http::field::connection, "close"); |
| 316 | |
| 317 | if (request[http::field::accept] == "application/json") { |
| 318 | HttpUtility::SendJsonError(response, nullptr, 401, "Unauthorized. Please check your user credentials."); |
| 319 | } else { |
| 320 | response.set(http::field::content_type, "text/html"); |
| 321 | response.body() << "<h1>Unauthorized. Please check your user credentials.</h1>"; |
| 322 | } |
| 323 | |
| 324 | response.Flush(yc); |
| 325 | |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | static inline |
| 333 | bool EnsureValidBody( |
no test coverage detected