From de5467811a52abcc16d0b536136e09f1b614d79c Mon Sep 17 00:00:00 2001 From: Abijeet Date: Thu, 28 Dec 2017 01:41:16 +0530 Subject: [PATCH 1/3] Fixes password input being prompted every time. Fixes #7106 - Echoing the current server time via a JS variable and storing the current time on page load in JS. - Calculating the diff and taking it into account when deciding whether to show the password confirmation. Signed-off-by: Abijeet --- core/js/js.js | 7 +++++-- lib/private/Template/JSConfigHelper.php | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/js/js.js b/core/js/js.js index f9a5f2b338..872761c02b 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -1675,13 +1675,16 @@ function initCore() { OC.PasswordConfirmation = { callback: null, - + pageLoadTime: null, init: function() { $('.password-confirm-required').on('click', _.bind(this.requirePasswordConfirmation, this)); + this.pageLoadTime = moment.now(); }, requiresPasswordConfirmation: function() { - var timeSinceLogin = moment.now() - (nc_lastLogin * 1000); + var serverTimeDiff = this.pageLoadTime - (nc_pageLoad * 1000); + var timeSinceLogin = moment.now() - (serverTimeDiff + (nc_lastLogin * 1000)); + // if timeSinceLogin > 30 minutes and user backend allows password confirmation return (backendAllowsPasswordConfirmation && timeSinceLogin > 30 * 60 * 1000); }, diff --git a/lib/private/Template/JSConfigHelper.php b/lib/private/Template/JSConfigHelper.php index bdb747e1c9..9f93ef784b 100644 --- a/lib/private/Template/JSConfigHelper.php +++ b/lib/private/Template/JSConfigHelper.php @@ -155,6 +155,7 @@ class JSConfigHelper { "oc_appswebroots" => str_replace('\\/', '/', json_encode($apps_paths)), // Ugly unescape slashes waiting for better solution "datepickerFormatDate" => json_encode($this->l->l('jsdate', null)), 'nc_lastLogin' => $lastConfirmTimestamp, + 'nc_pageLoad' => time(), "dayNames" => json_encode([ (string)$this->l->t('Sunday'), (string)$this->l->t('Monday'), From b246ca96ffe45bfad8fbcc744be6d2e158228987 Mon Sep 17 00:00:00 2001 From: Abijeet Date: Sat, 6 Jan 2018 18:05:23 +0530 Subject: [PATCH 2/3] Added test cases for the fix for the password confirmation box appearing repeatedly. Signed-off-by: Abijeet --- core/js/tests/specs/coreSpec.js | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index b6c617303c..b27294b0f3 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -1539,4 +1539,41 @@ describe('Core base tests', function() { expect(snapperStub.close.calledTwice).toBe(true); }); }); + describe('Requires password confirmation', function () { + var stubMomentNow; + var stubJsPageLoadTime; + + afterEach(function () { + delete window.nc_pageLoad; + delete window.nc_lastLogin; + delete window.backendAllowsPasswordConfirmation; + + stubMomentNow.restore(); + stubJsPageLoadTime.restore(); + }); + + it('should not show the password confirmation dialog', function () { + // add server variables + window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 1, 15, 0).getTime() / 1000); + window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 1, 0, 0).getTime() / 1000); + window.backendAllowsPasswordConfirmation = true; + + stubJsPageLoadTime = sinon.stub(OC.PasswordConfirmation, 'pageLoadTime').value(new Date(2018, 0, 3, 12, 15, 0).getTime()); + stubMomentNow = sinon.stub(moment, 'now').returns(new Date(2018, 0, 3, 12, 20, 0).getTime()); + + expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeFalsy(); + }); + + it('should show the password confirmation dialog', function () { + // add server variables + window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 1, 15, 0).getTime() / 1000); + window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 1, 0, 0).getTime() / 1000); + window.backendAllowsPasswordConfirmation = true; + + stubJsPageLoadTime = sinon.stub(OC.PasswordConfirmation, 'pageLoadTime').value(new Date(2018, 0, 3, 12, 15, 0).getTime()); + stubMomentNow = sinon.stub(moment, 'now').returns(new Date(2018, 0, 3, 12, 31, 0).getTime()); + + expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeTruthy(); + }); + }); }); From 9c22e99331fe7261c575bc26e2381d78c189521d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Wed, 10 Jan 2018 16:42:25 +0100 Subject: [PATCH 3/3] Add extra test cases for password confirmation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Calviño Sánchez --- core/js/tests/specs/coreSpec.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index b27294b0f3..616e7509f7 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -1552,7 +1552,7 @@ describe('Core base tests', function() { stubJsPageLoadTime.restore(); }); - it('should not show the password confirmation dialog', function () { + it('should not show the password confirmation dialog when server time is earlier than local time', function () { // add server variables window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 1, 15, 0).getTime() / 1000); window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 1, 0, 0).getTime() / 1000); @@ -1564,7 +1564,7 @@ describe('Core base tests', function() { expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeFalsy(); }); - it('should show the password confirmation dialog', function () { + it('should show the password confirmation dialog when server time is earlier than local time', function () { // add server variables window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 1, 15, 0).getTime() / 1000); window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 1, 0, 0).getTime() / 1000); @@ -1575,5 +1575,29 @@ describe('Core base tests', function() { expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeTruthy(); }); + + it('should not show the password confirmation dialog when server time is later than local time', function () { + // add server variables + window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 23, 15, 0).getTime() / 1000); + window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 23, 0, 0).getTime() / 1000); + window.backendAllowsPasswordConfirmation = true; + + stubJsPageLoadTime = sinon.stub(OC.PasswordConfirmation, 'pageLoadTime').value(new Date(2018, 0, 3, 12, 15, 0).getTime()); + stubMomentNow = sinon.stub(moment, 'now').returns(new Date(2018, 0, 3, 12, 20, 0).getTime()); + + expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeFalsy(); + }); + + it('should show the password confirmation dialog when server time is later than local time', function () { + // add server variables + window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 23, 15, 0).getTime() / 1000); + window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 23, 0, 0).getTime() / 1000); + window.backendAllowsPasswordConfirmation = true; + + stubJsPageLoadTime = sinon.stub(OC.PasswordConfirmation, 'pageLoadTime').value(new Date(2018, 0, 3, 12, 15, 0).getTime()); + stubMomentNow = sinon.stub(moment, 'now').returns(new Date(2018, 0, 3, 12, 31, 0).getTime()); + + expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeTruthy(); + }); }); });