utils.py 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. from functools import wraps
  2. from flask import request, Response
  3. from core.ConfigurationManager import SettingLoader
  4. def check_auth(username, password):
  5. """This function is called to check if a username /
  6. password combination is valid.
  7. """
  8. settings = SettingLoader.get_settings()
  9. return username == settings.rest_api.login and password == settings.rest_api.password
  10. def authenticate():
  11. """Sends a 401 response that enables basic auth"""
  12. return Response(
  13. 'Could not verify your access level for that URL.\n'
  14. 'You have to login with proper credentials', 401,
  15. {'WWW-Authenticate': 'Basic realm="Login Required"'})
  16. def requires_auth(f):
  17. @wraps(f)
  18. def decorated(*args, **kwargs):
  19. settings = SettingLoader.get_settings()
  20. if settings.rest_api.password_protected:
  21. auth = request.authorization
  22. if not auth or not check_auth(auth.username, auth.password):
  23. return authenticate()
  24. return f(*args, **kwargs)
  25. return decorated