From 2a689dd20b13cbe561a9f99980e79d22482ade36 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Sat, 23 Apr 2016 23:02:19 -0400 Subject: [PATCH] added encryption to player list --- bower.json | 3 ++- gulpfile.js | 35 +++++++++++++++++++++++++++----- index.tmpl.html | 4 ++++ npm-debug.log | 18 ---------------- package.json | 4 +++- script/main.js | 3 ++- script/views/confidentialPage.js | 2 +- script/views/loginPage.js | 2 +- script/views/playersPage.js | 12 +++++++++-- 9 files changed, 53 insertions(+), 30 deletions(-) delete mode 100644 npm-debug.log diff --git a/bower.json b/bower.json index 507513e..0324224 100644 --- a/bower.json +++ b/bower.json @@ -22,6 +22,7 @@ "mithril": "~0.2.1", "lodash": "~4.2.1", "almond": "~0.3.1", - "requirejs": "~2.1.22" + "requirejs": "~2.1.22", + "spritzjs": "https://gitlab.com/didiercrunch/spritzjs.git" } } diff --git a/gulpfile.js b/gulpfile.js index fc924eb..6b266ce 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -8,9 +8,10 @@ var rename = require("gulp-rename"); var yaml = require('gulp-yaml'); var template = require('gulp-template'); var wrapper = require('gulp-wrapper'); -var rename = require("gulp-rename"); var webserver = require('gulp-webserver'); var marked = require('marked'); +var spritz = require('spritzjs'); +var modify = require('gulp-modify'); var _ = require('lodash'); gulp.task('help', help); @@ -39,17 +40,41 @@ gulp.task('compile-results', function(){ .pipe(gulp.dest('./api')); }); +var encryptFile = function(message){ + var keyLengthInBytes = 128; + var stringKey = process.env.KEY || 'qwerty'; + var key = spritz.hash(stringKey, keyLengthInBytes); + var byteArrMessage = spritz.stringToUtf8ByteArray(message); + var cypher = spritz.encrypt(key, byteArrMessage); + return JSON.stringify(cypher); +}; + +gulp.task('encrypt-results', ['compile-results'], function(){ + return gulp.src('api/*.clear.json') + .pipe(modify({ + fileModifier: function(file, content) { + return encryptFile(content); + } + })) + .pipe(rename(function(path){ + path.basename = path.basename.replace(/\.clear$/i, ''); + path.extname = '.json.spritz'; + return path; + })) + .pipe(gulp.dest('./api')); +}); + gulp.task('template-prod', function () { return gulp.src('index.tmpl.html') .pipe(rename("index.html")) - .pipe(template({production: true})) + .pipe(template({production: true, salt: process.env.SALT, playerListLocation: process.env.PLAYER_LIST_LOCATION})) .pipe(gulp.dest('.')); }); gulp.task('template-dev', function () { return gulp.src('index.tmpl.html') .pipe(rename("index.html")) - .pipe(template({production: false})) + .pipe(template({production: false, salt: '', playerListLocation: 'api/players.json.spritz'})) .pipe(gulp.dest('.')); }); @@ -82,7 +107,7 @@ gulp.task('compile-articles', function(){ }); -gulp.task('default', ['bower', 'sass', 'compile-results', 'compile-articles']); +gulp.task('default', ['bower', 'sass', 'encrypt-results', 'compile-results', 'compile-articles']); gulp.task('prod', ['default', 'template-prod']); @@ -101,5 +126,5 @@ gulp.task('dev', ['default', 'webserver', 'template-dev', 'bower'], function(){ gulp.watch('./scss/*.scss', ['sass']); gulp.watch('*.tmpl.html', ['template-dev']); gulp.watch('./data/articles/**/*', ['compile-articles']); - return gulp.watch('./data/*.yaml', ['compile-results']); + return gulp.watch('./data/*.yaml', ['encrypt-results', 'compile-results']); }); diff --git a/index.tmpl.html b/index.tmpl.html index ec4d068..ae63481 100644 --- a/index.tmpl.html +++ b/index.tmpl.html @@ -10,6 +10,10 @@ + <%if(production){ %> <% }else{ %> diff --git a/npm-debug.log b/npm-debug.log deleted file mode 100644 index 4ace9c4..0000000 --- a/npm-debug.log +++ /dev/null @@ -1,18 +0,0 @@ -0 info it worked if it ends with ok -1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'install' ] -2 info using npm@1.4.28 -3 info using node@v0.10.37 -4 verbose node symlink /usr/bin/node -5 error install Couldn't read dependencies -6 error package.json ENOENT, open '/home/didier/dev/go/src/gitlab.com/didiercrunch/phenix-website/static/package.json' -6 error package.json This is most likely not a problem with npm itself. -6 error package.json npm can't find a package.json file in your current directory. -7 error System Linux 4.2.0-23-generic -8 error command "/usr/bin/node" "/usr/bin/npm" "install" -9 error cwd /home/didier/dev/go/src/gitlab.com/didiercrunch/phenix-website/static -10 error node -v v0.10.37 -11 error npm -v 1.4.28 -12 error path /home/didier/dev/go/src/gitlab.com/didiercrunch/phenix-website/static/package.json -13 error code ENOPACKAGEJSON -14 error errno 34 -15 verbose exit [ 34, true ] diff --git a/package.json b/package.json index d30da82..e7a9373 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "gulp": "^3.9.0", "gulp-bower": "0.0.13", "gulp-clean": "^0.3.1", + "gulp-modify": "^0.1.1", "gulp-rename": "^1.2.2", "gulp-sass": "^2.1.1", "gulp-task-listing": "^1.0.1", @@ -16,7 +17,8 @@ "gulp-wrapper": "^1.0.0", "lodash": "^4.6.1", "marked": "^0.3.5", - "requirejs": "^2.1.22" + "requirejs": "^2.1.22", + "spritzjs": "git+ssh://git@gitlab.com:didiercrunch/spritzjs.git" }, "devDependencies": { "gulp-webserver": "^0.9.1", diff --git a/script/main.js b/script/main.js index 33f107d..f572eaf 100644 --- a/script/main.js +++ b/script/main.js @@ -1,7 +1,8 @@ requirejs.config({ paths: { '_': '../lib/lodash/dist/lodash.min', - 'm': '../lib/mithril/mithril.min' + 'm': '../lib/mithril/mithril.min', + 'spritz': '../lib/spritzjs/dist/spritz' } }); diff --git a/script/views/confidentialPage.js b/script/views/confidentialPage.js index 9cb9bc9..6170bea 100644 --- a/script/views/confidentialPage.js +++ b/script/views/confidentialPage.js @@ -4,7 +4,7 @@ define(['m', '_', 'teams', 'views/loginPage', 'views/playersPage', 'views/topNav confidentialPage.view = function(){ var body; - if(!window.secret){ + if(_.isEmpty(window.secret)){ body = loginPage; }else{ body = playersPage; diff --git a/script/views/loginPage.js b/script/views/loginPage.js index 2c86de0..522d0ae 100644 --- a/script/views/loginPage.js +++ b/script/views/loginPage.js @@ -1,7 +1,7 @@ define(['m', '_'], function (m, _) { var loginPage = {}; - loginPage.salt = "9'D)E@E{DSq[|l2bq!v"; + loginPage.salt = window.salt; loginPage.controller = function(){ return {email: m.prop(''), password: m.prop('')}; diff --git a/script/views/playersPage.js b/script/views/playersPage.js index 4b64cab..459cd4e 100644 --- a/script/views/playersPage.js +++ b/script/views/playersPage.js @@ -1,4 +1,4 @@ -define(['m', '_', 'teams'], function (m, _ , teams) { +define(['m', '_', 'spritz', 'teams'], function (m, _ , spritz, teams) { var playersSelectionBar = {}; @@ -83,12 +83,20 @@ define(['m', '_', 'teams'], function (m, _ , teams) { return _.orderBy(playerList, sortingKeys, sortingDirections); }; + playersTable.decryptPlayerList = function(encryptedPlayerList){ + var keyLengthInBytes = 128; + var key = spritz.hash(window.secret, keyLengthInBytes); + var decryptedMessage = spritz.decrypt(key, encryptedPlayerList); + return JSON.parse(spritz.utf8ByteArrayToString(decryptedMessage)); + }; + playersTable.controller = function(model){ var players = m.prop([]); var ctrl = {}; ctrl.model = model; - ctrl.players = m.request({method: "GET", url: "api/players.json"}) + ctrl.players = m.request({method: "GET", url: window.playerListLocation}) + .then(playersTable.decryptPlayerList) .then(players); ctrl.sortPlayers = function(){ ctrl.players(playersTable._sortPlayers(ctrl.model.sorting, ctrl.players()));