mirror of
https://github.com/yawaflua/SpCloudCore.git
synced 2025-12-08 19:49:28 +02:00
introduce login service
This commit is contained in:
@@ -21,7 +21,7 @@ public:
|
||||
{
|
||||
std::string auth_code_processed = extract_code(auth_code);
|
||||
|
||||
//std::string body = "client_id=1273414933874479185&"
|
||||
//std::string body = "client_id=1273414933874479185&"//Todo delete comments if not needed
|
||||
// "client_secret=S_vG4frjlxWoi8mic_GlcxUO0aWxXwRJ&"
|
||||
// "grant_type=authorization_code&"
|
||||
// "code=" + auth_code_processed + "&"
|
||||
@@ -44,7 +44,6 @@ public:
|
||||
// return "Error: ";//Todo write handling this
|
||||
//}
|
||||
|
||||
|
||||
std::string command = "curl --location https://discord.com/api/oauth2/token "
|
||||
"--header \"Content-Type: application/x-www-form-urlencoded\" "
|
||||
"--data-urlencode \"client_id=1273414933874479185\" "
|
||||
@@ -53,8 +52,48 @@ public:
|
||||
"--data-urlencode \"code=" + auth_code_processed + "\" "
|
||||
"--data-urlencode \"redirect_uri=https://www.sp-donate.ru/pay/Hepatir\"";
|
||||
|
||||
auto future = std::async(std::launch::async, &DiscordService::execute_command, this, command);
|
||||
return future.get();
|
||||
auto code_request = std::async(std::launch::async, &DiscordService::execute_command, this, command);
|
||||
|
||||
std::string access_token = parse_access_token(code_request.get());
|
||||
|
||||
|
||||
command = "curl --location https://discord.com/api/users/@me "
|
||||
"--header \"Authorization: Bearer " + access_token + "\" ";
|
||||
|
||||
auto me_request = std::async(std::launch::async, &DiscordService::execute_command, this, command);
|
||||
|
||||
//httplib::Headers headers = {
|
||||
// {"Authorization", "Bearer " + access_token}
|
||||
//};
|
||||
|
||||
//auto res = client_.Get("/users/@me", headers);
|
||||
|
||||
//if (res && res->status == 200) {
|
||||
// return res->body;
|
||||
//}
|
||||
//else {
|
||||
// return "Error: ";//Todo write handling this
|
||||
//}
|
||||
|
||||
std::string user_id = extract_user_id(me_request.get());
|
||||
|
||||
return user_id;
|
||||
}
|
||||
|
||||
std::string extract_user_id(const std::string& input) {
|
||||
std::string search_pattern = "\"id\":\"";
|
||||
std::size_t start_pos = input.find(search_pattern);
|
||||
if (start_pos == std::string::npos) {
|
||||
throw std::runtime_error("discord_id not found");
|
||||
}
|
||||
|
||||
start_pos += search_pattern.length();
|
||||
std::size_t end_pos = input.find("\"", start_pos);
|
||||
if (end_pos == std::string::npos) {
|
||||
throw std::runtime_error("End of discord_id not found");
|
||||
}
|
||||
|
||||
return input.substr(start_pos, end_pos - start_pos);
|
||||
}
|
||||
|
||||
std::string execute_command(const std::string& command) {
|
||||
@@ -68,6 +107,23 @@ public:
|
||||
return result;
|
||||
}
|
||||
|
||||
//std::string extract_user_id(const std::string& input) {
|
||||
// std::string search_pattern = "\"discord_id\":\"";
|
||||
// std::size_t start_pos = input.find(search_pattern);
|
||||
// if (start_pos == std::string::npos) {
|
||||
// throw std::runtime_error("discord_id not found");
|
||||
// }
|
||||
|
||||
// start_pos += search_pattern.length(); // move to the start of the user_id
|
||||
// std::size_t end_pos = input.find("\"", start_pos);
|
||||
// if (end_pos == std::string::npos) {
|
||||
// throw std::runtime_error("End of discord_id not found");
|
||||
// }
|
||||
|
||||
// return input.substr(start_pos, end_pos - start_pos);
|
||||
//}
|
||||
|
||||
|
||||
std::string extract_code(const std::string& json_str) {
|
||||
std::string key = "\"code\":";
|
||||
size_t start = json_str.find(key);
|
||||
@@ -87,4 +143,17 @@ public:
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string parse_access_token(const std::string& response) {
|
||||
std::string token_label = "\"access_token\": \"";
|
||||
size_t start_pos = response.find(token_label);
|
||||
if (start_pos != std::string::npos) {
|
||||
start_pos += token_label.length();
|
||||
size_t end_pos = response.find("\"", start_pos);
|
||||
if (end_pos != std::string::npos) {
|
||||
return response.substr(start_pos, end_pos - start_pos);
|
||||
}
|
||||
}
|
||||
return ""; // Вернуть пустую строку, если токен не найден
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
// ReSharper disable CppClangTidyBugproneSuspiciousInclude
|
||||
//#include <curl/curl.h>/*
|
||||
#include <iostream>
|
||||
|
||||
#include "../httplib.h"
|
||||
#include <string>
|
||||
#include <future>
|
||||
//#include "../src/mongocxx/"s
|
||||
|
||||
class MongoDbService
|
||||
{
|
||||
|
||||
public:
|
||||
MongoDbService()
|
||||
MongoDbService()
|
||||
{
|
||||
//mongocxx::instance inst{};
|
||||
//const auto uri = mongocxx::uri{ "mongodb+srv://loker:<password>@spcloudcluster.xwpnw.mongodb.net/?retryWrites=true&w=majority&appName=SpCloudCluster" };
|
||||
@@ -24,4 +26,56 @@ public:
|
||||
//db.run_command(ping_cmd.view());
|
||||
//std::cout << "Pinged your deployment. You successfully connected to MongoDB!" << std::endl;
|
||||
}
|
||||
|
||||
std::string get_user_info(std::string user_id)
|
||||
{
|
||||
/*std::string json_data = R"({
|
||||
"dataSource": "SpCloudCluster",
|
||||
"database": "SpCloud",
|
||||
"collection": "User",
|
||||
"document": {
|
||||
"name": "Item Name",
|
||||
"value": "Item Value"
|
||||
}
|
||||
})";
|
||||
std::string command = "curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/insertOne' "
|
||||
"--header 'Content-Type: application/json' "
|
||||
"--header 'api-key: Q1NfSCrruUAzsxdrjhZd3sjSwiqbdSFmCLeaCatZiuohUXsvEq9RtEAeG0JL2Jd7' "
|
||||
"--data-raw '" + json_data + "'";
|
||||
|
||||
auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command);
|
||||
*/
|
||||
|
||||
std::string json_data = R"({
|
||||
"dataSource": "SpCloudCluster",
|
||||
"database": "SpCloud",
|
||||
"collection": "AllowedUsers",
|
||||
"filter": {
|
||||
"discord_id": ")" + user_id + R"("
|
||||
}
|
||||
})";
|
||||
|
||||
std::string command = "curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/findOne' "
|
||||
"--header 'Content-Type: application/json' "
|
||||
"--header 'api-key: Q1NfSCrruUAzsxdrjhZd3sjSwiqbdSFmCLeaCatZiuohUXsvEq9RtEAeG0JL2Jd7' "
|
||||
"--data-raw '" + json_data + "'";
|
||||
|
||||
auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command);
|
||||
|
||||
std::string response = request.get();
|
||||
|
||||
return response;
|
||||
//return response.find("\"document\": null") == std::string::npos;
|
||||
}
|
||||
|
||||
std::string execute_command(const std::string& command) {
|
||||
std::array<char, 128> buffer;
|
||||
std::string result;
|
||||
std::shared_ptr<FILE> pipe(popen(command.c_str(), "r"), pclose);
|
||||
if (!pipe) throw std::runtime_error("popen() failed!");
|
||||
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
|
||||
result += buffer.data();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "httplib.h"
|
||||
#include "Controllers/PublishController.cpp"
|
||||
#include "Service/DiscordService.cpp"
|
||||
#include "Service/MongoDbService.cpp"
|
||||
|
||||
//#include "Service/AuthorizationService.cpp"
|
||||
//#include "Service/FileProcessingService.cpp"
|
||||
@@ -53,19 +54,15 @@ int main()
|
||||
});
|
||||
|
||||
DiscordService discord_service;
|
||||
MongoDbService mongo_service;
|
||||
|
||||
svr.Post("/register", [&](const httplib::Request& req, httplib::Response& res)
|
||||
svr.Post("/login", [&](const httplib::Request& req, httplib::Response& res)
|
||||
{
|
||||
|
||||
|
||||
string discord_id = discord_service.get_discord_id(req.body);
|
||||
|
||||
res.set_content(discord_id, "text/plain");
|
||||
|
||||
/*logger.log(INFO, "Start publish from main");
|
||||
|
||||
publish_controller.process_publish(req, res);*/
|
||||
std::string result = mongo_service.get_user_info(discord_id);
|
||||
|
||||
res.set_content(result, "text/plain");
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user