Skip to content

Commit 1f358d3

Browse files
committed
Fix password specials chars + Add password button create cookie + Config refacto
1 parent f0febe2 commit 1f358d3

File tree

4 files changed

+176
-208
lines changed

4 files changed

+176
-208
lines changed

README.md

Lines changed: 79 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
11
# mkdocs-encryptcontent-plugin
22

3-
*This plugin allows you to have password protected articles and pages in MKdocs.*
3+
This plugin allows you to have password protected articles and pages in MKdocs.
44

5-
*The content is encrypted with AES-256 in Python using PyCryptodome, and decrypted in the browser with Crypto-JS.*
5+
The content is encrypted with AES-256 in Python using PyCryptodome, and decrypted in the browser with Crypto-JS.
66

77
*It has been tested in Python Python 3.5+*
88

99
**Usecase**
1010

11-
> I want to be able to protect my articles with password. And I would like this protection to be as granular as possible.
11+
> I want to be able to protect the content of the page with a password.
1212
>
13-
> It is possible to define a password to protect each article independently or a global password to encrypt all of them.
13+
> Define a password to protect each page independently or a global password to protect them all.
1414
>
15-
> If a global password exists, all articles and pages are encrypted with this password.
15+
> If a global password exists, all articles and pages are protected with this password.
1616
>
17-
> If a password is defined in an article or a page, it is always used even if a global password exists.
17+
> If a password is defined in an article or a page, it is always used even if there is a global password.
1818
>
19-
> If a password is defined as an empty character string, the page is not encrypted.
19+
> If a password is defined as an empty character string, the content is not protected.
2020
2121

