# Path: gslib/utils/wrapped_credentials.py
# class WrappedCredentials(oauth2client.client.OAuth2Credentials):
#   """A utility class to use Google Auth credentials in place of oauth2client credentials.
#   """
#   NON_SERIALIZED_MEMBERS = frozenset(
#       list(oauth2client.client.OAuth2Credentials.NON_SERIALIZED_MEMBERS) +
#       ['_base'])
# 
#   def __init__(self, external_account_creds):
#     """Initializes oauth2client credentials based on underlying Google Auth credentials.
# 
#     Args:
#       external_account_creds: subclass of google.auth.external_account.Credentials
#     """
#     if not isinstance(external_account_creds, external_account.Credentials):
#       raise TypeError("Invalid Credentials")
#     self._base = external_account_creds
#     super(WrappedCredentials, self).__init__(access_token=self._base.token,
#                                              client_id=self._base._audience,
#                                              client_secret=None,
#                                              refresh_token=None,
#                                              token_expiry=self._base.expiry,
#                                              token_uri=None,
#                                              user_agent=None)
# 
#   def _do_refresh_request(self, http):
#     self._base.refresh(requests.Request())
#     if self.store is not None:
#       self.store.locked_put(self)
# 
#   @property
#   def access_token(self):
#     return self._base.token
# 
#   @access_token.setter
#   def access_token(self, value):
#     self._base.token = value
# 
#   @property
#   def token_expiry(self):
#     return self._base.expiry
# 
#   @token_expiry.setter
#   def token_expiry(self, value):
#     self._base.expiry = value
# 
#   def to_json(self):
#     """Utility function that creates JSON repr. of a Credentials object.
# 
#     Returns:
#         string, a JSON representation of this instance, suitable to pass to
#         from_json().
#     """
# 
#     serialized_data = super().to_json()
#     deserialized_data = json.loads(serialized_data)
#     deserialized_data['_base'] = copy.copy(self._base.info)
#     deserialized_data['access_token'] = self._base.token
#     deserialized_data['token_expiry'] = _parse_expiry(self.token_expiry)
#     return json.dumps(deserialized_data)
# 
#   @classmethod
#   def for_external_account(cls, filename):
#     creds = _get_external_account_credentials_from_file(filename)
#     return cls(creds)
# 
#   @classmethod
#   def from_json(cls, json_data):
#     """Instantiate a Credentials object from a JSON description of it.
# 
#     The JSON should have been produced by calling .to_json() on the object.
# 
#     Args:
#         data: dict, A deserialized JSON object.
# 
#     Returns:
#         An instance of a Credentials subclass.
#     """
#     data = json.loads(json_data)
#     # Rebuild the credentials.
#     base = data.get("_base")
#     # Init base cred.
#     external_account_creds = _get_external_account_credentials_from_info(base)
#     creds = cls(external_account_creds)
#     # Inject token and expiry.
#     creds.access_token = data.get("access_token")
#     if (data.get('token_expiry') and
#         not isinstance(data['token_expiry'], datetime.datetime)):
#       try:
#         data['token_expiry'] = datetime.datetime.strptime(
#             data['token_expiry'], oauth2client.client.EXPIRY_FORMAT)
#       except ValueError:
#         data['token_expiry'] = None
#     creds.token_expiry = data.get("token_expiry")
#     return creds


import datetime
import json
import httplib2
import oauth2client
from google.auth import credentials
from google.auth import external_account
from google.auth import identity_pool
from gslib.tests import testcase
from gslib.utils.wrapped_credentials import WrappedCredentials
from six import add_move, MovedModule
from six.moves import mock

    # Make sure the default request gets called with the correct token.
    req.assert_called_once_with("www.google.com",
                                method="GET",
                                headers=HeadersWithAuth(ACCESS_TOKEN),
                                body=None,
                                connection_type=mock.ANY,
                                redirections=mock.ANY)

  def testWrappedCredentialSerialization(self):
    """Test logic for converting Wrapped Credentials to and from JSON for serialization."""
    creds = WrappedCredentials(
        identity_pool.Credentials(audience="foo",
                                  subject_token_type="bar",
                                  token_url="baz",
                                  credential_source={"url": "www.google.com"}))
    creds.access_token = ACCESS_TOKEN
    creds.token_expiry = datetime.datetime(2001, 12, 5, 0, 0)
    creds_json = creds.to_json()
    json_values = json.loads(creds_json)
    self.assertEquals(json_values["client_id"], "foo")
    self.assertEquals(json_values['access_token'], ACCESS_TOKEN)
    self.assertEquals(json_values['token_expiry'], "2001-12-05T00:00:00Z")
    self.assertEquals(json_values["_base"]["audience"], "foo")
    self.assertEquals(json_values["_base"]["subject_token_type"], "bar")
    self.assertEquals(json_values["_base"]["token_url"], "baz")
    self.assertEquals(json_values["_base"]["credential_source"]["url"],
                      "www.google.com")

    creds2 = WrappedCredentials.from_json(creds_json)

