Skip to content

Instantly share code, notes, and snippets.

@mattstibbs
Created March 14, 2017 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattstibbs/6c24aad80885bfbf8249f00cae05897f to your computer and use it in GitHub Desktop.
Save mattstibbs/6c24aad80885bfbf8249f00cae05897f to your computer and use it in GitHub Desktop.
A sample basic auth module for Flask
import config as config
from flask import Response, request
from functools import wraps
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
result = (username == config.API_USER and password == config.API_PASS)
return result
def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment