diff --git a/app/controllers/decrypt.js b/app/controllers/decrypt.js deleted file mode 100644 index 55e1b07..0000000 --- a/app/controllers/decrypt.js +++ /dev/null @@ -1,36 +0,0 @@ -var ursa = require('ursa'); -var fs = require('fs'); - -var decrypt = {}; - -decrypt.decrypter = false; - -decrypt.whenOk = function (cb) { - if (this.encrypter) { - cb(); - } else { - this.prepare(cb); - } -}; - -decrypt.prepare = function (cb) { - fs.readFile('config/ci_com.pem', function (err, data) { - if (err) { - throw err; - } - this.decrypter = ursa.createPrivateKey(data); - cb(); - }); -}; - -decrypt.preload = function (cb) { - this.whenOk(cb); -}; - -decrypt.decrypt = function (string, cb) { - this.whenOk(function () { - cb(this.decrypter.decrypt(string, 'base64', 'utf8', ursa.RSA_PKCS1_PADDING)); - }); -}; - -module.exports = decrypt; diff --git a/app/controllers/membres.js b/app/controllers/membres.js deleted file mode 100644 index 0ab7c28..0000000 --- a/app/controllers/membres.js +++ /dev/null @@ -1,37 +0,0 @@ -var Membre = require('../models/membre'); -var noms = require('../controllers/noms'); -var async = require('async'); - -var membres = {}; - -membres.list = function (cb) { - Membre.find({}).lean().exec(function (err, membres) { - addNom = function (membre, cbA) { - noms.get(membre.login, function (nom) { - if (nom) { - membre.nom = nom; - } else { - membre.nom = membre.login; - } - cbA(null, membre); - }); - }; - async.mapSeries(membres, addNom, cb); - }); -}; - -membres.add = function (data, cb) { - Membre.create({ - login: data.login, - role: data.role, - section: data.section, - }, cb); -}; - -membres.remove = function (id, cb) { - Membre.remove({ - _id: id - }, cb); -}; - -module.exports = membres; diff --git a/app/controllers/noms.js b/app/controllers/noms.js deleted file mode 100644 index 1dd6901..0000000 --- a/app/controllers/noms.js +++ /dev/null @@ -1,52 +0,0 @@ -var Noms = require('../models/noms'); -var LineTransform = require('node-line-reader').LineTransform; -var fs = require('fs'); - -var noms = {}; - -noms.get = function (login, cb) { - Noms.findOne({ - login: login - }, function (err, nom) { - if (err) { - console.error(err); - cb(false); - } else { - if (nom) { - cb(nom.nom); - } else { - passwdF = 'config/passwd'; - fs.exists(passwdF, function (exists) { - found = false; - if (exists) { - stream = fs.createReadStream(passwdF); - transform = new LineTransform(); - stream.pipe(transform); - transform.on('data', function (line) { - ex = line.split(':'); - if (ex[0] == login) { // Si trouvé - stream.close(); - cb(ex[4]); - found = true; - Noms.create({ - login: login, - nom: ex[4] - }); - } - }); - transform.on('end', function () { - if (!found) { - cb(false); - } - }); - } else { - console.error("Impossible de trouver le fichier passwd"); - cb(login.toUpperCase()); - } - }); - } - } - }); -}; - -module.exports = noms; diff --git a/app/controllers/sessions.js b/app/controllers/sessions.js deleted file mode 100644 index 1ba72eb..0000000 --- a/app/controllers/sessions.js +++ /dev/null @@ -1,118 +0,0 @@ -var Session = require('../models/session'); -var noms = require('../controllers/noms'); - -var sessions = {}; - -sessions.cur = false; - -sessions.addData = function (session, cb) { - noms.get(session.login, function (nom) { - if (typeof nom == 'string') { - session.nom = nom; - } else { - session.nom = 'Inconnu'; - } - session.canAddMembre = session.login == 'gbontoux'; - session.canDelMembre = session.login == 'gbontoux'; - cb(session); - }); -}; - -sessions.find = function (id, cb) { - _this = this; - Session.findById(id).lean().exec(function (err, session) { - if (typeof session == 'object') { - _this.addData(session, function (session) { - cb(err, session); - }); - } else { - cb(err, null); - } - }); -}; - -sessions.valid = function (session) { - return session.started.setSeconds(session.started.getSeconds() + 3600) > new Date(); -}; - -sessions.delete = function (id, cb) { - Session.remove({ - _id: id - }, cb); -}; - -sessions.verify = function (id, cb) { - _this = this; - _this.find(id, function (err, session) { - if (err) { - cb('error'); - } else { - if (session) { - if (sessions.valid(session)) { - cb(null, session); - } else { - cb('expired'); - _this.delete(id); - } - } else { - cb('unknown'); - } - } - }); -}; - -sessions.use = function (id, cb) { - _this = this; - _this.verify(id, function (err, session) { - if (err) { - cb(err); - } else { - _this.cur = session; - cb(null); - } - }); -}; - -sessions.create = function (login, cb) { - Session.create({ - login: login - }, cb); -}; - -sessions.login = function (data, cb) { - // DUMMY - noms.get(data.login, function (nom) { - if (nom === false) { - cb(null, false); - } else { - if (data.pass == 'cool') { - cb(null, true); - } else { - cb(null, false); - } - } - }); -}; - -sessions.open = function (data, cb) { - _this = this; - _this.login(data, function (err, res) { - if (err) { - cb('error'); - } else { - if (res) { - _this.create(data.login, function (err, session) { - if (err) { - cb('error'); - } else { - _this.use(session._id, cb); - } - }); - } else { - cb('invalid'); - } - } - }); -}; - -module.exports = sessions; diff --git a/app/models/MembreModl.js b/app/models/MembreModl.js new file mode 100644 index 0000000..2f4fb07 --- /dev/null +++ b/app/models/MembreModl.js @@ -0,0 +1,20 @@ +var mongoose = require('mongoose'); + +module.exports = mongoose.model('Membre', { + login: { // On récupèrera le nom via les passwd + type: String, + default: 'login' + }, + section: { + type: String, + default: 'IMA' + }, + promo: { // Nécessaire pour calculer le numéro de section + type: Number, + default: 2017 + }, + role: { + type: String, + default: 'Membre' + } +}); diff --git a/app/models/NomsModl.js b/app/models/NomsModl.js new file mode 100644 index 0000000..724a062 --- /dev/null +++ b/app/models/NomsModl.js @@ -0,0 +1,12 @@ +var mongoose = require('mongoose'); + +module.exports = mongoose.model('Noms', { + login: { // On récupèrera le nom via les passwd + type: String, + default: 'login' + }, + nom: { + type: String, + default: 'Nom' + } +}); diff --git a/app/models/SessionModl.js b/app/models/SessionModl.js new file mode 100644 index 0000000..465dce2 --- /dev/null +++ b/app/models/SessionModl.js @@ -0,0 +1,12 @@ +var mongoose = require('mongoose'); + +module.exports = mongoose.model('Session', { + login: { // On récupèrera le nom via les passwd + type: String, + default: 'login' + }, + started: { + type: Date, + default: Date.now + } +}); diff --git a/app/models/membre.js b/app/models/membre.js deleted file mode 100644 index 2f4fb07..0000000 --- a/app/models/membre.js +++ /dev/null @@ -1,20 +0,0 @@ -var mongoose = require('mongoose'); - -module.exports = mongoose.model('Membre', { - login: { // On récupèrera le nom via les passwd - type: String, - default: 'login' - }, - section: { - type: String, - default: 'IMA' - }, - promo: { // Nécessaire pour calculer le numéro de section - type: Number, - default: 2017 - }, - role: { - type: String, - default: 'Membre' - } -}); diff --git a/app/models/noms.js b/app/models/noms.js deleted file mode 100644 index 724a062..0000000 --- a/app/models/noms.js +++ /dev/null @@ -1,12 +0,0 @@ -var mongoose = require('mongoose'); - -module.exports = mongoose.model('Noms', { - login: { // On récupèrera le nom via les passwd - type: String, - default: 'login' - }, - nom: { - type: String, - default: 'Nom' - } -}); diff --git a/app/models/session.js b/app/models/session.js deleted file mode 100644 index 465dce2..0000000 --- a/app/models/session.js +++ /dev/null @@ -1,12 +0,0 @@ -var mongoose = require('mongoose'); - -module.exports = mongoose.model('Session', { - login: { // On récupèrera le nom via les passwd - type: String, - default: 'login' - }, - started: { - type: Date, - default: Date.now - } -}); diff --git a/app/routes.js b/app/routes.js index 29eecc6..8c3ff0a 100644 --- a/app/routes.js +++ b/app/routes.js @@ -1,5 +1,5 @@ var path = require('path'); -var api = require('./routes/api'); +var api = require('./routes/ApiRtes'); module.exports = function (app) { diff --git a/app/routes/ApiRtes.js b/app/routes/ApiRtes.js new file mode 100644 index 0000000..aef92a2 --- /dev/null +++ b/app/routes/ApiRtes.js @@ -0,0 +1,101 @@ +var MembresServ = require('../services/MembresServ'); +var SessionsServ = require('../services/SessionsServ'); +var DecryptServ = require('../services/DecryptServ'); +var express = require('express'); + +var api = express(); + +// Sessions +api.get('/session', function (req, res) { // Informations sur la session + if (req.cookies && req.cookies.session) { + SessionsServ.use(req.cookies.session, function (err) { + if (err) { + res.send(err); + } else { + res.send(SessionsServ.cur); + } + }); + // TODO si pas bon : res.clearCookie('session') + } else { + res.send('missing'); + } +}); + +api.post('/session', function (req, res) { // Se connecter + DecryptServ.decrypt(req.body[0], function (data) { + SessionsServ.open(JSON.parse(data), function (err) { + if (err) { + res.send(err); + } else { + res.cookie('session', SessionsServ.cur._id); + res.send(SessionsServ.cur); + } + }); + }); +}); + +api.delete('/session', function (req, res) { // Se déconnecter + if (req.cookies.session) { + SessionsServ.delete(req.cookies.session, function () { + res.clearCookie('session'); + res.end(); + }); + } else { + res.send('missing'); + } +}); + +ifPermission = function (req, res, perm, cb) { + SessionsServ.use(req.cookies.session, function (err) { + if (err) { + res.status(403).end(); + } else { + if (SessionsServ.cur[perm]) { + cb(); + } else { + res.status(403).end(); + } + } + }); +}; + + +// Membres +api.get('/membres', function (req, res) { // Liste des membres + MembresServ.list(function (err, membres) { + if (err) + res.send(err); + else + res.json(membres); + }); +}); + +api.post('/membres', function (req, res) { // Ajout d'un membre + ifPermission(req, res, 'canAddMembre', function () { + MembresServ.add(req.body, function (err, membre) { + if (err) + res.send(err); + MembresServ.list(function (err, membres) { + if (err) + res.send(err); + res.json(membres); + }); + }); + }); +}); + +api.delete('/membres/:membre_id', function (req, res) { // Supression d'un membre + ifPermission(req, res, 'canDelMembre', function () { + MembresServ.remove(req.params.membre_id, function (err, membre) { + if (err) + res.send(err); + MembresServ.list(function (err, membres) { + if (err) + res.send(err); + res.json(membres); + }); + }); + }); +}); + +module.exports = api; diff --git a/app/routes/api.js b/app/routes/api.js deleted file mode 100644 index b7fdab5..0000000 --- a/app/routes/api.js +++ /dev/null @@ -1,101 +0,0 @@ -var membres = require('../controllers/membres'); -var sessions = require('../controllers/sessions'); -var decrypt = require('../controllers/decrypt'); -var express = require('express'); - -var api = express(); - -// Sessions -api.get('/session', function (req, res) { // Informations sur la session - if (req.cookies && req.cookies.session) { - sessions.use(req.cookies.session, function (err) { - if (err) { - res.send(err); - } else { - res.send(sessions.cur); - } - }); - // TODO si pas bon : res.clearCookie('session') - } else { - res.send('missing'); - } -}); - -api.post('/session', function (req, res) { // Se connecter - decrypt.decrypt(req.body[0], function (data) { - sessions.open(JSON.parse(data), function (err) { - if (err) { - res.send(err); - } else { - res.cookie('session', sessions.cur._id); - res.send(sessions.cur); - } - }); - }); -}); - -api.delete('/session', function (req, res) { // Se déconnecter - if (req.cookies.session) { - sessions.delete(req.cookies.session, function () { - res.clearCookie('session'); - res.end(); - }); - } else { - res.send('missing'); - } -}); - -ifPermission = function (req, res, perm, cb) { - sessions.use(req.cookies.session, function (err) { - if (err) { - res.status(403).end(); - } else { - if (sessions.cur[perm]) { - cb(); - } else { - res.status(403).end(); - } - } - }); -}; - - -// Membres -api.get('/membres', function (req, res) { // Liste des membres - membres.list(function (err, membres) { - if (err) - res.send(err); - else - res.json(membres); - }); -}); - -api.post('/membres', function (req, res) { // Ajout d'un membre - ifPermission(req, res, 'canAddMembre', function () { - membres.add(req.body, function (err, membre) { - if (err) - res.send(err); - membres.list(function (err, membres) { - if (err) - res.send(err); - res.json(membres); - }); - }); - }); -}); - -api.delete('/membres/:membre_id', function (req, res) { // Supression d'un membre - ifPermission(req, res, 'canDelMembre', function () { - membres.remove(req.params.membre_id, function (err, membre) { - if (err) - res.send(err); - membres.list(function (err, membres) { - if (err) - res.send(err); - res.json(membres); - }); - }); - }); -}); - -module.exports = api; diff --git a/app/services/DecryptServ.js b/app/services/DecryptServ.js new file mode 100644 index 0000000..879f42f --- /dev/null +++ b/app/services/DecryptServ.js @@ -0,0 +1,36 @@ +var ursa = require('ursa'); +var fs = require('fs'); + +var DecryptServ = {}; + +DecryptServ.decrypter = false; + +DecryptServ.whenOk = function (cb) { + if (this.encrypter) { + cb(); + } else { + this.prepare(cb); + } +}; + +DecryptServ.prepare = function (cb) { + fs.readFile('config/ci_com.pem', function (err, data) { + if (err) { + throw err; + } + this.decrypter = ursa.createPrivateKey(data); + cb(); + }); +}; + +DecryptServ.preload = function (cb) { + this.whenOk(cb); +}; + +DecryptServ.decrypt = function (string, cb) { + this.whenOk(function () { + cb(this.decrypter.decrypt(string, 'base64', 'utf8', ursa.RSA_PKCS1_PADDING)); + }); +}; + +module.exports = DecryptServ; diff --git a/app/services/MembresServ.js b/app/services/MembresServ.js new file mode 100644 index 0000000..7b70dbf --- /dev/null +++ b/app/services/MembresServ.js @@ -0,0 +1,37 @@ +var MembreModl = require('../models/MembreModl'); +var NomsServ = require('../services/NomsServ'); +var async = require('async'); + +var membres = {}; + +membres.list = function (cb) { + MembreModl.find({}).lean().exec(function (err, membres) { + addNom = function (membre, cbA) { + NomsServ.get(membre.login, function (nom) { + if (nom) { + membre.nom = nom; + } else { + membre.nom = membre.login; + } + cbA(null, membre); + }); + }; + async.mapSeries(membres, addNom, cb); + }); +}; + +membres.add = function (data, cb) { + MembreModl.create({ + login: data.login, + role: data.role, + section: data.section, + }, cb); +}; + +membres.remove = function (id, cb) { + MembreModl.remove({ + _id: id + }, cb); +}; + +module.exports = membres; diff --git a/app/services/NomsServ.js b/app/services/NomsServ.js new file mode 100644 index 0000000..31af5aa --- /dev/null +++ b/app/services/NomsServ.js @@ -0,0 +1,52 @@ +var NomsModl = require('../models/NomsModl'); +var LineTransform = require('node-line-reader').LineTransform; +var fs = require('fs'); + +var noms = {}; + +noms.get = function (login, cb) { + NomsModl.findOne({ + login: login + }, function (err, nom) { + if (err) { + console.error(err); + cb(false); + } else { + if (nom) { + cb(nom.nom); + } else { + passwdF = 'config/passwd'; + fs.exists(passwdF, function (exists) { + found = false; + if (exists) { + stream = fs.createReadStream(passwdF); + transform = new LineTransform(); + stream.pipe(transform); + transform.on('data', function (line) { + ex = line.split(':'); + if (ex[0] == login) { // Si trouvé + stream.close(); + cb(ex[4]); + found = true; + NomsModl.create({ + login: login, + nom: ex[4] + }); + } + }); + transform.on('end', function () { + if (!found) { + cb(false); + } + }); + } else { + console.error("Impossible de trouver le fichier passwd"); + cb(login.toUpperCase()); + } + }); + } + } + }); +}; + +module.exports = noms; diff --git a/app/services/SessionsServ.js b/app/services/SessionsServ.js new file mode 100644 index 0000000..e4b18f4 --- /dev/null +++ b/app/services/SessionsServ.js @@ -0,0 +1,118 @@ +var SessionModl = require('../models/SessionModl'); +var NomsServ = require('../services/NomsServ'); + +var sessions = {}; + +sessions.cur = false; + +sessions.addData = function (session, cb) { + NomsServ.get(session.login, function (nom) { + if (typeof nom == 'string') { + session.nom = nom; + } else { + session.nom = 'Inconnu'; + } + session.canAddMembre = session.login == 'gbontoux'; + session.canDelMembre = session.login == 'gbontoux'; + cb(session); + }); +}; + +sessions.find = function (id, cb) { + _this = this; + SessionModl.findById(id).lean().exec(function (err, session) { + if (typeof session == 'object') { + _this.addData(session, function (session) { + cb(err, session); + }); + } else { + cb(err, null); + } + }); +}; + +sessions.valid = function (session) { + return session.started.setSeconds(session.started.getSeconds() + 3600) > new Date(); +}; + +sessions.delete = function (id, cb) { + SessionModl.remove({ + _id: id + }, cb); +}; + +sessions.verify = function (id, cb) { + _this = this; + _this.find(id, function (err, session) { + if (err) { + cb('error'); + } else { + if (session) { + if (sessions.valid(session)) { + cb(null, session); + } else { + cb('expired'); + _this.delete(id); + } + } else { + cb('unknown'); + } + } + }); +}; + +sessions.use = function (id, cb) { + _this = this; + _this.verify(id, function (err, session) { + if (err) { + cb(err); + } else { + _this.cur = session; + cb(null); + } + }); +}; + +sessions.create = function (login, cb) { + SessionModl.create({ + login: login + }, cb); +}; + +sessions.login = function (data, cb) { + // DUMMY + NomsServ.get(data.login, function (nom) { + if (nom === false) { + cb(null, false); + } else { + if (data.pass == 'cool') { + cb(null, true); + } else { + cb(null, false); + } + } + }); +}; + +sessions.open = function (data, cb) { + _this = this; + _this.login(data, function (err, res) { + if (err) { + cb('error'); + } else { + if (res) { + _this.create(data.login, function (err, session) { + if (err) { + cb('error'); + } else { + _this.use(session._id, cb); + } + }); + } else { + cb('invalid'); + } + } + }); +}; + +module.exports = sessions; diff --git a/public/js/appRoutes.js b/public/js/appRoutes.js index 9e3b7d3..ab83c9c 100644 --- a/public/js/appRoutes.js +++ b/public/js/appRoutes.js @@ -1,4 +1,3 @@ -// public/js/appRoutes.js angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) { $routeProvider @@ -7,11 +6,11 @@ angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', }) .when('/membres', { templateUrl: 'views/membres.html', - controller: 'MembreController' + controller: 'MembreCtrl' }) .when('/connect', { templateUrl: 'views/connect.html', - controller: 'ConnectController' + controller: 'ConnectCtrl' }); $locationProvider.html5Mode(true); diff --git a/public/js/controllers/ConnectCtrl.js b/public/js/controllers/ConnectCtrl.js index df90562..547e33a 100644 --- a/public/js/controllers/ConnectCtrl.js +++ b/public/js/controllers/ConnectCtrl.js @@ -1,11 +1,11 @@ -angular.module('ConnectCtrl', []).controller('ConnectController', ['$scope', 'SessionService', 'EncryptService', - function ($scope, SessionService, EncryptService) { - EncryptService.preload(function () { +angular.module('ConnectCtrl', []).controller('ConnectCtrl', ['$scope', 'SessionServ', 'EncryptServ', + function ($scope, SessionServ, EncryptServ) { + EncryptServ.preload(function () { return undefined; }); $scope.connect = { connect: function () { - SessionService.connect($scope.connect.login, $scope.connect.pass); + SessionServ.connect($scope.connect.login, $scope.connect.pass); } }; } diff --git a/public/js/controllers/MembreCtrl.js b/public/js/controllers/MembreCtrl.js index 684efdb..d0f5bee 100644 --- a/public/js/controllers/MembreCtrl.js +++ b/public/js/controllers/MembreCtrl.js @@ -1,10 +1,10 @@ -angular.module('MembreCtrl', []).controller('MembreController', ['$scope', '$http', 'SessionService', - function ($scope, $http, SessionService) { +angular.module('MembreCtrl', []).controller('MembreCtrl', ['$scope', '$http', 'SessionServ', + function ($scope, $http, SessionServ) { $scope.formData = {}; - $scope.session = SessionService.cur; - SessionService.onChange(function () { - $scope.session = SessionService.cur; + $scope.session = SessionServ.cur; + SessionServ.onChange(function () { + $scope.session = SessionServ.cur; }); $http.get('/api/membres') diff --git a/public/js/controllers/SessionCtrl.js b/public/js/controllers/SessionCtrl.js index f00ba15..6104987 100644 --- a/public/js/controllers/SessionCtrl.js +++ b/public/js/controllers/SessionCtrl.js @@ -1,11 +1,11 @@ -angular.module('SessionsCtrl', []).controller('SessionController', ['$scope', 'SessionService', - function ($scope, SessionService) { - $scope.session = SessionService.cur; +angular.module('SessionsCtrl', []).controller('SessionCtrl', ['$scope', 'SessionServ', + function ($scope, SessionServ) { + $scope.session = SessionServ.cur; $scope.disconnect = function () { - SessionService.disconnect(); + SessionServ.disconnect(); }; - SessionService.onChange(function () { - $scope.session = SessionService.cur; + SessionServ.onChange(function () { + $scope.session = SessionServ.cur; }); // $scope.$on("$destroy", function () { // // TODO diff --git a/public/js/services/EncryptServ.js b/public/js/services/EncryptServ.js index 119ee0e..ceecd92 100644 --- a/public/js/services/EncryptServ.js +++ b/public/js/services/EncryptServ.js @@ -1,4 +1,4 @@ -angular.module('EncryptServ', []).service('EncryptService', ['$http', +angular.module('EncryptServ', []).service('EncryptServ', ['$http', function ($http) { a = { encrypter: false, diff --git a/public/js/services/SessionServ.js b/public/js/services/SessionServ.js index 5a0bab2..5082dda 100644 --- a/public/js/services/SessionServ.js +++ b/public/js/services/SessionServ.js @@ -1,5 +1,5 @@ -angular.module('SessionsServ', []).service('SessionService', ['$http', 'EncryptService', - function ($http, EncryptService) { +angular.module('SessionsServ', []).service('SessionServ', ['$http', 'EncryptServ', + function ($http, EncryptServ) { a = { cur: false, status: 0, @@ -46,7 +46,7 @@ angular.module('SessionsServ', []).service('SessionService', ['$http', 'EncryptS login: login, pass: pass }); - EncryptService.encrypt(data, function (dataCrypt) { + EncryptServ.encrypt(data, function (dataCrypt) { $http.post('/api/session', [dataCrypt]).success(function (body) { _this.updateSessionInfos(body); if (cb) { diff --git a/public/views/index.html b/public/views/index.html index 0d6c13c..17cf8a6 100644 --- a/public/views/index.html +++ b/public/views/index.html @@ -33,7 +33,7 @@ -