auth.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. from ..http import parse_authorization_header
  2. from ..http import parse_www_authenticate_header
  3. from ..utils import cached_property
  4. class AuthorizationMixin(object):
  5. """Adds an :attr:`authorization` property that represents the parsed
  6. value of the `Authorization` header as
  7. :class:`~werkzeug.datastructures.Authorization` object.
  8. """
  9. @cached_property
  10. def authorization(self):
  11. """The `Authorization` object in parsed form."""
  12. header = self.environ.get("HTTP_AUTHORIZATION")
  13. return parse_authorization_header(header)
  14. class WWWAuthenticateMixin(object):
  15. """Adds a :attr:`www_authenticate` property to a response object."""
  16. @property
  17. def www_authenticate(self):
  18. """The `WWW-Authenticate` header in a parsed form."""
  19. def on_update(www_auth):
  20. if not www_auth and "www-authenticate" in self.headers:
  21. del self.headers["www-authenticate"]
  22. elif www_auth:
  23. self.headers["WWW-Authenticate"] = www_auth.to_header()
  24. header = self.headers.get("www-authenticate")
  25. return parse_www_authenticate_header(header, on_update)