_helpers.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # Copyright 2015 Google Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Helper functions for commonly used utilities."""
  15. import base64
  16. import calendar
  17. import datetime
  18. from email.message import Message
  19. import sys
  20. import urllib
  21. from google.auth import exceptions
  22. # The smallest MDS cache used by this library stores tokens until 4 minutes from
  23. # expiry.
  24. REFRESH_THRESHOLD = datetime.timedelta(minutes=3, seconds=45)
  25. def copy_docstring(source_class):
  26. """Decorator that copies a method's docstring from another class.
  27. Args:
  28. source_class (type): The class that has the documented method.
  29. Returns:
  30. Callable: A decorator that will copy the docstring of the same
  31. named method in the source class to the decorated method.
  32. """
  33. def decorator(method):
  34. """Decorator implementation.
  35. Args:
  36. method (Callable): The method to copy the docstring to.
  37. Returns:
  38. Callable: the same method passed in with an updated docstring.
  39. Raises:
  40. google.auth.exceptions.InvalidOperation: if the method already has a docstring.
  41. """
  42. if method.__doc__:
  43. raise exceptions.InvalidOperation("Method already has a docstring.")
  44. source_method = getattr(source_class, method.__name__)
  45. method.__doc__ = source_method.__doc__
  46. return method
  47. return decorator
  48. def parse_content_type(header_value):
  49. """Parse a 'content-type' header value to get just the plain media-type (without parameters).
  50. This is done using the class Message from email.message as suggested in PEP 594
  51. (because the cgi is now deprecated and will be removed in python 3.13,
  52. see https://peps.python.org/pep-0594/#cgi).
  53. Args:
  54. header_value (str): The value of a 'content-type' header as a string.
  55. Returns:
  56. str: A string with just the lowercase media-type from the parsed 'content-type' header.
  57. If the provided content-type is not parsable, returns 'text/plain',
  58. the default value for textual files.
  59. """
  60. m = Message()
  61. m["content-type"] = header_value
  62. return (
  63. m.get_content_type()
  64. ) # Despite the name, actually returns just the media-type
  65. def utcnow():
  66. """Returns the current UTC datetime.
  67. Returns:
  68. datetime: The current time in UTC.
  69. """
  70. # We used datetime.utcnow() before, since it's deprecated from python 3.12,
  71. # we are using datetime.now(timezone.utc) now. "utcnow()" is offset-native
  72. # (no timezone info), but "now()" is offset-aware (with timezone info).
  73. # This will cause datetime comparison problem. For backward compatibility,
  74. # we need to remove the timezone info.
  75. now = datetime.datetime.now(datetime.timezone.utc)
  76. now = now.replace(tzinfo=None)
  77. return now
  78. def datetime_to_secs(value):
  79. """Convert a datetime object to the number of seconds since the UNIX epoch.
  80. Args:
  81. value (datetime): The datetime to convert.
  82. Returns:
  83. int: The number of seconds since the UNIX epoch.
  84. """
  85. return calendar.timegm(value.utctimetuple())
  86. def to_bytes(value, encoding="utf-8"):
  87. """Converts a string value to bytes, if necessary.
  88. Args:
  89. value (Union[str, bytes]): The value to be converted.
  90. encoding (str): The encoding to use to convert unicode to bytes.
  91. Defaults to "utf-8".
  92. Returns:
  93. bytes: The original value converted to bytes (if unicode) or as
  94. passed in if it started out as bytes.
  95. Raises:
  96. google.auth.exceptions.InvalidValue: If the value could not be converted to bytes.
  97. """
  98. result = value.encode(encoding) if isinstance(value, str) else value
  99. if isinstance(result, bytes):
  100. return result
  101. else:
  102. raise exceptions.InvalidValue(
  103. "{0!r} could not be converted to bytes".format(value)
  104. )
  105. def from_bytes(value):
  106. """Converts bytes to a string value, if necessary.
  107. Args:
  108. value (Union[str, bytes]): The value to be converted.
  109. Returns:
  110. str: The original value converted to unicode (if bytes) or as passed in
  111. if it started out as unicode.
  112. Raises:
  113. google.auth.exceptions.InvalidValue: If the value could not be converted to unicode.
  114. """
  115. result = value.decode("utf-8") if isinstance(value, bytes) else value
  116. if isinstance(result, str):
  117. return result
  118. else:
  119. raise exceptions.InvalidValue(
  120. "{0!r} could not be converted to unicode".format(value)
  121. )
  122. def update_query(url, params, remove=None):
  123. """Updates a URL's query parameters.
  124. Replaces any current values if they are already present in the URL.
  125. Args:
  126. url (str): The URL to update.
  127. params (Mapping[str, str]): A mapping of query parameter
  128. keys to values.
  129. remove (Sequence[str]): Parameters to remove from the query string.
  130. Returns:
  131. str: The URL with updated query parameters.
  132. Examples:
  133. >>> url = 'http://example.com?a=1'
  134. >>> update_query(url, {'a': '2'})
  135. http://example.com?a=2
  136. >>> update_query(url, {'b': '3'})
  137. http://example.com?a=1&b=3
  138. >> update_query(url, {'b': '3'}, remove=['a'])
  139. http://example.com?b=3
  140. """
  141. if remove is None:
  142. remove = []
  143. # Split the URL into parts.
  144. parts = urllib.parse.urlparse(url)
  145. # Parse the query string.
  146. query_params = urllib.parse.parse_qs(parts.query)
  147. # Update the query parameters with the new parameters.
  148. query_params.update(params)
  149. # Remove any values specified in remove.
  150. query_params = {
  151. key: value for key, value in query_params.items() if key not in remove
  152. }
  153. # Re-encoded the query string.
  154. new_query = urllib.parse.urlencode(query_params, doseq=True)
  155. # Unsplit the url.
  156. new_parts = parts._replace(query=new_query)
  157. return urllib.parse.urlunparse(new_parts)
  158. def scopes_to_string(scopes):
  159. """Converts scope value to a string suitable for sending to OAuth 2.0
  160. authorization servers.
  161. Args:
  162. scopes (Sequence[str]): The sequence of scopes to convert.
  163. Returns:
  164. str: The scopes formatted as a single string.
  165. """
  166. return " ".join(scopes)
  167. def string_to_scopes(scopes):
  168. """Converts stringifed scopes value to a list.
  169. Args:
  170. scopes (Union[Sequence, str]): The string of space-separated scopes
  171. to convert.
  172. Returns:
  173. Sequence(str): The separated scopes.
  174. """
  175. if not scopes:
  176. return []
  177. return scopes.split(" ")
  178. def padded_urlsafe_b64decode(value):
  179. """Decodes base64 strings lacking padding characters.
  180. Google infrastructure tends to omit the base64 padding characters.
  181. Args:
  182. value (Union[str, bytes]): The encoded value.
  183. Returns:
  184. bytes: The decoded value
  185. """
  186. b64string = to_bytes(value)
  187. padded = b64string + b"=" * (-len(b64string) % 4)
  188. return base64.urlsafe_b64decode(padded)
  189. def unpadded_urlsafe_b64encode(value):
  190. """Encodes base64 strings removing any padding characters.
  191. `rfc 7515`_ defines Base64url to NOT include any padding
  192. characters, but the stdlib doesn't do that by default.
  193. _rfc7515: https://tools.ietf.org/html/rfc7515#page-6
  194. Args:
  195. value (Union[str|bytes]): The bytes-like value to encode
  196. Returns:
  197. Union[str|bytes]: The encoded value
  198. """
  199. return base64.urlsafe_b64encode(value).rstrip(b"=")
  200. def is_python_3():
  201. """Check if the Python interpreter is Python 2 or 3.
  202. Returns:
  203. bool: True if the Python interpreter is Python 3 and False otherwise.
  204. """
  205. return sys.version_info > (3, 0)