Skip to content

Commit c21af6d

Browse files
juliusknorrrullzer
authored andcommitted
Add config for auto logout after browser inactivity
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent e96a7d5 commit c21af6d

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

config/config.sample.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,15 @@
251251
*/
252252
'session_keepalive' => true,
253253

254+
/**
255+
* Enable or disable the automatic logout after session_lifetime, even if session
256+
* keepalive is enabled. This will make sure that an inactive browser will be logged out
257+
* even if requests to the server might extend the session lifetime.
258+
*
259+
* Defaults to ``false``
260+
*/
261+
'auto_logout' => false,
262+
254263
/**
255264
* Enforce token authentication for clients, which blocks requests using the user
256265
* password for enhanced security. Users need to generate tokens in personal settings

core/src/session-heartbeat.js

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@
2121

2222
import $ from 'jquery'
2323
import { emit } from '@nextcloud/event-bus'
24+
import { loadState } from '@nextcloud/initial-state'
25+
import { getCurrentUser } from '@nextcloud/auth'
2426

2527
import { generateUrl } from './OC/routing'
26-
import OC from './OC'
27-
import { setToken as setRequestToken } from './OC/requesttoken'
28+
import { setToken as setRequestToken, getToken as getRequestToken } from './OC/requesttoken'
29+
30+
const config = loadState('core', 'config')
2831

2932
/**
3033
* session heartbeat (defaults to enabled)
3134
* @returns {boolean}
3235
*/
3336
const keepSessionAlive = () => {
34-
return OC.config.session_keepalive === undefined
35-
|| !!OC.config.session_keepalive
37+
return config.session_keepalive === undefined
38+
|| !!config.session_keepalive
3639
}
3740

3841
/**
@@ -41,8 +44,8 @@ const keepSessionAlive = () => {
4144
*/
4245
const getInterval = () => {
4346
let interval = NaN
44-
if (OC.config.session_lifetime) {
45-
interval = Math.floor(OC.config.session_lifetime / 2)
47+
if (config.session_lifetime) {
48+
interval = Math.floor(config.session_lifetime / 2)
4649
}
4750

4851
// minimum one minute, max 24 hours, default 15 minutes
@@ -83,11 +86,46 @@ const startPolling = () => {
8386
return interval
8487
}
8588

89+
const registerAutoLogout = () => {
90+
if (!config.auto_logout || !getCurrentUser()) {
91+
return
92+
}
93+
94+
let lastActive = Date.now()
95+
window.addEventListener('mousemove', e => {
96+
lastActive = Date.now()
97+
localStorage.setItem('lastActive', lastActive)
98+
})
99+
100+
window.addEventListener('touchstart', e => {
101+
lastActive = Date.now()
102+
localStorage.setItem('lastActive', lastActive)
103+
})
104+
105+
window.addEventListener('storage', e => {
106+
if (e.key !== 'lastActive') {
107+
return
108+
}
109+
lastActive = e.newValue
110+
})
111+
112+
setInterval(function() {
113+
const timeout = Date.now() - config.session_lifetime * 1000
114+
if (lastActive < timeout) {
115+
console.info('Inactivity timout reached, logging out')
116+
const logoutUrl = generateUrl('/logout') + '?requesttoken=' + getRequestToken()
117+
window.location = logoutUrl
118+
}
119+
}, 1000)
120+
}
121+
86122
/**
87123
* Calls the server periodically to ensure that session and CSRF
88124
* token doesn't expire
89125
*/
90126
export const initSessionHeartBeat = () => {
127+
registerAutoLogout()
128+
91129
if (!keepSessionAlive()) {
92130
console.info('session heartbeat disabled')
93131
return

0 commit comments

Comments
 (0)