| 
									
										
										
										
											2017-11-04 19:38:50 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:36:36 -05:00
										 |  |  | const protected_session = (function() { | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |     const $dialog = $("#protected-session-password-dialog"); | 
					
						
							|  |  |  |     const $passwordForm = $("#protected-session-password-form"); | 
					
						
							|  |  |  |     const $password = $("#protected-session-password"); | 
					
						
							|  |  |  |     const $noteDetailWrapper = $("#note-detail-wrapper"); | 
					
						
							| 
									
										
										
										
											2018-03-24 00:54:50 -04:00
										 |  |  |     const $protectButton = $("#protect-button"); | 
					
						
							|  |  |  |     const $unprotectButton = $("#unprotect-button"); | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |     let protectedSessionDeferred = null; | 
					
						
							|  |  |  |     let lastProtectedSessionOperationDate = null; | 
					
						
							| 
									
										
										
										
											2017-11-14 22:44:45 -05:00
										 |  |  |     let protectedSessionTimeout = null; | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  |     let protectedSessionId = null; | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 23:30:35 -05:00
										 |  |  |     $(document).ready(() => { | 
					
						
							|  |  |  |         server.get('settings/all').then(settings => protectedSessionTimeout = settings.protected_session_timeout); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:57:40 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:44:45 -05:00
										 |  |  |     function setProtectedSessionTimeout(encSessTimeout) { | 
					
						
							|  |  |  |         protectedSessionTimeout = encSessTimeout; | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-03 20:01:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |     function ensureProtectedSession(requireProtectedSession, modal) { | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |         const dfd = $.Deferred(); | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |         if (requireProtectedSession && !isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |             protectedSessionDeferred = dfd; | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-23 20:10:29 -05:00
										 |  |  |             if (noteTree.getCurrentNode().data.isProtected) { | 
					
						
							|  |  |  |                 $noteDetailWrapper.hide(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-01-26 22:32:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |             $dialog.dialog({ | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |                 modal: modal, | 
					
						
							|  |  |  |                 width: 400, | 
					
						
							|  |  |  |                 open: () => { | 
					
						
							|  |  |  |                     if (!modal) { | 
					
						
							|  |  |  |                         // dialog steals focus for itself, which is not what we want for non-modal (viewing)
 | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |                         noteTree.getCurrentNode().setFocus(); | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             dfd.resolve(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |         return dfd.promise(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-08 20:55:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:21:56 -05:00
										 |  |  |     async function setupProtectedSession() { | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |         const password = $password.val(); | 
					
						
							|  |  |  |         $password.val(""); | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  |         const response = await enterProtectedSession(password); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!response.success) { | 
					
						
							|  |  |  |             showError("Wrong password."); | 
					
						
							| 
									
										
										
										
											2017-11-08 22:33:08 -05:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-17 00:18:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  |         protectedSessionId = response.protectedSessionId; | 
					
						
							| 
									
										
										
										
											2017-09-06 23:13:39 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |         $dialog.dialog("close"); | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 21:40:26 -05:00
										 |  |  |         noteEditor.reload(); | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  |         noteTree.reload(); | 
					
						
							| 
									
										
										
										
											2017-09-17 12:46:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |         if (protectedSessionDeferred !== null) { | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |             ensureDialogIsClosed($dialog, $password); | 
					
						
							| 
									
										
										
										
											2017-11-14 23:01:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |             $noteDetailWrapper.show(); | 
					
						
							| 
									
										
										
										
											2018-01-26 22:32:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |             protectedSessionDeferred.resolve(); | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |             protectedSessionDeferred = null; | 
					
						
							| 
									
										
										
										
											2017-11-08 22:33:08 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 00:10:11 -05:00
										 |  |  |     function ensureDialogIsClosed() { | 
					
						
							|  |  |  |         // this may fal if the dialog has not been previously opened
 | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |             $dialog.dialog('close'); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:10:11 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         catch (e) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |         $password.val(''); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:10:11 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  |     async function enterProtectedSession(password) { | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |         return await server.post('login/protected', { | 
					
						
							|  |  |  |             password: password | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getProtectedSessionId() { | 
					
						
							|  |  |  |         return protectedSessionId; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |     function resetProtectedSession() { | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  |         protectedSessionId = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 23:25:23 -05:00
										 |  |  |         // most secure solution - guarantees nothing remained in memory
 | 
					
						
							|  |  |  |         // since this expires because user doesn't use the app, it shouldn't be disruptive
 | 
					
						
							| 
									
										
										
										
											2017-11-29 23:30:35 -05:00
										 |  |  |         reloadApp(); | 
					
						
							| 
									
										
										
										
											2017-09-06 23:16:54 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-12 23:07:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |     function isProtectedSessionAvailable() { | 
					
						
							| 
									
										
										
										
											2017-11-12 21:40:26 -05:00
										 |  |  |         return protectedSessionId !== null; | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-08 22:43:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |     async function protectNoteAndSendToServer() { | 
					
						
							|  |  |  |         await ensureProtectedSession(true, true); | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |         const note = noteEditor.getCurrentNote(); | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |         noteEditor.updateNoteFromInputs(note); | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         note.detail.isProtected = true; | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |         await noteEditor.saveNoteToServer(note); | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         noteTree.setProtected(note.detail.noteId, note.detail.isProtected); | 
					
						
							| 
									
										
										
										
											2017-12-25 09:30:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:21:56 -05:00
										 |  |  |         noteEditor.setNoteBackgroundIfProtected(note); | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-02 23:36:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |     async function unprotectNoteAndSendToServer() { | 
					
						
							|  |  |  |         await ensureProtectedSession(true, true); | 
					
						
							| 
									
										
										
										
											2017-11-02 23:55:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |         const note = noteEditor.getCurrentNote(); | 
					
						
							| 
									
										
										
										
											2017-11-03 20:01:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |         noteEditor.updateNoteFromInputs(note); | 
					
						
							| 
									
										
										
										
											2017-11-02 23:36:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         note.detail.isProtected = false; | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |         await noteEditor.saveNoteToServer(note); | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         noteTree.setProtected(note.detail.noteId, note.detail.isProtected); | 
					
						
							| 
									
										
										
										
											2017-12-25 09:30:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:21:56 -05:00
										 |  |  |         noteEditor.setNoteBackgroundIfProtected(note); | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-17 12:46:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:50:56 -05:00
										 |  |  |     function touchProtectedSession() { | 
					
						
							|  |  |  |         if (isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |             lastProtectedSessionOperationDate = new Date(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     async function protectSubTree(noteId, protect) { | 
					
						
							| 
									
										
										
										
											2017-11-22 20:46:42 -05:00
										 |  |  |         await ensureProtectedSession(true, true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 20:53:00 -05:00
										 |  |  |         await server.put('notes/' + noteId + "/protect-sub-tree/" + (protect ? 1 : 0)); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         showMessage("Request to un/protect sub tree has finished successfully"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         noteTree.reload(); | 
					
						
							|  |  |  |         noteEditor.reload(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |     $passwordForm.submit(() => { | 
					
						
							| 
									
										
										
										
											2017-11-14 22:21:56 -05:00
										 |  |  |         setupProtectedSession(); | 
					
						
							| 
									
										
										
										
											2017-11-08 22:33:08 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setInterval(() => { | 
					
						
							| 
									
										
										
										
											2017-11-14 22:44:45 -05:00
										 |  |  |         if (lastProtectedSessionOperationDate !== null && new Date().getTime() - lastProtectedSessionOperationDate.getTime() > protectedSessionTimeout * 1000) { | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |             resetProtectedSession(); | 
					
						
							| 
									
										
										
										
											2017-11-08 22:33:08 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     }, 5000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 00:54:50 -04:00
										 |  |  |     $protectButton.click(protectNoteAndSendToServer); | 
					
						
							|  |  |  |     $unprotectButton.click(unprotectNoteAndSendToServer); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2017-11-14 22:44:45 -05:00
										 |  |  |         setProtectedSessionTimeout, | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |         ensureProtectedSession, | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |         resetProtectedSession, | 
					
						
							|  |  |  |         isProtectedSessionAvailable, | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |         protectNoteAndSendToServer, | 
					
						
							|  |  |  |         unprotectNoteAndSendToServer, | 
					
						
							| 
									
										
										
										
											2017-11-14 22:50:56 -05:00
										 |  |  |         getProtectedSessionId, | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |         touchProtectedSession, | 
					
						
							| 
									
										
										
										
											2017-11-15 00:10:11 -05:00
										 |  |  |         protectSubTree, | 
					
						
							|  |  |  |         ensureDialogIsClosed | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |     }; | 
					
						
							|  |  |  | })(); |