почти рабочая авторизация. оказывается сейчас нет payload у запроса, поэтому невозможно распарсить из него json.

This commit is contained in:
2024-11-04 17:57:47 +03:00
parent 0b794fac40
commit b561dedb2b
13 changed files with 362 additions and 138 deletions

78
src/auth/utils.cpp Normal file
View File

@@ -0,0 +1,78 @@
#include "utils.h"
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <string>
#include <iomanip>
std::string http::utils::sha256(const std::string &payload) {
// Вычисляем SHA256 хеш
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256(reinterpret_cast<const unsigned char *>(payload.c_str()), payload.length(), hash);
// Преобразуем хеш в шестнадцатеричную строку
std::stringstream ss;
for (unsigned char i : hash) {
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(i);
}
return ss.str();
}
std::string http::utils::b64Encode(const std::string &payload) {
BIO *bio_mem = BIO_new(BIO_s_mem());
BIO *bio_b64 = BIO_new(BIO_f_base64());
bio_b64 = BIO_push(bio_b64, bio_mem);
BIO_write(bio_b64, payload.c_str(), payload.length());
BIO_flush(bio_b64);
BUF_MEM *bptr = nullptr;
BIO_get_mem_data(bio_mem, &bptr);
std::string result(bptr->data, bptr->length);
BIO_free_all(bio_b64);
return result;
}
std::string http::utils::b64Decode(const std::string &payload) {
BIO *bio_mem = BIO_new(BIO_s_mem());
BIO *bio_b64 = BIO_new(BIO_f_base64());
bio_b64 = BIO_push(bio_b64, bio_mem);
BIO_write(bio_b64, payload.c_str(), static_cast<int>(payload.size()));
BIO_flush(bio_b64);
const int len = BIO_get_mem_data(bio_mem, NULL);
char buffer[static_cast<size_t>(len)];
BIO_read(bio_mem, buffer, len);
std::string result(buffer, static_cast<unsigned int>(len));
BIO_free_all(bio_b64);
return result;
}
std::map<std::string, std::string> http::utils::parseCookies(const std::string& cookieString) {
std::map<std::string, std::string> cookies;
std::istringstream cookieStream(cookieString);
std::string cookie;
while (std::getline(cookieStream, cookie, ';')) {
// Разделяем имя и значение Cookie
size_t equalPos = cookie.find('=');
if (equalPos == std::string::npos) {
continue; // Неверный формат Cookie
}
std::string name = cookie.substr(0, equalPos);
std::string value = cookie.substr(equalPos + 1);
// Удаляем пробелы с начала и конца значения Cookie
value.erase(0, value.find_first_not_of(' '));
value.erase(value.find_last_not_of(' ') + 1);
// Добавляем Cookie в map
cookies[name] = value;
}
return cookies;
}