From e2de57ea0acc837be551bf0ea8c00a901b0f1240 Mon Sep 17 00:00:00 2001 From: rxbn_ Date: Fri, 25 Sep 2020 20:05:16 +0200 Subject: [PATCH] web,db - comments, remove legacy code and add error page --- app.js | 6 +--- bin/database/module.js | 50 ++++++++-------------------- bin/logs/module.js | 2 +- bin/web/routes/api/login.js | 1 - bin/web/routes/api/register.js | 4 ++- bin/web/routes/static.js | 2 +- bin/web/views/blocks/error/error.pug | 19 +++++++++++ bin/web/views/error/error.pug | 4 +++ 8 files changed, 42 insertions(+), 46 deletions(-) create mode 100644 bin/web/views/blocks/error/error.pug create mode 100644 bin/web/views/error/error.pug diff --git a/app.js b/app.js index 2906f08..d6a9ede 100644 --- a/app.js +++ b/app.js @@ -5,11 +5,7 @@ */ -// GDS: Global Data System -global['gds'] = { - debug: (process.env.NODE_ENV === 'debug') ? true : false, - cfg: require(__dirname+'/bin/config') -}; +global['debug'] = (process.env.NODE_ENV === 'debug') ? true : false; global['__dirname'] = __dirname; /** diff --git a/bin/database/module.js b/bin/database/module.js index 81945f0..2ac7c75 100644 --- a/bin/database/module.js +++ b/bin/database/module.js @@ -103,7 +103,6 @@ methods.addUser = async (nick, email, passhash, group) => { * deletes user identified by haystack from database * @author Ruben Meyer * @async - * @TODO add functionality * @param {String} haystack email or nick * @return {Object} async(reply, err) */ @@ -216,17 +215,17 @@ methods.updateUser = async (id, obj) => { /** - * updates data based on login + * updates data based on user activity * @author Ruben Meyer * @async * @TODO UPDATE METHOD; PROBABLY OUTDATED * @param {Number} id User ID - * @param {Object} data data JSON -> remember - * @return {Object} async({date => 'Login Date', token => 'RememberMe Cookie Token'}, err) + * @param {Object} data data + * @return {Object} async({date => 'ISO Date', timestamp => 'Timestamp'}, err) */ methods.addActivity = async (id, data) => { - if(!(typeof id === 'string' || id instanceof mongoose.Types.ObjectId)) return {err: new TypeError('id is not a string::database.updateNewAction('+id+','+JSON.stringify(options)+')', module.filename)}; - if(typeof options !== 'object' && options !== null) return {err: new TypeError('obj is not an object::database.updateUserProfile('+id+','+JSON.stringify(obj)+')', module.filename)}; + if(!(typeof id === 'string' || id instanceof mongoose.Types.ObjectId)) return {err: new TypeError('id is not a string::database.addActivity('+id+','+JSON.stringify(options)+')', module.filename)}; + if(typeof options !== 'object' && options !== null) return {err: new TypeError('obj is not an object::database.addActivity('+id+','+JSON.stringify(obj)+')', module.filename)}; let date = new Date().toISOString(); let timestamp = new Date(date).getTime(); @@ -236,28 +235,11 @@ methods.addActivity = async (id, data) => { last_action: date }); - if(options.rememberme && options.new_token !== false) { - var token = ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, (c) => (c ^ crypto.randomBytes(new Uint8Array(1).length)[0] & 15 >> c / 4).toString(16)); - var Remember = models.remember; - try { - data = await Remember.findOneAndUpdate({userId: id}, {token: token, timestamp: Date.now()}, {upsert: true}).exec(); - return {reply: { - date: date, - timestamp: timestamp, - token: token - }}; - } - catch(err) { - return {err: err}; - } - } else { - return {reply: { - date: date, - timestamp: timestamp, - token: options.old_token - }}; - } + return {reply: { + date: date, + timestamp: timestamp + }}; } catch(err) { return {err: err}; } @@ -488,10 +470,9 @@ methods.getApps = async () => { }; /** - * return auth obj + * set authentication token for app,user combination * @author Ruben Meyer * @async - * @TODO * @param {Object} obj data obj (aId, uId) * @return {Object} async({timestamp, token}, err) */ @@ -522,15 +503,14 @@ methods.setAuthCode = async (obj) => { }; /** - * return auth obj + * verify authentication token for app,user combination * @author Ruben Meyer * @async - * @TODO * @param {Object} obj data obj (aId, aSecret, uId, token) * @return {Object} async(bool, err) */ methods.getAuth = async (obj) => { - if(typeof obj !== 'object') return {err: new TypeError('obj is not an object::database.getAuthCode('+JSON.stringify(obj)+')', module.filename)}; + if(typeof obj !== 'object') return {err: new TypeError('obj is not an object::database.getAuth('+JSON.stringify(obj)+')', module.filename)}; var AuthCode = models.authCode; @@ -562,10 +542,6 @@ methods.getAuth = async (obj) => { && obj.uId == String(data.userId) && obj.aSecret == data1.secret) { return {reply: true}; - //methods.setAuthCode({ - // aId: obj.aId, - // uId: obj.uId - //}); } else return{reply: false}; } catch(err) { @@ -579,7 +555,7 @@ methods.getAuth = async (obj) => { }; /** - * return app permission + * verify if the application exists and the redirectUrl is correct * @author Ruben Meyer * @async * @TODO diff --git a/bin/logs/module.js b/bin/logs/module.js index 2560f1a..2750847 100644 --- a/bin/logs/module.js +++ b/bin/logs/module.js @@ -82,7 +82,7 @@ methods.err = methods.error; // DEBUG methods.debug = (...data) => { - if(global['gds'].debug) { + if(global['debug'] === true) { log(console.log, data); if(data.length == 1) data = data[0]; diff --git a/bin/web/routes/api/login.js b/bin/web/routes/api/login.js index 0355547..ccee42b 100644 --- a/bin/web/routes/api/login.js +++ b/bin/web/routes/api/login.js @@ -8,7 +8,6 @@ module.exports = { * @url /api/login * @method POST * @POST ['email', 'password'] - * @TODO add new activity 'action.user.login' */ post: async (req, res) => { // if user is logged in (existing session); FAIL diff --git a/bin/web/routes/api/register.js b/bin/web/routes/api/register.js index 016d04f..ec95254 100644 --- a/bin/web/routes/api/register.js +++ b/bin/web/routes/api/register.js @@ -1,3 +1,5 @@ +let cfg = require(global['__dirname']+'/bin/config'); + module.exports = { path: "/register", /** @@ -7,7 +9,7 @@ module.exports = { */ post: async (req, res) => { // if registration is disabled - if(!global['gds'].cfg.web.registration) { + if(!cfg.web.registration) { return res.type('json').status(400).end(JSON.stringify({status: 400, message: "msg.auth.registration.deactivated"})); } else { // am i rite? diff --git a/bin/web/routes/static.js b/bin/web/routes/static.js index 80d351c..c1cb7d6 100644 --- a/bin/web/routes/static.js +++ b/bin/web/routes/static.js @@ -126,7 +126,7 @@ let getRoutes = async () => { * @url /* * @method all */ - route.get(['/request', '/login', '/logout', '/reset', '/admin'], asyncer(async (req, res, next) => { + route.get(['/request', '/register', '/login', '/logout', '/reset', '/admin'], asyncer(async (req, res, next) => { // passthrough to next route if(req.path.startsWith('/api')) return next(); diff --git a/bin/web/views/blocks/error/error.pug b/bin/web/views/blocks/error/error.pug new file mode 100644 index 0000000..d00db20 --- /dev/null +++ b/bin/web/views/blocks/error/error.pug @@ -0,0 +1,19 @@ +append var + if((!breadcrumb && !title) || overwrite_vars) + - var breadcrumb = {0: {"name": cfg.app.name, "href": "/"}, 1: {"name": "500 - unknown error", "active": true}}; + - var title = "I'm so sooryy"; + +.uk-flex.uk-margin-medium-top.uk-margin-medium-bottom + div(class="uk-width-auto uk-width-1-4@s") + .uk-flex.uk-flex-auto.uk-flex-column.uk-flex-center.uk-margin-left.uk-margin-right + if(!error) + h1 Me so sorry. + p Could not find something which belongs to your interests. + a(href="/").uk-button.uk-button-default Home + else + append var + - breadcrumb[1] = {"name": error.statusCode + " - "+error.name, "active": true}; + h1= error.statusCode + " Error: " + error.name + p= error.error_description + a(href="/").uk-button.uk-button-default Home + div(class="uk-width-auto uk-width-1-4@s") diff --git a/bin/web/views/error/error.pug b/bin/web/views/error/error.pug new file mode 100644 index 0000000..65b34e7 --- /dev/null +++ b/bin/web/views/error/error.pug @@ -0,0 +1,4 @@ +extends ../blocks/layout.pug + +append content + include ../blocks/error/error.pug