1 #include "BasicRequestAuthentication.h"
7 namespace Odb::Lib::App
9 BasicRequestAuthentication::BasicRequestAuthentication(
bool disableAuthentication)
10 : RequestAuthenticationBase(disableAuthentication)
14 crow::response BasicRequestAuthentication::AuthenticateRequest(
const crow::request& req)
16 auto resp = RequestAuthenticationBase::AuthenticateRequest(req);
17 if (resp.code != crow::status::OK)
19 const auto& authHeader = req.get_header_value(AUTHORIZATION_HEADER_NAME);
20 if (authHeader.empty())
return crow::response(401,
"Unauthorized");
22 auto authValue = authHeader.substr(6);
23 if (authValue.empty())
return crow::response(401,
"Unauthorized");
25 auto authValueDecoded = crow::utility::base64decode(authValue, authValue.size());
26 if (authValueDecoded.empty())
return crow::response(401,
"Unauthorized");
28 auto seperatorPos = authValueDecoded.find(
':');
29 if (seperatorPos == std::string::npos)
return crow::response(401,
"Unauthorized");
31 auto username = authValueDecoded.substr(0, seperatorPos);
32 auto password = authValueDecoded.substr(seperatorPos + 1);
35 resp = VerifyCredentials(username, password);
40 crow::response BasicRequestAuthentication::VerifyCredentials(
const std::string& username,
const std::string& password)
43 auto validUsername = std::getenv(USERNAME_ENV_NAME);
44 if (validUsername ==
nullptr)
47 validUsername =
"odb";
50 auto validPassword = std::getenv(PASSWORD_ENV_NAME);
51 if (validPassword ==
nullptr)
54 validPassword =
"plusplus";
58 if (username != validUsername ||
59 password != validPassword)
61 return crow::response(403,
"Invalid username or password");
65 return crow::response(200,
"Authorized");