C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 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 | """OAuth 2.0 client. |
| 16 | |
| 17 | This is a client for interacting with an OAuth 2.0 authorization server's |
| 18 | token endpoint. |
| 19 | |
| 20 | For more information about the token endpoint, see |
| 21 | `Section 3.1 of rfc6749`_ |
| 22 | |
| 23 | .. _Section 3.1 of rfc6749: https://tools.ietf.org/html/rfc6749#section-3.2 |
| 24 | """ |
| 25 | |
| 26 | import datetime |
| 27 | import json |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 28 | |
| 29 | import six |
| 30 | from six.moves import http_client |
| 31 | from six.moves import urllib |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 32 | |
| 33 | from google.auth import _helpers |
| 34 | from google.auth import exceptions |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 35 | from google.auth import jwt |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 36 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 37 | _URLENCODED_CONTENT_TYPE = "application/x-www-form-urlencoded" |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 38 | _JSON_CONTENT_TYPE = "application/json" |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 39 | _JWT_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer" |
| 40 | _REFRESH_GRANT_TYPE = "refresh_token" |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 41 | |
| 42 | |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 43 | def _handle_error_response(response_data): |
| 44 | """Translates an error response into an exception. |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 45 | |
| 46 | Args: |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 47 | response_data (Mapping): The decoded response data. |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 48 | |
| 49 | Raises: |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 50 | google.auth.exceptions.RefreshError: The errors contained in response_data. |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 51 | """ |
| 52 | try: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 53 | error_details = "{}: {}".format( |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 54 | response_data["error"], response_data.get("error_description") |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 55 | ) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 56 | # If no details could be extracted, use the response data. |
| 57 | except (KeyError, ValueError): |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 58 | error_details = json.dumps(response_data) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 59 | |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 60 | raise exceptions.RefreshError(error_details, response_data) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 61 | |
| 62 | |
| 63 | def _parse_expiry(response_data): |
| 64 | """Parses the expiry field from a response into a datetime. |
| 65 | |
| 66 | Args: |
| 67 | response_data (Mapping): The JSON-parsed response data. |
| 68 | |
| 69 | Returns: |
| 70 | Optional[datetime]: The expiration or ``None`` if no expiration was |
| 71 | specified. |
| 72 | """ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 73 | expires_in = response_data.get("expires_in", None) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 74 | |
| 75 | if expires_in is not None: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 76 | return _helpers.utcnow() + datetime.timedelta(seconds=expires_in) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 77 | else: |
| 78 | return None |
| 79 | |
| 80 | |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 81 | def _token_endpoint_request_no_throw( |
| 82 | request, token_uri, body, access_token=None, use_json=False |
| 83 | ): |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 84 | """Makes a request to the OAuth 2.0 authorization server's token endpoint. |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 85 | This function doesn't throw on response errors. |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 86 | |
| 87 | Args: |
| 88 | request (google.auth.transport.Request): A callable used to make |
| 89 | HTTP requests. |
| 90 | token_uri (str): The OAuth 2.0 authorizations server's token endpoint |
| 91 | URI. |
| 92 | body (Mapping[str, str]): The parameters to send in the request body. |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 93 | access_token (Optional(str)): The access token needed to make the request. |
| 94 | use_json (Optional(bool)): Use urlencoded format or json format for the |
| 95 | content type. The default value is False. |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 96 | |
| 97 | Returns: |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 98 | Tuple(bool, Mapping[str, str]): A boolean indicating if the request is |
| 99 | successful, and a mapping for the JSON-decoded response data. |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 100 | """ |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 101 | if use_json: |
| 102 | headers = {"Content-Type": _JSON_CONTENT_TYPE} |
| 103 | body = json.dumps(body).encode("utf-8") |
| 104 | else: |
| 105 | headers = {"Content-Type": _URLENCODED_CONTENT_TYPE} |
| 106 | body = urllib.parse.urlencode(body).encode("utf-8") |
| 107 | |
| 108 | if access_token: |
| 109 | headers["Authorization"] = "Bearer {}".format(access_token) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 110 | |
Anjali Doneria | eae1dcb | 2019-09-09 16:36:10 -0700 | [diff] [blame] | 111 | retry = 0 |
| 112 | # retry to fetch token for maximum of two times if any internal failure |
| 113 | # occurs. |
| 114 | while True: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 115 | response = request(method="POST", url=token_uri, headers=headers, body=body) |
Steve | 3b5d3e2 | 2019-12-02 10:57:30 -0800 | [diff] [blame] | 116 | response_body = ( |
| 117 | response.data.decode("utf-8") |
| 118 | if hasattr(response.data, "decode") |
| 119 | else response.data |
| 120 | ) |
Georgy Savva | 46bb58e | 2019-11-13 22:21:57 +0300 | [diff] [blame] | 121 | response_data = json.loads(response_body) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 122 | |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 123 | if response.status == http_client.OK: |
Anjali Doneria | eae1dcb | 2019-09-09 16:36:10 -0700 | [diff] [blame] | 124 | break |
| 125 | else: |
Georgy Savva | 46bb58e | 2019-11-13 22:21:57 +0300 | [diff] [blame] | 126 | error_desc = response_data.get("error_description") or "" |
| 127 | error_code = response_data.get("error") or "" |
| 128 | if ( |
| 129 | any(e == "internal_failure" for e in (error_code, error_desc)) |
| 130 | and retry < 1 |
| 131 | ): |
Anjali Doneria | eae1dcb | 2019-09-09 16:36:10 -0700 | [diff] [blame] | 132 | retry += 1 |
| 133 | continue |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 134 | return response.status == http_client.OK, response_data |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 135 | |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 136 | return response.status == http_client.OK, response_data |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 137 | |
| 138 | |
| 139 | def _token_endpoint_request( |
| 140 | request, token_uri, body, access_token=None, use_json=False |
| 141 | ): |
| 142 | """Makes a request to the OAuth 2.0 authorization server's token endpoint. |
| 143 | |
| 144 | Args: |
| 145 | request (google.auth.transport.Request): A callable used to make |
| 146 | HTTP requests. |
| 147 | token_uri (str): The OAuth 2.0 authorizations server's token endpoint |
| 148 | URI. |
| 149 | body (Mapping[str, str]): The parameters to send in the request body. |
| 150 | access_token (Optional(str)): The access token needed to make the request. |
| 151 | use_json (Optional(bool)): Use urlencoded format or json format for the |
| 152 | content type. The default value is False. |
| 153 | |
| 154 | Returns: |
| 155 | Mapping[str, str]: The JSON-decoded response data. |
| 156 | |
| 157 | Raises: |
| 158 | google.auth.exceptions.RefreshError: If the token endpoint returned |
| 159 | an error. |
| 160 | """ |
| 161 | response_status_ok, response_data = _token_endpoint_request_no_throw( |
| 162 | request, token_uri, body, access_token=access_token, use_json=use_json |
| 163 | ) |
| 164 | if not response_status_ok: |
| 165 | _handle_error_response(response_data) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 166 | return response_data |
| 167 | |
| 168 | |
| 169 | def jwt_grant(request, token_uri, assertion): |
| 170 | """Implements the JWT Profile for OAuth 2.0 Authorization Grants. |
| 171 | |
| 172 | For more details, see `rfc7523 section 4`_. |
| 173 | |
| 174 | Args: |
| 175 | request (google.auth.transport.Request): A callable used to make |
| 176 | HTTP requests. |
| 177 | token_uri (str): The OAuth 2.0 authorizations server's token endpoint |
| 178 | URI. |
| 179 | assertion (str): The OAuth 2.0 assertion. |
| 180 | |
| 181 | Returns: |
| 182 | Tuple[str, Optional[datetime], Mapping[str, str]]: The access token, |
| 183 | expiration, and additional data returned by the token endpoint. |
| 184 | |
| 185 | Raises: |
| 186 | google.auth.exceptions.RefreshError: If the token endpoint returned |
| 187 | an error. |
| 188 | |
| 189 | .. _rfc7523 section 4: https://tools.ietf.org/html/rfc7523#section-4 |
| 190 | """ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 191 | body = {"assertion": assertion, "grant_type": _JWT_GRANT_TYPE} |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 192 | |
| 193 | response_data = _token_endpoint_request(request, token_uri, body) |
| 194 | |
| 195 | try: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 196 | access_token = response_data["access_token"] |
Danny Hermes | 895e369 | 2017-11-09 11:35:57 -0800 | [diff] [blame] | 197 | except KeyError as caught_exc: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 198 | new_exc = exceptions.RefreshError("No access token in response.", response_data) |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 199 | six.raise_from(new_exc, caught_exc) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 200 | |
| 201 | expiry = _parse_expiry(response_data) |
| 202 | |
| 203 | return access_token, expiry, response_data |
| 204 | |
| 205 | |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 206 | def id_token_jwt_grant(request, token_uri, assertion): |
| 207 | """Implements the JWT Profile for OAuth 2.0 Authorization Grants, but |
| 208 | requests an OpenID Connect ID Token instead of an access token. |
| 209 | |
| 210 | This is a variant on the standard JWT Profile that is currently unique |
| 211 | to Google. This was added for the benefit of authenticating to services |
| 212 | that require ID Tokens instead of access tokens or JWT bearer tokens. |
| 213 | |
| 214 | Args: |
| 215 | request (google.auth.transport.Request): A callable used to make |
| 216 | HTTP requests. |
| 217 | token_uri (str): The OAuth 2.0 authorization server's token endpoint |
| 218 | URI. |
| 219 | assertion (str): JWT token signed by a service account. The token's |
| 220 | payload must include a ``target_audience`` claim. |
| 221 | |
| 222 | Returns: |
| 223 | Tuple[str, Optional[datetime], Mapping[str, str]]: |
| 224 | The (encoded) Open ID Connect ID Token, expiration, and additional |
| 225 | data returned by the endpoint. |
| 226 | |
| 227 | Raises: |
| 228 | google.auth.exceptions.RefreshError: If the token endpoint returned |
| 229 | an error. |
| 230 | """ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 231 | body = {"assertion": assertion, "grant_type": _JWT_GRANT_TYPE} |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 232 | |
| 233 | response_data = _token_endpoint_request(request, token_uri, body) |
| 234 | |
| 235 | try: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 236 | id_token = response_data["id_token"] |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 237 | except KeyError as caught_exc: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 238 | new_exc = exceptions.RefreshError("No ID token in response.", response_data) |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 239 | six.raise_from(new_exc, caught_exc) |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 240 | |
| 241 | payload = jwt.decode(id_token, verify=False) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 242 | expiry = datetime.datetime.utcfromtimestamp(payload["exp"]) |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 243 | |
| 244 | return id_token, expiry, response_data |
| 245 | |
| 246 | |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 247 | def _handle_refresh_grant_response(response_data, refresh_token): |
| 248 | """Extract tokens from refresh grant response. |
| 249 | |
| 250 | Args: |
| 251 | response_data (Mapping[str, str]): Refresh grant response data. |
| 252 | refresh_token (str): Current refresh token. |
| 253 | |
| 254 | Returns: |
| 255 | Tuple[str, str, Optional[datetime], Mapping[str, str]]: The access token, |
| 256 | refresh token, expiration, and additional data returned by the token |
| 257 | endpoint. If response_data doesn't have refresh token, then the current |
| 258 | refresh token will be returned. |
| 259 | |
| 260 | Raises: |
| 261 | google.auth.exceptions.RefreshError: If the token endpoint returned |
| 262 | an error. |
| 263 | """ |
| 264 | try: |
| 265 | access_token = response_data["access_token"] |
| 266 | except KeyError as caught_exc: |
| 267 | new_exc = exceptions.RefreshError("No access token in response.", response_data) |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 268 | six.raise_from(new_exc, caught_exc) |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 269 | |
| 270 | refresh_token = response_data.get("refresh_token", refresh_token) |
| 271 | expiry = _parse_expiry(response_data) |
| 272 | |
| 273 | return access_token, refresh_token, expiry, response_data |
| 274 | |
| 275 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 276 | def refresh_grant( |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 277 | request, |
| 278 | token_uri, |
| 279 | refresh_token, |
| 280 | client_id, |
| 281 | client_secret, |
| 282 | scopes=None, |
| 283 | rapt_token=None, |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 284 | ): |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 285 | """Implements the OAuth 2.0 refresh token grant. |
| 286 | |
| 287 | For more details, see `rfc678 section 6`_. |
| 288 | |
| 289 | Args: |
| 290 | request (google.auth.transport.Request): A callable used to make |
| 291 | HTTP requests. |
| 292 | token_uri (str): The OAuth 2.0 authorizations server's token endpoint |
| 293 | URI. |
| 294 | refresh_token (str): The refresh token to use to get a new access |
| 295 | token. |
| 296 | client_id (str): The OAuth 2.0 application's client ID. |
| 297 | client_secret (str): The Oauth 2.0 appliaction's client secret. |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 298 | scopes (Optional(Sequence[str])): Scopes to request. If present, all |
| 299 | scopes must be authorized for the refresh token. Useful if refresh |
| 300 | token has a wild card scope (e.g. |
| 301 | 'https://www.googleapis.com/auth/any-api'). |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 302 | rapt_token (Optional(str)): The reauth Proof Token. |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 303 | |
| 304 | Returns: |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 305 | Tuple[str, str, Optional[datetime], Mapping[str, str]]: The access |
| 306 | token, new or current refresh token, expiration, and additional data |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 307 | returned by the token endpoint. |
| 308 | |
| 309 | Raises: |
| 310 | google.auth.exceptions.RefreshError: If the token endpoint returned |
| 311 | an error. |
| 312 | |
| 313 | .. _rfc6748 section 6: https://tools.ietf.org/html/rfc6749#section-6 |
| 314 | """ |
| 315 | body = { |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 316 | "grant_type": _REFRESH_GRANT_TYPE, |
| 317 | "client_id": client_id, |
| 318 | "client_secret": client_secret, |
| 319 | "refresh_token": refresh_token, |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 320 | } |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 321 | if scopes: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 322 | body["scope"] = " ".join(scopes) |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 323 | if rapt_token: |
| 324 | body["rapt"] = rapt_token |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 325 | |
| 326 | response_data = _token_endpoint_request(request, token_uri, body) |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 327 | return _handle_refresh_grant_response(response_data, refresh_token) |