blob: abe002fec6547cb16775f9aa8059108445f54511 [file] [log] [blame]
Ed Tanousf9273472017-02-28 16:05:13 -08001#include <unordered_map>
2
3#include <boost/algorithm/string/predicate.hpp>
4
5#include <token_authorization_middleware.hpp>
6
7namespace crow
8{
9 std::string TokenAuthorizationMiddleware::context::get_cookie(const std::string& key)
10 {
11 if (cookie_sessions.count(key))
12 return cookie_sessions[key];
13 return {};
14 }
15
16 void TokenAuthorizationMiddleware::context::set_cookie(const std::string& key, const std::string& value)
17 {
18 cookies_to_push_to_client.emplace(key, value);
19 }
20
21
22 void TokenAuthorizationMiddleware::before_handle(crow::request& req, response& res, context& ctx)
23 {
Ed Tanous904063f2017-03-02 16:48:24 -080024 return;
Ed Tanousf9273472017-02-28 16:05:13 -080025 if (req.url == "/login"){
26 return;
27 }
28
29 // Check for an authorization header, reject if not present
30 if (req.headers.count("Authorization") != 1) {
31 res.code = 400;
32 res.end();
33 return;
34 }
35 std::string auth_header = req.get_header_value("Authorization");
36 // If the user is attempting any kind of auth other than token, reject
37 if (!boost::starts_with(auth_header, "Token ")) {
38 res.code = 400;
39 res.end();
40 }
41 }
42
43 void TokenAuthorizationMiddleware::after_handle(request& /*req*/, response& res, context& ctx)
44 {
45 for (auto& cookie : ctx.cookies_to_push_to_client) {
46 res.add_header("Set-Cookie", cookie.first + "=" + cookie.second);
47 }
48 }
49
50}