blob: 8237bc4fb693143a91214a247c7d0501a2d24800 [file] [log] [blame]
James Feist3909dc82020-04-03 10:58:55 -07001#pragma once
2
3#include "webroutes.hpp"
4
5#include <app.h>
6#include <common.h>
7#include <http_request.h>
8#include <http_response.h>
9
10#include <boost/algorithm/string/predicate.hpp>
11#include <boost/container/flat_set.hpp>
12#include <http_utility.hpp>
13#include <pam_authenticate.hpp>
14#include <persistent_data_middleware.hpp>
15
16#include <random>
17
18namespace crow
19{
20
21namespace authorization
22{
23
24static void cleanupTempSession(Request& req)
25{
26 // TODO(ed) THis should really be handled by the persistent data
27 // middleware, but because it is upstream, it doesn't have access to the
28 // session information. Should the data middleware persist the current
29 // user session?
30 if (req.session != nullptr &&
31 req.session->persistence ==
32 crow::persistent_data::PersistenceType::SINGLE_REQUEST)
33 {
34 persistent_data::SessionStore::getInstance().removeSession(req.session);
35 }
36}
37
38static const std::shared_ptr<crow::persistent_data::UserSession>
39 performBasicAuth(std::string_view auth_header)
40{
41 BMCWEB_LOG_DEBUG << "[AuthMiddleware] Basic authentication";
42
43 std::string authData;
44 std::string_view param = auth_header.substr(strlen("Basic "));
45 if (!crow::utility::base64Decode(param, authData))
46 {
47 return nullptr;
48 }
49 std::size_t separator = authData.find(':');
50 if (separator == std::string::npos)
51 {
52 return nullptr;
53 }
54
55 std::string user = authData.substr(0, separator);
56 separator += 1;
57 if (separator > authData.size())
58 {
59 return nullptr;
60 }
61 std::string pass = authData.substr(separator);
62
63 BMCWEB_LOG_DEBUG << "[AuthMiddleware] Authenticating user: " << user;
64
65 int pamrc = pamAuthenticateUser(user, pass);
66 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
67 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
68 {
69 return nullptr;
70 }
71
72 // TODO(ed) generateUserSession is a little expensive for basic
73 // auth, as it generates some random identifiers that will never be
74 // used. This should have a "fast" path for when user tokens aren't
75 // needed.
76 // This whole flow needs to be revisited anyway, as we can't be
77 // calling directly into pam for every request
78 return persistent_data::SessionStore::getInstance().generateUserSession(
79 user, crow::persistent_data::PersistenceType::SINGLE_REQUEST,
80 isConfigureSelfOnly);
81}
82
83static const std::shared_ptr<crow::persistent_data::UserSession>
84 performTokenAuth(std::string_view auth_header)
85{
86 BMCWEB_LOG_DEBUG << "[AuthMiddleware] Token authentication";
87
88 std::string_view token = auth_header.substr(strlen("Token "));
89 auto session =
90 persistent_data::SessionStore::getInstance().loginSessionByToken(token);
91 return session;
92}
93
94static const std::shared_ptr<crow::persistent_data::UserSession>
95 performXtokenAuth(const crow::Request& req)
96{
97 BMCWEB_LOG_DEBUG << "[AuthMiddleware] X-Auth-Token authentication";
98
99 std::string_view token = req.getHeaderValue("X-Auth-Token");
100 if (token.empty())
101 {
102 return nullptr;
103 }
104 auto session =
105 persistent_data::SessionStore::getInstance().loginSessionByToken(token);
106 return session;
107}
108
109static const std::shared_ptr<crow::persistent_data::UserSession>
110 performCookieAuth(const crow::Request& req)
111{
112 BMCWEB_LOG_DEBUG << "[AuthMiddleware] Cookie authentication";
113
114 std::string_view cookieValue = req.getHeaderValue("Cookie");
115 if (cookieValue.empty())
116 {
117 return nullptr;
118 }
119
120 auto startIndex = cookieValue.find("SESSION=");
121 if (startIndex == std::string::npos)
122 {
123 return nullptr;
124 }
125 startIndex += sizeof("SESSION=") - 1;
126 auto endIndex = cookieValue.find(";", startIndex);
127 if (endIndex == std::string::npos)
128 {
129 endIndex = cookieValue.size();
130 }
131 std::string_view authKey =
132 cookieValue.substr(startIndex, endIndex - startIndex);
133
134 const std::shared_ptr<crow::persistent_data::UserSession> session =
135 persistent_data::SessionStore::getInstance().loginSessionByToken(
136 authKey);
137 if (session == nullptr)
138 {
139 return nullptr;
140 }
141#ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION
142 // RFC7231 defines methods that need csrf protection
Ed Tanousb41187f2019-10-24 16:30:02 -0700143 if (req.method() != boost::beast::http::verb::get)
James Feist3909dc82020-04-03 10:58:55 -0700144 {
145 std::string_view csrf = req.getHeaderValue("X-XSRF-TOKEN");
146 // Make sure both tokens are filled
147 if (csrf.empty() || session->csrfToken.empty())
148 {
149 return nullptr;
150 }
151
152 if (csrf.size() != crow::persistent_data::sessionTokenSize)
153 {
154 return nullptr;
155 }
156 // Reject if csrf token not available
157 if (!crow::utility::constantTimeStringCompare(csrf, session->csrfToken))
158 {
159 return nullptr;
160 }
161 }
162#endif
163 return session;
164}
165
166// checks if request can be forwarded without authentication
167static bool isOnWhitelist(const crow::Request& req)
168{
169 // it's allowed to GET root node without authentication
Ed Tanousb41187f2019-10-24 16:30:02 -0700170 if (boost::beast::http::verb::get == req.method())
James Feist3909dc82020-04-03 10:58:55 -0700171 {
172 if (req.url == "/redfish/v1" || req.url == "/redfish/v1/" ||
173 req.url == "/redfish" || req.url == "/redfish/" ||
174 req.url == "/redfish/v1/odata" || req.url == "/redfish/v1/odata/")
175 {
176 return true;
177 }
178 else if (crow::webroutes::routes.find(std::string(req.url)) !=
179 crow::webroutes::routes.end())
180 {
181 return true;
182 }
183 }
184
185 // it's allowed to POST on session collection & login without
186 // authentication
Ed Tanousb41187f2019-10-24 16:30:02 -0700187 if (boost::beast::http::verb::post == req.method())
James Feist3909dc82020-04-03 10:58:55 -0700188 {
189 if ((req.url == "/redfish/v1/SessionService/Sessions") ||
190 (req.url == "/redfish/v1/SessionService/Sessions/") ||
191 (req.url == "/login"))
192 {
193 return true;
194 }
195 }
196
197 return false;
198}
199
200static void authenticate(crow::Request& req, Response& res)
201{
202 if (isOnWhitelist(req))
203 {
204 return;
205 }
206
207 const crow::persistent_data::AuthConfigMethods& authMethodsConfig =
208 crow::persistent_data::SessionStore::getInstance()
209 .getAuthMethodsConfig();
210
211 if (req.session == nullptr && authMethodsConfig.xtoken)
212 {
213 req.session = performXtokenAuth(req);
214 }
215 if (req.session == nullptr && authMethodsConfig.cookie)
216 {
217 req.session = performCookieAuth(req);
218 }
219 if (req.session == nullptr)
220 {
221 std::string_view authHeader = req.getHeaderValue("Authorization");
222 if (!authHeader.empty())
223 {
224 // Reject any kind of auth other than basic or token
225 if (boost::starts_with(authHeader, "Token ") &&
226 authMethodsConfig.sessionToken)
227 {
228 req.session = performTokenAuth(authHeader);
229 }
230 else if (boost::starts_with(authHeader, "Basic ") &&
231 authMethodsConfig.basic)
232 {
233 req.session = performBasicAuth(authHeader);
234 }
235 }
236 }
237
238 if (req.session == nullptr)
239 {
240 BMCWEB_LOG_WARNING << "[AuthMiddleware] authorization failed";
241
242 // If it's a browser connecting, don't send the HTTP authenticate
243 // header, to avoid possible CSRF attacks with basic auth
244 if (http_helpers::requestPrefersHtml(req))
245 {
246 res.result(boost::beast::http::status::temporary_redirect);
247 res.addHeader("Location",
248 "/#/login?next=" + http_helpers::urlEncode(req.url));
249 }
250 else
251 {
252 res.result(boost::beast::http::status::unauthorized);
253 // only send the WWW-authenticate header if this isn't a xhr
254 // from the browser. most scripts,
255 if (req.getHeaderValue("User-Agent").empty())
256 {
257 res.addHeader("WWW-Authenticate", "Basic");
258 }
259 }
260
261 res.end();
262 return;
263 }
264}
265
266} // namespace authorization
267} // namespace crow