Skip to content

Commit

Permalink
Replace password dialog with dialog from library
Browse files Browse the repository at this point in the history
Signed-off-by: julia.kirschenheuter <[email protected]>
  • Loading branch information
JuliaKirschenheuter committed Oct 17, 2023
1 parent 106bf6c commit 7d00c7f
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 163 deletions.
61 changes: 0 additions & 61 deletions core/js/tests/specs/coreSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1295,65 +1295,4 @@ 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 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);
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 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);
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();
});

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();
});
});
});
3 changes: 3 additions & 0 deletions core/src/OC/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ export default {

msg,
Notification,
/**
* @deprecated 28.0.0 use methods from '@nextcloud/password-confirmation'
*/
PasswordConfirmation,
Plugins,
theme,
Expand Down
96 changes: 5 additions & 91 deletions core/src/OC/password-confirmation.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,110 +22,24 @@
*
*/

import _ from 'underscore'
import $ from 'jquery'
import moment from 'moment'
import { generateUrl } from '@nextcloud/router'

import OC from './index.js'
import { confirmPassword, isPasswordConfirmationRequired } from '@nextcloud/password-confirmation'
import '@nextcloud/password-confirmation/dist/style.css'

/**
* @namespace OC.PasswordConfirmation
*/
export default {
callback: null,

pageLoadTime: null,

init() {
$('.password-confirm-required').on('click', _.bind(this.requirePasswordConfirmation, this))
this.pageLoadTime = moment.now()
},

requiresPasswordConfirmation() {
const serverTimeDiff = this.pageLoadTime - (window.nc_pageLoad * 1000)
const timeSinceLogin = moment.now() - (serverTimeDiff + (window.nc_lastLogin * 1000))

// if timeSinceLogin > 30 minutes and user backend allows password confirmation
return (window.backendAllowsPasswordConfirmation && timeSinceLogin > 30 * 60 * 1000)
return isPasswordConfirmationRequired()

Check warning on line 34 in core/src/OC/password-confirmation.js

View check run for this annotation

Codecov / codecov/patch

core/src/OC/password-confirmation.js#L34

Added line #L34 was not covered by tests
},

/**
* @param {Function} callback success callback function
* @param {object} options options
* @param {object} options options currently not used by confirmPassword
* @param {Function} rejectCallback error callback function
*/
requirePasswordConfirmation(callback, options, rejectCallback) {
options = typeof options !== 'undefined' ? options : {}
const defaults = {
title: t('core', 'Authentication required'),
text: t(
'core',
'This action requires you to confirm your password'
),
confirm: t('core', 'Confirm'),
label: t('core', 'Password'),
error: '',
}

const config = _.extend(defaults, options)

const self = this

if (this.requiresPasswordConfirmation()) {
OC.dialogs.prompt(
config.text,
config.title,
function(result, password) {
if (result && password !== '') {
self._confirmPassword(password, config)
} else if (_.isFunction(rejectCallback)) {
rejectCallback()
}
},
true,
config.label,
true
).then(function() {
const $dialog = $('.oc-dialog:visible')
$dialog.find('.ui-icon').remove()
$dialog.addClass('password-confirmation')
if (config.error !== '') {
const $error = $('<p></p>').addClass('msg warning').text(config.error)
$dialog.find('.oc-dialog-content').append($error)
}
const $buttonrow = $dialog.find('.oc-dialog-buttonrow')
$buttonrow.addClass('aside')

const $buttons = $buttonrow.find('button')
$buttons.eq(0).hide()
$buttons.eq(1).text(config.confirm)
})
}

this.callback = callback
},

_confirmPassword(password, config) {
const self = this

$.ajax({
url: generateUrl('/login/confirm'),
data: {
password,
},
type: 'POST',
success(response) {
window.nc_lastLogin = response.lastLogin

if (_.isFunction(self.callback)) {
self.callback()
}
},
error() {
config.error = t('core', 'Failed to authenticate, try again')
OC.PasswordConfirmation.requirePasswordConfirmation(self.callback, config)
},
})
confirmPassword().then(callback, rejectCallback)

Check warning on line 43 in core/src/OC/password-confirmation.js

View check run for this annotation

Codecov / codecov/patch

core/src/OC/password-confirmation.js#L43

Added line #L43 was not covered by tests
},
}
2 changes: 0 additions & 2 deletions core/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import OC from './OC/index.js'
import { setUp as setUpContactsMenu } from './components/ContactsMenu.js'
import { setUp as setUpMainMenu } from './components/MainMenu.js'
import { setUp as setUpUserMenu } from './components/UserMenu.js'
import PasswordConfirmation from './OC/password-confirmation.js'
import { interceptRequests } from './utils/xhr-request.js'

// keep in sync with core/css/variables.scss
Expand Down Expand Up @@ -298,5 +297,4 @@ export const initCore = () => {
}

initLiveTimestamps()
PasswordConfirmation.init()
}
4 changes: 2 additions & 2 deletions dist/core-common.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-common.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/core-login.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-login.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/core-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-main.js.map

Large diffs are not rendered by default.

0 comments on commit 7d00c7f

Please sign in to comment.