Advertisement
Guest User

Untitled

a guest
Oct 9th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. from functools import wraps
  2.  
  3. from flask import Flask, Response, request
  4. from flask_babel import Babel
  5.  
  6. app = Flask(__name__)
  7. babel = Babel(app)
  8.  
  9. app.config.from_object('config')
  10.  
  11.  
  12. def check_auth(username, password):
  13. return username == 'admin' and password == 'secret'
  14.  
  15.  
  16. def authenticate():
  17. return Response(
  18. 'Could not verify your access level for that URL.\n'
  19. 'You have to login with proper credentials', 401,
  20. {'WWW-Authenticate': 'Basic realm="Login Required"'})
  21.  
  22.  
  23. def requires_auth(f):
  24. @wraps(f)
  25. def decorated(*args, **kwargs):
  26. auth = request.authorization
  27. if not auth or not check_auth(auth.username, auth.password):
  28. return authenticate()
  29. return f(*args, **kwargs)
  30.  
  31. return decorated
  32.  
  33.  
  34. @app.route('/admin', methods=['GET', 'POST'])
  35. @requires_auth
  36. def admin():
  37. return 'admin'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement