xref: /openbmc/bmcweb/http/routing/baserule.hpp (revision 08bbe1199f02d09f908cd3adcf4329e4bd67fd52)
1*08bbe119SEd Tanous #pragma once
2*08bbe119SEd Tanous 
3*08bbe119SEd Tanous #include "async_resp.hpp"
4*08bbe119SEd Tanous #include "http_request.hpp"
5*08bbe119SEd Tanous #include "privileges.hpp"
6*08bbe119SEd Tanous #include "verb.hpp"
7*08bbe119SEd Tanous 
8*08bbe119SEd Tanous #include <boost/beast/ssl/ssl_stream.hpp>
9*08bbe119SEd Tanous 
10*08bbe119SEd Tanous #include <memory>
11*08bbe119SEd Tanous #include <string>
12*08bbe119SEd Tanous 
13*08bbe119SEd Tanous namespace crow
14*08bbe119SEd Tanous {
15*08bbe119SEd Tanous class BaseRule
16*08bbe119SEd Tanous {
17*08bbe119SEd Tanous   public:
18*08bbe119SEd Tanous     explicit BaseRule(const std::string& thisRule) : rule(thisRule) {}
19*08bbe119SEd Tanous 
20*08bbe119SEd Tanous     virtual ~BaseRule() = default;
21*08bbe119SEd Tanous 
22*08bbe119SEd Tanous     BaseRule(const BaseRule&) = delete;
23*08bbe119SEd Tanous     BaseRule(BaseRule&&) = delete;
24*08bbe119SEd Tanous     BaseRule& operator=(const BaseRule&) = delete;
25*08bbe119SEd Tanous     BaseRule& operator=(const BaseRule&&) = delete;
26*08bbe119SEd Tanous 
27*08bbe119SEd Tanous     virtual void validate() = 0;
28*08bbe119SEd Tanous     std::unique_ptr<BaseRule> upgrade()
29*08bbe119SEd Tanous     {
30*08bbe119SEd Tanous         if (ruleToUpgrade)
31*08bbe119SEd Tanous         {
32*08bbe119SEd Tanous             return std::move(ruleToUpgrade);
33*08bbe119SEd Tanous         }
34*08bbe119SEd Tanous         return {};
35*08bbe119SEd Tanous     }
36*08bbe119SEd Tanous 
37*08bbe119SEd Tanous     virtual void handle(const Request& /*req*/,
38*08bbe119SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>&,
39*08bbe119SEd Tanous                         const std::vector<std::string>&) = 0;
40*08bbe119SEd Tanous #ifndef BMCWEB_ENABLE_SSL
41*08bbe119SEd Tanous     virtual void
42*08bbe119SEd Tanous         handleUpgrade(const Request& /*req*/,
43*08bbe119SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
44*08bbe119SEd Tanous                       boost::asio::ip::tcp::socket&& /*adaptor*/)
45*08bbe119SEd Tanous     {
46*08bbe119SEd Tanous         asyncResp->res.result(boost::beast::http::status::not_found);
47*08bbe119SEd Tanous     }
48*08bbe119SEd Tanous #else
49*08bbe119SEd Tanous     virtual void handleUpgrade(
50*08bbe119SEd Tanous         const Request& /*req*/,
51*08bbe119SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
52*08bbe119SEd Tanous         boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&& /*adaptor*/)
53*08bbe119SEd Tanous     {
54*08bbe119SEd Tanous         asyncResp->res.result(boost::beast::http::status::not_found);
55*08bbe119SEd Tanous     }
56*08bbe119SEd Tanous #endif
57*08bbe119SEd Tanous 
58*08bbe119SEd Tanous     size_t getMethods() const
59*08bbe119SEd Tanous     {
60*08bbe119SEd Tanous         return methodsBitfield;
61*08bbe119SEd Tanous     }
62*08bbe119SEd Tanous 
63*08bbe119SEd Tanous     bool checkPrivileges(const redfish::Privileges& userPrivileges)
64*08bbe119SEd Tanous     {
65*08bbe119SEd Tanous         // If there are no privileges assigned, assume no privileges
66*08bbe119SEd Tanous         // required
67*08bbe119SEd Tanous         if (privilegesSet.empty())
68*08bbe119SEd Tanous         {
69*08bbe119SEd Tanous             return true;
70*08bbe119SEd Tanous         }
71*08bbe119SEd Tanous 
72*08bbe119SEd Tanous         for (const redfish::Privileges& requiredPrivileges : privilegesSet)
73*08bbe119SEd Tanous         {
74*08bbe119SEd Tanous             if (userPrivileges.isSupersetOf(requiredPrivileges))
75*08bbe119SEd Tanous             {
76*08bbe119SEd Tanous                 return true;
77*08bbe119SEd Tanous             }
78*08bbe119SEd Tanous         }
79*08bbe119SEd Tanous         return false;
80*08bbe119SEd Tanous     }
81*08bbe119SEd Tanous 
82*08bbe119SEd Tanous     size_t methodsBitfield{1 << static_cast<size_t>(HttpVerb::Get)};
83*08bbe119SEd Tanous     static_assert(std::numeric_limits<decltype(methodsBitfield)>::digits >
84*08bbe119SEd Tanous                       methodNotAllowedIndex,
85*08bbe119SEd Tanous                   "Not enough bits to store bitfield");
86*08bbe119SEd Tanous 
87*08bbe119SEd Tanous     std::vector<redfish::Privileges> privilegesSet;
88*08bbe119SEd Tanous 
89*08bbe119SEd Tanous     std::string rule;
90*08bbe119SEd Tanous 
91*08bbe119SEd Tanous     std::unique_ptr<BaseRule> ruleToUpgrade;
92*08bbe119SEd Tanous 
93*08bbe119SEd Tanous     friend class Router;
94*08bbe119SEd Tanous     template <typename T>
95*08bbe119SEd Tanous     friend struct RuleParameterTraits;
96*08bbe119SEd Tanous };
97*08bbe119SEd Tanous 
98*08bbe119SEd Tanous } // namespace crow
99