
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	J
r  SSK	Jr  SSKJr  SSKrS	rS
rSrSr " S S\R*                  5      rS rS rS rS rg)z/Utilities for generating Cloud CDN Signed URLs.    )absolute_import)division)unicode_literalsN)
exceptions)requests)encodingz,The URL scheme must be either HTTP or HTTPS.z,The URL must not have a '{}' query parameterz!The URL must not have a fragment.)ExpiresKeyName	Signaturec                       \ rS rSrSrSrg)InvalidCdnSignedUrlError(   zInvalid URL error. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       8lib/googlecloudsdk/command_lib/compute/sign_url_utils.pyr   r   (   s    r   r   c                     [         R                  " X[        R                  5      R	                  5       n[
        R                  " U5      $ )a  Gets the base64url encoded HMAC-SHA1 signature of the specified URL.

Args:
  key: The key value to use for signing.
  url: The url to use for signing.

Returns:
  The signature of the specified URL calculated using HMAC-SHA1 signature
  digest and encoding the result using base64url.
)hmacnewhashlibsha1digestbase64urlsafe_b64encode)keyurl	signatures      r   _GetSignaturer#   ,   s2     hhs.557)		!	!)	,,r   c                 D    [        [        R                  " 5       U -   5      $ )z>Converts the number of seconds from now into a unix timestamp.)inttime)seconds_from_nows    r   _SecondsFromNowToUnixTimestampr(   ;   s    	TYY[++	,,r   c                    U R                  5       n[        R                  R                  R                  R                  U5      nUR                  S:w  a  UR                  S:w  a  [        [        5      eUR                  (       a  [        [        5      e[        R                  R                  R                  R                  UR                  SS9n[         H&  nXv;   d  M
  [        [        R                  U5      5      e   SR                  UU(       a  SOS[!        U5      US9n[#        [$        R&                  " U5      UR)                  S	5      5      n	S
R                  U[*        R,                  " U	5      S9$ )aF  Gets the Signed URL string for the specified URL and configuration.

Args:
  url: The URL to sign.
  key_name: Signed URL key name to use for the 'KeyName=' query parameter.
  encoded_key_value: The base64url encoded key value to use for signing.
  validity_seconds: The number of seconds for which this signed URL will
      be valid, starting when this function is called.

Returns:
  Returns the Signed URL appended with the query parameters based on the
  specified configuration.

Raises:
  InvalidCdnSignedUrlError: if the URL is invalid and/or failed to parse the
      URL.
httpshttpT)keep_blank_valuesz3{url}{separator}Expires={expires}&KeyName={keyName}&?)r!   	separatorexpireskeyNamezutf-8z{url}&Signature={signature})r!   r"   )stripsixmovesurllibparseurlsplitschemer   &_URL_SCHEME_MUST_BE_HTTP_HTTPS_MESSAGEfragment#_URL_MUST_NOT_HAVE_FRAGMENT_MESSAGEparse_qsquery_DISALLOWED_QUERY_PARAMETERS _URL_MUST_NOT_HAVE_PARAM_MESSAGEformatr(   r#   r   urlsafe_b64decodeencoder   Decode)
r!   key_nameencoded_key_valuevalidity_secondsstripped_url
parsed_urlquery_paramsparamurl_to_signr"   s
             r   SignUrlrL   @   sF   $ ,yy%%..|<*'!j&7&76&A
"#I
JJ
"#F
GG!!''00$ 1 0, ,e$
*
1
1%
8: : , FLL
#,-=>	 M + 01;3E3Eg3NP)	&	-	-
!; 
. 
= =r   c                 h    [         R                  " 5       nUR                  SU 5      nUR                  $ )zValidates the Signed URL by returning the response code for HEAD request.

Args:
  signed_url: The Signed URL which should be validated.

Returns:
  Returns the response code for the HEAD request to the specified Signed
      URL.
HEAD)r   
GetSessionrequeststatus_code)
signed_urlhttp_clienthttp_responses      r   ValidateSignedUrlrU   t   s0     ##%+%%fj9-		"	""r   )r   
__future__r   r   r   r   r   r   r&   googlecloudsdk.corer   core_exceptionsr   googlecloudsdk.core.utilr   six.moves.urllib.parser3   r9   r?   r;   r>   Errorr   r#   r(   rL   rU   r   r   r   <module>r\      sr    6 &  '     = ( -  3 ' 5 !'J #B 44 --
1=h#r   