[PYTHON] Basic authentication and Digest authentication with Flask

It's very easy to do with the Flask extension Flask-HTTPAuth.

Installation

$ pip install flask-httpauth

Basic authentication

  1. from flask_httpauth import HTTPBasicAuth
  2. Create a ʻauth instance with ʻauth = HTTPBasicAuth ()
  3. Create a function to check the password: Decorate with @ auth.get_password
  4. Decorate the URL you want to authenticate with @ auth.login_required

app.py


from flask import Flask
from flask_httpauth import HTTPBasicAuth

app = Flask(__name__)
auth = HTTPBasicAuth()

users = {
    "john": "hello",
    "susan": "bye"
}

@auth.get_password
def get_pw(username):
    if username in users:
        return users.get(username)
    return None

@app.route('/')
@auth.login_required
def index():
    return "Hello, %s!" % auth.username()

if __name__ == '__main__':
    app.run()

Digest authentication

MD5 hash the username and password before sending them to the server. Measures against eavesdropping and tampering that could not be prevented by Basic authentication.

Just change HTTPBasicAuth to HTTPDigestAuth.

app.py


from flask import Flask
from flask_httpauth import HTTPDigestAuth

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret key here'
auth = HTTPDigestAuth()

users = {
    "john": "hello",
    "susan": "bye"
}

@auth.get_password
def get_pw(username):
    if username in users:
        return users.get(username)
    return None

@app.route('/')
@auth.login_required
def index():
    return "Hello, %s!" % auth.username()

if __name__ == '__main__':
    app.run()

ToDo: Find out about pathlib, ʻits dangerous`

Recommended Posts

Basic authentication and Digest authentication with Flask
Flask Basic authentication
BASIC authentication with Python bottle
Authentication process with gRPC and Firebase Authentication
[Python] Use Basic/Digest authentication with Flask
Passwordless authentication with RDS and IAM (Python)
POST variously with Python and receive with Flask
Flask basic memo
Achieve Basic Authentication with CloudFront Lambda @ Edge with Python 3.8
Try slack OAuth authentication with flask (Slack API V2)
Launch a web server with Python and Flask
Send HTTP with Basic authentication header in Python
Flow to complete Slack authentication with Flask (Python)
Development digest with Django
Easy machine learning with scikit-learn and flask ✕ Web app
Parse and visualize JSON (Web application ⑤ with Python + Flask)
IP restrictions with Flask
POST the image with json and receive it with flask
Hello world with flask
With and without WSGI
Programming with Python Flask
Notes and reference books when creating Web services with Flask
Easily create authentication, user management, and multilingual systems with Flask-AppBuilder
Basic authentication with an encrypted password (.htpasswd) in bottle with python
Twitter authentication using Flask and React is very forcible using WebSocket
BASIC and C and assembler speed comparison and optimization play with IchigoJam
Create an authentication feature with django-allauth and CustomUser in Django
Easy deep learning web app with NNC and Python + Flask
With me, cp, and Subprocess
Encryption and decryption with Python
Deploy Flask with ZEIT Now
Working with tkinter and mouse
Python and hardware-Using RS232C with Python-
Touch Flask + run with Heroku
Hello World with Flask + Hamlish
Unit test flask with pytest
API with Flask + uWSGI + Nginx
SNS Flask (Ajax) made with Flask
[Python] Using OpenCV with Python (Basic)
Web application development with Flask
Python installation and basic grammar
Connection between flask and sqlite3
Basic statistics and Gaussian distribution
Dictionary attack on basic authentication
Send and receive Flask images
View flask coverage with pytest-cov
React and Flask to GCP
Super-resolution with SRGAN and ESRGAN
[Python] [SQLite3] Operate SQLite with Python (Basic)
group_by with sqlalchemy and sum
python with pyenv and venv
Web application with Python + Flask ② ③
File upload with Flask + jQuery
With me, NER and Flair
Python (Python 3.7.7) installation and basic grammar
Web application with Python + Flask ④
Works with Python and R
I made a Nyanko tweet form with Python, Flask and Heroku
Firebase Authentication token issuance in Python and token verification with Fast API