diff options
author | Ed Tanous <ed.tanous@intel.com> | 2017-03-04 01:21:24 +0300 |
---|---|---|
committer | Ed Tanous <ed.tanous@intel.com> | 2017-03-04 01:21:24 +0300 |
commit | 9992332be956c173199257b55e592ec2d2331e3a (patch) | |
tree | 9b7b17d8c92f7ad2bf32404ff719961a5150d0af /src | |
parent | 38bdb986b9133f5d728cc44b3d82321ce132eee3 (diff) | |
download | bmcweb-9992332be956c173199257b55e592ec2d2331e3a.tar.xz |
clang-format
Diffstat (limited to 'src')
-rw-r--r-- | src/base64.cpp | 143 | ||||
-rw-r--r-- | src/base64_test.cpp | 92 | ||||
-rw-r--r-- | src/token_authorization_middleware.cpp | 79 | ||||
-rw-r--r-- | src/token_authorization_middleware_test.cpp | 36 | ||||
-rw-r--r-- | src/webserver_main.cpp | 30 |
5 files changed, 172 insertions, 208 deletions
diff --git a/src/base64.cpp b/src/base64.cpp index 259288757b..001a467899 100644 --- a/src/base64.cpp +++ b/src/base64.cpp @@ -1,81 +1,67 @@ #include <base64.hpp> #include <cassert> -namespace base64 -{ -bool base64_encode(const gsl::cstring_span<> &input, std::string &output) -{ - static const char encoding_data[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - - unsigned int input_length = input.size(); - - // allocate space for output string - output.clear(); - output.reserve(((input_length + 2) / 3) * 4); - - // for each 3-bytes sequence from the input, extract 4 6-bits sequences and - // encode using - // encoding_data lookup table. - // if input do not contains enough chars to complete 3-byte sequence,use pad - // char '=' - for (unsigned int i = 0; i < input_length; i++) { - int base64code0 = 0; - int base64code1 = 0; - int base64code2 = 0; - int base64code3 = 0; - - base64code0 = (input[i] >> 2) & 0x3f; // 1-byte 6 bits - output += encoding_data[base64code0]; - base64code1 = (input[i] << 4) & 0x3f; // 1-byte 2 bits + - - if (++i < input_length) { - base64code1 |= (input[i] >> 4) & 0x0f; // 2-byte 4 bits - output += encoding_data[base64code1]; - base64code2 = (input[i] << 2) & 0x3f; // 2-byte 4 bits + - - if (++i < input_length) { - base64code2 |= (input[i] >> 6) & 0x03; // 3-byte 2 bits - base64code3 = input[i] & 0x3f; // 3-byte 6 bits - output += encoding_data[base64code2]; - output += encoding_data[base64code3]; - } else { - output += encoding_data[base64code2]; - output += '='; - } - } else { - output += encoding_data[base64code1]; - output += '='; - output += '='; - } +namespace base64 { +bool base64_encode(const gsl::cstring_span<> &input, std::string &output) { + static const char encoding_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + unsigned int input_length = input.size(); + + // allocate space for output string + output.clear(); + output.reserve(((input_length + 2) / 3) * 4); + + // for each 3-bytes sequence from the input, extract 4 6-bits sequences and + // encode using + // encoding_data lookup table. + // if input do not contains enough chars to complete 3-byte sequence,use pad + // char '=' + for (unsigned int i = 0; i < input_length; i++) { + int base64code0 = 0; + int base64code1 = 0; + int base64code2 = 0; + int base64code3 = 0; + + base64code0 = (input[i] >> 2) & 0x3f; // 1-byte 6 bits + output += encoding_data[base64code0]; + base64code1 = (input[i] << 4) & 0x3f; // 1-byte 2 bits + + + if (++i < input_length) { + base64code1 |= (input[i] >> 4) & 0x0f; // 2-byte 4 bits + output += encoding_data[base64code1]; + base64code2 = (input[i] << 2) & 0x3f; // 2-byte 4 bits + + + if (++i < input_length) { + base64code2 |= (input[i] >> 6) & 0x03; // 3-byte 2 bits + base64code3 = input[i] & 0x3f; // 3-byte 6 bits + output += encoding_data[base64code2]; + output += encoding_data[base64code3]; + } else { + output += encoding_data[base64code2]; + output += '='; + } + } else { + output += encoding_data[base64code1]; + output += '='; + output += '='; } + } - return true; + return true; } - -bool base64_decode(const gsl::cstring_span<> &input, std::string &output) -{ +bool base64_decode(const gsl::cstring_span<> &input, std::string &output) { static const char nop = -1; static const char decoding_data[] = { - nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, - nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, - nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, 62, nop, - nop, nop, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, nop, nop, - nop, nop, nop, nop, nop, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, - 25, nop, nop, nop, nop, nop, nop, 26, 27, 28, 29, 30, 31, 32, 33, - 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, - 49, 50, 51, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, - nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, - nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, - nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, - nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, - nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, - nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, - nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, - nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, - nop}; + nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, + nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, 62, nop, nop, nop, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, + nop, nop, nop, nop, nop, nop, nop, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, + 22, 23, 24, 25, nop, nop, nop, nop, nop, nop, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, + 45, 46, 47, 48, 49, 50, 51, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, + nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, + nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, + nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, + nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop}; unsigned int input_length = input.size(); @@ -90,29 +76,29 @@ bool base64_decode(const gsl::cstring_span<> &input, std::string &output) for (unsigned int i = 0; i < input_length; i++) { char base64code0; char base64code1; - char base64code2 = 0; // initialized to 0 to suppress warnings + char base64code2 = 0; // initialized to 0 to suppress warnings char base64code3; base64code0 = decoding_data[static_cast<int>(input[i])]; - if (base64code0 == nop) // non base64 character + if (base64code0 == nop) // non base64 character return false; - if (!(++i < input_length)) // we need at least two input bytes for first - // byte output + if (!(++i < input_length)) // we need at least two input bytes for first + // byte output return false; base64code1 = decoding_data[static_cast<int>(input[i])]; - if (base64code1 == nop) // non base64 character + if (base64code1 == nop) // non base64 character return false; output += ((base64code0 << 2) | ((base64code1 >> 4) & 0x3)); if (++i < input_length) { char c = input[i]; - if (c == '=') { // padding , end of input + if (c == '=') { // padding , end of input assert((base64code1 & 0x0f) == 0); return true; } base64code2 = decoding_data[static_cast<int>(input[i])]; - if (base64code2 == nop) // non base64 character + if (base64code2 == nop) // non base64 character return false; output += ((base64code1 << 4) & 0xf0) | ((base64code2 >> 2) & 0x0f); @@ -120,12 +106,12 @@ bool base64_decode(const gsl::cstring_span<> &input, std::string &output) if (++i < input_length) { char c = input[i]; - if (c == '=') { // padding , end of input + if (c == '=') { // padding , end of input assert((base64code2 & 0x03) == 0); return true; } base64code3 = decoding_data[static_cast<int>(input[i])]; - if (base64code3 == nop) // non base64 character + if (base64code3 == nop) // non base64 character return false; output += (((base64code2 << 6) & 0xc0) | base64code3); @@ -134,5 +120,4 @@ bool base64_decode(const gsl::cstring_span<> &input, std::string &output) return true; } - }
\ No newline at end of file diff --git a/src/base64_test.cpp b/src/base64_test.cpp index 3484976034..2758dfa80f 100644 --- a/src/base64_test.cpp +++ b/src/base64_test.cpp @@ -1,62 +1,58 @@ #include "base64.hpp" -#include "gtest/gtest.h" #include "big_list_of_naughty_strings.hpp" +#include "gtest/gtest.h" // Tests that Base64 basic strings work -TEST(Base64, EncodeBasicString) -{ - std::string output; - EXPECT_TRUE(base64::base64_encode("Foo", output)); +TEST(Base64, EncodeBasicString) { + std::string output; + EXPECT_TRUE(base64::base64_encode("Foo", output)); } // Tests the test vectors available in the base64 spec -TEST(Base64, EncodeRFC4648) -{ - std::string output; - EXPECT_TRUE(base64::base64_encode("", output)); - EXPECT_EQ(output, ""); - EXPECT_TRUE(base64::base64_encode("f", output)); - EXPECT_EQ(output, "Zg=="); - EXPECT_TRUE(base64::base64_encode("fo", output)); - EXPECT_EQ(output, "Zm8="); - EXPECT_TRUE(base64::base64_encode("foo", output)); - EXPECT_EQ(output, "Zm9v"); - EXPECT_TRUE(base64::base64_encode("foob", output)); - EXPECT_EQ(output, "Zm9vYg=="); - EXPECT_TRUE(base64::base64_encode("fooba", output)); - EXPECT_EQ(output, "Zm9vYmE="); - EXPECT_TRUE(base64::base64_encode("foobar", output)); - EXPECT_EQ(output, "Zm9vYmFy"); +TEST(Base64, EncodeRFC4648) { + std::string output; + EXPECT_TRUE(base64::base64_encode("", output)); + EXPECT_EQ(output, ""); + EXPECT_TRUE(base64::base64_encode("f", output)); + EXPECT_EQ(output, "Zg=="); + EXPECT_TRUE(base64::base64_encode("fo", output)); + EXPECT_EQ(output, "Zm8="); + EXPECT_TRUE(base64::base64_encode("foo", output)); + EXPECT_EQ(output, "Zm9v"); + EXPECT_TRUE(base64::base64_encode("foob", output)); + EXPECT_EQ(output, "Zm9vYg=="); + EXPECT_TRUE(base64::base64_encode("fooba", output)); + EXPECT_EQ(output, "Zm9vYmE="); + EXPECT_TRUE(base64::base64_encode("foobar", output)); + EXPECT_EQ(output, "Zm9vYmFy"); } // Tests the test vectors available in the base64 spec -TEST(Base64, DecodeRFC4648) -{ - std::string output; - EXPECT_TRUE(base64::base64_decode("", output)); - EXPECT_EQ(output, ""); - EXPECT_TRUE(base64::base64_decode("Zg==", output)); - EXPECT_EQ(output, "f"); - EXPECT_TRUE(base64::base64_decode("Zm8=", output)); - EXPECT_EQ(output, "fo"); - EXPECT_TRUE(base64::base64_decode("Zm9v", output)); - EXPECT_EQ(output, "foo"); - EXPECT_TRUE(base64::base64_decode("Zm9vYg==", output)); - EXPECT_EQ(output, "foob"); - EXPECT_TRUE(base64::base64_decode("Zm9vYmE=", output)); - EXPECT_EQ(output, "fooba"); - EXPECT_TRUE(base64::base64_decode("Zm9vYmFy", output)); - EXPECT_EQ(output, "foobar"); +TEST(Base64, DecodeRFC4648) { + std::string output; + EXPECT_TRUE(base64::base64_decode("", output)); + EXPECT_EQ(output, ""); + EXPECT_TRUE(base64::base64_decode("Zg==", output)); + EXPECT_EQ(output, "f"); + EXPECT_TRUE(base64::base64_decode("Zm8=", output)); + EXPECT_EQ(output, "fo"); + EXPECT_TRUE(base64::base64_decode("Zm9v", output)); + EXPECT_EQ(output, "foo"); + EXPECT_TRUE(base64::base64_decode("Zm9vYg==", output)); + EXPECT_EQ(output, "foob"); + EXPECT_TRUE(base64::base64_decode("Zm9vYmE=", output)); + EXPECT_EQ(output, "fooba"); + EXPECT_TRUE(base64::base64_decode("Zm9vYmFy", output)); + EXPECT_EQ(output, "foobar"); } // Tests using pathalogical cases for all escapings -TEST(Base64, NaugtyStrings){ - std::string base64_string; - std::string decoded_string; - for (auto& str: naughty_strings){ - EXPECT_TRUE(base64::base64_encode(str, base64_string)); - EXPECT_TRUE(base64::base64_decode(base64_string, decoded_string)); - EXPECT_EQ(str, decoded_string); - } +TEST(Base64, NaugtyStrings) { + std::string base64_string; + std::string decoded_string; + for (auto& str : naughty_strings) { + EXPECT_TRUE(base64::base64_encode(str, base64_string)); + EXPECT_TRUE(base64::base64_decode(base64_string, decoded_string)); + EXPECT_EQ(str, decoded_string); + } } - diff --git a/src/token_authorization_middleware.cpp b/src/token_authorization_middleware.cpp index d1972fadf1..aeef58c6a9 100644 --- a/src/token_authorization_middleware.cpp +++ b/src/token_authorization_middleware.cpp @@ -4,49 +4,38 @@ #include <token_authorization_middleware.hpp> -namespace crow -{ - std::string TokenAuthorizationMiddleware::context::get_cookie(const std::string& key) - { - if (cookie_sessions.count(key)) - return cookie_sessions[key]; - return {}; - } - - void TokenAuthorizationMiddleware::context::set_cookie(const std::string& key, const std::string& value) - { - cookies_to_push_to_client.emplace(key, value); - } - - - void TokenAuthorizationMiddleware::before_handle(crow::request& req, response& res, context& ctx) - { - auto return_unauthorized = [&req, &res](){ - res.code = 401; - res.end(); - }; - if (req.url == "/login"){ - - } - // Check for an authorization header, reject if not present - if (req.headers.count("Authorization") != 1) { - return_unauthorized(); - return; - } - - std::string auth_header = req.get_header_value("Authorization"); - // If the user is attempting any kind of auth other than token, reject - if (!boost::starts_with(auth_header, "Token ")) { - return_unauthorized(); - return; - } - } - - void TokenAuthorizationMiddleware::after_handle(request& /*req*/, response& res, context& ctx) - { - for (auto& cookie : ctx.cookies_to_push_to_client) { - res.add_header("Set-Cookie", cookie.first + "=" + cookie.second); - } - } - +namespace crow { +std::string TokenAuthorizationMiddleware::context::get_cookie(const std::string& key) { + if (cookie_sessions.count(key)) return cookie_sessions[key]; + return {}; +} + +void TokenAuthorizationMiddleware::context::set_cookie(const std::string& key, const std::string& value) { cookies_to_push_to_client.emplace(key, value); } + +void TokenAuthorizationMiddleware::before_handle(crow::request& req, response& res, context& ctx) { + auto return_unauthorized = [&req, &res]() { + res.code = 401; + res.end(); + }; + if (req.url == "/login") { + } + // Check for an authorization header, reject if not present + if (req.headers.count("Authorization") != 1) { + return_unauthorized(); + return; + } + + std::string auth_header = req.get_header_value("Authorization"); + // If the user is attempting any kind of auth other than token, reject + if (!boost::starts_with(auth_header, "Token ")) { + return_unauthorized(); + return; + } +} + +void TokenAuthorizationMiddleware::after_handle(request& /*req*/, response& res, context& ctx) { + for (auto& cookie : ctx.cookies_to_push_to_client) { + res.add_header("Set-Cookie", cookie.first + "=" + cookie.second); + } +} }
\ No newline at end of file diff --git a/src/token_authorization_middleware_test.cpp b/src/token_authorization_middleware_test.cpp index 39a12cd546..aef33e35a7 100644 --- a/src/token_authorization_middleware_test.cpp +++ b/src/token_authorization_middleware_test.cpp @@ -2,27 +2,23 @@ #include <crow/app.h> #include "gtest/gtest.h" - // Tests that Base64 basic strings work -TEST(Authentication, TestBasicReject) -{ - /* - crow::App<crow::TokenAuthorizationMiddleware> app; - crow::request req; - crow::response res; - app.handle(req, res); - ASSERT_EQ(res.code, 400); +TEST(Authentication, TestBasicReject) { + /* + crow::App<crow::TokenAuthorizationMiddleware> app; + crow::request req; + crow::response res; + app.handle(req, res); + ASSERT_EQ(res.code, 400); - crow::App<crow::TokenAuthorizationMiddleware> app; - decltype(app)::server_t server(&app, "127.0.0.1", 45451); - CROW_ROUTE(app, "/")([&](const crow::request& req) - { - app.get_context<NullMiddleware>(req); - app.get_context<NullSimpleMiddleware>(req); - return ""; - }); - */ + crow::App<crow::TokenAuthorizationMiddleware> app; + decltype(app)::server_t server(&app, "127.0.0.1", 45451); + CROW_ROUTE(app, "/")([&](const crow::request& req) + { + app.get_context<NullMiddleware>(req); + app.get_context<NullSimpleMiddleware>(req); + return ""; + }); + */ } - - diff --git a/src/webserver_main.cpp b/src/webserver_main.cpp index 6c92e34737..32c5d35067 100644 --- a/src/webserver_main.cpp +++ b/src/webserver_main.cpp @@ -22,8 +22,8 @@ #include "crow/websocket.h" #include "color_cout_g3_sink.hpp" -#include "webassets.hpp" #include "token_authorization_middleware.hpp" +#include "webassets.hpp" #include <iostream> #include <string> @@ -31,25 +31,23 @@ #include <webassets.hpp> -int main(int argc, char** argv) -{ - auto worker = g3::LogWorker::createLogWorker(); - auto handle = worker->addDefaultLogger(argv[0], "/tmp/"); - g3::initializeLogging(worker.get()); - auto log_file_name = handle->call(&g3::FileSink::fileName); - auto sink_handle = worker->addSink(std::make_unique<crow::ColorCoutSink>(), - &crow::ColorCoutSink::ReceiveLogMessage); +int main(int argc, char** argv) { + auto worker = g3::LogWorker::createLogWorker(); + auto handle = worker->addDefaultLogger(argv[0], "/tmp/"); + g3::initializeLogging(worker.get()); + auto log_file_name = handle->call(&g3::FileSink::fileName); + auto sink_handle = worker->addSink(std::make_unique<crow::ColorCoutSink>(), &crow::ColorCoutSink::ReceiveLogMessage); - LOG(DEBUG) << "Logging to " << log_file_name.get() << "\n"; + LOG(DEBUG) << "Logging to " << log_file_name.get() << "\n"; - std::string ssl_pem_file("server.pem"); - ensuressl::ensure_openssl_key_present_and_valid(ssl_pem_file); + std::string ssl_pem_file("server.pem"); + ensuressl::ensure_openssl_key_present_and_valid(ssl_pem_file); - crow::App<crow::TokenAuthorizationMiddleware> app; + crow::App<crow::TokenAuthorizationMiddleware> app; - crow::webassets::request_routes(app); + crow::webassets::request_routes(app); - crow::logger::setLogLevel(crow::LogLevel::DEBUG); + crow::logger::setLogLevel(crow::LogLevel::DEBUG); - app.port(18080).run(); + app.port(18080).run(); } |