2018-03-26 09:16:57 +08:00
|
|
|
import utils from "./utils.js";
|
2020-02-06 05:08:45 +08:00
|
|
|
import options from './options.js';
|
2018-03-26 09:16:57 +08:00
|
|
|
|
2019-03-14 04:53:09 +08:00
|
|
|
const PROTECTED_SESSION_ID_KEY = 'protectedSessionId';
|
|
|
|
|
2020-02-06 05:08:45 +08:00
|
|
|
let lastProtectedSessionOperationDate = 0;
|
2018-03-26 09:16:57 +08:00
|
|
|
|
|
|
|
setInterval(() => {
|
2020-02-06 05:08:45 +08:00
|
|
|
const protectedSessionTimeout = options.getInt('protectedSessionTimeout');
|
|
|
|
if (lastProtectedSessionOperationDate
|
|
|
|
&& Date.now() - lastProtectedSessionOperationDate > protectedSessionTimeout * 1000) {
|
|
|
|
|
2018-03-26 09:16:57 +08:00
|
|
|
resetProtectedSession();
|
|
|
|
}
|
2020-05-14 05:06:13 +08:00
|
|
|
}, 10000);
|
2018-03-26 09:16:57 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-14 05:06:13 +08:00
|
|
|
function resetSessionCookie() {
|
2019-03-14 04:53:09 +08:00
|
|
|
utils.setSessionCookie(PROTECTED_SESSION_ID_KEY, null);
|
2020-05-14 05:06:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function resetProtectedSession() {
|
|
|
|
resetSessionCookie();
|
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()) {
|
2020-02-06 05:08:45 +08:00
|
|
|
lastProtectedSessionOperationDate = Date.now();
|
2019-06-20 04:23:55 +08:00
|
|
|
|
2019-03-14 04:53:09 +08:00
|
|
|
setProtectedSessionId(utils.getCookie(PROTECTED_SESSION_ID_KEY));
|
2018-03-26 09:16:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 17:09:07 +08:00
|
|
|
function touchProtectedSessionIfNecessary(note) {
|
|
|
|
if (note && note.isProtected && isProtectedSessionAvailable()) {
|
|
|
|
touchProtectedSession();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 09:16:57 +08:00
|
|
|
export default {
|
|
|
|
setProtectedSessionId,
|
2020-05-14 05:06:13 +08:00
|
|
|
resetSessionCookie,
|
2018-03-26 09:16:57 +08:00
|
|
|
resetProtectedSession,
|
|
|
|
isProtectedSessionAvailable,
|
2020-04-25 17:09:07 +08:00
|
|
|
touchProtectedSession,
|
|
|
|
touchProtectedSessionIfNecessary
|
2020-05-14 05:06:13 +08:00
|
|
|
};
|