__init__.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright 2009 Canonical Ltd.
  2. # This file is part of lazr.restfulclient.
  3. #
  4. # lazr.restfulclient is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Lesser General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # lazr.restfulclient is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with lazr.restfulclient. If not, see
  16. # <http://www.gnu.org/licenses/>.
  17. """Classes to authorize lazr.restfulclient with various web services.
  18. This module includes an authorizer classes for HTTP Basic Auth,
  19. as well as a base-class authorizer that does nothing.
  20. A set of classes for authorizing with OAuth is located in the 'oauth'
  21. module.
  22. """
  23. __metaclass__ = type
  24. __all__ = [
  25. "BasicHttpAuthorizer",
  26. "HttpAuthorizer",
  27. ]
  28. import base64
  29. class HttpAuthorizer:
  30. """Handles authentication for HTTP requests.
  31. There are two ways to authenticate.
  32. The authorize_session() method is called once when the client is
  33. initialized. This works for authentication methods like Basic
  34. Auth. The authorize_request is called for every HTTP request,
  35. which is useful for authentication methods like Digest and OAuth.
  36. The base class is a null authorizer which does not perform any
  37. authentication at all.
  38. """
  39. def authorizeSession(self, client):
  40. """Set up credentials for the entire session."""
  41. pass
  42. def authorizeRequest(self, absolute_uri, method, body, headers):
  43. """Set up credentials for a single request.
  44. This probably involves setting the Authentication header.
  45. """
  46. pass
  47. @property
  48. def user_agent_params(self):
  49. """Any parameters necessary to identify this user agent.
  50. By default this is an empty dict (because authentication
  51. details don't contain any information about the application
  52. making the request), but when a resource is protected by
  53. OAuth, the OAuth consumer name is part of the user agent.
  54. """
  55. return {}
  56. class BasicHttpAuthorizer(HttpAuthorizer):
  57. """Handles authentication for services that use HTTP Basic Auth."""
  58. def __init__(self, username, password):
  59. """Constructor.
  60. :param username: User to send as authorization for all requests.
  61. :param password: Password to send as authorization for all requests.
  62. """
  63. self.username = username
  64. self.password = password
  65. def authorizeRequest(self, absolute_uri, method, body, headers):
  66. """Set up credentials for a single request.
  67. This sets the authorization header with the username/password.
  68. """
  69. headers["authorization"] = (
  70. "Basic "
  71. + base64.b64encode(
  72. "%s:%s" % (self.username, self.password)
  73. ).strip()
  74. )
  75. def authorizeSession(self, client):
  76. client.add_credentials(self.username, self.password)