Skip to content

Commit

Permalink
protected session expiration timer moved to backend, closes #2847
Browse files Browse the repository at this point in the history
  • Loading branch information
zadam committed May 13, 2022
1 parent 8318ab7 commit e87e065
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
16 changes: 2 additions & 14 deletions src/public/app/services/protected_session_holder.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import options from './options.js';
import server from "./server.js";

let lastProtectedSessionOperationDate = 0;

setInterval(() => {
const protectedSessionTimeout = options.getInt('protectedSessionTimeout');
if (lastProtectedSessionOperationDate
&& Date.now() - lastProtectedSessionOperationDate > protectedSessionTimeout * 1000) {

resetProtectedSession();
}
}, 10000);

function enableProtectedSession() {
glob.isProtectedSessionAvailable = true;

Expand All @@ -26,9 +14,9 @@ function isProtectedSessionAvailable() {
return glob.isProtectedSessionAvailable;
}

function touchProtectedSession() {
async function touchProtectedSession() {
if (isProtectedSessionAvailable()) {
lastProtectedSessionOperationDate = Date.now();
await server.post("login/protected/touch");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/public/app/services/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ function sleep(time_ms) {
return new Promise((resolve) => {
setTimeout(resolve, time_ms);
});
};
}

export default {
reloadFrontendApp,
Expand Down
7 changes: 6 additions & 1 deletion src/routes/api/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ function logoutFromProtectedSession() {
ws.sendMessageToAllClients({ type: 'protectedSessionLogout' });
}

function touchProtectedSession() {
protectedSessionService.touchProtectedSession();
}

function token(req) {
const password = req.body.password;

Expand All @@ -92,7 +96,7 @@ function token(req) {

// for backwards compatibility with Sender which does not send the name
const tokenName = req.body.tokenName || "Trilium Sender / Web Clipper";

const {authToken} = etapiTokenService.createToken(tokenName);

return { token: authToken };
Expand All @@ -102,5 +106,6 @@ module.exports = {
loginSync,
loginToProtectedSession,
logoutFromProtectedSession,
touchProtectedSession,
token
};
1 change: 1 addition & 0 deletions src/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ function register(app) {
route(POST, '/api/login/sync', [], loginApiRoute.loginSync, apiResultHandler);
// this is for entering protected mode so user has to be already logged-in (that's the reason we don't require username)
apiRoute(POST, '/api/login/protected', loginApiRoute.loginToProtectedSession);
apiRoute(POST, '/api/login/protected/touch', loginApiRoute.touchProtectedSession);
apiRoute(POST, '/api/logout/protected', loginApiRoute.logoutFromProtectedSession);

route(POST, '/api/login/token', [], loginApiRoute.token, apiResultHandler);
Expand Down
25 changes: 24 additions & 1 deletion src/services/protected_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const log = require('./log');
const dataEncryptionService = require('./data_encryption');
const options = require("./options");

let dataKey = null;

Expand Down Expand Up @@ -54,12 +55,34 @@ function decryptString(cipherText) {
return dataEncryptionService.decryptString(getDataKey(), cipherText);
}

let lastProtectedSessionOperationDate = null;

function touchProtectedSession() {
if (isProtectedSessionAvailable()) {
lastProtectedSessionOperationDate = Date.now();
}
}

setInterval(() => {
const protectedSessionTimeout = options.getOptionInt('protectedSessionTimeout');
if (isProtectedSessionAvailable()
&& lastProtectedSessionOperationDate
&& Date.now() - lastProtectedSessionOperationDate > protectedSessionTimeout * 1000) {

resetDataKey();

require('./ws').reloadFrontend();
}
}, 30000);


module.exports = {
setDataKey,
resetDataKey,
isProtectedSessionAvailable,
encrypt,
decrypt,
decryptString,
decryptNotes
decryptNotes,
touchProtectedSession
};

0 comments on commit e87e065

Please sign in to comment.