auth /api/token
This commit is contained in:
+4
-4
@@ -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)
|
||||
add_executable(auth_service src/auth_service.cpp src/auth/auth.cpp)
|
||||
target_link_libraries(auth_service jwt-cpp::jwt-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;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef AUTH_HPP
|
||||
#define AUTH_HPP
|
||||
|
||||
#include <httplib.h>
|
||||
#include <jwt-cpp/jwt.h>
|
||||
#include <json.hpp>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include "../includes.hpp"
|
||||
#include <sstream>
|
||||
|
||||
void auth(const httplib::Request& request, httplib::Response& response);
|
||||
bool verify_auth(const std::string token);
|
||||
|
||||
#endif // AUTH_HPP
|
||||
@@ -1,14 +1,15 @@
|
||||
#include <iostream>
|
||||
#include "libs/httplib.h"
|
||||
#include "libs/json.hpp"
|
||||
#include <fstream>
|
||||
#include <httplib.h>
|
||||
#include <json.hpp>
|
||||
#include <jwt-cpp/jwt.h>
|
||||
|
||||
#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");
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
#define JWT_SECRET_KEY "secret"
|
||||
Reference in New Issue
Block a user