Fixed mail

This commit is contained in:
Mutzi 2022-08-31 21:54:48 +02:00
parent 16177b98ef
commit 0939525cf3
5 changed files with 14 additions and 49 deletions

View File

@ -37,7 +37,7 @@ add_executable(backend
find_package(Drogon CONFIG REQUIRED) find_package(Drogon CONFIG REQUIRED)
find_package(CURL CONFIG REQUIRED) find_package(mailio CONFIG REQUIRED)
find_package(lodepng CONFIG REQUIRED) find_package(lodepng CONFIG REQUIRED)
find_package(OpenSSL REQUIRED) find_package(OpenSSL REQUIRED)
find_path(JWT_CPP_INCLUDE_DIRS "jwt-cpp/base.h") find_path(JWT_CPP_INCLUDE_DIRS "jwt-cpp/base.h")
@ -55,7 +55,7 @@ target_include_directories(backend PRIVATE
target_link_libraries(backend target_link_libraries(backend
Drogon::Drogon Drogon::Drogon
CURL::libcurl mailio
lodepng lodepng
OpenSSL::SSL OpenSSL::SSL
${BOTAN_LIBRARY} ${BOTAN_LIBRARY}
@ -74,5 +74,5 @@ else()
endif(NOT MSVC) endif(NOT MSVC)
if(WIN32) if(WIN32)
target_compile_definitions(backend PRIVATE NOMINMAX) target_compile_definitions(backend PRIVATE NOMINMAX _WIN32_WINNT=0x0A00)
endif() endif()

View File

@ -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); lodepng::encode(secret, image, row_size, row_size, LCT_GREY, 8);
/*
png::image<png::gray_pixel> 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<uint8_t> secret(image_str.data(), image_str.data()+image_str.size());
*/
return "data:image/png;base64," + Botan::base64_encode(secret); return "data:image/png;base64," + Botan::base64_encode(secret);
} }

View File

@ -17,16 +17,12 @@
#include <jwt-cpp/traits/kazuho-picojson/traits.h> #include <jwt-cpp/traits/kazuho-picojson/traits.h>
#include <jwt-cpp/jwt.h> #include <jwt-cpp/jwt.h>
#include <curl/curl.h> #include <mailio/smtp.hpp>
#include "controllers/controllers.h" #include "controllers/controllers.h"
#include "db/db.h" #include "db/db.h"
#include "dto/dto.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 { namespace api {
#if defined(BOTAN_HAS_SYSTEM_RNG) #if defined(BOTAN_HAS_SYSTEM_RNG)
@ -42,33 +38,20 @@ namespace api {
} }
void auth::send_mail(const db::User& user) { void auth::send_mail(const db::User& user) {
std::stringstream ss;
std::time_t t = std::time(nullptr); std::time_t t = std::time(nullptr);
const auto& totp_secret = (const std::vector<uint8_t>&) user.getValueOfTfaSecret(); const auto& totp_secret = (const std::vector<uint8_t>&) user.getValueOfTfaSecret();
char totp[16]; char totp[16];
std::snprintf(totp, 16, "%06d", Botan::TOTP(Botan::OctetString(totp_secret)).generate_totp(t)); 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(); mailio::message msg;
curl_easy_setopt(curl, CURLOPT_USERNAME, "no-reply@mattv.de"); msg.from(mailio::mail_address("Fileserver", "fileserver@mattv.de"));
curl_easy_setopt(curl, CURLOPT_PASSWORD, "noreplyLONGPASS123"); msg.add_recipient(mailio::mail_address(user.getValueOfName(), user.getValueOfName()));
curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.mattv.de:587"); msg.subject("Subject: Fileserver - Email 2fa code");
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); msg.content("Your code is: " + std::string(totp) +"\r\nIt is valid for 5 Minutes");
auto recp = curl_slist_append(nullptr, user.getValueOfName().c_str());
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recp); mailio::smtps conn("mail.mattv.de", 587);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &payload_source); conn.authenticate("no-reply@mattv.de", "noreplyLONGPASS123", mailio::smtps::auth_method_t::START_TLS);
curl_easy_setopt(curl, CURLOPT_READDATA, &ss); conn.submit(msg);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
curl_easy_perform(curl);
curl_slist_free_all(recp);
curl_easy_cleanup(curl);
} }
std::string auth::get_token(const db::User& user) { std::string auth::get_token(const db::User& user) {

View File

@ -2,7 +2,6 @@
#include <fstream> #include <fstream>
#include <drogon/drogon.h> #include <drogon/drogon.h>
#include <curl/curl.h>
#include "dto/dto.h" #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 (std::find(args.begin(), args.end(), "--dev") != args.end()) dev_mode = true;
if (dev_mode) std::cout << "Starting in development mode" << std::endl; if (dev_mode) std::cout << "Starting in development mode" << std::endl;
std::cout << "Setting up..." << 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")) { if (!std::filesystem::exists("files")) {
std::cout << "Creating files..." << std::flush; std::cout << "Creating files..." << std::flush;
std::filesystem::create_directory("files"); std::filesystem::create_directory("files");

View File

@ -9,7 +9,7 @@
}, },
"jwt-cpp", "jwt-cpp",
"botan", "botan",
"curl", "mailio",
"nayuki-qr-code-generator", "nayuki-qr-code-generator",
"lodepng", "lodepng",
"openssl" "openssl"