
    \                         S r SSKrSSKJr  SSKJr  SSKrSSKJr  SSKrSSKJ	r	  Sr
SrS	rS
rSrS r  SS jrS r\4S jrg)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                  `    [         R                  " [        R                  S9R	                  S S9$ )N)tz)tzinfo)r   nowr   utcreplace     -platform/gsutil/gslib/utils/signurl_helper.py_NowUTCr   $   s#    		&	.	.d	.	;;r   c
                 B   [        5       n
U
R                  S5      nU
R                  S5      nSR                  XS9n[        U S-   U-   USR	                  [        UR                  5       5      5      SUR                  5       -  S.nUb  XS	'   Ub  XNS
'   SR                  U5      nSR	                  [        UR                  5       5       Vs/ s H5  nSR                  U[        R                  R                  UU   5      5      PM7     sn5      nSR	                  [        UR                  5       5       Vs/ s H&  nSR                  UR                  5       UU   5      PM(     sn5      S-   nSR	                  [        UR                  5       5      5      n[        R                  UUUUU[        S9n[        R                  (       a  UR!                  ["        5      n[$        R&                  " 5       nUR)                  U5        [*        R,                  " UR/                  5       5      R                  5       R1                  ["        5      n[2        R                  [        UUUS9nU	(       a/  U(       a(  UR5                  SU-  5        UR5                  SU-  5        UU4$ s  snf s  snf )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   
<J3-Jc                     [         R                  " U 5      R                  5       R                  5       n[        R                  UUUUS9$ )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                 R    [        U [        5      (       a  U $ U R                  U5      $ )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   