blob: 07fb2b5bd05540d398c0efd377a7431633282088 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanousd4b6c662021-03-10 13:29:30 -08003#pragma once
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08004#include "http_request.hpp"
5#include "http_response.hpp"
6#include "http_utility.hpp"
Ed Tanousd4b6c662021-03-10 13:29:30 -08007
Ed Tanousc51a58e2023-03-27 14:43:19 -07008#include <boost/url/format.hpp>
9#include <boost/url/url.hpp>
10
Ed Tanousd4b6c662021-03-10 13:29:30 -080011namespace forward_unauthorized
12{
13
Ed Tanouscf9e4172022-12-21 09:30:16 -080014// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Ed Tanous4f48d5f2021-06-21 08:27:45 -070015static bool hasWebuiRoute = false;
Ed Tanousd4b6c662021-03-10 13:29:30 -080016
Ed Tanousc127a0f2022-05-11 15:23:59 -070017inline void sendUnauthorized(std::string_view url,
18 std::string_view xRequestedWith,
John Edward Broadbent59b98b22021-07-13 15:36:32 -070019 std::string_view accept, crow::Response& res)
Ed Tanousd4b6c662021-03-10 13:29:30 -080020{
21 // If it's a browser connecting, don't send the HTTP authenticate
22 // header, to avoid possible CSRF attacks with basic auth
Ed Tanous4a0e1a02022-09-21 15:28:04 -070023 if (http_helpers::isContentTypeAllowed(
24 accept, http_helpers::ContentType::HTML, false /*allowWildcard*/))
Ed Tanousd4b6c662021-03-10 13:29:30 -080025 {
26 // If we have a webui installed, redirect to that login page
27 if (hasWebuiRoute)
28 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040029 boost::urls::url forward =
30 boost::urls::format("/?next={}#/login", url);
Ed Tanousd4b6c662021-03-10 13:29:30 -080031 res.result(boost::beast::http::status::temporary_redirect);
Ed Tanousd9f6c622022-03-17 09:12:17 -070032 res.addHeader(boost::beast::http::field::location,
Ed Tanousc51a58e2023-03-27 14:43:19 -070033 forward.buffer());
Ed Tanous347d1a12022-06-22 15:49:37 -070034 return;
Ed Tanousd4b6c662021-03-10 13:29:30 -080035 }
Ed Tanous347d1a12022-06-22 15:49:37 -070036 // If we don't have a webui installed, just return an unauthorized
37 // body
Ed Tanousd4b6c662021-03-10 13:29:30 -080038 res.result(boost::beast::http::status::unauthorized);
Ed Tanous27b0cf92023-08-07 12:02:40 -070039 res.write(
40 "No authentication provided, and no login UI present to forward to.");
Ed Tanous347d1a12022-06-22 15:49:37 -070041 return;
Ed Tanousd4b6c662021-03-10 13:29:30 -080042 }
Ed Tanous347d1a12022-06-22 15:49:37 -070043
44 res.result(boost::beast::http::status::unauthorized);
45
46 // XHR requests from a browser will set the X-Requested-With header when
47 // doing their requests, even though they might not be requesting html.
48 if (!xRequestedWith.empty())
49 {
50 return;
51 }
52 // if basic auth is disabled, don't propose it.
53 if (!persistent_data::SessionStore::getInstance()
54 .getAuthMethodsConfig()
55 .basic)
56 {
57 return;
58 }
Ed Tanousd9f6c622022-03-17 09:12:17 -070059 res.addHeader(boost::beast::http::field::www_authenticate, "Basic");
Ed Tanousd4b6c662021-03-10 13:29:30 -080060}
61} // namespace forward_unauthorized