2018-03-26 09:16:57 +08:00
|
|
|
import utils from "./utils.js";
|
2018-06-03 01:02:20 +08:00
|
|
|
import optionsInitService from './options_init.js';
|
2018-03-26 09:16:57 +08:00
|
|
|
|
2019-03-14 04:53:09 +08:00
|
|
|
const PROTECTED_SESSION_ID_KEY = 'protectedSessionId';
|
|
|
|
|
2018-03-26 09:16:57 +08:00
|
|
|
let lastProtectedSessionOperationDate = null;
|
|
|
|
let protectedSessionTimeout = null;
|
|
|
|
|
2019-05-13 03:45:30 +08:00
|
|
|
optionsInitService.addLoadListener(options => setProtectedSessionTimeout(options.protectedSessionTimeout));
|
2018-03-26 09:16:57 +08:00
|
|
|
|
|
|
|
setInterval(() => {
|
2019-02-10 23:36:25 +08:00
|
|
|
if (lastProtectedSessionOperationDate !== null && Date.now() - lastProtectedSessionOperationDate.getTime() > protectedSessionTimeout * 1000) {
|
2018-03-26 09:16:57 +08:00
|
|
|
resetProtectedSession();
|
|
|
|
}
|
|
|
|
}, 5000);
|
|
|
|
|
|
|
|
function setProtectedSessionTimeout(encSessTimeout) {
|
|
|
|
protectedSessionTimeout = encSessTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setProtectedSessionId(id) {
|
2019-03-14 04:53:09 +08:00
|
|
|
// using session cookie so that it disappears after browser/tab is closed
|
|
|
|
utils.setSessionCookie(PROTECTED_SESSION_ID_KEY, id);
|
2018-03-26 09:16:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function resetProtectedSession() {
|
2019-03-14 04:53:09 +08:00
|
|
|
utils.setSessionCookie(PROTECTED_SESSION_ID_KEY, null);
|
2018-03-26 09:16:57 +08:00
|
|
|
|
|
|
|
// most secure solution - guarantees nothing remained in memory
|
|
|
|
// since this expires because user doesn't use the app, it shouldn't be disruptive
|
|
|
|
utils.reloadApp();
|
|
|
|
}
|
|
|
|
|
|
|
|
function isProtectedSessionAvailable() {
|
2019-03-14 04:53:09 +08:00
|
|
|
return !!utils.getCookie(PROTECTED_SESSION_ID_KEY);
|
2018-03-26 09:16:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function touchProtectedSession() {
|
|
|
|
if (isProtectedSessionAvailable()) {
|
2019-06-20 04:23:55 +08:00
|
|
|
lastProtectedSessionOperationDate = new Date();
|
|
|
|
|
2019-03-14 04:53:09 +08:00
|
|
|
setProtectedSessionId(utils.getCookie(PROTECTED_SESSION_ID_KEY));
|
2018-03-26 09:16:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
setProtectedSessionId,
|
|
|
|
resetProtectedSession,
|
|
|
|
isProtectedSessionAvailable,
|
|
|
|
setProtectedSessionTimeout,
|
|
|
|
touchProtectedSession
|
|
|
|
};
|