# Path: gslib/utils/wrapped_credentials.py
# class WrappedCredentials(oauth2client.client.OAuth2Credentials):
#   """A utility class to use Google Auth credentials in place of oauth2client credentials.
#   """
#   NON_SERIALIZED_MEMBERS = frozenset(
#       list(oauth2client.client.OAuth2Credentials.NON_SERIALIZED_MEMBERS) +
#       ['_base'])
# 
#   def __init__(self, external_account_creds):
#     """Initializes oauth2client credentials based on underlying Google Auth credentials.
# 
#     Args:
#       external_account_creds: subclass of google.auth.external_account.Credentials
#     """
#     if not isinstance(external_account_creds, external_account.Credentials):
#       raise TypeError("Invalid Credentials")
#     self._base = external_account_creds
#     super(WrappedCredentials, self).__init__(access_token=self._base.token,
#                                              client_id=self._base._audience,
#                                              client_secret=None,
#                                              refresh_token=None,
#                                              token_expiry=self._base.expiry,
#                                              token_uri=None,
#                                              user_agent=None)
# 
#   def _do_refresh_request(self, http):
#     self._base.refresh(requests.Request())
#     if self.store is not None:
#       self.store.locked_put(self)
# 
#   @property
#   def access_token(self):
#     return self._base.token
# 
#   @access_token.setter
#   def access_token(self, value):
#     self._base.token = value
# 
#   @property
#   def token_expiry(self):
#     return self._base.expiry
# 
#   @token_expiry.setter
#   def token_expiry(self, value):
#     self._base.expiry = value
# 
#   def to_json(self):
#     """Utility function that creates JSON repr. of a Credentials object.
# 
#     Returns:
#         string, a JSON representation of this instance, suitable to pass to
#         from_json().
#     """
# 
#     serialized_data = super().to_json()
#     deserialized_data = json.loads(serialized_data)
#     deserialized_data['_base'] = copy.copy(self._base.info)
#     deserialized_data['access_token'] = self._base.token
#     deserialized_data['token_expiry'] = _parse_expiry(self.token_expiry)
#     return json.dumps(deserialized_data)
# 
#   @classmethod
#   def for_external_account(cls, filename):
#     creds = _get_external_account_credentials_from_file(filename)
#     return cls(creds)
# 
#   @classmethod
#   def from_json(cls, json_data):
#     """Instantiate a Credentials object from a JSON description of it.
# 
#     The JSON should have been produced by calling .to_json() on the object.
# 
#     Args:
#         data: dict, A deserialized JSON object.
# 
#     Returns:
#         An instance of a Credentials subclass.
#     """
#     data = json.loads(json_data)
#     # Rebuild the credentials.
#     base = data.get("_base")
#     # Init base cred.
#     external_account_creds = _get_external_account_credentials_from_info(base)
#     creds = cls(external_account_creds)
#     # Inject token and expiry.
#     creds.access_token = data.get("access_token")
#     if (data.get('token_expiry') and
#         not isinstance(data['token_expiry'], datetime.datetime)):
#       try:
#         data['token_expiry'] = datetime.datetime.strptime(
#             data['token_expiry'], oauth2client.client.EXPIRY_FORMAT)
#       except ValueError:
#         data['token_expiry'] = None
#     creds.token_expiry = data.get("token_expiry")
#     return creds


# Path: gslib/tests/test_wrapped_credentials.py
import datetime
import json
import httplib2
import oauth2client
from google.auth import credentials
from google.auth import external_account
from google.auth import identity_pool
from gslib.tests import testcase
from gslib.utils.wrapped_credentials import WrappedCredentials
from six import add_move, MovedModule
from six.moves import mock


    # Make sure the default request gets called with the correct token.
    req.assert_called_once_with("www.google.com",
                                method="GET",
                                headers=HeadersWithAuth(ACCESS_TOKEN),
                                body=None,
                                connection_type=mock.ANY,
                                redirections=mock.ANY)

  def testWrappedCredentialSerialization(self):
    """Test logic for converting Wrapped Credentials to and from JSON for serialization."""
    creds = WrappedCredentials(
        identity_pool.Credentials(audience="foo",
                                  subject_token_type="bar",
                                  token_url="baz",
                                  credential_source={"url": "www.google.com"}))
    creds.access_token = ACCESS_TOKEN
    creds.token_expiry = datetime.datetime(2001, 12, 5, 0, 0)
    creds_json = creds.to_json()
    json_values = json.loads(creds_json)
    self.assertEquals(json_values["client_id"], "foo")
    self.assertEquals(json_values['access_token'], ACCESS_TOKEN)
    self.assertEquals(json_values['token_expiry'], "2001-12-05T00:00:00Z")
    self.assertEquals(json_values["_base"]["audience"], "foo")
    self.assertEquals(json_values["_base"]["subject_token_type"], "bar")
    self.assertEquals(json_values["_base"]["token_url"], "baz")
    self.assertEquals(json_values["_base"]["credential_source"]["url"],
                      "www.google.com")

    creds2 = WrappedCredentials.from_json(creds_json)

    self.assertIsInstance(creds2, WrappedCredentials)