Basic auth

This commit is contained in:
Inex Code 2021-04-27 16:09:11 +00:00
parent 62e9027911
commit c4f1fb760a

View file

@ -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){ void handle_get(http_request message){
cout<<"Handle get: "<<message.to_string()<<endl; cout<<"Handle get: "<<message.to_string()<<endl;
json::value jsonObject; if (is_authentificated(message)) {
jsonObject[U("target_height")] = json::value::number(targetH); json::value jsonObject;
message.reply(status_codes::OK,jsonObject); jsonObject[U("target_height")] = json::value::number(targetH);
message.reply(status_codes::OK,jsonObject);
} else {
message.reply(status_codes::Forbidden);
}
} }
void handle_post(http_request message){ void handle_post(http_request message){
cout<<"Handle post: "<<message.to_string()<<endl; cout<<"Handle post: "<<message.to_string()<<endl;
json::value jsonObject;
try{ if (is_authentificated(message)) {
message.extract_json() json::value jsonObject;
.then([&jsonObject](json::value jo){ try{
cout<<"Val:"<<jo.serialize() << endl; message.extract_json()
jsonObject = jo; .then([&jsonObject](json::value jo){
mtx.lock(); cout<<"Val:"<<jo.serialize() << endl;
targetH = jsonObject.at(U("target_height")).as_number().to_double(); jsonObject = jo;
cout<<"Val:"<<targetH<< endl; mtx.lock();
mtx.unlock(); targetH = jsonObject.at(U("target_height")).as_number().to_double();
}) cout<<"Val:"<<targetH<< endl;
.wait(); mtx.unlock();
} })
catch (const std::exception & e) { .wait();
printf("Error exception:%s\n", e.what()); }
} catch (const std::exception & e) {
message.reply(status_codes::OK,jsonObject); printf("Error exception:%s\n", e.what());
}
message.reply(status_codes::OK,jsonObject);
} else {
message.reply(status_codes::Forbidden);
}
} }
void handle_quit(http_request message){ void handle_quit(http_request message){
cout<<"Handle quit: "<<message.to_string()<<endl; cout<<"Handle quit: "<<message.to_string()<<endl;
cmd = EExit; if (is_authentificated(message)) {
message.reply(status_codes::OK); cmd = EExit;
message.reply(status_codes::OK);
} else {
message.reply(status_codes::Forbidden);
}
} }
int main() int main()
@ -347,7 +385,7 @@ int main()
int rc = 0; int rc = 0;
web::http::experimental::listener::http_listener web::http::experimental::listener::http_listener
listener(U("http://localhost:8080/cpprest")); listener(U("http://localhost:8080/"));
listener.support(methods::GET,handle_get); listener.support(methods::GET,handle_get);
listener.support(methods::POST,handle_post); listener.support(methods::POST,handle_post);
listener.support(methods::DEL,handle_quit); listener.support(methods::DEL,handle_quit);