Flask-HTTPAuth: how to pass an extra argument to a function decorated with @auth.verify_password?

2024/11/16 0:40:10

Here's a small Flask app authenticated with Flask-HTTPAuth.

How to pass an argument (such as authentication on/off flag, or verbosity level / debug on/off flag) to a function (such as authenticate below) decorated with @auth.verify_password (without raising an error)?

Current code:


auth = HTTPBasicAuth()authorized_users_dict = [..]# [..]@auth.verify_password
def authenticate(username, password):    if username in authorized_users_dict:if check_password_hash(pwhash=authorized_users_dict[username], password=password):return True# [..]# [..]@auth.login_required()# [..]
Answer

I don't think passing this dictionary as an argument is the best choice. I assume this dictionary is fixed, so there is actually no technical reason to pass it as an argument every single time there is an incoming request.

But, if you feel strongly about it, one way to do it is with the partial function from the Python standard library. Something like this:

from functools import partialauthorized_users_dict = [..]def authenticate(users_dict, username, password):    if username in users_dict:# ...auth.login_required(partial(authenticate, authorized_users_dict))
https://en.xdnf.cn/q/119595.html

Related Q&A

AttributeError: numpy.ndarray object has no attribute split

Given a text file with one DNA sequence on each line and joining these together I now want to split the string into 5 sequences (corresponding to each of the 5 rows). This is the file source: http://ww…

How do I determine if a lat/long point is within a polygon?

I have a shapefile of all the counties that make up my state. Using the shapefile (which contains geometric for the district polygons) I was able to use geopandas to plot the shapes in a figure. I have…

How to return different types of arrays?

The high level problem Im having in C# is to make a single copy of a data structure that describes a robot control network packet (Ethercat), and then to use that single data structure to extract data …

How do I pass an array of strings to a python script as an argument?

Ive written a swift app that outputs an array of strings. I would like to import this array into a python script for further processing into an excel file via xlsxwriter, I would like to do this as an …

Match values of different dataframes

This dataframe is the principal with the original tweets. "original_ds_.csv" id tweet --------------------------------------------- 78 "onetoone"…

EOF while parsing

def main():NUMBER_OF_DAYS = 10NUMBER_OF_HOURS = 24data = []for i in range(NUMBER_OF_DAYS):data.append([])for j in range(NUMBER_OF_HOURS):data[i].append([])data[i][j].append(0)data[i][j].append(0)for k …

Why is bool(x) where x is any integer equal to True

I expected bool(1) to equate to True using Python - it does - then I expected other integers to error when converted to bool but that doesnt seem to be the case:>>> x=23 #<-- replace with a…

Getting TypeError while fetching value from table using Python and Django

I am getting error while fetching value from table using Python and Django. The error is below:Exception Type: TypeError Exception Value: not all arguments converted during string formattingMy code…

ValueError: The view **** didnt return an HttpResponse object. It returned None instead

Im using Django forms to handle user input for some point on my Django app. but it keeps showing this error whenever the user tries to submit the form. ValueError: The view *my view name goes here* di…

Game Development in Python, ruby or LUA? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…