|
19 | 19 | import datetime |
20 | 20 | from email.message import Message |
21 | 21 | import hashlib |
| 22 | +import logging |
22 | 23 | import sys |
| 24 | +from typing import Optional, Dict, Any |
23 | 25 | import urllib |
24 | 26 |
|
25 | 27 | from google.auth import exceptions |
|
28 | 30 | # expiry. |
29 | 31 | REFRESH_THRESHOLD = datetime.timedelta(minutes=3, seconds=45) |
30 | 32 |
|
31 | | -_SENSITIVE_FIELDS = { |
32 | | - "accessToken", |
33 | | - "access_token", |
34 | | - "id_token", |
35 | | - "client_id", |
36 | | - "refresh_token", |
37 | | - "client_secret", |
38 | | -} |
39 | | - |
| 33 | +# TODO(https://github.com/googleapis/google-auth-library-python/issues/1684): Audit and update the list below. |
| 34 | +_SENSITIVE_FIELDS = {"accessToken", "access_token", "id_token", "client_id", "refresh_token", "client_secret"} |
40 | 35 |
|
41 | 36 | def copy_docstring(source_class): |
42 | 37 | """Decorator that copies a method's docstring from another class. |
@@ -311,3 +306,34 @@ def _hash_value(value, field_name: str) -> str: |
311 | 306 | hash_object.update(encoded_value) |
312 | 307 | hex_digest = hash_object.hexdigest() |
313 | 308 | return f"hashed_{field_name}-{hex_digest}" |
| 309 | + |
| 310 | + |
| 311 | +def request_log(logger: logging.Logger, method: str, url: str, body: Optional[Any], headers: Optional[Dict[str, str]]) -> None: |
| 312 | + """ |
| 313 | + Logs an HTTP request at the DEBUG level. |
| 314 | +
|
| 315 | + Args: |
| 316 | + logger: The logging.Logger instance to use. |
| 317 | + method: The HTTP method (e.g., "GET", "POST"). |
| 318 | + url: The URL of the request. |
| 319 | + body: The request body (can be None). |
| 320 | + headers: The request headers (can be None). |
| 321 | + """ |
| 322 | + # TODO(https://github.com/googleapis/google-auth-library-python/issues/1680): Log only if enabled. |
| 323 | + # TODO(https://github.com/googleapis/google-auth-library-python/issues/1682): Add httpRequest extra to log event. |
| 324 | + # TODO(https://github.com/googleapis/google-auth-library-python/issues/1681): Hash sensitive information. |
| 325 | + logger.debug("Making request: %s %s", method, url) |
| 326 | + |
| 327 | + |
| 328 | +def response_log(logger: logging.Logger, response: Any) -> None: |
| 329 | + """ |
| 330 | + Logs an HTTP response at the DEBUG level. |
| 331 | +
|
| 332 | + Args: |
| 333 | + logger: The logging.Logger instance to use. |
| 334 | + response: The HTTP response object to log. |
| 335 | + """ |
| 336 | + # TODO(https://github.com/googleapis/google-auth-library-python/issues/1680): Log only if enabled. |
| 337 | + # TODO(https://github.com/googleapis/google-auth-library-python/issues/1683): Add httpResponse extra to log event. |
| 338 | + # TODO(https://github.com/googleapis/google-auth-library-python/issues/1681): Hash sensitive information. |
| 339 | + logger.debug("Response received...") |
0 commit comments