├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── docs ├── index.md └── mkdocs.md ├── mkdocs.yml ├── requirements.txt └── theme_addons ├── base.html ├── basic_auth.inc ├── basic_auth.php └── do_php_thing.sh /.gitignore: -------------------------------------------------------------------------------- 1 | site/* 2 | .pyenv/* 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2020, Osvaldo 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: build 2 | 3 | all: help 4 | 5 | full: clean build serve 6 | 7 | clean: ## rm site/ 8 | rm -fr site/ 9 | mkdir site 10 | 11 | build: ## build files 12 | mkdocs build --clean 13 | theme_addons/do_php_thing.sh 14 | 15 | serve: ## publish site on :8080 16 | #mkdocs serve 17 | cd site && php -S 0:8080 18 | 19 | gpush: ## git push HEAD to origin 20 | @git push -u origin HEAD 21 | gpushf: ##git force push HEAD to origin 22 | @git push -u origin HEAD -f 23 | gupdate: ## update local branch from master 24 | @git fetch origin master; git rebase origin/master 25 | gam: ## add new changes to previous commit 26 | @git commit -a --amend 27 | 28 | .PHONY: help 29 | help: 30 | @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' 31 | 32 | 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mkdocs basic authentication 2 | 3 | An static site generador like [mkdocs](http://www.mkdocs.org/) is great for writing documentation. Sometimes the information is not secret but we don't want it available to general public. Here's a workaround for such scenarios. 4 | 5 | This solution adds basic authentication the pages generated by mkdocs. It renames static html files as php files and adds php code which provides the basic authentication functionality. 6 | 7 | # tl;dr 8 | 9 | * setup credentials in the `mkdocs.yml` file. 10 | * add php code to `base.html` template file (if other than the readthedocs theme). 11 | * write some docs. 12 | * run `make full` and check the results. 13 | 14 | # configuration 15 | 16 | Mkdocs allows for customizing the current theme by using the `custom_dir` variable in the `mkdocs.yml` configuration file. 17 | 18 | > The `theme.custom_dir` configuration value is used in combination with an existing theme, the `theme.custom_dir` can be used to replace only specific parts of a built-in theme. 19 | 20 | From the [docs](https://www.mkdocs.org/user-guide/custom-themes/). 21 | 22 | ``` 23 | theme: 24 | name: readthedocs 25 | custom_dir: 'theme_addons/' 26 | ``` 27 | 28 | The user/password credentials are also defined in the `mkdocs.yml` file. 29 | 30 | ``` 31 | extra: 32 | username: "admin" 33 | password: "secret" 34 | ``` 35 | 36 | # usage 37 | 38 | ``` 39 | git clone git@github.com:otsuarez/mkdocs_auth.git 40 | cd mkdocs_auth 41 | pip install -r requirements.txt 42 | make full 43 | ``` 44 | 45 | ## using virtualenv 46 | 47 | ``` 48 | # install virtualenv 49 | git clone git@github.com:otsuarez/mkdocs_auth.git 50 | cd mkdocs_auth 51 | virtualenv --python=/usr/bin/python2.7 .pyenv 52 | source .pyenv/bin/activate 53 | pip install -r requirements.txt 54 | make full 55 | ``` 56 | 57 | Open in your browser [http://localhost:8080/](http://localhost:8080/). 58 | 59 | ## using a container 60 | 61 | ``` 62 | docker run -p 8080:8080 --rm -it ubuntu:bionic bin/bash 63 | apt update -y 64 | apt install -y git build-essential python-pip php 65 | cd root 66 | git clone https://github.com/otsuarez/mkdocs_auth.git 67 | cd mkdocs_auth 68 | pip install -r requirements.txt 69 | make full 70 | ``` 71 | 72 | # other themes 73 | 74 | The `readthedocs` theme is being used, for another theme, make sure to have the right `base.html` template file with the php code added. 75 | 76 | ``` 77 | cp $(theme="$(egrep "^theme" mkdocs.yml| cut -f2 -d:)" ; locate base.html | grep $theme) theme_addons/ 78 | ``` 79 | 80 | If using a virtualenv: 81 | 82 | ``` 83 | cp $(theme="$(egrep "^theme" mkdocs.yml| cut -f2 -d:)" ; find .pyenv -name base.html | grep $theme) theme_addons/ 84 | ``` 85 | 86 | Then add the php code to the `base.html` file. 87 | 88 | ``` 89 | tmp="$(mktemp)" && cat theme_addons/basic_auth.inc theme_addons/base.html >"$tmp" && mv "$tmp" theme_addons/base.html 90 | ``` 91 | 92 | Check the `Makefile` file for reference on the actual commands and scripts being executed. 93 | 94 | 95 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | # demo 2 | 3 | empty home page 4 | -------------------------------------------------------------------------------- /docs/mkdocs.md: -------------------------------------------------------------------------------- 1 | # Welcome to MkDocs 2 | 3 | For full documentation visit [mkdocs.org](http://mkdocs.org). 4 | 5 | ## Commands 6 | 7 | * `mkdocs new [dir-name]` - Create a new project. 8 | * `mkdocs serve` - Start the live-reloading docs server. 9 | * `mkdocs build` - Build the documentation site. 10 | * `mkdocs help` - Print this help message. 11 | 12 | ## Project layout 13 | 14 | mkdocs.yml # The configuration file. 15 | docs/ 16 | index.md # The documentation homepage. 17 | ... # Other markdown pages, images and other files. 18 | -------------------------------------------------------------------------------- /mkdocs.yml: -------------------------------------------------------------------------------- 1 | # Project information 2 | site_name: Mkdocs_Auth 3 | site_description: mkdocs basic authentication example 4 | site_author: ACME 5 | 6 | # Documentation and theme 7 | theme: 8 | name: readthedocs 9 | custom_dir: 'theme_addons/' 10 | 11 | # Page tree 12 | pages: 13 | - Home: index.md 14 | - Mkdocs: mkdocs.md 15 | 16 | extra: 17 | username: "admin" 18 | password: "secret" 19 | 20 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | backports-abc==0.5 2 | click==6.7 3 | futures==3.2.0 4 | Jinja2>=2.10.1 5 | livereload==2.5.2 6 | Markdown==2.6.11 7 | MarkupSafe==1.0 8 | mkdocs==1.0 9 | PyYAML==4.2b1 10 | singledispatch==3.4.0.3 11 | six==1.11.0 12 | tornado==5.1 13 | -------------------------------------------------------------------------------- /theme_addons/base.html: -------------------------------------------------------------------------------- 1 | '{{ config.extra.password }}'); 3 | require_once('{{ base_url }}/basic_auth.php'); 4 | ?> 5 | 6 | 7 | 8 |
9 | {%- block site_meta %} 10 | 11 | 12 | 13 | {% if page and page.is_homepage %}{% endif %} 14 | {% if config.site_author %}{% endif %} 15 | {% if config.site_favicon %} 16 | {% else %}{% endif %} 17 | {%- endblock %} 18 | 19 | {%- block htmltitle %} 20 |