Skip to content

Commit

Permalink
fix: parse token for expiry and add debug header
Browse files Browse the repository at this point in the history
  • Loading branch information
sai-sunder-s committed Sep 22, 2022
1 parent e829897 commit 9ad68cf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
4 changes: 1 addition & 3 deletions google/auth/compute_engine/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ def refresh(self, request):
credentials.
"""
scopes = self._scopes if self._scopes is not None else self._default_scopes
_LOGGER.info(
"Refreshing access token."
)
_LOGGER.info("Refreshing access token.")

try:
self._retrieve_info(request)
Expand Down
32 changes: 29 additions & 3 deletions google/auth/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import datetime
import logging

import requests
import six

from google.auth import _helpers


_LOGGER = logging.getLogger(__name__)


@six.add_metaclass(abc.ABCMeta)
class Credentials(object):
"""Base class for all credentials.
Expand Down Expand Up @@ -114,9 +116,33 @@ def apply(self, headers, token=None):
_helpers.from_bytes(token or self.token)
)

close_to_expiry = self.expiry - datetime.timedelta(seconds=600)
if _helpers.utcnow() >= close_to_expiry:
_LOGGER.debug("Attached token with expiry:{} at {}".format(self.expiry, _helpers.utcnow()))
try:
print_log = True if self.expiry is None or _helpers.utcnow() >= self.expiry - datetime.timedelta(seconds=600) else False
if print_log:
token_info_response = requests.get(
"https://oauth2.googleapis.com/tokeninfo?access_token={}".format(
self.token
)
)
token_expires_in = 10000
if token_info_response.status_code == 200:
parsed_json = token_info_response.json()
if "expires_in" in parsed_json:
token_expires_in = parsed_json["expires_in"]

_LOGGER.debug(
"Attached token with expiry:{} at {}, expires in: {}".format(
self.expiry, _helpers.utcnow(), token_expires_in
)
)
except Exception as e:
_LOGGER.debug("Unable to log in apply because of:{}".format(e))

try:
headers["x-return-encrypted-headers"] = "request_and_response"
except Exception as e:
_LOGGER.debug("Unable to add debug header because of:{}".format(e))


if self.quota_project_id:
headers["x-goog-user-project"] = self.quota_project_id
Expand Down
2 changes: 1 addition & 1 deletion google/auth/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = "2.11.2-dev1"
__version__ = "2.11.2-dev2"

0 comments on commit 9ad68cf

Please sign in to comment.