2019-09-25 19:19:42 +03:00
|
|
|
/**
|
2019-01-31 20:41:06 +03:00
|
|
|
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
|
|
|
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import _ from 'underscore'
|
|
|
|
import $ from 'jquery'
|
|
|
|
import moment from 'moment'
|
|
|
|
|
|
|
|
import OC from './index'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @namespace OC.PasswordConfirmation
|
|
|
|
*/
|
|
|
|
export default {
|
|
|
|
callback: null,
|
|
|
|
|
|
|
|
pageLoadTime: null,
|
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
init: function() {
|
|
|
|
$('.password-confirm-required').on('click', _.bind(this.requirePasswordConfirmation, this))
|
|
|
|
this.pageLoadTime = moment.now()
|
2019-01-31 20:41:06 +03:00
|
|
|
},
|
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
requiresPasswordConfirmation: function() {
|
2019-11-13 15:05:10 +03:00
|
|
|
const serverTimeDiff = this.pageLoadTime - (window.nc_pageLoad * 1000)
|
|
|
|
const timeSinceLogin = moment.now() - (serverTimeDiff + (window.nc_lastLogin * 1000))
|
2019-01-31 20:41:06 +03:00
|
|
|
|
|
|
|
// if timeSinceLogin > 30 minutes and user backend allows password confirmation
|
2019-09-25 19:19:42 +03:00
|
|
|
return (window.backendAllowsPasswordConfirmation && timeSinceLogin > 30 * 60 * 1000)
|
2019-01-31 20:41:06 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2019-09-25 19:19:42 +03:00
|
|
|
* @param {Function} callback success callback function
|
|
|
|
* @param {Object} options options
|
|
|
|
* @param {Function} rejectCallback error callback function
|
2019-01-31 20:41:06 +03:00
|
|
|
*/
|
2019-09-25 19:19:42 +03:00
|
|
|
requirePasswordConfirmation: function(callback, options, rejectCallback) {
|
|
|
|
options = typeof options !== 'undefined' ? options : {}
|
2019-11-13 15:05:10 +03:00
|
|
|
const defaults = {
|
2019-01-31 20:41:06 +03:00
|
|
|
title: t('core', 'Authentication required'),
|
|
|
|
text: t(
|
|
|
|
'core',
|
|
|
|
'This action requires you to confirm your password'
|
|
|
|
),
|
|
|
|
confirm: t('core', 'Confirm'),
|
|
|
|
label: t('core', 'Password'),
|
2019-11-13 15:05:10 +03:00
|
|
|
error: '',
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|
2019-01-31 20:41:06 +03:00
|
|
|
|
2019-11-13 15:05:10 +03:00
|
|
|
const config = _.extend(defaults, options)
|
2019-01-31 20:41:06 +03:00
|
|
|
|
2019-11-13 15:05:10 +03:00
|
|
|
const self = this
|
2019-01-31 20:41:06 +03:00
|
|
|
|
|
|
|
if (this.requiresPasswordConfirmation()) {
|
|
|
|
OC.dialogs.prompt(
|
|
|
|
config.text,
|
|
|
|
config.title,
|
2019-09-25 19:19:42 +03:00
|
|
|
function(result, password) {
|
2019-01-31 20:41:06 +03:00
|
|
|
if (result && password !== '') {
|
2019-09-25 19:19:42 +03:00
|
|
|
self._confirmPassword(password, config)
|
2019-01-31 20:41:06 +03:00
|
|
|
} else if (_.isFunction(rejectCallback)) {
|
|
|
|
rejectCallback()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
config.label,
|
|
|
|
true
|
2019-09-25 19:19:42 +03:00
|
|
|
).then(function() {
|
2019-11-13 15:05:10 +03:00
|
|
|
const $dialog = $('.oc-dialog:visible')
|
2019-09-25 19:19:42 +03:00
|
|
|
$dialog.find('.ui-icon').remove()
|
|
|
|
$dialog.addClass('password-confirmation')
|
2019-01-31 20:41:06 +03:00
|
|
|
if (config.error !== '') {
|
2019-11-13 15:05:10 +03:00
|
|
|
const $error = $('<p></p>').addClass('msg warning').text(config.error)
|
|
|
|
$dialog.find('.oc-dialog-content').append($error)
|
2019-01-31 20:41:06 +03:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
$dialog.find('.oc-dialog-buttonrow').addClass('aside')
|
2019-01-31 20:41:06 +03:00
|
|
|
|
2019-11-13 15:05:10 +03:00
|
|
|
const $buttons = $dialog.find('button')
|
2019-09-25 19:19:42 +03:00
|
|
|
$buttons.eq(0).hide()
|
|
|
|
$buttons.eq(1).text(config.confirm)
|
|
|
|
})
|
2019-01-31 20:41:06 +03:00
|
|
|
}
|
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
this.callback = callback
|
2019-01-31 20:41:06 +03:00
|
|
|
},
|
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
_confirmPassword: function(password, config) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const self = this
|
2019-01-31 20:41:06 +03:00
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: OC.generateUrl('/login/confirm'),
|
|
|
|
data: {
|
2019-11-13 15:05:10 +03:00
|
|
|
password: password,
|
2019-01-31 20:41:06 +03:00
|
|
|
},
|
|
|
|
type: 'POST',
|
2019-09-25 19:19:42 +03:00
|
|
|
success: function(response) {
|
|
|
|
window.nc_lastLogin = response.lastLogin
|
2019-01-31 20:41:06 +03:00
|
|
|
|
|
|
|
if (_.isFunction(self.callback)) {
|
2019-09-25 19:19:42 +03:00
|
|
|
self.callback()
|
2019-01-31 20:41:06 +03:00
|
|
|
}
|
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
error: function() {
|
|
|
|
config.error = t('core', 'Failed to authenticate, try again')
|
|
|
|
OC.PasswordConfirmation.requirePasswordConfirmation(self.callback, config)
|
2019-11-13 15:05:10 +03:00
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
})
|
2019-11-13 15:05:10 +03:00
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|