| 1 |
#!/usr/bin/env python |
| 2 |
"""Digest authentication example |
| 3 |
|
| 4 |
This example demonstrates how to protect Flask endpoints with digest |
| 5 |
authentication. |
| 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 HTTPDigestAuth |
| 13 |
|
| 14 |
app = Flask(__name__) |
| 15 |
app.secret_key = 'this-is-a-secret-key' |
| 16 |
auth = HTTPDigestAuth(qop='auth') |
| 17 |
|
| 18 |
users = { |
| 19 |
"john": "hello", |
| 20 |
"susan": "bye", |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
@auth.get_password |
| 25 |
def get_password(username): |
| 26 |
return users.get(username) |
| 27 |
|
| 28 |
|
| 29 |
@app.route('/') |
| 30 |
@auth.login_required |
| 31 |
def index(): |
| 32 |
return "Hello, %s!" % auth.current_user() |
| 33 |
|
| 34 |
|
| 35 |
if __name__ == '__main__': |
| 36 |
app.run(debug=True, host='0.0.0.0') |