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) { |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 79 | PersistentData::SessionStore::getInstance().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 |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 117 | return PersistentData::SessionStore::getInstance().generate_user_session( |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 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 ")); |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 126 | auto session = |
| 127 | PersistentData::SessionStore::getInstance().login_session_by_token( |
| 128 | token); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 129 | return session; |
| 130 | } |
| 131 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 132 | const std::shared_ptr<crow::PersistentData::UserSession> perform_xtoken_auth( |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 133 | const crow::request& req) const { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 134 | CROW_LOG_DEBUG << "[AuthMiddleware] X-Auth-Token authentication"; |
| 135 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 136 | boost::string_view token = req.get_header_value("X-Auth-Token"); |
Ed Tanous | 1ea9f06 | 2018-03-27 17:45:20 -0700 | [diff] [blame] | 137 | if (token.empty()) { |
| 138 | return nullptr; |
| 139 | } |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 140 | auto session = |
| 141 | PersistentData::SessionStore::getInstance().login_session_by_token( |
| 142 | token); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 143 | return session; |
| 144 | } |
| 145 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 146 | const std::shared_ptr<crow::PersistentData::UserSession> perform_cookie_auth( |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 147 | const crow::request& req) const { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 148 | CROW_LOG_DEBUG << "[AuthMiddleware] Cookie authentication"; |
| 149 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 150 | boost::string_view cookie_value = req.get_header_value("Cookie"); |
Ed Tanous | 1ea9f06 | 2018-03-27 17:45:20 -0700 | [diff] [blame] | 151 | if (cookie_value.empty()) { |
| 152 | return nullptr; |
| 153 | } |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 154 | |
| 155 | auto start_index = cookie_value.find("SESSION="); |
| 156 | if (start_index == std::string::npos) { |
| 157 | return nullptr; |
| 158 | } |
Ed Tanous | 41ff64d | 2018-01-30 13:13:38 -0800 | [diff] [blame] | 159 | start_index += sizeof("SESSION=") - 1; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 160 | auto end_index = cookie_value.find(";", start_index); |
| 161 | if (end_index == std::string::npos) { |
| 162 | end_index = cookie_value.size(); |
| 163 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 164 | boost::string_view auth_key = |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 165 | cookie_value.substr(start_index, end_index - start_index); |
| 166 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 167 | const std::shared_ptr<crow::PersistentData::UserSession> session = |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 168 | PersistentData::SessionStore::getInstance().login_session_by_token( |
| 169 | auth_key); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 170 | if (session == nullptr) { |
| 171 | return nullptr; |
| 172 | } |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 173 | #ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 174 | // RFC7231 defines methods that need csrf protection |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 175 | if (req.method() != "GET"_method) { |
| 176 | boost::string_view csrf = req.get_header_value("X-XSRF-TOKEN"); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 177 | // Make sure both tokens are filled |
| 178 | if (csrf.empty() || session->csrf_token.empty()) { |
| 179 | return nullptr; |
| 180 | } |
| 181 | // Reject if csrf token not available |
| 182 | if (csrf != session->csrf_token) { |
| 183 | return nullptr; |
| 184 | } |
| 185 | } |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 186 | #endif |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 187 | return session; |
| 188 | } |
| 189 | |
| 190 | // checks if request can be forwarded without authentication |
| 191 | bool is_on_whitelist(const crow::request& req) const { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 192 | // it's allowed to GET root node without authentica tion |
| 193 | if ("GET"_method == req.method()) { |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 194 | if (req.url == "/redfish/v1" || req.url == "/redfish/v1/") { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 195 | return true; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 196 | } else if (crow::webassets::routes.find(std::string(req.url)) != |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 197 | crow::webassets::routes.end()) { |
| 198 | return true; |
| 199 | } |
| 200 | } |
| 201 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 202 | // it's allowed to POST on session collection & login without |
| 203 | // authentication |
| 204 | if ("POST"_method == req.method()) { |
| 205 | if ((req.url == "/redfish/v1/SessionService/Sessions") || |
| 206 | (req.url == "/redfish/v1/SessionService/Sessions/") || |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 207 | (req.url == "/login")) { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return false; |
| 213 | } |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 214 | }; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 215 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 216 | // TODO(ed) see if there is a better way to allow middlewares to request |
| 217 | // routes. |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 218 | // Possibly an init function on first construction? |
| 219 | template <typename... Middlewares> |
| 220 | void request_routes(Crow<Middlewares...>& app) { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 221 | static_assert( |
| 222 | black_magic::contains<PersistentData::Middleware, Middlewares...>::value, |
| 223 | "TokenAuthorization middleware must be enabled in app to use " |
| 224 | "auth routes"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 225 | CROW_ROUTE(app, "/login") |
| 226 | .methods( |
| 227 | "POST"_method)([&](const crow::request& req, crow::response& res) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 228 | boost::string_view content_type = req.get_header_value("content-type"); |
| 229 | boost::string_view username; |
| 230 | boost::string_view password; |
| 231 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 232 | bool looks_like_ibm = false; |
Ed Tanous | db024a5 | 2018-03-06 12:50:34 -0800 | [diff] [blame] | 233 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 234 | // This object needs to be declared at this scope so the strings |
| 235 | // within it are not destroyed before we can use them |
Ed Tanous | db024a5 | 2018-03-06 12:50:34 -0800 | [diff] [blame] | 236 | nlohmann::json login_credentials; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 237 | // Check if auth was provided by a payload |
| 238 | if (content_type == "application/json") { |
Ed Tanous | db024a5 | 2018-03-06 12:50:34 -0800 | [diff] [blame] | 239 | login_credentials = nlohmann::json::parse(req.body, nullptr, false); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 240 | if (login_credentials.is_discarded()) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 241 | res.result(boost::beast::http::status::bad_request); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 242 | res.end(); |
| 243 | return; |
| 244 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 245 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 246 | // check for username/password in the root object |
| 247 | // THis method is how intel APIs authenticate |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 248 | nlohmann::json::iterator user_it = login_credentials.find("username"); |
| 249 | nlohmann::json::iterator pass_it = login_credentials.find("password"); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 250 | if (user_it != login_credentials.end() && |
| 251 | pass_it != login_credentials.end()) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 252 | const std::string* user_str = |
| 253 | user_it->get_ptr<const std::string*>(); |
| 254 | const std::string* pass_str = |
| 255 | pass_it->get_ptr<const std::string*>(); |
| 256 | if (user_str != nullptr && pass_str != nullptr) { |
| 257 | username = *user_str; |
| 258 | password = *pass_str; |
| 259 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 260 | } else { |
| 261 | // Openbmc appears to push a data object that contains the same |
| 262 | // keys (username and password), attempt to use that |
| 263 | auto data_it = login_credentials.find("data"); |
| 264 | if (data_it != login_credentials.end()) { |
| 265 | // Some apis produce an array of value ["username", |
| 266 | // "password"] |
| 267 | if (data_it->is_array()) { |
| 268 | if (data_it->size() == 2) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 269 | nlohmann::json::iterator user_it2 = data_it->begin(); |
| 270 | nlohmann::json::iterator pass_it2 = data_it->begin() + 1; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 271 | looks_like_ibm = true; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 272 | if (user_it2 != data_it->end() && |
| 273 | pass_it2 != data_it->end()) { |
| 274 | const std::string* user_str = |
| 275 | user_it2->get_ptr<const std::string*>(); |
| 276 | const std::string* pass_str = |
| 277 | pass_it2->get_ptr<const std::string*>(); |
| 278 | if (user_str != nullptr && pass_str != nullptr) { |
| 279 | username = *user_str; |
| 280 | password = *pass_str; |
| 281 | } |
| 282 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 283 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 284 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 285 | } else if (data_it->is_object()) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 286 | nlohmann::json::iterator user_it2 = data_it->find("username"); |
| 287 | nlohmann::json::iterator pass_it2 = data_it->find("password"); |
| 288 | if (user_it2 != data_it->end() && pass_it2 != data_it->end()) { |
| 289 | const std::string* user_str = |
| 290 | user_it2->get_ptr<const std::string*>(); |
| 291 | const std::string* pass_str = |
| 292 | pass_it2->get_ptr<const std::string*>(); |
| 293 | if (user_str != nullptr && pass_str != nullptr) { |
| 294 | username = *user_str; |
| 295 | password = *pass_str; |
| 296 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 301 | } else { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 302 | // check if auth was provided as a headers |
| 303 | username = req.get_header_value("username"); |
| 304 | password = req.get_header_value("password"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 307 | if (!username.empty() && !password.empty()) { |
| 308 | if (!pam_authenticate_user(username, password)) { |
| 309 | res.result(boost::beast::http::status::unauthorized); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 310 | } else { |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 311 | auto session = PersistentData::SessionStore::getInstance() |
| 312 | .generate_user_session(username); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 313 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 314 | if (looks_like_ibm) { |
| 315 | // IBM requires a very specific login structure, and doesn't |
| 316 | // actually look at the status code. TODO(ed).... Fix that |
| 317 | // upstream |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 318 | res.json_value = { |
| 319 | {"data", "User '" + std::string(username) + "' logged in"}, |
| 320 | {"message", "200 OK"}, |
| 321 | {"status", "ok"}}; |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 322 | |
| 323 | // Hack alert. Boost beast by default doesn't let you declare |
| 324 | // multiple headers of the same name, and in most cases this is |
| 325 | // fine. Unfortunately here we need to set the Session cookie, |
| 326 | // which requires the httpOnly attribute, as well as the XSRF |
| 327 | // cookie, which requires it to not have an httpOnly attribute. |
| 328 | // To get the behavior we want, we simply inject the second |
| 329 | // "set-cookie" string into the value header, and get the result |
| 330 | // we want, even though we are technicaly declaring two headers |
| 331 | // here. |
| 332 | res.add_header("Set-Cookie", |
| 333 | "XSRF-TOKEN=" + session->csrf_token + |
| 334 | "; Secure\r\nSet-Cookie: SESSION=" + |
| 335 | session->session_token + "; Secure; HttpOnly"); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 336 | } else { |
| 337 | // if content type is json, assume json token |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 338 | res.json_value = {{"token", session->session_token}}; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | } else { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 343 | res.result(boost::beast::http::status::bad_request); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 344 | } |
| 345 | res.end(); |
| 346 | }); |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 347 | |
| 348 | CROW_ROUTE(app, "/logout") |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 349 | .methods( |
| 350 | "POST"_method)([&](const crow::request& req, crow::response& res) { |
| 351 | auto& session = |
| 352 | app.template get_context<TokenAuthorization::Middleware>(req) |
| 353 | .session; |
| 354 | if (session != nullptr) { |
| 355 | PersistentData::SessionStore::getInstance().remove_session(session); |
| 356 | } |
| 357 | res.end(); |
| 358 | return; |
| 359 | }); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 360 | } |
Ed Tanous | db024a5 | 2018-03-06 12:50:34 -0800 | [diff] [blame] | 361 | } // namespace TokenAuthorization |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 362 | } // namespace crow |