Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <crow/http_request.h> |
| 4 | #include <crow/http_response.h> |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 5 | #include <boost/container/flat_set.hpp> |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 6 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 7 | #include <base64.hpp> |
| 8 | |
| 9 | #include <pam_authenticate.hpp> |
| 10 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 11 | namespace crow { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 12 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 13 | struct User {}; |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 14 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 15 | using random_bytes_engine = |
| 16 | std::independent_bits_engine<std::default_random_engine, CHAR_BIT, |
| 17 | unsigned char>; |
| 18 | |
| 19 | template <class AuthenticationFunction> |
| 20 | struct TokenAuthorization { |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 21 | private: |
| 22 | random_bytes_engine rbe; |
| 23 | |
| 24 | public: |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 25 | struct context { |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 26 | // std::string auth_token; |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 27 | }; |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 28 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 29 | TokenAuthorization(){}; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 30 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 31 | void before_handle(crow::request& req, response& res, context& ctx) { |
| 32 | auto return_unauthorized = [&req, &res]() { |
| 33 | res.code = 401; |
| 34 | res.end(); |
| 35 | }; |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 36 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 37 | auto return_bad_request = [&req, &res]() { |
| 38 | res.code = 400; |
| 39 | res.end(); |
| 40 | }; |
| 41 | |
| 42 | auto return_internal_error = [&req, &res]() { |
| 43 | res.code = 500; |
| 44 | res.end(); |
| 45 | }; |
| 46 | |
| 47 | if (req.url == "/" || boost::starts_with(req.url, "/static/")) { |
| 48 | // TODO this is total hackery to allow the login page to work before the |
| 49 | // user is authenticated. Also, it will be quite slow for all pages |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 50 | // instead of a one time hit for the whitelist entries. Ideally, this |
| 51 | // should be |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 52 | // done in the url router handler, with tagged routes for the whitelist |
| 53 | // entries. Another option would be to whitelist a minimal for based page |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 54 | // that didn't load the full angular UI until after login |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | |
| 58 | if (req.url == "/login") { |
| 59 | if (req.method != HTTPMethod::POST) { |
| 60 | return_unauthorized(); |
| 61 | return; |
| 62 | } else { |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 63 | std::string username; |
| 64 | std::string password; |
| 65 | try { |
| 66 | auto login_credentials = nlohmann::json::parse(req.body); |
| 67 | username = login_credentials["username"]; |
| 68 | password = login_credentials["password"]; |
| 69 | } catch (...) { |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 70 | return_bad_request(); |
| 71 | return; |
| 72 | } |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 73 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 74 | auto p = AuthenticationFunction(); |
| 75 | if (p.authenticate(username, password)) { |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 76 | nlohmann::json x; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 77 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 78 | std::string token('a', 20); |
| 79 | // TODO(ed) for some reason clang-tidy finds a divide by zero error in |
| 80 | // cstdlibc here commented out for now. Needs investigation |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 81 | std::generate(std::begin(token), std::end(token), |
| 82 | std::ref(rbe)); // NOLINT |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 83 | std::string encoded_token; |
| 84 | base64::base64_encode(token, encoded_token); |
| 85 | // ctx.auth_token = encoded_token; |
| 86 | this->auth_token2.insert(encoded_token); |
| 87 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 88 | nlohmann::json ret{{"token", encoded_token}}; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 89 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 90 | res.write(ret.dump()); |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 91 | res.add_header("Content-Type", "application/json"); |
| 92 | res.end(); |
| 93 | } else { |
| 94 | return_unauthorized(); |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | } else { // Normal, non login, non static file request |
| 100 | // Check to make sure we're logged in |
| 101 | if (this->auth_token2.empty()) { |
| 102 | return_unauthorized(); |
| 103 | return; |
| 104 | } |
| 105 | // Check for an authorization header, reject if not present |
| 106 | if (req.headers.count("Authorization") != 1) { |
| 107 | return_unauthorized(); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | std::string auth_header = req.get_header_value("Authorization"); |
| 112 | // If the user is attempting any kind of auth other than token, reject |
| 113 | if (!boost::starts_with(auth_header, "Token ")) { |
| 114 | return_unauthorized(); |
| 115 | return; |
| 116 | } |
| 117 | std::string auth_key = auth_header.substr(6); |
| 118 | // TODO(ed), use span here instead of constructing a new string |
| 119 | if (this->auth_token2.find(auth_key) == this->auth_token2.end()) { |
| 120 | return_unauthorized(); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | if (req.url == "/logout") { |
| 125 | this->auth_token2.erase(auth_key); |
| 126 | res.code = 200; |
| 127 | res.end(); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | // else let the request continue unharmed |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void after_handle(request& req, response& res, context& ctx) { |
| 136 | // Do nothing |
| 137 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 138 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 139 | private: |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 140 | boost::container::flat_set<std::string> auth_token2; |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 141 | }; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 142 | |
| 143 | using TokenAuthorizationMiddleware = TokenAuthorization<PamAuthenticator>; |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 144 | } |