From 70c9aa2b1e0c1ed0296c9434d87d24dc57ecae48 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Fri, 8 Mar 2024 21:39:39 +0000 Subject: [PATCH] Begin migration to registration codes Signed-off-by: Fred Boniface --- mail-templates/register.txt | 10 ++++----- src/services/registration.services.js | 30 +++++++++++++++++++++++++++ src/utils/auth.utils.ts | 6 ++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/mail-templates/register.txt b/mail-templates/register.txt index 1cf68eb..e49454d 100644 --- a/mail-templates/register.txt +++ b/mail-templates/register.txt @@ -1,14 +1,14 @@ -Complete your OwlBoard (Staff) Registration using the link below. +Complete your OwlBoard (Staff) Registration by entering your six digit code. -https://owlboard.info/more/reg/submit?key=>>ACCESSCODE<< +>>ACCESSCODE<< -Alternatively you can copy and paste the above link into your browser. +Use the same browser you wish to use OwlBoard with -You can also view a QR code to register on another device: https://barcodes.fjla.uk/generate?type=qr&text=https%3A%2F%2Fowlboard.info%2Fmore%2Freg%2Fsubmit%3Fkey%3D>>ACCESSCODE<< +Visit https://owlboard.info/more/reg/submit to enter your key Watch support videos or leave a message on our Facebook page: https://facebook.com/owlboard.support If you did not request to register to OwlBoard then you can safely ignore this email. Your email address has not been stored by us and will not be required unless you wish to register again. -The link will expire after 4 hours. \ No newline at end of file +The link will expire after 1 hour. \ No newline at end of file diff --git a/src/services/registration.services.js b/src/services/registration.services.js index 796bf4c..9a892e3 100644 --- a/src/services/registration.services.js +++ b/src/services/registration.services.js @@ -6,7 +6,36 @@ const domains = require("../configs/domains.configs"); const errors = require("../configs/errorCodes.configs"); import { logger } from "../utils/logger.utils"; +import { getDomainFromEmail } from "../utils/sanitizer.utils"; +import { valid as validDomains } from "../configs/domains.configs"; +import { generateCode } from "../utils/auth.utils"; +async function createRegKey(body) { + logger.debug("registerServices.createRegKey: Incoming request"); + if (body.email) { + const domain = getDomainFromEmail(body.email); + logger.info(`registerServices: Registration request from: ${domain}`); + if (validDomains.includes(domain)) { + logger.debug(`registerServices.createRegKey: Key from valid: ${domain}`); + const key = generateCode() + const message = await auth.generateConfirmationEmail(body.email, key); + if (!message) { + const err = new Error("Message Generation Error"); + logger.error(err, "registerServices.createRegKey: Error generating email"); + return 500; + } + if ((await mail.send(message)) == true) { + return {status: 201, message: "email sent"}; + } + return {status:500, errorCode:950, errorMsg: errors[950]} + } + return { status: 403, errorCode: 702, errorMsg: errors[702] }; + } else { + return { status: 400, errorCode: 901, errorMsg: errors[902] }; + } +} // NEXT-- UPDATE EMAIL TEMPLATE TO ACCEPT SIX DIGIT CODE RATHER THAN UUID - HTML TEMPLATE NEEDS COMPLETELY REDOING + +/* OLD FUNCTION async function createRegKey(body) { logger.debug("registerServices.createRegKey: Incoming request"); if (body.email) { @@ -35,6 +64,7 @@ async function createRegKey(body) { return { status: 400, errorCode: 901, errorMsg: errors[902] }; } } +*/ async function regUser(req) { // Add input validation diff --git a/src/utils/auth.utils.ts b/src/utils/auth.utils.ts index cd287ea..81ae8db 100644 --- a/src/utils/auth.utils.ts +++ b/src/utils/auth.utils.ts @@ -38,6 +38,12 @@ async function generateKey() { return crypt.randomUUID(); } +export function generateCode(): string { + const bytes = crypt.randomBytes(3); + const randomNumber = bytes.readUIntBE(0, 3) % 1000000; + return randomNumber.toString().padStart(6, '0'); +} + async function generateConfirmationEmail(eml: string, uuid: string) { try { const htmlTpl = await fs.readFile("mail-templates/register.html", "utf-8");