Secure Cloud Functions With Http Basic Authentication (HTTPBasicAuth) - Python

Apr 21, 2019

requirements.txt

# https://pypi.org/project/Flask-HTTPAuth/
Flask-HTTPAuth==3.3.0

main.py

from flask_httpauth import HTTPBasicAuthauth = HTTPBasicAuth()users = {    "username": "password"}@auth.get_passworddef get_password(username):    return users.get(username)@auth.login_requireddef test_basic_auth(request):    return f"{auth.username()}, you may pass!"

or using a more versatile verification method

from werkzeug.security import generate_password_hash, check_password_hashusers = {    "username": generate_password_hash("password"),    "another": generate_password_hash("secret")}@auth.verify_passworddef verify_password(username, password):    if username in users:        return check_password_hash(users.get(username), password)    return False

Test

curl -u username:password [URL]/test_basic_auth

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.