Advertisement
Guest User

Untitled

a guest
Jan 25th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. from functools import wraps
  4. from flask import request, Response, Flask
  5.  
  6.  
  7. def check_auth(username, password):
  8. """This function is called to check if a username /
  9. password combination is valid.
  10. """
  11. return username == 'admin' and password == 'secret'
  12.  
  13.  
  14. def authenticate():
  15. """Sends a 401 response that enables basic auth"""
  16. return Response(
  17. 'Could not verify your access level for that URL.\n'
  18. 'You have to login with proper credentials', 401,
  19. {'WWW-Authenticate': 'Basic realm="Login Required"'})
  20.  
  21.  
  22. def requires_auth(f):
  23. @wraps(f)
  24. def decorated(*args, **kwargs):
  25. auth = request.authorization
  26. if not auth or not check_auth(auth.username, auth.password):
  27. return authenticate()
  28. return f(*args, **kwargs)
  29. return decorated
  30.  
  31.  
  32. app = Flask(__name__)
  33.  
  34.  
  35. @app.route('/secret-page')
  36. @requires_auth
  37. def secret_page():
  38. return 'secret_page'
  39.  
  40.  
  41. if __name__ == '__main__':
  42. app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement