项目作者: miguelgrinberg

项目描述 :
Simple extension that provides Basic, Digest and Token HTTP authentication for Flask routes
高级语言: Python
项目地址: git://github.com/miguelgrinberg/Flask-HTTPAuth.git
创建时间: 2013-05-18T05:10:52Z
项目社区:https://github.com/miguelgrinberg/Flask-HTTPAuth

开源协议:MIT License

下载


Flask-HTTPAuth

Build status codecov

Simple extension that provides Basic, Digest and Token HTTP authentication for Flask routes.

Installation

The easiest way to install this is through pip.

  1. pip install Flask-HTTPAuth

Basic authentication example

  1. from flask import Flask
  2. from flask_httpauth import HTTPBasicAuth
  3. from werkzeug.security import generate_password_hash, check_password_hash
  4. app = Flask(__name__)
  5. auth = HTTPBasicAuth()
  6. users = {
  7. "john": generate_password_hash("hello"),
  8. "susan": generate_password_hash("bye")
  9. }
  10. @auth.verify_password
  11. def verify_password(username, password):
  12. if username in users and \
  13. check_password_hash(users.get(username), password):
  14. return username
  15. @app.route('/')
  16. @auth.login_required
  17. def index():
  18. return "Hello, %s!" % auth.current_user()
  19. if __name__ == '__main__':
  20. app.run()

Note: See the documentation for more complex examples that involve password hashing and custom verification callbacks.

Digest authentication example

  1. from flask import Flask
  2. from flask_httpauth import HTTPDigestAuth
  3. app = Flask(__name__)
  4. app.config['SECRET_KEY'] = 'secret key here'
  5. auth = HTTPDigestAuth()
  6. users = {
  7. "john": "hello",
  8. "susan": "bye"
  9. }
  10. @auth.get_password
  11. def get_pw(username):
  12. if username in users:
  13. return users.get(username)
  14. return None
  15. @app.route('/')
  16. @auth.login_required
  17. def index():
  18. return "Hello, %s!" % auth.username()
  19. if __name__ == '__main__':
  20. app.run()

Resources