Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 3 | #include <pam_authenticate.hpp> |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 4 | #include <persistent_data_middleware.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 5 | #include <webassets.hpp> |
| 6 | #include <random> |
| 7 | #include <crow/app.h> |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 8 | #include <crow/common.h> |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 9 | #include <crow/http_request.h> |
| 10 | #include <crow/http_response.h> |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 11 | #include <boost/container/flat_set.hpp> |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 12 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 13 | namespace crow { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 14 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 15 | namespace TokenAuthorization { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 16 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 17 | class Middleware { |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 18 | public: |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 19 | struct context { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 20 | std::shared_ptr<crow::PersistentData::UserSession> session; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 21 | }; |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 22 | |
| 23 | void before_handle(crow::request& req, response& res, context& ctx) { |
Ed Tanous | bae064e | 2018-03-22 15:44:39 -0700 | [diff] [blame] | 24 | if (is_on_whitelist(req)) { |
| 25 | return; |
| 26 | } |
| 27 | |
Ed Tanous | 1ea9f06 | 2018-03-27 17:45:20 -0700 | [diff] [blame] | 28 | ctx.session = perform_xtoken_auth(req); |
Ed Tanous | 1ea9f06 | 2018-03-27 17:45:20 -0700 | [diff] [blame] | 29 | if (ctx.session == nullptr) { |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 30 | ctx.session = perform_cookie_auth(req); |
Ed Tanous | 1ea9f06 | 2018-03-27 17:45:20 -0700 | [diff] [blame] | 31 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 32 | if (ctx.session == nullptr) { |
| 33 | boost::string_view auth_header = req.get_header_value("Authorization"); |
| 34 | if (!auth_header.empty()) { |
| 35 | // Reject any kind of auth other than basic or token |
| 36 | if (boost::starts_with(auth_header, "Token ")) { |
| 37 | ctx.session = perform_token_auth(auth_header); |
| 38 | } else if (boost::starts_with(auth_header, "Basic ")) { |
| 39 | ctx.session = perform_basic_auth(auth_header); |
| 40 | } |
| 41 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Ed Tanous | bae064e | 2018-03-22 15:44:39 -0700 | [diff] [blame] | 44 | if (ctx.session == nullptr) { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 45 | CROW_LOG_WARNING << "[AuthMiddleware] authorization failed"; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 46 | |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 47 | // If it's a browser connecting, don't send the HTTP authenticate header, |
| 48 | // to avoid possible CSRF attacks with basic auth |
| 49 | if (http_helpers::request_prefers_html(req)) { |
| 50 | res.result(boost::beast::http::status::temporary_redirect); |
| 51 | res.add_header("Location", "/#/login"); |
| 52 | } else { |
| 53 | res.result(boost::beast::http::status::unauthorized); |
| 54 | // only send the WWW-authenticate header if this isn't a xhr from the |
| 55 | // browser. most scripts, |
| 56 | if (req.get_header_value("User-Agent").empty()) { |
| 57 | res.add_header("WWW-Authenticate", "Basic"); |
| 58 | } |
| 59 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 60 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 61 | res.end(); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 62 | return; |
| 63 | } |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 64 | |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 65 | // TODO get user privileges here and propagate it via MW context |
| 66 | // else let the request continue unharmed |
| 67 | } |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 68 | |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 69 | template <typename AllContext> |
| 70 | void after_handle(request& req, response& res, context& ctx, |
| 71 | AllContext& allctx) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 72 | // TODO(ed) THis should really be handled by the persistent data |
| 73 | // middleware, but because it is upstream, it doesn't have access to the |
| 74 | // session information. Should the data middleware persist the current |
| 75 | // user session? |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 76 | if (ctx.session != nullptr && |
| 77 | ctx.session->persistence == |
| 78 | crow::PersistentData::PersistenceType::SINGLE_REQUEST) { |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 79 | PersistentData::session_store->remove_session(ctx.session); |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 83 | private: |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 84 | const std::shared_ptr<crow::PersistentData::UserSession> perform_basic_auth( |
| 85 | boost::string_view auth_header) const { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 86 | CROW_LOG_DEBUG << "[AuthMiddleware] Basic authentication"; |
| 87 | |
| 88 | std::string auth_data; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 89 | boost::string_view param = auth_header.substr(strlen("Basic ")); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 90 | if (!crow::utility::base64_decode(param, auth_data)) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | std::size_t separator = auth_data.find(':'); |
| 94 | if (separator == std::string::npos) { |
| 95 | return nullptr; |
| 96 | } |
| 97 | |
| 98 | std::string user = auth_data.substr(0, separator); |
| 99 | separator += 1; |
| 100 | if (separator > auth_data.size()) { |
| 101 | return nullptr; |
| 102 | } |
| 103 | std::string pass = auth_data.substr(separator); |
| 104 | |
| 105 | CROW_LOG_DEBUG << "[AuthMiddleware] Authenticating user: " << user; |
| 106 | |
| 107 | if (!pam_authenticate_user(user, pass)) { |
| 108 | return nullptr; |
| 109 | } |
| 110 | |
| 111 | // TODO(ed) generate_user_session is a little expensive for basic |
| 112 | // auth, as it generates some random identifiers that will never be |
| 113 | // used. This should have a "fast" path for when user tokens aren't |
| 114 | // needed. |
| 115 | // This whole flow needs to be revisited anyway, as we can't be |
| 116 | // calling directly into pam for every request |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 117 | return PersistentData::session_store->generate_user_session( |
| 118 | user, crow::PersistentData::PersistenceType::SINGLE_REQUEST); |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 119 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 120 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 121 | const std::shared_ptr<crow::PersistentData::UserSession> perform_token_auth( |
| 122 | boost::string_view auth_header) const { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 123 | CROW_LOG_DEBUG << "[AuthMiddleware] Token authentication"; |
| 124 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 125 | boost::string_view token = auth_header.substr(strlen("Token ")); |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 126 | auto session = PersistentData::session_store->login_session_by_token(token); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 127 | return session; |
| 128 | } |
| 129 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 130 | const std::shared_ptr<crow::PersistentData::UserSession> perform_xtoken_auth( |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 131 | const crow::request& req) const { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 132 | CROW_LOG_DEBUG << "[AuthMiddleware] X-Auth-Token authentication"; |
| 133 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 134 | boost::string_view token = req.get_header_value("X-Auth-Token"); |
Ed Tanous | 1ea9f06 | 2018-03-27 17:45:20 -0700 | [diff] [blame] | 135 | if (token.empty()) { |
| 136 | return nullptr; |
| 137 | } |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 138 | auto session = PersistentData::session_store->login_session_by_token(token); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 139 | return session; |
| 140 | } |
| 141 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 142 | const std::shared_ptr<crow::PersistentData::UserSession> perform_cookie_auth( |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 143 | const crow::request& req) const { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 144 | CROW_LOG_DEBUG << "[AuthMiddleware] Cookie authentication"; |
| 145 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 146 | boost::string_view cookie_value = req.get_header_value("Cookie"); |
Ed Tanous | 1ea9f06 | 2018-03-27 17:45:20 -0700 | [diff] [blame] | 147 | if (cookie_value.empty()) { |
| 148 | return nullptr; |
| 149 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 150 | |
| 151 | auto start_index = cookie_value.find("SESSION="); |
| 152 | if (start_index == std::string::npos) { |
| 153 | return nullptr; |
| 154 | } |
Ed Tanous | 41ff64d | 2018-01-30 13:13:38 -0800 | [diff] [blame] | 155 | start_index += sizeof("SESSION=") - 1; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 156 | auto end_index = cookie_value.find(";", start_index); |
| 157 | if (end_index == std::string::npos) { |
| 158 | end_index = cookie_value.size(); |
| 159 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 160 | boost::string_view auth_key = |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 161 | cookie_value.substr(start_index, end_index - start_index); |
| 162 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 163 | const std::shared_ptr<crow::PersistentData::UserSession> session = |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 164 | PersistentData::session_store->login_session_by_token(auth_key); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 165 | if (session == nullptr) { |
| 166 | return nullptr; |
| 167 | } |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame^] | 168 | #ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 169 | // RFC7231 defines methods that need csrf protection |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 170 | if (req.method() != "GET"_method) { |
| 171 | boost::string_view csrf = req.get_header_value("X-XSRF-TOKEN"); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 172 | // Make sure both tokens are filled |
| 173 | if (csrf.empty() || session->csrf_token.empty()) { |
| 174 | return nullptr; |
| 175 | } |
| 176 | // Reject if csrf token not available |
| 177 | if (csrf != session->csrf_token) { |
| 178 | return nullptr; |
| 179 | } |
| 180 | } |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame^] | 181 | #endif |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 182 | return session; |
| 183 | } |
| 184 | |
| 185 | // checks if request can be forwarded without authentication |
| 186 | bool is_on_whitelist(const crow::request& req) const { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 187 | // it's allowed to GET root node without authentica tion |
| 188 | if ("GET"_method == req.method()) { |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 189 | if (req.url == "/redfish/v1" || req.url == "/redfish/v1/") { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 190 | return true; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 191 | } else if (crow::webassets::routes.find(std::string(req.url)) != |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 192 | crow::webassets::routes.end()) { |
| 193 | return true; |
| 194 | } |
| 195 | } |
| 196 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 197 | // it's allowed to POST on session collection & login without |
| 198 | // authentication |
| 199 | if ("POST"_method == req.method()) { |
| 200 | if ((req.url == "/redfish/v1/SessionService/Sessions") || |
| 201 | (req.url == "/redfish/v1/SessionService/Sessions/") || |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 202 | (req.url == "/login")) { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return false; |
| 208 | } |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 209 | }; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 210 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 211 | // TODO(ed) see if there is a better way to allow middlewares to request |
| 212 | // routes. |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 213 | // Possibly an init function on first construction? |
| 214 | template <typename... Middlewares> |
| 215 | void request_routes(Crow<Middlewares...>& app) { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 216 | static_assert( |
| 217 | black_magic::contains<PersistentData::Middleware, Middlewares...>::value, |
| 218 | "TokenAuthorization middleware must be enabled in app to use " |
| 219 | "auth routes"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 220 | CROW_ROUTE(app, "/login") |
| 221 | .methods( |
| 222 | "POST"_method)([&](const crow::request& req, crow::response& res) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 223 | boost::string_view content_type = req.get_header_value("content-type"); |
| 224 | boost::string_view username; |
| 225 | boost::string_view password; |
| 226 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 227 | bool looks_like_ibm = false; |
Ed Tanous | db024a5 | 2018-03-06 12:50:34 -0800 | [diff] [blame] | 228 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 229 | // This object needs to be declared at this scope so the strings |
| 230 | // within it are not destroyed before we can use them |
Ed Tanous | db024a5 | 2018-03-06 12:50:34 -0800 | [diff] [blame] | 231 | nlohmann::json login_credentials; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 232 | // Check if auth was provided by a payload |
| 233 | if (content_type == "application/json") { |
Ed Tanous | db024a5 | 2018-03-06 12:50:34 -0800 | [diff] [blame] | 234 | login_credentials = nlohmann::json::parse(req.body, nullptr, false); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 235 | if (login_credentials.is_discarded()) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 236 | res.result(boost::beast::http::status::bad_request); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 237 | res.end(); |
| 238 | return; |
| 239 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 240 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 241 | // check for username/password in the root object |
| 242 | // THis method is how intel APIs authenticate |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 243 | nlohmann::json::iterator user_it = login_credentials.find("username"); |
| 244 | nlohmann::json::iterator pass_it = login_credentials.find("password"); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 245 | if (user_it != login_credentials.end() && |
| 246 | pass_it != login_credentials.end()) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 247 | const std::string* user_str = |
| 248 | user_it->get_ptr<const std::string*>(); |
| 249 | const std::string* pass_str = |
| 250 | pass_it->get_ptr<const std::string*>(); |
| 251 | if (user_str != nullptr && pass_str != nullptr) { |
| 252 | username = *user_str; |
| 253 | password = *pass_str; |
| 254 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 255 | } else { |
| 256 | // Openbmc appears to push a data object that contains the same |
| 257 | // keys (username and password), attempt to use that |
| 258 | auto data_it = login_credentials.find("data"); |
| 259 | if (data_it != login_credentials.end()) { |
| 260 | // Some apis produce an array of value ["username", |
| 261 | // "password"] |
| 262 | if (data_it->is_array()) { |
| 263 | if (data_it->size() == 2) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 264 | nlohmann::json::iterator user_it2 = data_it->begin(); |
| 265 | nlohmann::json::iterator pass_it2 = data_it->begin() + 1; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 266 | looks_like_ibm = true; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 267 | if (user_it2 != data_it->end() && |
| 268 | pass_it2 != data_it->end()) { |
| 269 | const std::string* user_str = |
| 270 | user_it2->get_ptr<const std::string*>(); |
| 271 | const std::string* pass_str = |
| 272 | pass_it2->get_ptr<const std::string*>(); |
| 273 | if (user_str != nullptr && pass_str != nullptr) { |
| 274 | username = *user_str; |
| 275 | password = *pass_str; |
| 276 | } |
| 277 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 278 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 279 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 280 | } else if (data_it->is_object()) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 281 | nlohmann::json::iterator user_it2 = data_it->find("username"); |
| 282 | nlohmann::json::iterator pass_it2 = data_it->find("password"); |
| 283 | if (user_it2 != data_it->end() && pass_it2 != data_it->end()) { |
| 284 | const std::string* user_str = |
| 285 | user_it2->get_ptr<const std::string*>(); |
| 286 | const std::string* pass_str = |
| 287 | pass_it2->get_ptr<const std::string*>(); |
| 288 | if (user_str != nullptr && pass_str != nullptr) { |
| 289 | username = *user_str; |
| 290 | password = *pass_str; |
| 291 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 296 | } else { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 297 | // check if auth was provided as a headers |
| 298 | username = req.get_header_value("username"); |
| 299 | password = req.get_header_value("password"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 302 | if (!username.empty() && !password.empty()) { |
| 303 | if (!pam_authenticate_user(username, password)) { |
| 304 | res.result(boost::beast::http::status::unauthorized); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 305 | } else { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 306 | auto session = |
| 307 | PersistentData::session_store->generate_user_session(username); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 308 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 309 | if (looks_like_ibm) { |
| 310 | // IBM requires a very specific login structure, and doesn't |
| 311 | // actually look at the status code. TODO(ed).... Fix that |
| 312 | // upstream |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 313 | res.json_value = { |
| 314 | {"data", "User '" + std::string(username) + "' logged in"}, |
| 315 | {"message", "200 OK"}, |
| 316 | {"status", "ok"}}; |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 317 | |
| 318 | // Hack alert. Boost beast by default doesn't let you declare |
| 319 | // multiple headers of the same name, and in most cases this is |
| 320 | // fine. Unfortunately here we need to set the Session cookie, |
| 321 | // which requires the httpOnly attribute, as well as the XSRF |
| 322 | // cookie, which requires it to not have an httpOnly attribute. |
| 323 | // To get the behavior we want, we simply inject the second |
| 324 | // "set-cookie" string into the value header, and get the result |
| 325 | // we want, even though we are technicaly declaring two headers |
| 326 | // here. |
| 327 | res.add_header("Set-Cookie", |
| 328 | "XSRF-TOKEN=" + session->csrf_token + |
| 329 | "; Secure\r\nSet-Cookie: SESSION=" + |
| 330 | session->session_token + "; Secure; HttpOnly"); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 331 | } else { |
| 332 | // if content type is json, assume json token |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 333 | res.json_value = {{"token", session->session_token}}; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | } else { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 338 | res.result(boost::beast::http::status::bad_request); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 339 | } |
| 340 | res.end(); |
| 341 | }); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 342 | |
| 343 | CROW_ROUTE(app, "/logout") |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 344 | .methods("POST"_method)( |
| 345 | [&](const crow::request& req, crow::response& res) { |
| 346 | auto& session = |
| 347 | app.template get_context<TokenAuthorization::Middleware>(req) |
| 348 | .session; |
| 349 | if (session != nullptr) { |
| 350 | PersistentData::session_store->remove_session(session); |
| 351 | } |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 352 | res.end(); |
| 353 | return; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 354 | |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 355 | }); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 356 | } |
Ed Tanous | db024a5 | 2018-03-06 12:50:34 -0800 | [diff] [blame] | 357 | } // namespace TokenAuthorization |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 358 | } // namespace crow |