From 2675bb15e3c49365190b581bafce4553089499d5 Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Wed, 22 May 2024 01:17:45 +0700 Subject: [PATCH] auth /api/token --- CMakeLists.txt | 8 ++++---- src/auth/auth.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/auth/auth.hpp | 15 +++++++++++++++ src/auth_service.cpp | 11 ++++++----- src/includes.hpp | 1 + 5 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 src/auth/auth.cpp create mode 100644 src/auth/auth.hpp create mode 100644 src/includes.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b55c3da..164a8ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,9 +2,9 @@ cmake_minimum_required (VERSION 2.6) project (auth_service) set(CMAKE_CXX_STANDARD 11) -include_directories(./src/lib) +include_directories(./src/libs/) -# find_package(cpp-httplib REQUIRED) +find_package(jwt-cpp REQUIRED) -add_executable(auth_service src/auth_service.cpp) -# target_link_libraries(auth_service cpp-httplib) \ No newline at end of file +add_executable(auth_service src/auth_service.cpp src/auth/auth.cpp) +target_link_libraries(auth_service jwt-cpp::jwt-cpp) \ No newline at end of file diff --git a/src/auth/auth.cpp b/src/auth/auth.cpp new file mode 100644 index 0000000..85c4136 --- /dev/null +++ b/src/auth/auth.cpp @@ -0,0 +1,38 @@ +#include "auth.hpp" + +void auth(const httplib::Request& request, httplib::Response& response) { + if (request.body == "") {response.set_content("{\"needed\":\"[uername,password]\"}", "application/json");return;} + nlohmann::json json_body = nlohmann::json::parse(request.body); + + if (json_body["username"] == nullptr) {response.set_content("{\"needed\":\"[uername,password]\"}", "application/json");return;} + if (json_body["password"] == nullptr) {response.set_content("{\"needed\":\"[uername,password]\"}", "application/json");return;} + const std::string request_username = json_body["username"]; + const std::string request_password = json_body["password"]; + + std::ifstream usersfile("users.json"); + nlohmann::json all_users = nlohmann::json::parse(usersfile); + usersfile.close(); + + int userid = -1; + for (int i = 0; i < all_users.size(); i++) { + if ((all_users[i]["username"] == request_username) && (all_users[i]["password"] == request_password)) { + userid = all_users[i]["id"]; + } + } + + if (userid == -1) {response.set_content("{\"status\":\"403\"}", "application/json");return;} + + auto token = jwt::create() + .set_type("JWT") + // .set_issuer("auth0") + .set_payload_claim("userId", jwt::claim(std::to_string(userid))) + .sign(jwt::algorithm::hs256{JWT_SECRET_KEY}); + + std::stringstream response_json; + response_json << "{\"token\":\"" << token << "\"}"; + response.set_content(response_json.str(), "application/json"); +} + +bool verify_auth(const std::string token) { + return true; +} diff --git a/src/auth/auth.hpp b/src/auth/auth.hpp new file mode 100644 index 0000000..422013a --- /dev/null +++ b/src/auth/auth.hpp @@ -0,0 +1,15 @@ +#ifndef AUTH_HPP +#define AUTH_HPP + +#include +#include +#include +#include +#include +#include "../includes.hpp" +#include + +void auth(const httplib::Request& request, httplib::Response& response); +bool verify_auth(const std::string token); + +#endif // AUTH_HPP diff --git a/src/auth_service.cpp b/src/auth_service.cpp index df43c94..3e01d3a 100644 --- a/src/auth_service.cpp +++ b/src/auth_service.cpp @@ -1,14 +1,15 @@ #include -#include "libs/httplib.h" -#include "libs/json.hpp" #include +#include +#include +#include + +#include "auth/auth.hpp" int main() { httplib::Server srv; - srv.Get("/api/token", [](const httplib::Request& request, httplib::Response& response) { - response.set_content("{\"token\":\"qwerty\"}", "application/json"); - }); + srv.Post("/api/token", auth); srv.Get("/api/users", [](const httplib::Request& request, httplib::Response& response) { std::ifstream usersfile("users.json"); diff --git a/src/includes.hpp b/src/includes.hpp new file mode 100644 index 0000000..7e70284 --- /dev/null +++ b/src/includes.hpp @@ -0,0 +1 @@ +#define JWT_SECRET_KEY "secret" \ No newline at end of file