22-
## Installation
22+
# Table of Contents
23+
24+
* [Installation](#installation)
25+
* [Usage](#usage)
26+
* [Global password protection](#global-password-protection)
27+
* [Customization](#extra-vars-customization)
28+
* [Features](#features)
29+
* [HighlightJS support](#highlightjs-support) *(default)*
30+
* [Arithmatex support](#arithmatex-support) *(default)*
31+
* [Tag encrypted page](#tag-encrypted-page) *(default)*
32+
* [Add button](#add-button)
33+
* [Rebember password](#rebember-password)
34+
* [Encrypt something](#encrypt-something)
35+
* [Do not encrypt search index](#do-not-encrypt-search-index)
36+
* [Reload scripts](#reload-scripts)
37+
* [Contributing](#contributing)
38+
39+
40+
# Installation
2341

2442
Install the package with pip:
2543

@@ -32,22 +50,26 @@ Install the package from source with pip:
3250
```bash
3351
cd mkdocs-encryptcontent-plugin/
3452
python3 setup.py sdist bdist_wheel
35-
pip3 install dist/mkdocs_encryptcontent_plugin-1.2.0-py3-none-any.whl
53+
pip3 install dist/mkdocs_encryptcontent_plugin-1.2.1-py3-none-any.whl
3654
```
3755

3856
Enable the plugin in your `mkdocs.yml`:
3957

4058
```yaml
4159
plugins:
60+
- search:
4261
- encryptcontent: {}
4362
```
4463
45-
You are then able to use the meta tag `password: secret_password` in your markdown files to protect them.
64+
> **Note:** If you have no `plugins` entry in your config file yet, you'll likely also want to add the `search` plugin.
65+
> MkDocs enables it by default if there is no `plugins` entry set, but now you have to enable it explicitly.
4666

47-
> **Note:** If you have no `plugins` entry in your config file yet, you'll likely also want to add the `search` plugin. MkDocs enables it by default if there is no `plugins` entry set, but now you have to enable it explicitly.
4867

68+
# Usage
4969

50-
### Using global password protection
70+
Add an meta tag `password: secret_password` in your markdown files to protect the
71+
72+
### Global password protection
5173

5274
Add `global_password: your_password` in plugin config variable, to protect by default your articles with this password
5375

@@ -57,7 +79,7 @@ plugins:
5779
global_password: 'your_password'
5880
```
5981

60-
If a password is defined in an article, it will ALWAYS overwrite the global password.
82+
If a password is defined in an article, it will **ALWAYS** overwrite the global password.
6183

6284
> **NOTE** Keep in mind that if the `password:` tag exists without value in an article, it will not be protected !
6385

@@ -75,25 +97,28 @@ plugins:
7597
encryption_info_message: 'another information message when you dont have acess !'
7698
```
7799

78-
Default prefix title is `[Protected]`
100+
Default prefix title is `[Protected]`.
79101

80-
Default summary message is `This content is protected with AES encryption.`
102+
Default summary message is `This content is protected with AES encryption.`.
81103

82-
Default password palceholder is `Provide password and press ENTER`
104+
Default password palceholder is `Provide password and press ENTER`.
83105

84-
Default decryption failure message is `Invalid password.`
106+
Default decryption failure message is `Invalid password.`.
85107

86-
Defaut encryption information message is `Contact your administrator for access to this page.`
108+
Defaut encryption information message is `Contact your administrator for access to this page.`.
87109

88110
> **NOTE** Adding a prefix to the title does not change the default navigation path !
89111

90112

91-
## Features
113+
# Features
92114

93115
### HighlightJS support
94116

95-
If your theme use HighlightJS module to improve color, set `highlightjs: true` in your `mkdocs.yml`, to enable color reloading after decryption process.
96-
117+
> **Enable by default**
118+
119+
If HighlightJS module is detected in your theme, reload colors renderer after decryption process.
120+
If HighlightJS module is not correctly detected, you can force it by adding `hljs: True` on the plugin configuration or set `hljs: False` to disable this feature.
121+
97122
When enable the following part of the template is add to force reloading decrypted content.
98123

99124
```jinja
@@ -106,33 +131,37 @@ document.getElementById("mkdocs-decrypted-content").querySelectorAll('pre code')
106131

107132
### Arithmatex support
108133

134+
> **Enable by default**
135+
109136
Related to [issue #12](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin/issues/12)
110137

111-
If Arithmatex markdown extension is set in your markdown extensions to improve math equations rendering, reload rendering after decryption process.
112-
138+
If Arithmatex markdown extension is detected in your markdown extensions, reload math equation rendering after decryption process.
139+
If Arithmatex markdown extension is not correctly detected, you can force it by adding `arithmatex: True` on the plugin configuration or set `arithmatex: False` to disable this feature.
140+
113141
When enable the following part of the template is add to force math equations rendering on decrypted content.
114142

115143
```jinja
116-
{% if arithmatex %}MathJax.typesetPromise(){% endif %}
144+
{% if arithmatex %}
145+
MathJax.typesetPromise()
146+
{% endif %}
117147
```
118148

119149
> **NOTE** It has been tested in Arithmatex `generic` mode only.
120150

121151
### Tag encrypted page
122152

153+
> **Enable by default**
154+
123155
Related to [issue #7](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin/issues/7)
124156

125-
You can add `tag_encrypted_page: True` in plugin config variable, to enable tagging of encrypted pages.
157+
This feature add an additional attribute `encrypted` with value `True` to the mkdocs type `mkdocs.nav.page` object.
126158

127-
When this feature is enabled, an additional attribute `encrypted` with value `True,` is added to the mkdocs type `mkdocs.nav.page` object.
159+
You can add `tag_encrypted_page: False` in plugin configuration, to disable tagging of encrypted pages.
160+
This feature is neccessary for others feature working correctly.
128161

129-
```yaml
130-
plugins:
131-
- encryptcontent:
132-
tag_encrypted_page: True
133-
```
162+
If you disable this feature, **do no use** [Encrypt Somethings](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin#encrypt-something),
134163

135-
It becomes possible to use this attribute in the jinja template of your theme, as a condition to perform custom modification.
164+
When enable, it becomes possible to use `encrypted` attribute in the jinja template of your theme, as a condition to perform custom modification.
136165

137166
```jinja
138167
{%- for nav_item in nav %}
@@ -142,12 +171,25 @@ It becomes possible to use this attribute in the jinja template of your theme, a
142171
{%- endfor %}
143172
```
144173

145-
For example, in your template, you can use conditional check to add custom class :
174+
For example, in your theme template, you can use conditional check to add custom class :
146175

147176
```jinja
148177
<a {% if nav_item.encrypted %}class="mkdocs-encrypted-class"{% endif %}href="{{ nav_item.url|url }}">{{ nav_item.title }}</a>
149178
```
150179

180+
### Add button
181+
182+
Add `password_button: True` in plugin config variable, to add button to the right of the password field decrypt the content.
183+
184+
Optionnally, you can add `password_button_text: 'custome_text_button'` to customize the button text.
185+
186+
```yaml
187+
plugins:
188+
- encryptcontent:
189+
password_button: True
190+
password_button_text: 'custome_text_button'
191+
```
192+
151193
### Rebember password
152194
153195
Related to [issue #6](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin/issues/6)
@@ -182,30 +224,14 @@ Your configuration should look like this when you enabled this feature :
182224
plugins:
183225
- encryptcontent:
184226
remember_password: True
185-
disable_cookie_protection: True # <-- Really a bad idea
227+
disable_cookie_protection: True # <-- Really a bad idea (Dev mode)
186228
```
187229

188-
### Add button
189-
190-
Add `password_button: True` in plugin config variable, to add button to the right of the password field.
191-
192-
When enable, it allows to decrypt the content without creating a cookie *(if remember password feature is activated)*
193-
194-
Optionnally, you can add `password_button_text: 'custome_text_button'` to customize the button text.
195-
196-
```yaml
197-
plugins:
198-
- encryptcontent:
199-
password_button: True
200-
password_button_text: 'custome_text_button'
201-
```
202-
203-
204230
### Encrypt Something
205231

206232
Related to [issue #9](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin/issues/9)
207233

208-
You **have to** enable [feature tag encrypt page](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin#tag-encrypted-page) for this feature to work properly.
234+
The [tag encrypted page feature](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin#tag-encrypted-page) **MUST** be enabled *(it's default)* for this feature to work properly.
209235

210236
Add `encrypted_something: {}` in the plugin configuration variable, to encrypt something else.
211237

@@ -232,7 +258,7 @@ When the feature is enabled, you can use any methods *(password, button, cookie)
232258

233259
By default **every child items** are encrypted and the encrypted elements have `style=display:none` to hide their content.
234260

235-
#### How to use it :exploding_head: ?! Examples
261+
#### How to use it :exploding_head: ?! Examples
236262

237263
Use the `page.encrypted` conditions to add attributes of type id or class in the HTML templates of your theme.
238264
Each attribute is identified with a unique name and is contained in an html element.
@@ -334,7 +360,7 @@ var reload_js = function(src) {
334360
```
335361

336362

337-
## Contributing
363+
# Contributing
338364

339365
From reporting a bug to submitting a pull request: every contribution is appreciated and welcome.
340366

@@ -345,5 +371,3 @@ If you want to contribute to the code of this project, please read the [Contribu
345371
[mkdocs-plugins]: https://www.mkdocs.org/dev-guide/plugins/
346372
[github-issues]: https://github.com/CoinK0in/mkdocs-encryptcontent-plugin/issues
347373
[contributing]: CONTRIBUTING.md
348-
349-
### [Contributors](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin/graphs/contributors)

encryptcontent/decrypt-form.tpl.html

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ <h1>{{ summary }}</h1>
4545
}
4646
};
4747
{% if remember_password -%}
48-
var setCookie = function(name,value,days,path) {
48+
var setCookie = function(name, value, days, path) {
4949
/* Set local cookie to store password */
5050
var expires = "";
51-
var current_path = "; path=/"
51+
var current_path = "; path=/";
5252
if (days) {
5353
var date = new Date();
54-
date.setTime(date.getTime() + (days*24*60*60*1000));
54+
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
5555
expires = "; expires=" + date.toUTCString();
5656
}
5757
if (path) {
58-
current_path = "; path=" + path
58+
current_path = "; path=" + path;
5959
}
6060
{% if disable_cookie_protection -%}
6161
encryptcontent = name + "=" + encodeURIComponent(value || "") + expires + current_path;
@@ -70,10 +70,12 @@ <h1>{{ summary }}</h1>
7070
/* Get password store in cookie */
7171
var nameEQ = name + "=";
7272
var ca = document.cookie.split(';');
73-
for(var i=0;i < ca.length;i++) {
73+
for (var i = 0; i < ca.length; i++) {
7474
var c = ca[i];
75-
while (c.charAt(0)==' ') c = c.substring(1,c.length);
76-
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
75+
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
76+
if (c.indexOf(nameEQ) == 0) {
77+
return decodeURIComponent(c.substring(nameEQ.length, c.length));
78+
}
7779
}
7880
return null;
7981
};
@@ -95,10 +97,10 @@ <h1>{{ summary }}</h1>
9597
let input = document.getElementById("mkdocs-content-password");
9698
input.setAttribute('size', input.getAttribute('placeholder').length);
9799
{% if encrypted_something -%}
98-
var encrypted_something = {{ encrypted_something }}
100+
var encrypted_something = {{ encrypted_something }};
99101
// Decrypt all others elements
100102
var decrypt_somethings = function() {
101-
var html_item = ''
103+
var html_item = '';
102104
for (const [name, tag] of Object.entries(encrypted_something)) {
103105
if (tag[1] == 'id') {
104106
html_item = [document.getElementById(name)];
@@ -125,7 +127,7 @@ <h1>{{ summary }}</h1>
125127
}
126128
}
127129
}
128-
}
130+
};
129131
{%- endif %}
130132
// Decrypt content
131133
var decrypt_action = function() {
@@ -144,23 +146,25 @@ <h1>{{ summary }}</h1>
144146
encrypted_content.parentNode.removeChild(encrypted_content);
145147
// any post processing on the decrypted content should be done here
146148
{% if arithmatex -%}
147-
MathJax.typesetPromise()
149+
MathJax.typesetPromise();
148150
{%- endif %}
149151
{% if hljs -%}
150152
document.getElementById("mkdocs-decrypted-content").querySelectorAll('pre code').forEach((block) => {
151153
hljs.highlightBlock(block);
152154
});
153155
{%- endif %}
154156
{% if reload_scripts | length > 0 -%}
155-
let reload_scripts = {{ reload_scripts }}
156-
for (i = 0; i < reload_scripts.length; i++) { reload_js(reload_scripts[i]); };
157+
let reload_scripts = {{ reload_scripts }};
158+
for (i = 0; i < reload_scripts.length; i++) {
159+
reload_js(reload_scripts[i]);
160+
}
157161
{%- endif %}
158162
} else {
159163
// create HTML element for the inform message
160164
var decrypt_msg = document.createElement('p');
161-
decrypt_msg.setAttribute('id', 'mkdocs-decrypt-msg')
162-
var node = document.createTextNode('{{ decryption_failure_message }}')
163-
decrypt_msg.appendChild(node)
165+
decrypt_msg.setAttribute('id', 'mkdocs-decrypt-msg');
166+
var node = document.createTextNode('{{ decryption_failure_message }}');
167+
decrypt_msg.appendChild(node);
164168
var mkdocs_decrypt_msg = document.getElementById('mkdocs-decrypt-msg');
165169
// clear all previous failure messages
166170
while (mkdocs_decrypt_msg.firstChild) {
@@ -170,12 +174,12 @@ <h1>{{ summary }}</h1>
170174
password_input.value = '';
171175
password_input.focus();
172176
}
173-
}
177+
};
174178
{% if remember_password -%}
175179
/* If remember_password is set, try to use cookie to decrypt content when page is loaded */
176-
var password_cookie = getCookie('encryptcontent')
180+
var password_cookie = getCookie('encryptcontent');
177181
if (password_cookie) {
178-
password_input.value = password_cookie
182+
password_input.value = password_cookie;
179183
decrypt_action();
180184
{% if encrypted_something %}
181185
decrypt_somethings();
@@ -185,6 +189,9 @@ <h1>{{ summary }}</h1>
185189
{% if password_button -%}
186190
if (decrypt_button) {
187191
decrypt_button.onclick = function(event) {
192+
{% if remember_password -%}
193+
document.cookie = setCookie("encryptcontent", password_input.value, 1, document.location.pathname);
194+
{%- endif %}
188195
event.preventDefault();
189196
decrypt_action();
190197
{% if encrypted_something %}
@@ -199,11 +206,11 @@ <h1>{{ summary }}</h1>
199206
if (event.ctrlKey) {
200207
// set password on cookie with default path=/ (Overwrite specific cookie)
201208
// this cookie can by use on all page of your site
202-
document.cookie = setCookie("encryptcontent", password_input.value, 1)
209+
document.cookie = setCookie("encryptcontent", password_input.value, 1);
203210
} else {
204211
// set password on cookie with specific path=document.location.pathname
205212
// this cookie can only be use on this specific page of your site
206-
document.cookie = setCookie("encryptcontent", password_input.value, 1, document.location.pathname)
213+
document.cookie = setCookie("encryptcontent", password_input.value, 1, document.location.pathname);
207214
}
208215
{%- endif %}
209216
event.preventDefault();

0 commit comments

Comments
 (0)