Now we will perform the following tasks:
- Configure the
Flask-HTTPAuth
extension to work with ourUser
model to verify passwords and set the authenticated user associated with a request. - Declare a custom function that the
Flask-HTTPAuth
extension will use as a callback to verify a password. - Create a new base class for our resources that will require authentication.
Open the views.py
file within the service
folder and add the following code after the last line that uses the import
statement and before the lines that declare the Blueprint
instance named service_blueprint
. The code file for the sample is included in the restful_python_2_03_02
folder, in the Flask01/service/views.py
file:
from flask_httpauth import HTTPBasicAuth from flask import g from models import User, UserSchema auth = HTTPBasicAuth() @auth.verify_password def verify_user_password(name, password): user = User.query.filter_by(name=name).first() if not user or not user...