Basic authentication with an encrypted password (.htpasswd) in bottle with python

Store the user name and password in ".htpasswd", and assume that the password is already encrypted. This time, Basic authentication will be used when accessing the management screen "http: // hogehoge / manage". For the description of "@auth_basic (check)", refer to http://qiita.com/yubessy/items/33789eccb35b659b0b4e. The new one is SAH1 encryption.

Below, the folder structure

/
├── app
│   └── app.py
├── config
│   └─── .htpasswd
└── views
    └── manage.j2

Generate a password in advance and store it in .htpasswd. The options of htpasswd are c: new file creation, s: sha1 (encryption format). The user name and password are saved in the above format (user name + ":" + "encrypted format" + encrypted password).

$ htpasswd -csb .htpasswd user password
$ cat .htpasswd 
user:{SHA}H9WJLeNwKEfNwYPyPeiv9nuZoxk=

In python, the above is read from the file and compared with the value input by the user, so it is necessary to assemble a character string in the above format with the value input by the user.

The information read from the file did not match in the comparison as it was, probably because there was a line feed code, so trim processing is performed by strip ().

from bottle import route, run, request, auth_basic
import hashlib
import base64
from bottle imp

#Add template path
TEMPLATE_PATH.append("../views")
ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) #Absolute path to the folder where this script is located

def check(username, password):
    #Reading server-side settings
    f = open(ROOT_PATH + '/../config/.htpasswd', 'r')
    auth_check_word = f.readline() #Returns one line of data in a file
    f.close()
    #Read login information
    hs= hashlib.sha1()
    hs.update(password.encode("utf-8"))
    login_word = username + ":{SHA}" + str(base64.b64encode(hs.digest()).decode("utf-8"))
    return auth_check_word.strip() ==login_word.strip() 


@route("/manage")
@auth_basic(check)
def manage():
    return template('manage.j2')

To be honest, I don't check the best practices around user authentication. This time, we only needed one management screen for the administrator to use, so we have addressed it above. How do other people implement it? Do you have Active Directory etc.?

Recommended Posts

Basic authentication with an encrypted password (.htpasswd) in bottle with python
BASIC authentication with Python bottle
Send HTTP with Basic authentication header in Python
Scraping with Selenium in Python (Basic)
Password generation in texto with python
Addictive point when going through http proxy with basic authentication in python
Achieve Basic Authentication with CloudFront Lambda @ Edge with Python 3.8
Experiment with NIST 800-63B password rules in Python
Create an image with characters in python (Japanese)
Send an email with Excel attached in Python
Basic sorting in Python
How to create an image uploader in Bottle (Python)
Scraping with selenium in Python
Refactoring Learned in Python (Basic)
Working with LibreOffice in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Creating an egg with python
[Python] Using OpenCV with Python (Basic)
Working with sounds in Python
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Create an authentication feature with django-allauth and CustomUser in Django
[Python] [SQLite3] Operate SQLite with Python (Basic)
Easy password box in Python
Password management with python: keyring
Create an application that just searches using the Google Custom Search API with Python 3.3.1 in Bottle
Log in to Raspberry PI with ssh without password (key authentication)
How to create a heatmap with an arbitrary domain in Python
Firebase Authentication token issuance in Python and token verification with Fast API
Number recognition in images with Python
Cut out an image with python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Implemented file download with Python + Bottle
CSS parsing with cssutils in Python
Write an HTTP / 2 server in Python
[Python] Use Basic/Digest authentication with Flask
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Create an Excel file with Python3
[Python] Basic knowledge used in AtCoder
Develop an investment algorithm in Python 2
Decrypt files encrypted with OpenSSL with Python 3
I sent an SMS with Python
Use rospy with virtualenv in Python3
Basic authentication and Digest authentication with Flask
Use Python in pyenv with NeoVim
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
Heatmap with Dendrogram in Python + matplotlib
[Python] Generate a password with Slackbot
Read files in parallel with Python
Use OpenCV with Python 3 in Window
Draw an illustration with Python + OpenCV
Until dealing with python in Atom
Python in is also an operator
[Python] Send an email with outlook
Get started with Python in Blender
An alternative to `pause` in Python