From 0939525cf3aca20ae32b3a2226b0f7bd673ebda9 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 31 Aug 2022 21:54:48 +0200 Subject: [PATCH] Fixed mail --- backend/CMakeLists.txt | 6 ++-- backend/src/controllers/auth/auth_2fa.cpp | 14 -------- backend/src/controllers/auth/auth_common.cpp | 37 ++++++-------------- backend/src/main.cpp | 4 --- backend/vcpkg.json | 2 +- 5 files changed, 14 insertions(+), 49 deletions(-) diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index e5b22d9..643a33b 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -37,7 +37,7 @@ add_executable(backend find_package(Drogon CONFIG REQUIRED) -find_package(CURL CONFIG REQUIRED) +find_package(mailio CONFIG REQUIRED) find_package(lodepng CONFIG REQUIRED) find_package(OpenSSL REQUIRED) find_path(JWT_CPP_INCLUDE_DIRS "jwt-cpp/base.h") @@ -55,7 +55,7 @@ target_include_directories(backend PRIVATE target_link_libraries(backend Drogon::Drogon - CURL::libcurl + mailio lodepng OpenSSL::SSL ${BOTAN_LIBRARY} @@ -74,5 +74,5 @@ else() endif(NOT MSVC) if(WIN32) - target_compile_definitions(backend PRIVATE NOMINMAX) + target_compile_definitions(backend PRIVATE NOMINMAX _WIN32_WINNT=0x0A00) endif() diff --git a/backend/src/controllers/auth/auth_2fa.cpp b/backend/src/controllers/auth/auth_2fa.cpp index 2e084ab..a8c067b 100644 --- a/backend/src/controllers/auth/auth_2fa.cpp +++ b/backend/src/controllers/auth/auth_2fa.cpp @@ -38,20 +38,6 @@ std::string create_totp_qrcode(const db::User& user, const std::string& b32_secr 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); - const int x_img_start = x * qrcode_pixel_size, y_img_start = y * qrcode_pixel_size; - for (int x_img = x_img_start; x_img < x_img_start + qrcode_pixel_size; x_img++) for (int y_img = y_img_start; y_img < y_img_start + qrcode_pixel_size; y_img++) - image[x_img][y_img] = mod ? 0 : 0xff; - } - std::stringstream image_ss; - image.write_stream(image_ss); - - 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/src/controllers/auth/auth_common.cpp b/backend/src/controllers/auth/auth_common.cpp index 5ce8d73..120c3fc 100644 --- a/backend/src/controllers/auth/auth_common.cpp +++ b/backend/src/controllers/auth/auth_common.cpp @@ -17,16 +17,12 @@ #include #include -#include +#include #include "controllers/controllers.h" #include "db/db.h" #include "dto/dto.h" -size_t payload_source(char* ptr, size_t size, size_t nmemb, void* userp) { - auto* ss = (std::stringstream*)userp; - return ss->readsome(ptr, (long)(size*nmemb)); -} namespace api { #if defined(BOTAN_HAS_SYSTEM_RNG) @@ -42,33 +38,20 @@ namespace api { } void auth::send_mail(const db::User& user) { - std::stringstream ss; std::time_t t = std::time(nullptr); const auto& totp_secret = (const std::vector&) user.getValueOfTfaSecret(); char totp[16]; std::snprintf(totp, 16, "%06d", Botan::TOTP(Botan::OctetString(totp_secret)).generate_totp(t)); - ss.imbue(std::locale("en_US.utf8")); - ss << "Date: " << std::put_time(std::localtime(&t), "%a, %d %b %Y %T %z") << "\r\n"; - ss << "To: " << user.getValueOfName() << "\r\n"; - ss << "From: fileserver@mattv.de\r\n"; - ss << "Message-ID: " << Botan::UUID(*rng).to_string() << "@mattv.de>\r\n"; - ss << "Subject: Fileserver - EMail 2fa code\r\n"; - ss << "Your code is: " << totp << "\r\n"; - ss << "It is valid for 5 Minutes\r\n"; - CURL* curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_USERNAME, "no-reply@mattv.de"); - curl_easy_setopt(curl, CURLOPT_PASSWORD, "noreplyLONGPASS123"); - curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.mattv.de:587"); - curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); - auto recp = curl_slist_append(nullptr, user.getValueOfName().c_str()); - curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recp); - curl_easy_setopt(curl, CURLOPT_READFUNCTION, &payload_source); - curl_easy_setopt(curl, CURLOPT_READDATA, &ss); - curl_easy_setopt(curl, CURLOPT_UPLOAD, 1); - curl_easy_perform(curl); - curl_slist_free_all(recp); - curl_easy_cleanup(curl); + mailio::message msg; + msg.from(mailio::mail_address("Fileserver", "fileserver@mattv.de")); + msg.add_recipient(mailio::mail_address(user.getValueOfName(), user.getValueOfName())); + msg.subject("Subject: Fileserver - Email 2fa code"); + msg.content("Your code is: " + std::string(totp) +"\r\nIt is valid for 5 Minutes"); + + mailio::smtps conn("mail.mattv.de", 587); + conn.authenticate("no-reply@mattv.de", "noreplyLONGPASS123", mailio::smtps::auth_method_t::START_TLS); + conn.submit(msg); } std::string auth::get_token(const db::User& user) { diff --git a/backend/src/main.cpp b/backend/src/main.cpp index d67109e..802b680 100644 --- a/backend/src/main.cpp +++ b/backend/src/main.cpp @@ -2,7 +2,6 @@ #include #include -#include #include "dto/dto.h" @@ -38,9 +37,6 @@ int main(int argc, char* argv[]) { if (std::find(args.begin(), args.end(), "--dev") != args.end()) dev_mode = true; if (dev_mode) std::cout << "Starting in development mode" << std::endl; std::cout << "Setting up..." << std::endl; - std::cout << "Initializing curl..." << std::flush; - curl_global_init(CURL_GLOBAL_ALL); - std::cout << " [Done]" << std::endl; if (!std::filesystem::exists("files")) { std::cout << "Creating files..." << std::flush; std::filesystem::create_directory("files"); diff --git a/backend/vcpkg.json b/backend/vcpkg.json index 2ba3bcd..310b235 100644 --- a/backend/vcpkg.json +++ b/backend/vcpkg.json @@ -9,7 +9,7 @@ }, "jwt-cpp", "botan", - "curl", + "mailio", "nayuki-qr-code-generator", "lodepng", "openssl"