a
    !f|$                     @   s~   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	 Zd
d Zdd ZdddZdS )z7A simple wrapper around the OAuth2 credentials library.    N)	urlencode)client)UTC)_NOW)_microseconds_from_datetimec                   C   s
   t j S )a$  Gets credentials implicitly from the current environment.

    .. note::

        You should not need to use this function directly. Instead, use a
        helper method which uses this method under the hood.

    Checks environment in order of precedence:

    * Google App Engine (production and testing)
    * Environment variable :envvar:`GOOGLE_APPLICATION_CREDENTIALS` pointing to
      a file with stored credentials information.
    * Stored "well known" file associated with ``gcloud`` command line tool.
    * Google Compute Engine production environment.

    The file referred to in :envvar:`GOOGLE_APPLICATION_CREDENTIALS` is
    expected to contain information about credentials that are ready to use.
    This means either service account information or user account information
    with a ready-to-use refresh token:

    .. code:: json

      {
          'type': 'authorized_user',
          'client_id': '...',
          'client_secret': '...',
          'refresh_token': '...'
      }

    or

    .. code:: json

      {
          'type': 'service_account',
          'client_id': '...',
          'client_email': '...',
          'private_key_id': '...',
          'private_key': '...'
      }

    The second of these is simply a JSON key downloaded from the Google APIs
    console. The first is a close cousin of the "client secrets" JSON file
    used by :mod:`oauth2client.clientsecrets` but differs in formatting.

    :rtype: :class:`oauth2client.client.GoogleCredentials`,
            :class:`oauth2client.contrib.appengine.AppAssertionCredentials`,
            :class:`oauth2client.contrib.gce.AppAssertionCredentials`,
            :class:`oauth2client.service_account.ServiceAccountCredentials`
    :returns: A new credentials instance corresponding to the implicit
              environment.
    )r   ZGoogleCredentialsZget_application_default r   r   S/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/credentials.pyget_credentials   s    5r	   c                 C   sH   t | dstdt|  | |\}}t|}| j}|t||dS )al  Gets query parameters for creating a signed URL.

    :type credentials: :class:`oauth2client.client.AssertionCredentials`
    :param credentials: The credentials used to create a private key
                        for signing text.

    :type expiration: int or long
    :param expiration: When the signed URL should expire.

    :type string_to_sign: string
    :param string_to_sign: The string to be signed by the credentials.

    :raises AttributeError: If :meth: sign_blob is unavailable.

    :rtype: dict
    :returns: Query parameters matching the signing credentials with a
              signed payload.
    	sign_blobzyou need a private key to sign credentials.the credentials you are currently using %s just contains a token. see https://googlecloudplatform.github.io/gcloud-python/stable/gcloud-auth.html#setting-up-a-service-account for more details.)ZGoogleAccessIdZExpires	Signature)hasattrAttributeErrortyper
   base64	b64encodeZservice_account_emailstr)credentials
expirationstring_to_sign_Zsignature_bytes	signatureZservice_account_namer   r   r   _get_signed_query_paramsU   s    

r   c                 C   s^   t | tjr"t jtd}||  } t | tjr>t| }|d } t | tjsZt	dt
|  | S )aI  Convert 'expiration' to a number of seconds in the future.

    :type expiration: int, long, datetime.datetime, datetime.timedelta
    :param expiration: When the signed URL should expire.

    :raises TypeError: When expiration is not an integer.

    :rtype: int
    :returns: a timestamp as an absolute number of seconds.
    )tzinfoi@B z=Expected an integer timestamp, datetime, or timedelta. Got %s)
isinstancedatetime	timedeltar   replacer   r   sixinteger_types	TypeErrorr   )r   nowmicrosr   r   r   _get_expiration_secondsz   s    r"    GETc
                 C   sx   t |}d||pd|pdt||g}
t| ||
}|durD||d< |durT||d< |	durd|	|d< dj||t|dS )	a
  Generate signed URL to provide query-string auth'n to a resource.

    .. note::

        Assumes ``credentials`` implements a ``sign_blob()`` method that takes
        bytes to sign and returns a pair of the key ID (unused here) and the
        signed bytes (this is abstract in the base class
        :class:`oauth2client.client.AssertionCredentials`). Also assumes
        ``credentials`` has a ``service_account_email`` property which
        identifies the credentials.

    .. note::

        If you are on Google Compute Engine, you can't generate a signed URL.
        Follow `Issue 922`_ for updates on this. If you'd like to be able to
        generate a signed URL from GCE, you can use a standard service account
        from a JSON file rather than a GCE service account.

    See headers `reference`_ for more details on optional arguments.

    .. _Issue 922: https://github.com/GoogleCloudPlatform/                   gcloud-python/issues/922
    .. _reference: https://cloud.google.com/storage/docs/reference-headers

    :type credentials: :class:`oauth2client.appengine.AppAssertionCredentials`
    :param credentials: Credentials object with an associated private key to
                        sign text.

    :type resource: string
    :param resource: A pointer to a specific resource
                     (typically, ``/bucket-name/path/to/blob.txt``).

    :type expiration: :class:`int`, :class:`long`, :class:`datetime.datetime`,
                      :class:`datetime.timedelta`
    :param expiration: When the signed URL should expire.

    :type api_access_endpoint: str
    :param api_access_endpoint: Optional URI base. Defaults to empty string.

    :type method: str
    :param method: The HTTP verb that will be used when requesting the URL.
                   Defaults to ``'GET'``.

    :type content_md5: str
    :param content_md5: (Optional) The MD5 hash of the object referenced by
                        ``resource``.

    :type content_type: str
    :param content_type: (Optional) The content type of the object referenced
                         by ``resource``.

    :type response_type: str
    :param response_type: (Optional) Content type of responses to requests for
                          the signed URL. Used to over-ride the content type of
                          the underlying resource.

    :type response_disposition: str
    :param response_disposition: (Optional) Content disposition of responses to
                                 requests for the signed URL.

    :type generation: str
    :param generation: (Optional) A value that indicates which generation of
                       the resource to fetch.

    :rtype: string
    :returns: A signed URL you can use to access the resource
              until expiration.
    
r#   Nzresponse-content-typezresponse-content-disposition
generationz"{endpoint}{resource}?{querystring})ZendpointresourceZquerystring)r"   joinr   r   formatr   )r   r'   r   Zapi_access_endpointmethodZcontent_md5content_typeZresponse_typeZresponse_dispositionr&   r   Zquery_paramsr   r   r   generate_signed_url   s,    Ir,   )r#   r$   NNNNN)__doc__r   r   r   Zsix.moves.urllib.parser   Zoauth2clientr   Zgcloud._helpersr   r   r   r	   r   r"   r,   r   r   r   r   <module>   s    8%    