Needs testing

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2024-03-09 20:46:17 +00:00
parent 5904ee37cd
commit 874b236f09
2 changed files with 11 additions and 38 deletions

View File

@ -1,8 +1,6 @@
const auth = require("../utils/auth.utils"); const auth = require("../utils/auth.utils");
const db = require("./dbAccess.services"); const db = require("./dbAccess.services");
const mail = require("./mail.services"); const mail = require("./mail.services");
const clean = require("../utils/sanitizer.utils");
const domains = require("../configs/domains.configs");
const errors = require("../configs/errorCodes.configs"); const errors = require("../configs/errorCodes.configs");
import { logger } from "../utils/logger.utils"; import { logger } from "../utils/logger.utils";
@ -33,38 +31,7 @@ async function createRegKey(body) {
} else { } else {
return { status: 400, errorCode: 901, errorMsg: errors[902] }; 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) { async function regUser(req) {
// Add input validation // Add input validation

View File

@ -20,7 +20,6 @@ async function isAuthed(uuid: string): Promise<boolean> {
// Checks whether a registration request key is valid // Checks whether a registration request key is valid
async function checkRequest(key: string) { 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 collection = "registrations";
const query = { uuid: key }; const query = { uuid: key };
const res = await db.query(collection, query); const res = await db.query(collection, query);
@ -34,14 +33,21 @@ async function checkRequest(key: string) {
// Creates an API key for a user // Creates an API key for a user
async function generateKey() { async function generateKey() {
// Needs testing & moving to 'register.utils' ??? Why does it need moving?
return crypt.randomUUID(); return crypt.randomUUID();
} }
export function generateCode(): string { export function generateCode(): string {
const bytes = crypt.randomBytes(3); const characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
const randomNumber = bytes.readUIntBE(0, 3) % 1000000; const codeLength = 6;
return randomNumber.toString().padStart(6, '0');
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) { async function generateConfirmationEmail(eml: string, uuid: string) {