From 3a79ca327a29b1077046c20c70847ad371b41ccf Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sun, 1 Jul 2018 15:35:57 +0200 Subject: [PATCH 01/21] Implemented password update --- .../controllers/AccountController.js | 11 -------- raspbot/application/models/Account.js | 7 ----- raspbot/application/router/index.js | 3 +-- raspbot/helpers/auth/index.js | 26 ++++++++++++++++--- raspbot/helpers/auth/token.js | 20 +++++++++----- 5 files changed, 38 insertions(+), 29 deletions(-) delete mode 100644 raspbot/application/controllers/AccountController.js delete mode 100644 raspbot/application/models/Account.js diff --git a/raspbot/application/controllers/AccountController.js b/raspbot/application/controllers/AccountController.js deleted file mode 100644 index 4db5d88..0000000 --- a/raspbot/application/controllers/AccountController.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict' -const account = require('../models/Account.js'); - -exports.updatePassword = (req, res) => { - let password = req.body.password; - account.updatePassword(password).then(response => { - res.json({success: true}); - }).catch(error => { - res.status(500).json({success: false, error: error.message}); - }); -}; diff --git a/raspbot/application/models/Account.js b/raspbot/application/models/Account.js deleted file mode 100644 index 43152bc..0000000 --- a/raspbot/application/models/Account.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict' - -exports.updatePassword = (password) => { - return new Promise((resolve, reject) => { - reject(Error("Not implemented!")); - }); -} diff --git a/raspbot/application/router/index.js b/raspbot/application/router/index.js index c0a797d..b5e6b9a 100644 --- a/raspbot/application/router/index.js +++ b/raspbot/application/router/index.js @@ -6,7 +6,6 @@ const auth = require('../../helpers/auth/'); const browserController = require('../controllers/BrowserController'); const dashboardController = require('../controllers/DashboardController'); const systemController = require('../controllers/SystemController'); -const accountController = require('../controllers/AccountController'); module.exports = app => { @@ -87,7 +86,7 @@ module.exports = app => { // ------------------------------ // /account/password // ------------------------------ - router.route('/account/password').post(accountController.updatePassword); + router.route('/account/password').post(auther.updatePassword); app.use(express.static(app.get('dist'))); app.use('/api', router); diff --git a/raspbot/helpers/auth/index.js b/raspbot/helpers/auth/index.js index 3db3581..7c00b18 100644 --- a/raspbot/helpers/auth/index.js +++ b/raspbot/helpers/auth/index.js @@ -39,6 +39,8 @@ module.exports = databasePath => { const auth = { + verify: verify, + authenticate: req => { return new Promise((resolve, reject) => { let credentials = decodeBasicAuth(req); @@ -67,7 +69,7 @@ module.exports = databasePath => { token.generate((error, userToken) => { if (error) { throw error; } const clientIP = req.headers['x-forwarded-for'] || req.connection.remoteAddress - token.save(userToken, clientIP); + token.save(userToken, clientIP, user.username); resolve(userToken); }); }); @@ -77,8 +79,6 @@ module.exports = databasePath => { }); }, - verify: verify, - isAuthorized: (req, res, next) => { if (verify(req)) { return next(); } @@ -88,6 +88,26 @@ module.exports = databasePath => { message: 'Unauthorized access.' } }); + }, + + updatePassword: (req, res) => { + const credential = decodeTokenAuth(req); + if (credential) { + const username = token.getUsernameFromToken(credential); + const password = req.body.password; + const database = require('lowdb')(new FileSync(databasePath)); + const userInfo = database.get('users').find({ username: username }).value(); + + if (userInfo) { + bcrypt.hash(password, 10, function(err, hash) { + userInfo.password = hash; + database.write(); + res.json({ success: true }); + }); + } else { + res.json({ success: false, error: { message: 'User not found.'}}); + } + } } } diff --git a/raspbot/helpers/auth/token.js b/raspbot/helpers/auth/token.js index a15ab49..75df2ec 100644 --- a/raspbot/helpers/auth/token.js +++ b/raspbot/helpers/auth/token.js @@ -12,12 +12,11 @@ exports.generate = callback => { callback(null, buffer.toString('hex')); } }); -} +}; -exports.save = (token, clientIP) => { - lowdb.get('tokens').push({ token: token, clientIP: clientIP }).write(); - console.log(lowdb.get('tokens').size().value()); -} +exports.save = (token, clientIP, username) => { + lowdb.get('tokens').push({ token: token, clientIP: clientIP, username: username }).write(); +}; exports.check = (token, clientIP) => { const credentials = lowdb.get('tokens').find({ token: token }).value(); @@ -26,4 +25,13 @@ exports.check = (token, clientIP) => { } return false; -} +}; + +exports.getUsernameFromToken = token => { + const credentials = lowdb.get('tokens').find({ token: token }).value(); + if (credentials && credentials.username) { + return credentials.username; + } + + return null; +}; From 96a8795a09cae4270fa143c8e41d228f3c1dddb7 Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sun, 1 Jul 2018 15:41:15 +0200 Subject: [PATCH 02/21] Improved password change logic client side --- .../Settings/Account/Password/Password.vue | 2 +- .../Settings/Account/Password/password.css | 2 +- .../Settings/Account/Password/password.js | 14 ++++++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/raspbot/web/src/components/Settings/Account/Password/Password.vue b/raspbot/web/src/components/Settings/Account/Password/Password.vue index 9d98193..d41b6e8 100644 --- a/raspbot/web/src/components/Settings/Account/Password/Password.vue +++ b/raspbot/web/src/components/Settings/Account/Password/Password.vue @@ -2,7 +2,7 @@
Edit pasword
- +
Save
{{message}}
diff --git a/raspbot/web/src/components/Settings/Account/Password/password.css b/raspbot/web/src/components/Settings/Account/Password/password.css index 182c385..95e45ff 100644 --- a/raspbot/web/src/components/Settings/Account/Password/password.css +++ b/raspbot/web/src/components/Settings/Account/Password/password.css @@ -4,7 +4,7 @@ flex-direction: column; justify-content: space-around; align-items: center; - margin-top: 20%; + margin-top: 10%; } input { diff --git a/raspbot/web/src/components/Settings/Account/Password/password.js b/raspbot/web/src/components/Settings/Account/Password/password.js index 91768c2..0c04cfd 100644 --- a/raspbot/web/src/components/Settings/Account/Password/password.js +++ b/raspbot/web/src/components/Settings/Account/Password/password.js @@ -3,19 +3,29 @@ exports.data = function() { return { password: '', - message: '' + message: '', + isDisabled: false } }; exports.methods = { updatePassword: function() { - if (this.password.length > 0) { + if (this.password.length < 3) { + this.message = 'Password must be more than 3 characters long.' + return; + } + + if (this.isDisabled == false) { + this.message = "Updating password..." + this.isDisabled = true; this.$APIManager.updatePassword(this.password, response => { if (response.success) { this.message = 'Password changed!'; } else { this.message = 'Could not change password' } + + this.isDisabled = false; }); } } From 889860cb519d8f99bba244fba44cf789c9033664 Mon Sep 17 00:00:00 2001 From: Ardalan Samimi Date: Sun, 1 Jul 2018 16:07:11 +0200 Subject: [PATCH 03/21] Implemented shutdown/reboot --- .../controllers/SystemController.js | 20 +++++++++++++++++ raspbot/application/models/System.js | 8 +++++++ raspbot/application/router/index.js | 8 +++++++ .../src/components/Dashboard/Dashboard.vue | 4 ++-- .../web/src/components/Dashboard/dashboard.js | 22 +++++++++++++++++++ raspbot/web/src/shared/apimanager.js | 16 ++++++++++++++ 6 files changed, 76 insertions(+), 2 deletions(-) diff --git a/raspbot/application/controllers/SystemController.js b/raspbot/application/controllers/SystemController.js index 9844684..9878ff6 100644 --- a/raspbot/application/controllers/SystemController.js +++ b/raspbot/application/controllers/SystemController.js @@ -20,3 +20,23 @@ exports.launchBootstrapper = (req, res) => { res.status(500).json({success: false, error: {message: "Could not launch bootstrapper."}}); }); }; + +exports.reboot = (req, res) => { + console.log("Reboot requested."); + system.reboot().then(response => { + res.json({success: true}); + }).catch(error => { + console.log(error); + res.json({status: false, error: {message: error}}); + }) +}; + +exports.shutdown = (req, res) => { + console.log("Shutdown requested."); + system.shutdown().then(response => { + res.json({success: true}); + }).catch(error => { + console.log(error); + res.json({status: false, error: {message: error}}); + }) +}; diff --git a/raspbot/application/models/System.js b/raspbot/application/models/System.js index 9c6f089..5f3cdfb 100644 --- a/raspbot/application/models/System.js +++ b/raspbot/application/models/System.js @@ -31,6 +31,14 @@ exports.launchBootstrapper = () => { return executeCommand('cd ../ && make start_bootstrapper'); }; +exports.reboot = () => { + return executeCommand('reboot'); +}; + +exports.shutdown = () => { + return executeCommand('shutdown'); +}; + function getLatestRelease(callback, reject) { let url = 'https://api.github.com/repos/pkrll/Raspy/releases'; if (oauth.id && oauth.secret) { diff --git a/raspbot/application/router/index.js b/raspbot/application/router/index.js index b5e6b9a..f94a059 100644 --- a/raspbot/application/router/index.js +++ b/raspbot/application/router/index.js @@ -84,6 +84,14 @@ module.exports = app => { // ------------------------------ router.route('/bootstrapper').get(systemController.launchBootstrapper); // ------------------------------ + // /checkForUpdate + // ------------------------------ + router.route('/system/reboot').get(systemController.reboot); + // ------------------------------ + // /checkForUpdate + // ------------------------------ + router.route('/system/shutdown').get(systemController.shutdown); + // ------------------------------ // /account/password // ------------------------------ router.route('/account/password').post(auther.updatePassword); diff --git a/raspbot/web/src/components/Dashboard/Dashboard.vue b/raspbot/web/src/components/Dashboard/Dashboard.vue index 3299784..d1edb1c 100644 --- a/raspbot/web/src/components/Dashboard/Dashboard.vue +++ b/raspbot/web/src/components/Dashboard/Dashboard.vue @@ -1,11 +1,11 @@