From 874b236f09b30e809fa6ea5ac2e1c9dece3ddcc6 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 9 Mar 2024 20:46:17 +0000 Subject: [PATCH] Needs testing Signed-off-by: Fred Boniface --- src/services/registration.services.js | 33 --------------------------- src/utils/auth.utils.ts | 16 +++++++++---- 2 files changed, 11 insertions(+), 38 deletions(-) diff --git a/src/services/registration.services.js b/src/services/registration.services.js index 9a892e3..9b0664c 100644 --- a/src/services/registration.services.js +++ b/src/services/registration.services.js @@ -1,8 +1,6 @@ const auth = require("../utils/auth.utils"); const db = require("./dbAccess.services"); const mail = require("./mail.services"); -const clean = require("../utils/sanitizer.utils"); -const domains = require("../configs/domains.configs"); const errors = require("../configs/errorCodes.configs"); import { logger } from "../utils/logger.utils"; @@ -33,38 +31,7 @@ async function createRegKey(body) { } 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) { - const domain = await clean.getDomainFromEmail(body.email); - logger.info(`registerServices: Registration request from: ${domain}`); - if (domains.includes(domain)) { - logger.debug(`registerServices.createRegKey: Key from valid: ${domain}`); - const uuid = await auth.generateKey(); - db.addRegReq(uuid, domain); - const message = await auth.generateConfirmationEmail(body.email, uuid); - 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] }; - } } -*/ async function regUser(req) { // Add input validation diff --git a/src/utils/auth.utils.ts b/src/utils/auth.utils.ts index c452a6e..1c9c75e 100644 --- a/src/utils/auth.utils.ts +++ b/src/utils/auth.utils.ts @@ -20,7 +20,6 @@ async function isAuthed(uuid: string): Promise { // Checks whether a registration request key is valid async function checkRequest(key: string) { - // For some reason db.query seems to return correctly, but the second logs.out statement prints []??? so registration fails!! const collection = "registrations"; const query = { uuid: key }; const res = await db.query(collection, query); @@ -34,14 +33,21 @@ async function checkRequest(key: string) { // Creates an API key for a user async function generateKey() { - // Needs testing & moving to 'register.utils' ??? Why does it need moving? 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'); + const characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; + const codeLength = 6; + + let code = ''; + const bytes = crypt.randomBytes(codeLength); // Generate random bytes + for (let i = 0; i < codeLength; i++) { + const randomIndex = bytes[i] % characters.length; // Map bytes to characters + code += characters.charAt(randomIndex); + } + + return code; } async function generateConfirmationEmail(eml: string, uuid: string) {