blob: 91feaf10ddf7022e50e5d2fdc14216b5aa675039 [file] [log] [blame]
Ed Tanous8041f312017-04-03 09:47:01 -07001#include <boost/algorithm/string/predicate.hpp>
Ed Tanous1ccd57c2017-03-21 13:15:58 -07002#include <random>
Ed Tanousf9273472017-02-28 16:05:13 -08003#include <unordered_map>
Ed Tanousf9273472017-02-28 16:05:13 -08004
Ed Tanous1ccd57c2017-03-21 13:15:58 -07005#include <crow/logging.h>
Ed Tanousc4771fb2017-03-13 13:39:49 -07006#include <base64.hpp>
Ed Tanous8041f312017-04-03 09:47:01 -07007#include <token_authorization_middleware.hpp>
Ed Tanousf9273472017-02-28 16:05:13 -08008
Ed Tanousc4771fb2017-03-13 13:39:49 -07009namespace crow {
10
Ed Tanous8041f312017-04-03 09:47:01 -070011using random_bytes_engine =
12 std::independent_bits_engine<std::default_random_engine, CHAR_BIT,
13 unsigned char>;
Ed Tanousc4771fb2017-03-13 13:39:49 -070014
Ed Tanous8041f312017-04-03 09:47:01 -070015TokenAuthorizationMiddleware::TokenAuthorizationMiddleware()
16 : auth_token2(""){
Ed Tanousf9273472017-02-28 16:05:13 -080017
Ed Tanous8041f312017-04-03 09:47:01 -070018 };
19
20void TokenAuthorizationMiddleware::before_handle(crow::request& req,
21 response& res, context& ctx) {
Ed Tanous99923322017-03-03 14:21:24 -080022 auto return_unauthorized = [&req, &res]() {
23 res.code = 401;
24 res.end();
25 };
Ed Tanous1ccd57c2017-03-21 13:15:58 -070026
Ed Tanous8041f312017-04-03 09:47:01 -070027 LOG(DEBUG) << "Token Auth Got route " << req.url;
Ed Tanous1ccd57c2017-03-21 13:15:58 -070028
Ed Tanous8041f312017-04-03 09:47:01 -070029 if (req.url == "/" || boost::starts_with(req.url, "/static/")) {
30 // TODO this is total hackery to allow the login page to work before the
31 // user is authenticated. Also, it will be quite slow for all pages instead
32 // of a one time hit for the whitelist entries. Ideally, this should be
33 // done
34 // in the url router handler, with tagged routes for the whitelist entries.
Ed Tanous1ccd57c2017-03-21 13:15:58 -070035 // Another option would be to whitelist a minimal
Ed Tanous9b65f1f2017-03-07 15:17:13 -080036 return;
37 }
38
Ed Tanous99923322017-03-03 14:21:24 -080039 if (req.url == "/login") {
Ed Tanous8041f312017-04-03 09:47:01 -070040 if (req.method != HTTPMethod::POST) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070041 return_unauthorized();
42 return;
43 } else {
44 auto login_credentials = crow::json::load(req.body);
Ed Tanous8041f312017-04-03 09:47:01 -070045 if (!login_credentials) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070046 return_unauthorized();
47 return;
48 }
49 auto username = login_credentials["username"].s();
50 auto password = login_credentials["password"].s();
Ed Tanous8041f312017-04-03 09:47:01 -070051
Ed Tanous1ccd57c2017-03-21 13:15:58 -070052 // TODO(ed) pull real passwords from PAM
Ed Tanous8041f312017-04-03 09:47:01 -070053 if (username == "dude" && password == "dude") {
54 // TODO(ed) the RNG should be initialized at start, not every time we
55 // want a token
Ed Tanousc4771fb2017-03-13 13:39:49 -070056 std::random_device rand;
57 random_bytes_engine rbe;
58 std::string token('a', 20);
59 std::generate(begin(token), end(token), std::ref(rbe));
60 std::string encoded_token;
61 base64::base64_encode(token, encoded_token);
62 ctx.auth_token = encoded_token;
63 this->auth_token2 = encoded_token;
Ed Tanous8041f312017-04-03 09:47:01 -070064 crow::json::wvalue x;
65 auto auth_token = ctx.auth_token;
66 x["token"] = auth_token;
67
68 res.write(json::dump(x));
69 res.end();
Ed Tanousc4771fb2017-03-13 13:39:49 -070070 } else {
71 return_unauthorized();
72 return;
73 }
74 }
Ed Tanous8041f312017-04-03 09:47:01 -070075
Ed Tanousc4771fb2017-03-13 13:39:49 -070076 } else if (req.url == "/logout") {
77 this->auth_token2 = "";
Ed Tanous8041f312017-04-03 09:47:01 -070078 res.code = 200;
79 res.end();
80 } else { // Normal, non login, non static file request
Ed Tanousc4771fb2017-03-13 13:39:49 -070081 // Check to make sure we're logged in
Ed Tanous8041f312017-04-03 09:47:01 -070082 if (this->auth_token2.empty()) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070083 return_unauthorized();
84 return;
85 }
86 // Check for an authorization header, reject if not present
87 if (req.headers.count("Authorization") != 1) {
88 return_unauthorized();
89 return;
90 }
91
92 std::string auth_header = req.get_header_value("Authorization");
93 // If the user is attempting any kind of auth other than token, reject
94 if (!boost::starts_with(auth_header, "Token ")) {
95 return_unauthorized();
96 return;
97 }
98
Ed Tanous8041f312017-04-03 09:47:01 -070099 // TODO(ed), use span here instead of constructing a new string
100 if (auth_header.substr(6) != this->auth_token2) {
Ed Tanousc4771fb2017-03-13 13:39:49 -0700101 return_unauthorized();
102 return;
103 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700104 // else let the request continue unharmed
Ed Tanous99923322017-03-03 14:21:24 -0800105 }
106}
Ed Tanousf9273472017-02-28 16:05:13 -0800107
Ed Tanous8041f312017-04-03 09:47:01 -0700108void TokenAuthorizationMiddleware::after_handle(request& req, response& res,
109 context& ctx) {}
Ed Tanousc4771fb2017-03-13 13:39:49 -0700110}