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.

session-heartbeat.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  3. *
  4. * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. import $ from 'jquery'
  22. import { emit } from '@nextcloud/event-bus'
  23. import { generateUrl } from './OC/routing'
  24. import OC from './OC'
  25. import { setToken as setRequestToken } from './OC/requesttoken'
  26. /**
  27. * session heartbeat (defaults to enabled)
  28. * @returns {boolean}
  29. */
  30. const keepSessionAlive = () => {
  31. return OC.config.session_keepalive === undefined
  32. || !!OC.config.session_keepalive
  33. }
  34. /**
  35. * get interval in seconds
  36. * @returns {Number}
  37. */
  38. const getInterval = () => {
  39. let interval = NaN
  40. if (OC.config.session_lifetime) {
  41. interval = Math.floor(OC.config.session_lifetime / 2)
  42. }
  43. // minimum one minute, max 24 hours, default 15 minutes
  44. return Math.min(
  45. 24 * 3600,
  46. Math.max(
  47. 60,
  48. isNaN(interval) ? 900 : interval
  49. )
  50. )
  51. }
  52. const getToken = async() => {
  53. const url = generateUrl('/csrftoken')
  54. // Not using Axios here as Axios is not stubbable with the sinon fake server
  55. // see https://stackoverflow.com/questions/41516044/sinon-mocha-test-with-async-ajax-calls-didnt-return-promises
  56. // see js/tests/specs/coreSpec.js for the tests
  57. const resp = await $.get(url)
  58. return resp.token
  59. }
  60. const poll = async() => {
  61. try {
  62. const token = await getToken()
  63. setRequestToken(token)
  64. } catch (e) {
  65. console.error('session heartbeat failed', e)
  66. }
  67. }
  68. const startPolling = () => {
  69. const interval = setInterval(poll, getInterval() * 1000)
  70. console.info('session heartbeat polling started')
  71. return interval
  72. }
  73. /**
  74. * Calls the server periodically to ensure that session and CSRF
  75. * token doesn't expire
  76. */
  77. export const initSessionHeartBeat = () => {
  78. if (!keepSessionAlive()) {
  79. console.info('session heartbeat disabled')
  80. return
  81. }
  82. let interval = startPolling()
  83. window.addEventListener('online', async() => {
  84. console.info('browser is online again, resuming heartbeat')
  85. interval = startPolling()
  86. try {
  87. await poll()
  88. console.info('session token successfully updated after resuming network')
  89. // Let apps know we're online and requests will have the new token
  90. emit('networkOnline', {
  91. success: true,
  92. })
  93. } catch (e) {
  94. console.error('could not update session token after resuming network', e)
  95. // Let apps know we're online but requests might have an outdated token
  96. emit('networkOnline', {
  97. success: false,
  98. })
  99. }
  100. })
  101. window.addEventListener('offline', () => {
  102. console.info('browser is offline, stopping heartbeat')
  103. // Let apps know we're offline
  104. emit('networkOffline', {})
  105. clearInterval(interval)
  106. console.info('session heartbeat polling stopped')
  107. })
  108. }