From 38cf679da764e605f35bfdc8ae00d89571ed64c4 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Wed, 5 Apr 2023 20:53:07 +0100 Subject: [PATCH] Transform the register.request function stack It now works in dev environment pending testing --- app.js | 3 ++- src/configs/domains.configs.js | 4 +-- src/controllers/auth.controllers.js | 14 ---------- src/controllers/registration.controllers.js | 26 +++++++++++++++++++ src/routes/auth.routes.js | 7 ----- src/routes/registration.routes.js | 8 ++++++ ...r.services.js => registration.services.js} | 20 +++++++++----- 7 files changed, 51 insertions(+), 31 deletions(-) delete mode 100644 src/controllers/auth.controllers.js create mode 100644 src/controllers/registration.controllers.js delete mode 100644 src/routes/auth.routes.js create mode 100644 src/routes/registration.routes.js rename src/services/{register.services.js => registration.services.js} (59%) diff --git a/app.js b/app.js index f5b077c..5c128bb 100644 --- a/app.js +++ b/app.js @@ -23,7 +23,7 @@ const kubeRtr = require('./src/routes/kube.routes'); // /kube endpoints const findRtr = require('./src/routes/find.routes'); // /find endpoints const issueRtr = require('./src/routes/issue.routes') // /issue endpoints const statRtr = require('./src/routes/stats.routes'); // /stat endpoints -const auth = require('./src/routes/auth.routes'); // /auth endpoints +const regRtr = require('./src/routes/registration.routes'); // /auth endpoints // Set Server Configurations const srvListen = process.env.OWL_SRV_LISTEN || "0.0.0.0" @@ -60,6 +60,7 @@ app.use('/api/v1/kube', kubeRtr); app.use('/api/v1/find', findRtr); app.use('/api/v1/issue', issueRtr); app.use('/api/v1/stats', statRtr) +app.use('/api/v1/register', regRtr) // Authented Routes app.use('/api/v1/ldbs', authenticate) diff --git a/src/configs/domains.configs.js b/src/configs/domains.configs.js index 0b93a75..f7a3e79 100644 --- a/src/configs/domains.configs.js +++ b/src/configs/domains.configs.js @@ -1,4 +1,4 @@ -const valid = [ +module.exports = valid = [ "owlboard.info", "gwr.com", "swrailway.com", @@ -8,4 +8,4 @@ const valid = [ // Use Network Rail Control contact book to add domains -module.exports = valid \ No newline at end of file +// module.exports = valid \ No newline at end of file diff --git a/src/controllers/auth.controllers.js b/src/controllers/auth.controllers.js deleted file mode 100644 index 4df90a4..0000000 --- a/src/controllers/auth.controllers.js +++ /dev/null @@ -1,14 +0,0 @@ -const auth = require('../services/auth.services'); - -async function register(req, res, next){ - try { - res.json(await auth.addUser(req.body)) - } catch (err) { - console.error(`Controller Error`, err.message); - next(err); - } -} - -module.exports = { - register -} \ No newline at end of file diff --git a/src/controllers/registration.controllers.js b/src/controllers/registration.controllers.js new file mode 100644 index 0000000..f81706e --- /dev/null +++ b/src/controllers/registration.controllers.js @@ -0,0 +1,26 @@ +const reg = require('../services/registration.services'); + +async function register(req, res, next) { + try { + let response = await reg.addUser(req.body) + res.status(response.status).json(response) + } catch (err) { + console.error(`Controller Error`, err.message) + next(err) + } +} + +async function request(req, res, next) { + try { + let response = await reg.createRegKey(req.body) + res.status(response.status).json(response) + } catch (err) { + console.error(err) + next(err) + } +} + +module.exports = { + register, + request +} \ No newline at end of file diff --git a/src/routes/auth.routes.js b/src/routes/auth.routes.js deleted file mode 100644 index 3c2f96e..0000000 --- a/src/routes/auth.routes.js +++ /dev/null @@ -1,7 +0,0 @@ -const express = require('express'); -const router = express.Router(); -const authController = require('../controllers/auth.controllers'); - -router.post('/register', authController.register); - -module.exports = router; \ No newline at end of file diff --git a/src/routes/registration.routes.js b/src/routes/registration.routes.js new file mode 100644 index 0000000..afee9cd --- /dev/null +++ b/src/routes/registration.routes.js @@ -0,0 +1,8 @@ +const express = require('express'); +const router = express.Router(); +const regController = require('../controllers/registration.controllers'); + +router.post('/request', regController.request); +router.post('/register', regController.register) + +module.exports = router; \ No newline at end of file diff --git a/src/services/register.services.js b/src/services/registration.services.js similarity index 59% rename from src/services/register.services.js rename to src/services/registration.services.js index b60d9a8..b390f5d 100644 --- a/src/services/register.services.js +++ b/src/services/registration.services.js @@ -3,23 +3,29 @@ 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') +const domains = require('../configs/domains.configs') -async function createRegKey(eml){ - const domain = await clean.splitDomain(eml) +async function createRegKey(body){ + log.out(`registerServices.createRegKey: Incoming request`, "INFO") + log.out(`registerServices.createRegKey: ${JSON.stringify(domains)}`) + const domain = await clean.splitDomain(body.email) log.out(`registerServices: New registration request from domain: ${domain}`, "info") - if (domList.valid.includes(domain)) { // Needs testing + if (domains.includes(domain)) { // Needs testing + log.out(`registerServices.createRegKey: Key from valid domain: ${domain}`) const uuid = auth.generateKey(); db.addRegReq(await uuid, domain) - const message = auth.generateConfirmationEmail(eml, uuid) + const message = auth.generateConfirmationEmail(body.email, 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; } - return await mail.send(message); + if (await mail.send(message) == true) { + return {status: 201, message: "email sent"} + } + return {status: 500, message: "server error, email send failed"} } - return 401; + return {status: 403, message: "forbidden, domain is not on whitelist"} } async function regUser(req) {