parameters.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """
  2. oauthlib.parameters
  3. ~~~~~~~~~~~~~~~~~~~
  4. This module contains methods related to `section 3.5`_ of the OAuth 1.0a spec.
  5. .. _`section 3.5`: https://tools.ietf.org/html/rfc5849#section-3.5
  6. """
  7. from urllib.parse import urlparse, urlunparse
  8. from oauthlib.common import extract_params, urlencode
  9. from . import utils
  10. # TODO: do we need filter_params now that oauth_params are handled by Request?
  11. # We can easily pass in just oauth protocol params.
  12. @utils.filter_params
  13. def prepare_headers(oauth_params, headers=None, realm=None):
  14. """**Prepare the Authorization header.**
  15. Per `section 3.5.1`_ of the spec.
  16. Protocol parameters can be transmitted using the HTTP "Authorization"
  17. header field as defined by `RFC2617`_ with the auth-scheme name set to
  18. "OAuth" (case insensitive).
  19. For example::
  20. Authorization: OAuth realm="Example",
  21. oauth_consumer_key="0685bd9184jfhq22",
  22. oauth_token="ad180jjd733klru7",
  23. oauth_signature_method="HMAC-SHA1",
  24. oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",
  25. oauth_timestamp="137131200",
  26. oauth_nonce="4572616e48616d6d65724c61686176",
  27. oauth_version="1.0"
  28. .. _`section 3.5.1`: https://tools.ietf.org/html/rfc5849#section-3.5.1
  29. .. _`RFC2617`: https://tools.ietf.org/html/rfc2617
  30. """
  31. headers = headers or {}
  32. # Protocol parameters SHALL be included in the "Authorization" header
  33. # field as follows:
  34. authorization_header_parameters_parts = []
  35. for oauth_parameter_name, value in oauth_params:
  36. # 1. Parameter names and values are encoded per Parameter Encoding
  37. # (`Section 3.6`_)
  38. #
  39. # .. _`Section 3.6`: https://tools.ietf.org/html/rfc5849#section-3.6
  40. escaped_name = utils.escape(oauth_parameter_name)
  41. escaped_value = utils.escape(value)
  42. # 2. Each parameter's name is immediately followed by an "=" character
  43. # (ASCII code 61), a """ character (ASCII code 34), the parameter
  44. # value (MAY be empty), and another """ character (ASCII code 34).
  45. part = '{}="{}"'.format(escaped_name, escaped_value)
  46. authorization_header_parameters_parts.append(part)
  47. # 3. Parameters are separated by a "," character (ASCII code 44) and
  48. # OPTIONAL linear whitespace per `RFC2617`_.
  49. #
  50. # .. _`RFC2617`: https://tools.ietf.org/html/rfc2617
  51. authorization_header_parameters = ', '.join(
  52. authorization_header_parameters_parts)
  53. # 4. The OPTIONAL "realm" parameter MAY be added and interpreted per
  54. # `RFC2617 section 1.2`_.
  55. #
  56. # .. _`RFC2617 section 1.2`: https://tools.ietf.org/html/rfc2617#section-1.2
  57. if realm:
  58. # NOTE: realm should *not* be escaped
  59. authorization_header_parameters = ('realm="%s", ' % realm +
  60. authorization_header_parameters)
  61. # the auth-scheme name set to "OAuth" (case insensitive).
  62. authorization_header = 'OAuth %s' % authorization_header_parameters
  63. # contribute the Authorization header to the given headers
  64. full_headers = {}
  65. full_headers.update(headers)
  66. full_headers['Authorization'] = authorization_header
  67. return full_headers
  68. def _append_params(oauth_params, params):
  69. """Append OAuth params to an existing set of parameters.
  70. Both params and oauth_params is must be lists of 2-tuples.
  71. Per `section 3.5.2`_ and `3.5.3`_ of the spec.
  72. .. _`section 3.5.2`: https://tools.ietf.org/html/rfc5849#section-3.5.2
  73. .. _`3.5.3`: https://tools.ietf.org/html/rfc5849#section-3.5.3
  74. """
  75. merged = list(params)
  76. merged.extend(oauth_params)
  77. # The request URI / entity-body MAY include other request-specific
  78. # parameters, in which case, the protocol parameters SHOULD be appended
  79. # following the request-specific parameters, properly separated by an "&"
  80. # character (ASCII code 38)
  81. merged.sort(key=lambda i: i[0].startswith('oauth_'))
  82. return merged
  83. def prepare_form_encoded_body(oauth_params, body):
  84. """Prepare the Form-Encoded Body.
  85. Per `section 3.5.2`_ of the spec.
  86. .. _`section 3.5.2`: https://tools.ietf.org/html/rfc5849#section-3.5.2
  87. """
  88. # append OAuth params to the existing body
  89. return _append_params(oauth_params, body)
  90. def prepare_request_uri_query(oauth_params, uri):
  91. """Prepare the Request URI Query.
  92. Per `section 3.5.3`_ of the spec.
  93. .. _`section 3.5.3`: https://tools.ietf.org/html/rfc5849#section-3.5.3
  94. """
  95. # append OAuth params to the existing set of query components
  96. sch, net, path, par, query, fra = urlparse(uri)
  97. query = urlencode(
  98. _append_params(oauth_params, extract_params(query) or []))
  99. return urlunparse((sch, net, path, par, query, fra))