| 
									
										
										
										
											2017-11-04 19:38:50 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:36:36 -05:00
										 |  |  | const protected_session = (function() { | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |     const dialogEl = $("#protected-session-password-dialog"); | 
					
						
							|  |  |  |     const passwordFormEl = $("#protected-session-password-form"); | 
					
						
							|  |  |  |     const passwordEl = $("#protected-session-password"); | 
					
						
							| 
									
										
										
										
											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-04 19:57:40 -04:00
										 |  |  |     $.ajax({ | 
					
						
							|  |  |  |         url: baseApiUrl + 'settings/all', | 
					
						
							|  |  |  |         type: 'GET', | 
					
						
							| 
									
										
										
										
											2017-11-14 23:01:23 -05:00
										 |  |  |         error: () => showError("Error getting protected session settings.") | 
					
						
							| 
									
										
										
										
											2017-11-04 19:57:40 -04:00
										 |  |  |     }).then(settings => { | 
					
						
							| 
									
										
										
										
											2017-11-14 22:44:45 -05:00
										 |  |  |         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()) { | 
					
						
							| 
									
										
										
										
											2017-11-08 22:33:08 -05:00
										 |  |  |             // if this is entry point then we need to show the app even before the note is loaded
 | 
					
						
							|  |  |  |             showAppIfHidden(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |             protectedSessionDeferred = dfd; | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 18:18:55 -04:00
										 |  |  |             dialogEl.dialog({ | 
					
						
							|  |  |  |                 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() { | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |         const password = passwordEl.val(); | 
					
						
							|  |  |  |         passwordEl.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; | 
					
						
							|  |  |  |         initAjax(); | 
					
						
							| 
									
										
										
										
											2017-09-06 23:13:39 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  |         dialogEl.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) { | 
					
						
							| 
									
										
										
										
											2017-11-15 00:10:11 -05:00
										 |  |  |             ensureDialogIsClosed(dialogEl, passwordEl); | 
					
						
							| 
									
										
										
										
											2017-11-14 23:01:23 -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 { | 
					
						
							|  |  |  |             dialogEl.dialog('close'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         catch (e) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         passwordEl.val(''); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  |     async function enterProtectedSession(password) { | 
					
						
							|  |  |  |         return await $.ajax({ | 
					
						
							|  |  |  |             url: baseApiUrl + 'login/protected', | 
					
						
							|  |  |  |             type: 'POST', | 
					
						
							|  |  |  |             contentType: 'application/json', | 
					
						
							|  |  |  |             data: JSON.stringify({ | 
					
						
							|  |  |  |                 password: password | 
					
						
							|  |  |  |             }), | 
					
						
							|  |  |  |             error: () => showError("Error entering protected session.") | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getProtectedSessionId() { | 
					
						
							|  |  |  |         return protectedSessionId; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |     function resetProtectedSession() { | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  |         protectedSessionId = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         initAjax(); | 
					
						
							| 
									
										
										
										
											2017-09-06 22:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |         window.location.reload(true); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |         note.detail.is_protected = 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |         note.detail.is_protected = 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  |         await $.ajax({ | 
					
						
							|  |  |  |             url: baseApiUrl + 'tree/' + noteId + "/protectSubTree/" + (protect ? 1 : 0), | 
					
						
							|  |  |  |             type: 'PUT', | 
					
						
							|  |  |  |             contentType: 'application/json', | 
					
						
							|  |  |  |             error: () => showError("Request to un/protect sub tree has failed.") | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         showMessage("Request to un/protect sub tree has finished successfully"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         noteTree.reload(); | 
					
						
							|  |  |  |         noteEditor.reload(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:34:33 -05:00
										 |  |  |     passwordFormEl.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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     }; | 
					
						
							|  |  |  | })(); |