Refactoring mail code
Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
parent
7d0b9f9d44
commit
f512f7db3f
@ -8,7 +8,11 @@ async function getAlive(){
|
||||
|
||||
async function getReady(){
|
||||
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";
|
||||
};
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
const log = require('../utils/log.utils')
|
||||
const fs = require('fs/promises')
|
||||
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 smtpPass = process.env.OWL_EML_PASS
|
||||
const smtpHost = process.env.OWL_EML_HOST
|
||||
@ -18,57 +17,19 @@ let transporter = mail.createTransport({
|
||||
}
|
||||
})
|
||||
|
||||
async function sendTest(to, cc, bcc) {
|
||||
log.out(`mailServices.sendTest: Sending test 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 = await transporter.sendMail({
|
||||
from: fromAdrr,
|
||||
to: to,
|
||||
cc: cc,
|
||||
bcc: bcc,
|
||||
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;
|
||||
async function send(message){ // message is an object containing strings for: *to, cc, bcc, *subject, *txt, html (* denotes required)
|
||||
log.out(`mailServices.send: Message send request received`, "info")
|
||||
message.from = fromAddr
|
||||
try {
|
||||
var res = await transporter.sendMail(message)
|
||||
} catch(err) {
|
||||
log.out(`mailServices.send: Message send failed`, "err")
|
||||
return false;
|
||||
}
|
||||
log.out(`mailServices.send: SMTP Response: ${res.response}`)
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
sendTest,
|
||||
sendRegister,
|
||||
sendAlert
|
||||
send
|
||||
}
|
@ -1,16 +1,27 @@
|
||||
const log = require('../utils/log.utils') // Currently no logging on this page
|
||||
const util = require('../utils/auth.utils')
|
||||
const db = require('../services/dbAccess.services')
|
||||
const mail = require('../services/mail.services')
|
||||
const auth = require('../utils/auth.utils')
|
||||
const db = require('./dbAccess.services')
|
||||
const mail = require('./mail.services')
|
||||
const clean = require('../utils/sanitizer.utils')
|
||||
const domList= require('../configs/domains.configs')
|
||||
|
||||
async function createRegKey(eml){ // Needs moving to register.services
|
||||
async function createRegKey(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
|
||||
const uuid = util.generateKey();
|
||||
const uuid = auth.generateKey();
|
||||
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
|
||||
} else {
|
||||
return 401 // As above
|
@ -1,6 +1,7 @@
|
||||
const log = require('../utils/log.utils');
|
||||
const log = require('../utils/log.utils');
|
||||
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
|
||||
async function isAuthed(key) { // Needs testing
|
||||
@ -19,7 +20,24 @@ async function generateKey() { // Needs testing & moving to 'register.utils'
|
||||
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 = {
|
||||
isAuthed,
|
||||
generateKey
|
||||
generateKey,
|
||||
generateConfirmationEmail
|
||||
}
|
Loading…
Reference in New Issue
Block a user