You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

password-confirmation.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  3. *
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. import _ from 'underscore'
  25. import $ from 'jquery'
  26. import moment from 'moment'
  27. import { generateUrl } from '@nextcloud/router'
  28. import OC from './index'
  29. /**
  30. * @namespace OC.PasswordConfirmation
  31. */
  32. export default {
  33. callback: null,
  34. pageLoadTime: null,
  35. init() {
  36. $('.password-confirm-required').on('click', _.bind(this.requirePasswordConfirmation, this))
  37. this.pageLoadTime = moment.now()
  38. },
  39. requiresPasswordConfirmation() {
  40. const serverTimeDiff = this.pageLoadTime - (window.nc_pageLoad * 1000)
  41. const timeSinceLogin = moment.now() - (serverTimeDiff + (window.nc_lastLogin * 1000))
  42. // if timeSinceLogin > 30 minutes and user backend allows password confirmation
  43. return (window.backendAllowsPasswordConfirmation && timeSinceLogin > 30 * 60 * 1000)
  44. },
  45. /**
  46. * @param {Function} callback success callback function
  47. * @param {object} options options
  48. * @param {Function} rejectCallback error callback function
  49. */
  50. requirePasswordConfirmation(callback, options, rejectCallback) {
  51. options = typeof options !== 'undefined' ? options : {}
  52. const defaults = {
  53. title: t('core', 'Authentication required'),
  54. text: t(
  55. 'core',
  56. 'This action requires you to confirm your password'
  57. ),
  58. confirm: t('core', 'Confirm'),
  59. label: t('core', 'Password'),
  60. error: '',
  61. }
  62. const config = _.extend(defaults, options)
  63. const self = this
  64. if (this.requiresPasswordConfirmation()) {
  65. OC.dialogs.prompt(
  66. config.text,
  67. config.title,
  68. function(result, password) {
  69. if (result && password !== '') {
  70. self._confirmPassword(password, config)
  71. } else if (_.isFunction(rejectCallback)) {
  72. rejectCallback()
  73. }
  74. },
  75. true,
  76. config.label,
  77. true
  78. ).then(function() {
  79. const $dialog = $('.oc-dialog:visible')
  80. $dialog.find('.ui-icon').remove()
  81. $dialog.addClass('password-confirmation')
  82. if (config.error !== '') {
  83. const $error = $('<p></p>').addClass('msg warning').text(config.error)
  84. $dialog.find('.oc-dialog-content').append($error)
  85. }
  86. $dialog.find('.oc-dialog-buttonrow').addClass('aside')
  87. const $buttons = $dialog.find('button')
  88. $buttons.eq(0).hide()
  89. $buttons.eq(1).text(config.confirm)
  90. })
  91. }
  92. this.callback = callback
  93. },
  94. _confirmPassword(password, config) {
  95. const self = this
  96. $.ajax({
  97. url: generateUrl('/login/confirm'),
  98. data: {
  99. password,
  100. },
  101. type: 'POST',
  102. success(response) {
  103. window.nc_lastLogin = response.lastLogin
  104. if (_.isFunction(self.callback)) {
  105. self.callback()
  106. }
  107. },
  108. error() {
  109. config.error = t('core', 'Failed to authenticate, try again')
  110. OC.PasswordConfirmation.requirePasswordConfirmation(self.callback, config)
  111. },
  112. })
  113. },
  114. }