blob: 89146024c3f70acced62dda854c9195caf2ef814 [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_response.hpp"
5#include "http_utility.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08006#include "sessions.hpp"
Ed Tanousd4b6c662021-03-10 13:29:30 -08007
Ed Tanousd7857202025-01-28 15:32:26 -08008#include <boost/beast/http/field.hpp>
9#include <boost/beast/http/status.hpp>
Ed Tanousc51a58e2023-03-27 14:43:19 -070010#include <boost/url/format.hpp>
11#include <boost/url/url.hpp>
12
Ed Tanousd7857202025-01-28 15:32:26 -080013#include <string_view>
14
Ed Tanousd4b6c662021-03-10 13:29:30 -080015namespace forward_unauthorized
16{
17
Ed Tanouscf9e4172022-12-21 09:30:16 -080018// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Ed Tanous4f48d5f2021-06-21 08:27:45 -070019static bool hasWebuiRoute = false;
Ed Tanousd4b6c662021-03-10 13:29:30 -080020
Ed Tanousc127a0f2022-05-11 15:23:59 -070021inline void sendUnauthorized(std::string_view url,
22 std::string_view xRequestedWith,
John Edward Broadbent59b98b22021-07-13 15:36:32 -070023 std::string_view accept, crow::Response& res)
Ed Tanousd4b6c662021-03-10 13:29:30 -080024{
25 // If it's a browser connecting, don't send the HTTP authenticate
26 // header, to avoid possible CSRF attacks with basic auth
Ed Tanous4a0e1a02022-09-21 15:28:04 -070027 if (http_helpers::isContentTypeAllowed(
28 accept, http_helpers::ContentType::HTML, false /*allowWildcard*/))
Ed Tanousd4b6c662021-03-10 13:29:30 -080029 {
30 // If we have a webui installed, redirect to that login page
31 if (hasWebuiRoute)
32 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040033 boost::urls::url forward =
34 boost::urls::format("/?next={}#/login", url);
Ed Tanousd4b6c662021-03-10 13:29:30 -080035 res.result(boost::beast::http::status::temporary_redirect);
Ed Tanousd9f6c622022-03-17 09:12:17 -070036 res.addHeader(boost::beast::http::field::location,
Ed Tanousc51a58e2023-03-27 14:43:19 -070037 forward.buffer());
Ed Tanous347d1a12022-06-22 15:49:37 -070038 return;
Ed Tanousd4b6c662021-03-10 13:29:30 -080039 }
Ed Tanous347d1a12022-06-22 15:49:37 -070040 // If we don't have a webui installed, just return an unauthorized
41 // body
Ed Tanousd4b6c662021-03-10 13:29:30 -080042 res.result(boost::beast::http::status::unauthorized);
Ed Tanous27b0cf92023-08-07 12:02:40 -070043 res.write(
44 "No authentication provided, and no login UI present to forward to.");
Ed Tanous347d1a12022-06-22 15:49:37 -070045 return;
Ed Tanousd4b6c662021-03-10 13:29:30 -080046 }
Ed Tanous347d1a12022-06-22 15:49:37 -070047
48 res.result(boost::beast::http::status::unauthorized);
49
50 // XHR requests from a browser will set the X-Requested-With header when
51 // doing their requests, even though they might not be requesting html.
52 if (!xRequestedWith.empty())
53 {
54 return;
55 }
56 // if basic auth is disabled, don't propose it.
57 if (!persistent_data::SessionStore::getInstance()
58 .getAuthMethodsConfig()
59 .basic)
60 {
61 return;
62 }
Ed Tanousd9f6c622022-03-17 09:12:17 -070063 res.addHeader(boost::beast::http::field::www_authenticate, "Basic");
Ed Tanousd4b6c662021-03-10 13:29:30 -080064}
65} // namespace forward_unauthorized