From c4f1fb760a809db089a6ea16d477b426185d7ad5 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Tue, 27 Apr 2021 16:09:11 +0000 Subject: [PATCH] Basic auth --- UML1/superpoohREST.cpp | 86 ++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 24 deletions(-) diff --git a/UML1/superpoohREST.cpp b/UML1/superpoohREST.cpp index 2b64460..037be6d 100644 --- a/UML1/superpoohREST.cpp +++ b/UML1/superpoohREST.cpp @@ -307,38 +307,76 @@ void message_callback(struct mosquitto* mosq, void* obj, const struct mosquitto_ } }*/ +bool is_authentificated(http_request message) { + auto headers = message.headers(); + if (message.headers().find("Authorization") == headers.end()) return false; + auto authHeader = headers["Authorization"]; + auto credsPos = authHeader.find("Basic"); + if (credsPos == std::string::npos) + return false; + + auto base64 = authHeader.substr(credsPos + std::string("Basic").length() + 1); + if (base64.empty()) return false; + auto bytes = utility::conversions::from_base64(base64); + std::string creds(bytes.begin(), bytes.end()); + auto colonPos = creds.find(":"); + if (colonPos == std::string::npos) return false; + auto user = creds.substr(0, colonPos); + auto password = creds.substr(colonPos + 1, creds.size() - colonPos - 1); + + if (user == "pooh" && password == "honey") { + return true; + } else { + return false; + } +} + void handle_get(http_request message){ cout<<"Handle get: "<