mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 04:51:31 +08:00 
			
		
		
		
	protected session expiration timer moved to backend, closes #2847
This commit is contained in:
		
							parent
							
								
									8318ab7ac0
								
							
						
					
					
						commit
						e87e065100
					
				| @ -1,17 +1,5 @@ | |||||||
| import options from './options.js'; |  | ||||||
| import server from "./server.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() { | function enableProtectedSession() { | ||||||
|     glob.isProtectedSessionAvailable = true; |     glob.isProtectedSessionAvailable = true; | ||||||
| 
 | 
 | ||||||
| @ -26,9 +14,9 @@ function isProtectedSessionAvailable() { | |||||||
|     return glob.isProtectedSessionAvailable; |     return glob.isProtectedSessionAvailable; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function touchProtectedSession() { | async function touchProtectedSession() { | ||||||
|     if (isProtectedSessionAvailable()) { |     if (isProtectedSessionAvailable()) { | ||||||
|         lastProtectedSessionOperationDate = Date.now(); |         await server.post("login/protected/touch"); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -363,7 +363,7 @@ function sleep(time_ms) { | |||||||
|     return new Promise((resolve) => { |     return new Promise((resolve) => { | ||||||
|         setTimeout(resolve, time_ms); |         setTimeout(resolve, time_ms); | ||||||
|     }); |     }); | ||||||
| }; | } | ||||||
| 
 | 
 | ||||||
| export default { | export default { | ||||||
|     reloadFrontendApp, |     reloadFrontendApp, | ||||||
|  | |||||||
| @ -83,6 +83,10 @@ function logoutFromProtectedSession() { | |||||||
|     ws.sendMessageToAllClients({ type: 'protectedSessionLogout' }); |     ws.sendMessageToAllClients({ type: 'protectedSessionLogout' }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | function touchProtectedSession() { | ||||||
|  |     protectedSessionService.touchProtectedSession(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| function token(req) { | function token(req) { | ||||||
|     const password = req.body.password; |     const password = req.body.password; | ||||||
| 
 | 
 | ||||||
| @ -102,5 +106,6 @@ module.exports = { | |||||||
|     loginSync, |     loginSync, | ||||||
|     loginToProtectedSession, |     loginToProtectedSession, | ||||||
|     logoutFromProtectedSession, |     logoutFromProtectedSession, | ||||||
|  |     touchProtectedSession, | ||||||
|     token |     token | ||||||
| }; | }; | ||||||
|  | |||||||
| @ -359,6 +359,7 @@ function register(app) { | |||||||
|     route(POST, '/api/login/sync', [], loginApiRoute.loginSync, apiResultHandler); |     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)
 |     // 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', loginApiRoute.loginToProtectedSession); | ||||||
|  |     apiRoute(POST, '/api/login/protected/touch', loginApiRoute.touchProtectedSession); | ||||||
|     apiRoute(POST, '/api/logout/protected', loginApiRoute.logoutFromProtectedSession); |     apiRoute(POST, '/api/logout/protected', loginApiRoute.logoutFromProtectedSession); | ||||||
| 
 | 
 | ||||||
|     route(POST, '/api/login/token', [], loginApiRoute.token, apiResultHandler); |     route(POST, '/api/login/token', [], loginApiRoute.token, apiResultHandler); | ||||||
|  | |||||||
| @ -2,6 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| const log = require('./log'); | const log = require('./log'); | ||||||
| const dataEncryptionService = require('./data_encryption'); | const dataEncryptionService = require('./data_encryption'); | ||||||
|  | const options = require("./options"); | ||||||
| 
 | 
 | ||||||
| let dataKey = null; | let dataKey = null; | ||||||
| 
 | 
 | ||||||
| @ -54,6 +55,27 @@ function decryptString(cipherText) { | |||||||
|     return dataEncryptionService.decryptString(getDataKey(), 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 = { | module.exports = { | ||||||
|     setDataKey, |     setDataKey, | ||||||
|     resetDataKey, |     resetDataKey, | ||||||
| @ -61,5 +83,6 @@ module.exports = { | |||||||
|     encrypt, |     encrypt, | ||||||
|     decrypt, |     decrypt, | ||||||
|     decryptString, |     decryptString, | ||||||
|     decryptNotes |     decryptNotes, | ||||||
|  |     touchProtectedSession | ||||||
| }; | }; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zadam
						zadam