diff --git a/modules/server/include/authorizationtopic.h b/modules/server/include/authorizationtopic.h index ed33713003..edd6b83951 100644 --- a/modules/server/include/authorizationtopic.h +++ b/modules/server/include/authorizationtopic.h @@ -43,24 +43,22 @@ public: bool isDone(); /* https://httpstatuses.com/ */ - static const struct Statuses { - enum StatusCode { - OK = 200, - Accepted = 202, + enum class StatusCode : int { + OK = 200, + Accepted = 202, - BadRequest = 400, - Unauthorized = 401, - NotAcceptable = 406, + BadRequest = 400, + Unauthorized = 401, + NotAcceptable = 406, - NotImplemented = 501 - }; + NotImplemented = 501 }; private: bool _isAuthenticated; const std::string getKey() const; bool authorize(const std::string key); - nlohmann::json message(const std::string &message, const int &statusCode = Statuses::NotImplemented); + nlohmann::json message(const std::string &message, StatusCode statusCode = StatusCode::NotImplemented); }; } diff --git a/modules/server/src/topics/authorizationtopic.cpp b/modules/server/src/topics/authorizationtopic.cpp index 9b9939fcbd..bc000989c3 100644 --- a/modules/server/src/topics/authorizationtopic.cpp +++ b/modules/server/src/topics/authorizationtopic.cpp @@ -40,21 +40,21 @@ bool AuthorizationTopic::isDone() { void AuthorizationTopic::handleJson(nlohmann::json json) { if (isDone()) { - _connection->sendJson(message("Already authorized.", Statuses::OK)); + _connection->sendJson(message("Already authorized.", StatusCode::OK)); } else { try { auto providedKey = json.at("key").get(); if (authorize(providedKey)) { - _connection->sendJson(message("Authorization OK.", Statuses::Accepted)); + _connection->sendJson(message("Authorization OK.", StatusCode::Accepted)); _connection->setAuthorized(true); LINFO("Client successfully authorized."); } else { - _connection->sendJson(message("Invalid key", Statuses::NotAcceptable)); + _connection->sendJson(message("Invalid key", StatusCode::NotAcceptable)); } } catch (const std::out_of_range& e) { - _connection->sendJson(message("Invalid request, key must be provided.", Statuses::BadRequest)); + _connection->sendJson(message("Invalid request, key must be provided.", StatusCode::BadRequest)); } catch (const std::domain_error& e) { - _connection->sendJson(message("Invalid request, invalid key format.", Statuses::BadRequest)); + _connection->sendJson(message("Invalid request, invalid key format.", StatusCode::BadRequest)); } } }; @@ -75,8 +75,8 @@ const std::string AuthorizationTopic::getKey() const { return "17308"; } -nlohmann::json AuthorizationTopic::message(const std::string &message, const int &statusCode) { - nlohmann::json error = {{"message", message}, {"code", statusCode}}; +nlohmann::json AuthorizationTopic::message(const std::string &message, StatusCode statusCode) { + nlohmann::json error = {{"message", message}, {"code", static_cast(statusCode)}}; return error; }