@@ -14,7 +14,7 @@ function decrypt_key(password, iv_b64, ciphertext_b64, salt_b64) {
1414 try {
1515 keystore = JSON . parse ( key . toString ( CryptoJS . enc . Utf8 ) ) ;
1616 if ( encryptcontent_id in keystore ) {
17- return keystore [ encryptcontent_id ] ;
17+ return keystore ;
1818 } else {
1919 //id not found in keystore
2020 return false ;
@@ -33,10 +33,7 @@ function decrypt_key_from_bundle(password, ciphertext_bundle, username) {
3333 for ( let i = 0 ; i < ciphertext_bundle . length ; i ++ ) {
3434 let parts = ciphertext_bundle [ i ] . split ( ';' ) ;
3535 if ( parts . length == 3 ) {
36- let key = decrypt_key ( password , parts [ 0 ] , parts [ 1 ] , parts [ 2 ] ) ;
37- if ( key ) {
38- return key ;
39- }
36+ return decrypt_key ( password , parts [ 0 ] , parts [ 1 ] , parts [ 2 ] ) ;
4037 } else if ( parts . length == 4 && username ) {
4138 let userhash = CryptoJS . SHA256 ( encodeURIComponent ( username . value ) ) . toString ( CryptoJS . enc . Base64 ) ;
4239 if ( parts [ 3 ] == userhash ) {
@@ -46,11 +43,6 @@ function decrypt_key_from_bundle(password, ciphertext_bundle, username) {
4643 return false ;
4744 }
4845 }
49- } else {
50- let parts = ciphertext_bundle . split ( ';' ) ;
51- if ( parts . length == 3 ) {
52- return decrypt_key ( password , parts [ 0 ] , parts [ 1 ] , parts [ 2 ] ) ;
53- }
5446 }
5547 }
5648 return false ;
@@ -89,64 +81,33 @@ function decrypt_content_from_bundle(key, ciphertext_bundle) {
8981} ;
9082
9183{ % if remember_password - % }
92- /* Set key:value in sessionStorage/localStorage */
93- function setItem ( key , value ) {
84+ /* Save decrypted keystore to sessionStorage/localStorage */
85+ function setKeys ( keys_from_keystore ) {
86+ for ( const id in keys_from_keystore ) {
9487 { % if session_storage - % }
95- sessionStorage . setItem ( 'encryptcontent_' + encodeURIComponent ( key ) , encodeURIComponent ( value ) )
88+ sessionStorage . setItem ( id , keys_from_keystore [ id ] ) ;
9689 { % - else % }
97- localStorage . setItem ( 'encryptcontent_' + encodeURIComponent ( key ) , encodeURIComponent ( value ) )
90+ localStorage . setItem ( id , keys_from_keystore [ id ] ) ;
9891 { % - endif % }
92+ }
9993} ;
10094
10195/* Delete key with specific name in sessionStorage/localStorage */
10296function delItemName ( key ) {
10397 { % if session_storage - % }
104- sessionStorage . removeItem ( 'encryptcontent_' + encodeURIComponent ( key ) ) ;
98+ sessionStorage . removeItem ( key ) ;
10599 { % - else % }
106- localStorage . removeItem ( 'encryptcontent_' + encodeURIComponent ( key ) ) ;
100+ localStorage . removeItem ( key ) ;
107101 { % - endif % }
108102} ;
109103
110- function getItem ( key ) {
104+ function getItemName ( key ) {
111105 { % if session_storage - % }
112106 return sessionStorage . getItem ( key ) ;
113107 { % - else % }
114108 return localStorage . getItem ( key ) ;
115109 { % - endif % }
116110} ;
117-
118- /* Get key:value from sessionStorage/localStorage */
119- function getItemFallback ( key ) {
120- let ret = {
121- value : null ,
122- fallback : false
123- } ;
124- let item_value ;
125- item_value = getItem ( 'encryptcontent_' + encodeURIComponent ( key ) ) ;
126- if ( ! item_value ) {
127- ret . fallback = true ; //fallback is set to not display a "decryption failed" message
128- // fallback one level up
129- let last_slash = key . slice ( 0 , - 1 ) . lastIndexOf ( '/' )
130- if ( last_slash !== - 1 && last_slash > 0 ) {
131- let keyup = key . substring ( 0 , last_slash + 1 ) ;
132- item_value = getItem ( 'encryptcontent_' + encodeURIComponent ( keyup ) ) ;
133- }
134- if ( ! item_value ) {
135- // fallback site_path
136- item_value = getItem ( 'encryptcontent_' + encodeURIComponent ( "{{ site_path }}" ) ) ;
137- if ( ! item_value ) {
138- // fallback global
139- item_value = getItem ( 'encryptcontent_' ) ;
140- if ( ! item_value ) {
141- //no password saved and no fallback found
142- return null ;
143- }
144- }
145- }
146- }
147- ret . value = decodeURIComponent ( item_value ) ;
148- return ret ;
149- } ;
150111{ % - endif % }
151112
152113/* Reload scripts src after decryption process */
@@ -260,14 +221,18 @@ function decrypt_somethings(key, encrypted_something) {
260221
261222/* Decrypt content of a page */
262223function decrypt_action ( password_input , encrypted_content , decrypted_content , key_from_storage , username_input ) {
263- // grab the ciphertext bundle
264- // and decrypt it
265- let key ;
224+ let key = false ;
225+ let keys_from_keystore = false ;
226+
266227 if ( key_from_storage !== false ) {
267228 key = key_from_storage ;
268229 } else {
269- key = decrypt_key_from_bundle ( password_input . value , encryptcontent_keystore , username_input ) ;
230+ keys_from_keystore = decrypt_key_from_bundle ( password_input . value , encryptcontent_keystore , username_input ) ;
231+ if ( keys_from_keystore ) {
232+ key = keys_from_keystore [ encryptcontent_id ] ;
233+ }
270234 }
235+
271236 let content = false ;
272237 if ( key ) {
273238 content = decrypt_content_from_bundle ( key , encrypted_content . innerHTML ) ;
@@ -294,32 +259,32 @@ function decrypt_action(password_input, encrypted_content, decrypted_content, ke
294259 reload_js ( reload_scripts [ i ] ) ;
295260 }
296261 { % - endif % }
297- return key
262+ if ( keys_from_keystore !== false ) {
263+ return keys_from_keystore
264+ } else {
265+ return key
266+ }
298267 } else {
299268 return false
300269 }
301270} ;
302271
303- function decryptor_reaction ( key , password_input , fallback_used , set_global , save_cookie ) {
304- let location_path ;
305- if ( set_global ) {
306- location_path = "/{{ site_path}}" ; //global password decrypts at "/{site_path}"
307- } else {
308- location_path = encryptcontent_path ;
309- }
310- if ( key ) {
311- { % if remember_password - % }
312- // keep password value on sessionStorage/localStorage with specific path (relative)
313- if ( set_global ) {
314- setItem ( "" , key ) ;
315- }
316- else if ( ! fallback_used ) {
317- setItem ( location_path , key ) ;
272+ function decryptor_reaction ( key_or_keys , password_input , fallback_used = false ) {
273+
274+ if ( key_or_keys ) {
275+ let key ;
276+ if ( typeof key_or_keys === "object" ) {
277+ key = key_or_keys [ encryptcontent_id ] ;
278+ { % if remember_password - % }
279+ setKeys ( key_or_keys ) ;
280+ { % - endif % }
281+ } else {
282+ key = key_or_keys ;
318283 }
319- { % - endif % }
284+
320285 // continue to decrypt others parts
321286 { % if experimental - % }
322- decrypt_search ( key , location_path ) ;
287+ // decrypt_search(key, location_path);
323288 { % - endif % }
324289 { % if encrypted_something - % }
325290 let encrypted_something = { { encrypted_something } } ;
@@ -336,18 +301,18 @@ function decryptor_reaction(key, password_input, fallback_used, set_global, save
336301 { % - endif % }
337302 } else {
338303 // remove item on sessionStorage/localStorage if decryption process fail (Invalid item)
339- if ( ! fallback_used || set_global ) {
304+ if ( ! fallback_used ) {
340305 if ( ! encryptcontent_obfuscate ) {
341306 // create HTML element for the inform message
342307 let mkdocs_decrypt_msg = document . getElementById ( 'mkdocs-decrypt-msg' ) ;
343308 mkdocs_decrypt_msg . textContent = decryption_failure_message ;
344309 password_input . value = '' ;
345310 password_input . focus ( ) ;
346311 }
347- { % if remember_password - % }
348- delItemName ( location_path ) ;
349- { % - endif % }
350312 }
313+ { % if remember_password - % }
314+ delItemName ( encryptcontent_id ) ;
315+ { % - endif % }
351316 }
352317}
353318
@@ -363,12 +328,12 @@ function init_decryptor() {
363328 var decrypted_content = document . getElementById ( 'mkdocs-decrypted-content' ) ;
364329 { % if remember_password - % }
365330 /* If remember_password is set, try to use sessionStorage/localstorage item to decrypt content when page is loaded */
366- let key_from_storage = getItemFallback ( encryptcontent_path ) ;
331+ let key_from_storage = getItemName ( encryptcontent_id ) ;
367332 if ( key_from_storage ) {
368333 let content_decrypted = decrypt_action (
369- password_input , encrypted_content , decrypted_content , key_from_storage . value , username_input
334+ password_input , encrypted_content , decrypted_content , key_from_storage , username_input
370335 ) ;
371- decryptor_reaction ( content_decrypted , password_input , key_from_storage . fallback , false , false ) ; //dont save cookie as it was loaded from cookie
336+ decryptor_reaction ( content_decrypted , password_input , true ) ;
372337 }
373338 { % - endif % }
374339 { % if password_button - % }
@@ -380,22 +345,18 @@ function init_decryptor() {
380345 let content_decrypted = decrypt_action (
381346 password_input , encrypted_content , decrypted_content , false , username_input
382347 ) ;
383- decryptor_reaction ( content_decrypted , password_input , false , false , true ) ; //no fallback, save cookie
348+ decryptor_reaction ( content_decrypted , password_input ) ;
384349 } ;
385350 }
386351 { % - endif % }
387352 /* Default, try decrypt content when key (ctrl) enter is press */
388353 password_input . addEventListener ( 'keypress' , function ( event ) {
389- let set_global = false ;
390354 if ( event . key === "Enter" ) {
391- if ( event . ctrlKey ) {
392- set_global = true ;
393- }
394355 event . preventDefault ( ) ;
395356 let content_decrypted = decrypt_action (
396357 password_input , encrypted_content , decrypted_content , false , username_input
397358 ) ;
398- decryptor_reaction ( content_decrypted , password_input , false , set_global , true ) ; //no fallback, set_global?, save cookie
359+ decryptor_reaction ( content_decrypted , password_input ) ;
399360 }
400361 } ) ;
401362}
0 commit comments