Skip to content

Instantly share code, notes, and snippets.

@joelverhagen
Created September 13, 2012 12:02
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 joelverhagen/3713895 to your computer and use it in GitHub Desktop.
Save joelverhagen/3713895 to your computer and use it in GitHub Desktop.
HTTP Basic Authorization with Flask and Flask-Login
# Flask, http://flask.pocoo.org/
# Flask-Login, https://github.com/maxcountryman/flask-login
@app.before_request
def basic_authorize():
auth = request.authorization
if not current_user.is_active() and auth and auth.type == 'basic':
# change to your specific call to get the user based off username and password
# you're hopefully bcrypting your password, so fetch by username then check password :)
user = User.get_login_user(unicode(auth.username), unicode(auth.password))
if user is not None:
login_user(user)
else:
return 'The provided username and password are invalid.', 403
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment