
    \                         d Z ddlZddlmZ ddlmZ ddlZddlmZ ddlZddlm	Z	 dZ
dZd	Zd
ZdZd Z	 	 ddZd ZefdZy)z&Utility functions for signurl command.    N)datetime)timezone)UTF8)urllibzN{method}
{resource}
{query_string}
{headers}
{signed_headers}
{hashed_payload}zGOOG4-RSA-SHA256zA{signing_algo}
{request_time}
{credential_scope}
{hashed_request}z;https://{host}/{path}?x-goog-signature={sig}&{query_string}zUNSIGNED-PAYLOADc                  j    t        j                  t        j                        j	                  d       S )N)tz)tzinfo)r   nowr   utcreplace     -platform/gsutil/gslib/utils/signurl_helper.py_NowUTCr   $   s#    		&	.	.d	.	;;r   c
                 (   t               }
|
j                  d      }|
j                  d      }dj                  ||      }t        | dz   |z   |dj	                  t        |j                                     d|j                         z  d}|||d	<   |||d
<   dj                  |      }dj	                  t        |j                               D cg c]4  }dj                  |t        j                  j                  ||               6 c}      }dj	                  t        |j                               D cg c]%  }dj                  |j                         ||         ' c}      dz   }dj	                  t        |j                                     }t        j                  |||||t              }t        j                  r|j!                  t"              }t%        j&                         }|j)                  |       t+        j,                  |j/                               j                         j1                  t"              }t2        j                  t        |||      }|	r*|r(|j5                  d|z         |j5                  d|z         ||fS c c}w c c}w )a  Create a string that needs to be signed.

  Args:
    client_id: Client ID signing this URL.
    method: The HTTP method to be used with the signed URL.
    duration: timedelta for which the constructed signed URL should be valid.
    path: String path to the bucket of object for signing, in the form
        'bucket' or 'bucket/object'.
    generation: If not None, specifies a version of an object for signing.
    logger: logging.Logger for warning and debug output.
    region: Geographic region in which the requested resource resides.
    signed_headers: Dict containing the header  info like host
          content-type etc.
    billing_project: Specify a user project to be billed for the request.
    string_to_sign_debug: If true AND logger is enabled for debug level,
        print string to sign to debug. Used to differentiate user's
        signed URL from the probing permissions-check signed URL.

  Returns:
    A tuple where the 1st element is the string to sign.
    The second element is the query string.
  z%Y%m%dz%Y%m%dT%H%M%SZz%{date}/{region}/storage/goog4_request)dateregion/;z%d)zx-goog-algorithmzx-goog-credentialzx-goog-datezx-goog-signedheaderszx-goog-expiresuserProject
generationz/{}&z{}={}
z{}:{})methodresourcequery_stringheaderssigned_headershashed_payload)signing_algorequest_timecredential_scopehashed_requestz=Canonical request (ignore opening/closing brackets): [[[%s]]]z:String to sign (ignore opening/closing brackets): [[[%s]]])r   strftimeformat_SIGNING_ALGOjoinsortedkeystotal_secondsr   parse
quote_pluslower_CANONICAL_REQUEST_FORMAT_UNSIGNED_PAYLOADsixPY3encoder   hashlibsha256updatebase64	b16encodedigestdecode_STRING_TO_SIGN_FORMATdebug)	client_idr   durationpathr   loggerr   r   billing_projectstring_to_sign_debugsigning_timecanonical_daycanonical_timecanonical_scopesigned_query_paramscanonical_resourceparamcanonical_query_stringheadercanonical_headerscanonical_signed_headerscanonical_requestcanonical_request_hasherhashed_canonical_requeststring_to_signs                            r   CreatePayloadrQ   (   s   @ ,''1-(()9:.;BB C )/ ($s?_<#!hhvn.A.A.C'DEx5577 !)8&(2%||D)88-2245%5% nnUFLL334G4NOP5%  ii>..01!1& nnV\\^^F%;<1!   !XXf^-@-@-B&CD/66!)-& 7 ( 	WW)006$^^-!!"34#--%%')).  *00 !&-	 1 /. f
LLG	 LLM  ! 
/	//O%!s   9J
4*Jc                     t        j                  |       j                         j                         }t        j                  ||||      S )zGet the final signed url.)hostr>   sigr   )r6   r7   r-   r9   _SIGNED_URL_FORMATr%   )raw_signaturerS   r>   rI   	signatures        r   GetFinalUrlrX      sI    }-335<<>)		"	"(,'00F 
# 
H Hr   c                 H    t        | t              r| S | j                  |      S )N)
isinstancebytesr2   )valueencodings     r   to_bytesr^      s     uL	h	r   )NF)__doc__r6   r   r   r3   gslib.utils.constantsr   r0   	six.movesr   r.   r&   r:   rU   r/   r   rQ   rX   r^   r   r   r   <module>rb      sg    -     & 
 D "/ ' & < #'',]0@H "  r   