Debian Codegraph @

examples/basic_auth.py

raw
1 #!/usr/bin/env python
2 """Basic authentication example
3
4 This example demonstrates how to protect Flask endpoints with basic
5 authentication, using secure hashed passwords.
6
7 After running this example, visit http://localhost:5000 in your browser. To
8 gain access, you can use (username=john, password=hello) or
9 (username=susan, password=bye).
10 """
11 from flask import Flask
12 from flask_httpauth import HTTPBasicAuth
13 from werkzeug.security import generate_password_hash, check_password_hash
14
15 app = Flask(__name__)
16 auth = HTTPBasicAuth()
17
18 users = {
19 "john": generate_password_hash("hello"),
20 "susan": generate_password_hash("bye")
21 }
22
23
24 @auth.verify_password
25 def verify_password(username, password):
26 if username in users and check_password_hash(users.get(username),
27 password):
28 return username
29
30
31 @app.route('/')
32 @auth.login_required
33 def index():
34 return "Hello, %s!" % auth.current_user()
35
36
37 if __name__ == '__main__':
38 app.run(debug=True, host='0.0.0.0')