Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 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 | |
| 15 | """Helper functions for commonly used utilities.""" |
| 16 | |
Jon Wayne Parrott | 97eb870 | 2016-11-17 09:43:16 -0800 | [diff] [blame] | 17 | import base64 |
Jon Wayne Parrott | 5824ad8 | 2016-10-06 09:27:44 -0700 | [diff] [blame] | 18 | import calendar |
| 19 | import datetime |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 20 | import urllib |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 21 | |
| 22 | |
Liron Newman | 45c4491 | 2021-09-07 23:07:07 +0100 | [diff] [blame^] | 23 | CLOCK_SKEW_SECS = 60 # 60 seconds |
Jon Wayne Parrott | e60c124 | 2017-03-23 16:00:24 -0700 | [diff] [blame] | 24 | CLOCK_SKEW = datetime.timedelta(seconds=CLOCK_SKEW_SECS) |
| 25 | |
| 26 | |
Jon Wayne Parrott | 6b21d75 | 2016-10-14 14:49:35 -0700 | [diff] [blame] | 27 | def copy_docstring(source_class): |
Jon Wayne Parrott | 2c253a5 | 2016-10-14 14:50:58 -0700 | [diff] [blame] | 28 | """Decorator that copies a method's docstring from another class. |
| 29 | |
| 30 | Args: |
| 31 | source_class (type): The class that has the documented method. |
| 32 | |
| 33 | Returns: |
| 34 | Callable: A decorator that will copy the docstring of the same |
| 35 | named method in the source class to the decorated method. |
| 36 | """ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 37 | |
Jon Wayne Parrott | 6b21d75 | 2016-10-14 14:49:35 -0700 | [diff] [blame] | 38 | def decorator(method): |
Jon Wayne Parrott | 2c253a5 | 2016-10-14 14:50:58 -0700 | [diff] [blame] | 39 | """Decorator implementation. |
| 40 | |
| 41 | Args: |
Jon Wayne Parrott | 0a0be14 | 2016-10-14 14:53:29 -0700 | [diff] [blame] | 42 | method (Callable): The method to copy the docstring to. |
Jon Wayne Parrott | 2c253a5 | 2016-10-14 14:50:58 -0700 | [diff] [blame] | 43 | |
| 44 | Returns: |
| 45 | Callable: the same method passed in with an updated docstring. |
| 46 | |
| 47 | Raises: |
| 48 | ValueError: if the method already has a docstring. |
| 49 | """ |
Jon Wayne Parrott | 6b21d75 | 2016-10-14 14:49:35 -0700 | [diff] [blame] | 50 | if method.__doc__: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 51 | raise ValueError("Method already has a docstring.") |
Jon Wayne Parrott | 6b21d75 | 2016-10-14 14:49:35 -0700 | [diff] [blame] | 52 | |
| 53 | source_method = getattr(source_class, method.__name__) |
| 54 | method.__doc__ = source_method.__doc__ |
| 55 | |
| 56 | return method |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 57 | |
Jon Wayne Parrott | 6b21d75 | 2016-10-14 14:49:35 -0700 | [diff] [blame] | 58 | return decorator |
| 59 | |
| 60 | |
Jon Wayne Parrott | 5824ad8 | 2016-10-06 09:27:44 -0700 | [diff] [blame] | 61 | def utcnow(): |
| 62 | """Returns the current UTC datetime. |
| 63 | |
| 64 | Returns: |
| 65 | datetime: The current time in UTC. |
| 66 | """ |
| 67 | return datetime.datetime.utcnow() |
| 68 | |
| 69 | |
| 70 | def datetime_to_secs(value): |
| 71 | """Convert a datetime object to the number of seconds since the UNIX epoch. |
| 72 | |
| 73 | Args: |
| 74 | value (datetime): The datetime to convert. |
| 75 | |
| 76 | Returns: |
| 77 | int: The number of seconds since the UNIX epoch. |
| 78 | """ |
| 79 | return calendar.timegm(value.utctimetuple()) |
| 80 | |
| 81 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 82 | def to_bytes(value, encoding="utf-8"): |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 83 | """Converts a string value to bytes, if necessary. |
| 84 | |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 85 | Args: |
| 86 | value (Union[str, bytes]): The value to be converted. |
| 87 | encoding (str): The encoding to use to convert unicode to bytes. |
| 88 | Defaults to "utf-8". |
| 89 | |
| 90 | Returns: |
| 91 | bytes: The original value converted to bytes (if unicode) or as |
| 92 | passed in if it started out as bytes. |
| 93 | |
| 94 | Raises: |
| 95 | ValueError: If the value could not be converted to bytes. |
| 96 | """ |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 97 | result = value.encode(encoding) if isinstance(value, str) else value |
| 98 | if isinstance(result, bytes): |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 99 | return result |
| 100 | else: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 101 | raise ValueError("{0!r} could not be converted to bytes".format(value)) |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def from_bytes(value): |
| 105 | """Converts bytes to a string value, if necessary. |
| 106 | |
| 107 | Args: |
| 108 | value (Union[str, bytes]): The value to be converted. |
| 109 | |
| 110 | Returns: |
| 111 | str: The original value converted to unicode (if bytes) or as passed in |
| 112 | if it started out as unicode. |
| 113 | |
| 114 | Raises: |
| 115 | ValueError: If the value could not be converted to unicode. |
| 116 | """ |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 117 | result = value.decode("utf-8") if isinstance(value, bytes) else value |
| 118 | if isinstance(result, str): |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 119 | return result |
| 120 | else: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 121 | raise ValueError("{0!r} could not be converted to unicode".format(value)) |
Jon Wayne Parrott | 64a9d6e | 2016-10-07 14:02:53 -0700 | [diff] [blame] | 122 | |
| 123 | |
| 124 | def update_query(url, params, remove=None): |
| 125 | """Updates a URL's query parameters. |
| 126 | |
| 127 | Replaces any current values if they are already present in the URL. |
| 128 | |
| 129 | Args: |
| 130 | url (str): The URL to update. |
| 131 | params (Mapping[str, str]): A mapping of query parameter |
| 132 | keys to values. |
| 133 | remove (Sequence[str]): Parameters to remove from the query string. |
| 134 | |
| 135 | Returns: |
| 136 | str: The URL with updated query parameters. |
| 137 | |
| 138 | Examples: |
| 139 | |
| 140 | >>> url = 'http://example.com?a=1' |
| 141 | >>> update_query(url, {'a': '2'}) |
| 142 | http://example.com?a=2 |
| 143 | >>> update_query(url, {'b': '3'}) |
| 144 | http://example.com?a=1&b=3 |
| 145 | >> update_query(url, {'b': '3'}, remove=['a']) |
| 146 | http://example.com?b=3 |
| 147 | |
| 148 | """ |
| 149 | if remove is None: |
| 150 | remove = [] |
| 151 | |
| 152 | # Split the URL into parts. |
| 153 | parts = urllib.parse.urlparse(url) |
| 154 | # Parse the query string. |
| 155 | query_params = urllib.parse.parse_qs(parts.query) |
| 156 | # Update the query parameters with the new parameters. |
| 157 | query_params.update(params) |
| 158 | # Remove any values specified in remove. |
| 159 | query_params = { |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 160 | key: value for key, value in query_params.items() if key not in remove |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 161 | } |
Jon Wayne Parrott | 64a9d6e | 2016-10-07 14:02:53 -0700 | [diff] [blame] | 162 | # Re-encoded the query string. |
| 163 | new_query = urllib.parse.urlencode(query_params, doseq=True) |
| 164 | # Unsplit the url. |
| 165 | new_parts = parts._replace(query=new_query) |
| 166 | return urllib.parse.urlunparse(new_parts) |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 167 | |
| 168 | |
| 169 | def scopes_to_string(scopes): |
| 170 | """Converts scope value to a string suitable for sending to OAuth 2.0 |
| 171 | authorization servers. |
| 172 | |
| 173 | Args: |
| 174 | scopes (Sequence[str]): The sequence of scopes to convert. |
| 175 | |
| 176 | Returns: |
| 177 | str: The scopes formatted as a single string. |
| 178 | """ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 179 | return " ".join(scopes) |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 180 | |
| 181 | |
| 182 | def string_to_scopes(scopes): |
| 183 | """Converts stringifed scopes value to a list. |
| 184 | |
| 185 | Args: |
| 186 | scopes (Union[Sequence, str]): The string of space-separated scopes |
| 187 | to convert. |
| 188 | Returns: |
| 189 | Sequence(str): The separated scopes. |
| 190 | """ |
| 191 | if not scopes: |
| 192 | return [] |
| 193 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 194 | return scopes.split(" ") |
Jon Wayne Parrott | 97eb870 | 2016-11-17 09:43:16 -0800 | [diff] [blame] | 195 | |
| 196 | |
| 197 | def padded_urlsafe_b64decode(value): |
| 198 | """Decodes base64 strings lacking padding characters. |
| 199 | |
| 200 | Google infrastructure tends to omit the base64 padding characters. |
| 201 | |
| 202 | Args: |
| 203 | value (Union[str, bytes]): The encoded value. |
| 204 | |
| 205 | Returns: |
| 206 | bytes: The decoded value |
| 207 | """ |
| 208 | b64string = to_bytes(value) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 209 | padded = b64string + b"=" * (-len(b64string) % 4) |
Jon Wayne Parrott | 97eb870 | 2016-11-17 09:43:16 -0800 | [diff] [blame] | 210 | return base64.urlsafe_b64decode(padded) |
Aditya Natraj | ae7e4f3 | 2019-02-15 00:02:10 -0500 | [diff] [blame] | 211 | |
| 212 | |
| 213 | def unpadded_urlsafe_b64encode(value): |
| 214 | """Encodes base64 strings removing any padding characters. |
| 215 | |
| 216 | `rfc 7515`_ defines Base64url to NOT include any padding |
| 217 | characters, but the stdlib doesn't do that by default. |
| 218 | |
| 219 | _rfc7515: https://tools.ietf.org/html/rfc7515#page-6 |
| 220 | |
| 221 | Args: |
| 222 | value (Union[str|bytes]): The bytes-like value to encode |
| 223 | |
| 224 | Returns: |
| 225 | Union[str|bytes]: The encoded value |
| 226 | """ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 227 | return base64.urlsafe_b64encode(value).rstrip(b"=") |