Refactoring mail code

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-04-05 11:27:01 +01:00
parent 7d0b9f9d44
commit f512f7db3f
4 changed files with 56 additions and 62 deletions

View File

@ -8,7 +8,11 @@ async function getAlive(){
async function getReady(){ async function getReady(){
log.out(`kubeServices.getReady: ready hook checked`, "info") log.out(`kubeServices.getReady: ready hook checked`, "info")
testing.sendTest("fred@fjla.uk"); testing.send({
to: "fred@fjla.uk",
subject: "OwlBoard Test",
txt: "This is a test message from OwlBoard (testing)"
});
return "not_implemented"; return "not_implemented";
}; };

View File

@ -1,8 +1,7 @@
const log = require('../utils/log.utils') const log = require('../utils/log.utils')
const fs = require('fs/promises')
const mail = require('nodemailer'); //>> Probs wrong const mail = require('nodemailer'); //>> Probs wrong
const fromAdrr = process.env.OWL_EML_FROM const fromAddr = process.env.OWL_EML_FROM
const smtpUser = process.env.OWL_EML_USER const smtpUser = process.env.OWL_EML_USER
const smtpPass = process.env.OWL_EML_PASS const smtpPass = process.env.OWL_EML_PASS
const smtpHost = process.env.OWL_EML_HOST const smtpHost = process.env.OWL_EML_HOST
@ -18,57 +17,19 @@ let transporter = mail.createTransport({
} }
}) })
async function sendTest(to, cc, bcc) { async function send(message){ // message is an object containing strings for: *to, cc, bcc, *subject, *txt, html (* denotes required)
log.out(`mailServices.sendTest: Sending test message to: ${to}`, "info") log.out(`mailServices.send: Message send request received`, "info")
let tHtml = fs.readFile('mail-templates/register.html', 'utf-8'); message.from = fromAddr
let tTxt = fs.readFile('mail-templates/register.txt', 'utf-8'); try {
// Send test mail message var res = await transporter.sendMail(message)
try { } catch(err) {
var res = await transporter.sendMail({ log.out(`mailServices.send: Message send failed`, "err")
from: fromAdrr, return false;
to: to, }
cc: cc, log.out(`mailServices.send: SMTP Response: ${res.response}`)
bcc: bcc, return true;
subject: "Test Message from OwlBoard",
text: (await tTxt).replace(/>>ACCESSCODE<</g, "TEST-MESSAGE-ONLY"),
html: (await tHtml).replace(/>>ACCESSCODE<</g, "TEST-MESSAGE-ONLY")
});
} catch(err) {
log.out(err, "warn")
var res = "failed"
}
return res;
}
async function sendRegister(to, accesscode) {
log.out(`mailServices.sendRegister: Sending registration message to: ${to}`, "info")
let tHtml = fs.readFile('mail-templates/register.html', 'utf-8');
let tTxt = fs.readFile('mail-templates/register.txt', 'utf-8');
// Send test mail message
try {
var res = transporter.sendMail({
from: fromAdrr,
to: to,
cc: cc,
bcc: bcc,
subject: "OwlBoard - Complete Registration",
text: (await tTxt).replace(/>>ACCESSCODE<</g, accesscode),
html: (await tHtml).replace(/>>ACCESSCODE<</g, accesscode)
});
return await res;
} catch(err) {
log.out("mailServices.sendRegister: Mail send failed")
log.out(err, "warn")
return "failed"
}
}
async function sendAlert() {
return;
} }
module.exports = { module.exports = {
sendTest, send
sendRegister,
sendAlert
} }

View File

@ -1,16 +1,27 @@
const log = require('../utils/log.utils') // Currently no logging on this page const log = require('../utils/log.utils') // Currently no logging on this page
const util = require('../utils/auth.utils') const auth = require('../utils/auth.utils')
const db = require('../services/dbAccess.services') const db = require('./dbAccess.services')
const mail = require('../services/mail.services') const mail = require('./mail.services')
const clean = require('../utils/sanitizer.utils') const clean = require('../utils/sanitizer.utils')
const domList= require('../configs/domains.configs') const domList= require('../configs/domains.configs')
async function createRegKey(eml){ // Needs moving to register.services async function createRegKey(eml){
const domain = clean.splitDomain(eml) const domain = clean.splitDomain(eml)
log.out(`registerServices: New registration request from domain: ${domain}`, "info")
if (domain in domList.valid) { // Don't know if this is correct if (domain in domList.valid) { // Don't know if this is correct
const uuid = util.generateKey(); const uuid = auth.generateKey();
db.addRegReq(await uuid, await domain) db.addRegReq(await uuid, await domain)
mail.sendRegister("mail", "uuid"); const message = auth.generateConfirmationEmail(eml, uuid)
if (await message == false) { // This error should be handled in the upstream function
const err = new Error("Message generation error");
log.out(`registerServices.createRegKey: Error generating registration email`, "err")
return 500
}
try { // Send the email
await mail.send(message);
} catch(err){ // HTTP 500 if sen failed
return 500
}
return 201 // These returns still need handling return 201 // These returns still need handling
} else { } else {
return 401 // As above return 401 // As above

View File

@ -1,6 +1,7 @@
const log = require('../utils/log.utils'); const log = require('../utils/log.utils');
const crypto = require('crypto') const crypto = require('crypto')
const db = require('../services/dbAccess.services') const db = require('../services/dbAccess.services');
const fs = require('fs/promises')
// Checks users registration key against issued keys // Checks users registration key against issued keys
async function isAuthed(key) { // Needs testing async function isAuthed(key) { // Needs testing
@ -19,7 +20,24 @@ async function generateKey() { // Needs testing & moving to 'register.utils'
return crypto.randomUUID() return crypto.randomUUID()
}; };
async function generateConfirmationEmail(eml, uuid) {
try {
let htmlTpl = fs.readFile('mail-templates/register.html', 'utf-8');
let txtTpl = fs.readFile('mail-templates/register.txt', 'utf-8');
return msg = {
to: eml,
subject: "OwlBoard Registration",
text: (await txtTpl).replace(/>>ACCESSCODE<</g, uuid),
html: (await htmlTpl).replace(/>>ACCESSCODE<</g, uuid)
}
} catch(err) {
log.out(`mailServices.generateConfirmationEmail: Error reading templates, $(err)`, "err");
return false;
}
}
module.exports = { module.exports = {
isAuthed, isAuthed,
generateKey generateKey,
generateConfirmationEmail
} }