Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 1 | #include <unordered_map> |
| 2 | |
| 3 | #include <boost/algorithm/string/predicate.hpp> |
| 4 | |
| 5 | #include <token_authorization_middleware.hpp> |
| 6 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 7 | namespace crow { |
| 8 | std::string TokenAuthorizationMiddleware::context::get_cookie(const std::string& key) { |
| 9 | if (cookie_sessions.count(key)) return cookie_sessions[key]; |
| 10 | return {}; |
| 11 | } |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 12 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 13 | void TokenAuthorizationMiddleware::context::set_cookie(const std::string& key, const std::string& value) { cookies_to_push_to_client.emplace(key, value); } |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 14 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 15 | void TokenAuthorizationMiddleware::before_handle(crow::request& req, response& res, context& ctx) { |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 16 | return; |
| 17 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 18 | auto return_unauthorized = [&req, &res]() { |
| 19 | res.code = 401; |
| 20 | res.end(); |
| 21 | }; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 22 | if (req.url == "/" || boost::starts_with(req.url, "/static/")){ |
| 23 | //TODO this is total hackery to allow the login page to work before the user |
| 24 | // is authenticated. Also, it will be quite slow for all pages. |
| 25 | // Ideally, this should be done in the url router handler, with tagged routes |
| 26 | // for the whitelist entries. |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | //TODO this |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 31 | if (req.url == "/login") { |
| 32 | } |
| 33 | // Check for an authorization header, reject if not present |
| 34 | if (req.headers.count("Authorization") != 1) { |
| 35 | return_unauthorized(); |
| 36 | return; |
| 37 | } |
Ed Tanous | 38bdb98 | 2017-03-03 14:19:33 -0800 | [diff] [blame] | 38 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 39 | std::string auth_header = req.get_header_value("Authorization"); |
| 40 | // If the user is attempting any kind of auth other than token, reject |
| 41 | if (!boost::starts_with(auth_header, "Token ")) { |
| 42 | return_unauthorized(); |
| 43 | return; |
| 44 | } |
| 45 | } |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 46 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 47 | void TokenAuthorizationMiddleware::after_handle(request& /*req*/, response& res, context& ctx) { |
| 48 | for (auto& cookie : ctx.cookies_to_push_to_client) { |
| 49 | res.add_header("Set-Cookie", cookie.first + "=" + cookie.second); |
| 50 | } |
| 51 | } |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 52 | } |