From ac9e9b468aedf58eeb2ce7203578bbc5481861ec Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Sat, 13 Jul 2024 11:17:30 +0100 Subject: [PATCH] Update SSO mail sending --- engine/user/one-time-code.html | 4 +++ engine/user/send-email-code.js | 55 ++++++++++++++++++++++++++++++++-- users/send-email-code.js | 27 +++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 engine/user/one-time-code.html diff --git a/engine/user/one-time-code.html b/engine/user/one-time-code.html new file mode 100644 index 0000000..a290728 --- /dev/null +++ b/engine/user/one-time-code.html @@ -0,0 +1,4 @@ +

One Time Code

+

Please use this code to login

+

{{code}}

+

Please not that this code expires after 10 minutes

diff --git a/engine/user/send-email-code.js b/engine/user/send-email-code.js index 9b3f460..3c82d75 100644 --- a/engine/user/send-email-code.js +++ b/engine/user/send-email-code.js @@ -2,6 +2,9 @@ const hashPassword = require("../../functions/hashPassword"); const varDatabaseDbHandler = require("../engine/utils/varDatabaseDbHandler"); +const nodemailer = require("nodemailer"); +const fs = require("fs"); +const path = require("path"); /** * @@ -9,9 +12,23 @@ const varDatabaseDbHandler = require("../engine/utils/varDatabaseDbHandler"); * @param {string} param0.email * @param {import("../../types/database-schema.td").DSQL_DatabaseSchemaType} [param0.dbSchema] * @param {string} param0.email_login_field + * @param {string} param0.mail_domain + * @param {string} param0.mail_username + * @param {string} param0.mail_password + * @param {number} [param0.mail_port] + * @param {string} [param0.sender] * @returns */ -async function localSendEmailCode({ email, dbSchema, email_login_field }) { +async function localSendEmailCode({ + email, + dbSchema, + email_login_field, + mail_domain, + mail_username, + mail_password, + mail_port, + sender, +}) { try { /** * User auth @@ -71,9 +88,43 @@ async function localSendEmailCode({ email, dbSchema, email_login_field }) { if (foundUser && foundUser[0] && email_login_field) { const tempCode = generateCode(); + + let transporter = nodemailer.createTransport({ + host: mail_domain, + port: mail_port || 465, + secure: true, + auth: { + user: mail_username, + pass: mail_password, + }, + }); + + let mailObject = {}; + + mailObject["from"] = `"Datasquirel SSO" <${ + sender || "support@datasquirel.com" + }>`; + mailObject["sender"] = sender || "support@summitlending.com"; + mailObject["to"] = email; + mailObject["subject"] = "One Time Email Login Code"; + mailObject["html"] = fs + .readFileSync( + path.resolve(__dirname, "one-time-code.html"), + "utf-8" + ) + .replace(/{{code}}/, tempCode); + + const info = await transporter.sendMail(mailObject); + + if (!info?.accepted) throw new Error("Mail not Sent!"); + + /** ********************************************** */ + /** ********************************************** */ + /** ********************************************** */ + let setTempCode = await varDatabaseDbHandler({ queryString: `UPDATE users SET ${email_login_field} = ? WHERE email = ?`, - queryValuesArray: [tempCode, email], + queryValuesArray: [tempCode + `-${Date.now()}`, email], database: dbFullName.replace(/[^a-z0-9_]/g, ""), tableSchema, }); diff --git a/users/send-email-code.js b/users/send-email-code.js index cfafc7e..9bc05a7 100644 --- a/users/send-email-code.js +++ b/users/send-email-code.js @@ -41,6 +41,11 @@ const localSendEmailCode = require("../engine/user/send-email-code"); * @param {String} params.encryptionKey - Encryption Key * @param {String} params.encryptionSalt - Encryption Salt * @param {string} [params.temp_code_field] - Database table field name for temporary code + * @param {string} params.mail_domain + * @param {string} params.mail_username + * @param {string} params.mail_password + * @param {number} [params.mail_port] + * @param {string} [params.sender] * * @returns { Promise} */ @@ -51,6 +56,11 @@ async function sendEmailCode({ encryptionKey, encryptionSalt, temp_code_field, + mail_domain, + mail_password, + mail_username, + mail_port, + sender, }) { const scheme = process.env.DSQL_HTTP_SCHEME; const localHost = process.env.DSQL_LOCAL_HOST; @@ -116,6 +126,11 @@ async function sendEmailCode({ email, dbSchema, email_login_field: emailLoginTempCodeFieldName, + mail_domain, + mail_password, + mail_username, + mail_port, + sender, }); } } else { @@ -131,6 +146,18 @@ async function sendEmailCode({ email, database, email_login_field: emailLoginTempCodeFieldName, + mail_domain, + mail_password, + mail_username, + mail_port, + sender, + html: fs.readFileSync( + path.resolve( + __dirname, + "../../engine/user/one-time-code.html" + ), + "utf-8" + ), }); const httpsRequest = (