
                             d Z ddlmZ ddlmZ ddlmZ ddl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Zd	Zd
ZdZdZ G d dej*                        Zd Zd Zd Zd Zy)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                       e Zd ZdZy)InvalidCdnSignedUrlErrorzInvalid URL error.N)__name__
__module____qualname____doc__     8lib/googlecloudsdk/command_lib/compute/sign_url_utils.pyr   r   (   s    r   r   c                     t        j                  | |t        j                        j	                         }t        j                  |      S )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    ,   s4     hhsC.557)		!	!)	,,r   c                 B    t        t        j                         | z         S )z>Converts the number of seconds from now into a unix timestamp.)inttime)seconds_from_nows    r   _SecondsFromNowToUnixTimestampr%   ;   s    	TYY[++	,,r   c                    | j                         }t        j                  j                  j                  j                  |      }|j                  dk7  r|j                  dk7  rt        t              |j                  rt        t              t        j                  j                  j                  j                  |j                  d      }t        D ]$  }||v st        t        j                  |             dj                  ||rdndt!        |      |      }t#        t%        j&                  |      |j)                  d	            }	d
j                  |t+        j,                  |	            S )a`  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   SignUrlrI   @   sH   $ ,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                 f    t        j                         }|j                  d|       }|j                  S )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   ValidateSignedUrlrR   t   s0     ##%+%%fj9-		"	""r   )r   
__future__r   r   r   r   r   r   r#   googlecloudsdk.corer   core_exceptionsr   googlecloudsdk.core.utilr   six.moves.urllib.parser0   r6   r<   r8   r;   Errorr   r    r%   rI   rR   r   r   r   <module>rY      sr    6 &  '     = ( -  3 ' 5 !'J #B 44 --
1=h#r   