From 8669eed13c31a1c43afc53c9cfcacc21584a5ff3 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 31 Aug 2022 14:28:07 +0200 Subject: [PATCH] Replaced png library in backend --- backend/CMakeLists.txt | 28 +++++++++++++++-------- backend/src/controllers/auth/auth_2fa.cpp | 23 ++++++++++++++++--- backend/vcpkg.json | 4 ++-- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index ad20563..e5b22d9 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20) project(backend) -set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED YES) add_executable(backend @@ -38,12 +38,12 @@ add_executable(backend find_package(Drogon CONFIG REQUIRED) find_package(CURL CONFIG REQUIRED) -find_package(PNG REQUIRED) +find_package(lodepng CONFIG REQUIRED) +find_package(OpenSSL REQUIRED) find_path(JWT_CPP_INCLUDE_DIRS "jwt-cpp/base.h") find_path(BOTAN_INCLUDE_DIRS "botan/botan.h") find_path(QR_INCLUDE_DIRS "qrcodegen.hpp") -find_path(PNGPP_INCLUDE_DIRS "png++/color.hpp") -find_library(BOTAN_LIBRARY botan-2) +find_library(BOTAN_LIBRARY NAMES botan-2 botan) find_library(QR_LIBRARY nayuki-qr-code-generator) target_include_directories(backend PRIVATE @@ -51,20 +51,28 @@ target_include_directories(backend PRIVATE ${JWT_CPP_INCLUDE_DIRS} ${BOTAN_INCLUDE_DIRS} ${QR_INCLUDE_DIRS} - ${PNGPP_INCLUDE_DIRS} ) target_link_libraries(backend Drogon::Drogon CURL::libcurl - PNG::PNG + lodepng + OpenSSL::SSL ${BOTAN_LIBRARY} ${QR_LIBRARY} ) install(TARGETS backend) -target_compile_options(backend PRIVATE - $<$:-g -Wall -Wno-unknown-pragmas> - $<$:-O3> -) \ No newline at end of file +if(NOT MSVC) + target_compile_options(backend PRIVATE + $<$:-g -Wall -Wno-unknown-pragmas> + $<$:-O3> + ) +else() + target_compile_options(backend PRIVATE /W4 /wd4068) +endif(NOT MSVC) + +if(WIN32) + target_compile_definitions(backend PRIVATE NOMINMAX) +endif() diff --git a/backend/src/controllers/auth/auth_2fa.cpp b/backend/src/controllers/auth/auth_2fa.cpp index a0a5c52..2e084ab 100644 --- a/backend/src/controllers/auth/auth_2fa.cpp +++ b/backend/src/controllers/auth/auth_2fa.cpp @@ -5,14 +5,14 @@ #include #include #include -#include +#include #include "controllers/controllers.h" #include "db/db.h" #include "dto/dto.h" std::string create_totp_qrcode(const db::User& user, const std::string& b32_secret) { - const int qrcode_pixel_size = 4; + constexpr int qrcode_pixel_size = 4; std::stringstream code_ss; code_ss << "otpauth://totp/MFileserver:" @@ -22,6 +22,23 @@ std::string create_totp_qrcode(const db::User& user, const std::string& b32_secr << "&issuer=MFileserver"; auto code = qrcodegen::QrCode::encodeText(code_ss.str().c_str(), qrcodegen::QrCode::Ecc::MEDIUM); const int mod_count = code.getSize(); + + const int row_size = qrcode_pixel_size * mod_count; + std::vector secret, image, row; + row.reserve(row_size); + image.reserve(row_size * row_size); + + for (int y = 0; y < mod_count; y++) { + row.clear(); + for (int x = 0; x < mod_count; x++) + row.insert(row.end(), qrcode_pixel_size, code.getModule(x, y) ? 0 : 0xff); + for (int i = 0; i < qrcode_pixel_size; i++) + image.insert(image.end(), row.begin(), row.end()); + } + + lodepng::encode(secret, image, row_size, row_size, LCT_GREY, 8); + + /* png::image image(mod_count*qrcode_pixel_size, mod_count*qrcode_pixel_size); for (int x = 0; x < mod_count; x++) for (int y = 0; y < mod_count; y++) { const bool mod = code.getModule(x, y); @@ -34,7 +51,7 @@ std::string create_totp_qrcode(const db::User& user, const std::string& b32_secr std::string image_str = image_ss.str(); std::vector secret(image_str.data(), image_str.data()+image_str.size()); - +*/ return "data:image/png;base64," + Botan::base64_encode(secret); } diff --git a/backend/vcpkg.json b/backend/vcpkg.json index 5027b25..2ba3bcd 100644 --- a/backend/vcpkg.json +++ b/backend/vcpkg.json @@ -10,8 +10,8 @@ "jwt-cpp", "botan", "curl", - "pngpp", "nayuki-qr-code-generator", - "libpng" + "lodepng", + "openssl" ] } \ No newline at end of file