
?F[c           @   s  d  Z  d d l Z d d l Z d d l Z d d l 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 Z d d l Z d d l Z d d l m Z m Z m Z m Z d d l m Z d d l m Z y$ d d l m Z d d l m Z Wn# e k
 r%d d l Z e Z n Xd	 d
 d d d d d d d d d d d d d d d d d d g Z d d d d  d! d" d# d$ d% d& d' d( d) g Z d* e f d+     YZ d, e e f d-     YZ d. e e f d/     YZ  d0 e e f d1     YZ! d2 e e f d3     YZ" d4 e e f d5     YZ# d6 e e f d7     YZ$ d8 e$ e f d9     YZ% d: e f d;     YZ& d< e f d=     YZ' d> e' e f d?     YZ( d@ e' e f dA     YZ) dB e' e f dC     YZ* dD e* e f dE     YZ+ e dF  Z, dG   Z- dH   Z. d S(I   s/   
Handles authentication required to AWS and GS
iN(   t
   formatdate(   t   urllibt   encodebytest   parse_qs_safet   urlparse(   t   AuthHandler(   t   BotoClientError(   t   sha1(   t   sha256s   -ap-northeast-1s   .ap-northeast-1s   -ap-southeast-1s   .ap-southeast-1s   -ap-southeast-2s   .ap-southeast-2s
   -eu-west-1s
   .eu-west-1s   -external-1s   .external-1s
   -sa-east-1s
   .sa-east-1s
   -us-east-1s
   .us-east-1s   -us-gov-west-1s   .us-gov-west-1s
   -us-west-1s
   .us-west-1s
   -us-west-2s
   .us-west-2s   .cn-s   .eu-centrals   -eu-centrals   .ap-northeast-2s   -ap-northeast-2s   .ap-south-1s   -ap-south-1s
   .us-east-2s
   -us-east-2s   -ca-centrals   .ca-centrals
   .eu-west-2s
   -eu-west-2t   HmacKeysc           B   sM   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 RS(   s   Key based Auth handler helper.c         C   sJ   | j  d  k s | j d  k r0 t j j    n  | |  _ |  j |  d  S(   N(   t
   access_keyt   Nonet
   secret_keyt   botot   auth_handlert   NotReadyToAuthenticatet   hostt   update_provider(   t   selfR   t   configt   provider(    (    s(   lib/python2.7/site-packages/boto/auth.pyt   __init__e   s    	c         C   sm   | |  _  t j |  j  j j d  d t |  _ t r` t j |  j  j j d  d t |  _ n	 d  |  _ d  S(   Ns   utf-8t	   digestmod(
   t	   _providert   hmact   newR   t   encodet   shat   _hmacR   t	   _hmac_256R   (   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR   k   s    	c         C   s   |  j  r d Sd Sd  S(   Nt
   HmacSHA256t   HmacSHA1(   R   (   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt	   algorithmu   s    	c         C   s:   |  j  r t } n t } t j |  j j j d  d | S(   Ns   utf-8R   (   R   R   R   R   R   R   R   R   (   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt	   _get_hmac{   s
    		c         C   sA   |  j    } | j | j d   t | j    j d  j   S(   Ns   utf-8(   R!   t   updateR   R   t   digestt   decodet   strip(   R   t   string_to_signt   new_hmac(    (    s(   lib/python2.7/site-packages/boto/auth.pyt   sign_string   s    c         C   s$   t  j  |  j  } | d =| d =| S(   NR   R   (   t   copyt   __dict__(   R   t   pickled_dict(    (    s(   lib/python2.7/site-packages/boto/auth.pyt   __getstate__   s    c         C   s   | |  _  |  j |  j  d  S(   N(   R*   R   R   (   R   t   dct(    (    s(   lib/python2.7/site-packages/boto/auth.pyt   __setstate__   s    	(
   t   __name__t
   __module__t   __doc__R   R   R    R!   R(   R,   R.   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyR	   b   s   		
				t   AnonAuthHandlerc           B   s)   e  Z d  Z d g Z d   Z d   Z RS(   s(   
    Implements Anonymous requests.
    t   anonc         C   s    t  t |   j | | |  d  S(   N(   t   superR2   R   (   R   R   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR      s    c         K   s   d  S(   N(    (   R   t   http_requestt   kwargs(    (    s(   lib/python2.7/site-packages/boto/auth.pyt   add_auth   s    (   R/   R0   R1   t
   capabilityR   R7   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyR2      s   		t   HmacAuthV1Handlerc           B   s5   e  Z d  Z d d g Z d   Z d   Z d   Z RS(   s:       Implements the HMAC request signing used by S3 and GS.s   hmac-v1t   s3c         C   s9   t  j |  | | |  t j |  | | |  d  |  _ d  S(   N(   R   R   R	   R   R   (   R   R   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR      s    c         C   s#   t  t |   j |  d  |  _ d  S(   N(   R4   R9   R   R   R   (   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR      s    c         K   s   | j  } | j } | j } d | k r= t d t  | d <n  |  j j rh |  j j } |  j j | | <n  t j	 j
 | | | d  |  j  } t j j d |  |  j |  } |  j j }	 d |	 |  j j | f }
 t j j d |
  |
 | d <d  S(   Nt   Datet   usegmts   StringToSign:
%ss   %s %s:%ss   Signature:
%st   Authorization(   t   headerst   methodt	   auth_pathR    t   TrueR   t   security_tokent   security_token_headerR   t   utilst   canonical_stringR   t   logt   debugR(   t   auth_headerR
   (   R   R5   R6   R>   R?   R@   t   keyR&   t   b64_hmact   auth_hdrt   auth(    (    s(   lib/python2.7/site-packages/boto/auth.pyR7      s"    			(   R/   R0   R1   R8   R   R   R7   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyR9      s
   		t   HmacAuthV2Handlerc           B   s5   e  Z d  Z d d g Z d   Z d   Z d   Z RS(   sJ   
    Implements the simplified HMAC authorization used by CloudFront.
    s   hmac-v2t
   cloudfrontc         C   s9   t  j |  | | |  t j |  | | |  d  |  _ d  S(   N(   R   R   R	   R   R   (   R   R   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR      s    c         C   s#   t  t |   j |  d  |  _ d  S(   N(   R4   RM   R   R   R   (   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR      s    c         K   s   | j  } d | k r+ t d t  | d <n  |  j j rV |  j j } |  j j | | <n  |  j | d  } |  j j } d | |  j j | f | d <d  S(   NR;   R<   s   %s %s:%sR=   (	   R>   R    RA   R   RB   RC   R(   RH   R
   (   R   R5   R6   R>   RI   RJ   RK   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR7      s    	(   R/   R0   R1   R8   R   R   R7   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyRM      s
   		t   HmacAuthV3Handlerc           B   s/   e  Z d  Z d d d g Z d   Z d   Z RS(   s@   Implements the new Version 3 HMAC authorization used by Route53.s   hmac-v3t   route53t   sesc         C   s0   t  j |  | | |  t j |  | | |  d  S(   N(   R   R   R	   (   R   R   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR      s    c         K   s   | j  } d | k r+ t d t  | d <n  |  j j rV |  j j } |  j j | | <n  |  j | d  } d |  j j } | d |  j   | f 7} | | d <d  S(   NR;   R<   s   AWS3-HTTPS AWSAccessKeyId=%s,s   Algorithm=%s,Signature=%ss   X-Amzn-Authorization(	   R>   R    RA   R   RB   RC   R(   R
   R    (   R   R5   R6   R>   RI   RJ   t   s(    (    s(   lib/python2.7/site-packages/boto/auth.pyR7      s    	(   R/   R0   R1   R8   R   R7   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyRO      s   	t   HmacAuthV3HTTPHandlerc           B   sD   e  Z d  Z d g Z d   Z d   Z d   Z d   Z d   Z RS(   sK   
    Implements the new Version 3 HMAC authorization used by DynamoDB.
    s   hmac-v3-httpc         C   s0   t  j |  | | |  t j |  | | |  d  S(   N(   R   R   R	   (   R   R   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR     s    c         C   s\   i |  j  d 6} xE | j j   D]4 \ } } | j   } | j d  r  | | | <q  q  W| S(   sk   
        Select the headers from the request that need to be included
        in the StringToSign.
        t   Hosts   x-amz(   R   R>   t   itemst   lowert
   startswith(   R   R5   t   headers_to_signt   namet   valuet   lname(    (    s(   lib/python2.7/site-packages/boto/auth.pyRX     s    c         C   sL   t  g  | D], } d | j   j   | | j   f ^ q
  } d j |  S(   s  
        Return the headers that need to be included in the StringToSign
        in their canonical form by converting all header keys to lower
        case, sorting them in alphabetical order and then joining
        them into a string, separated by newlines.
        s   %s:%ss   
(   t   sortedRV   R%   t   join(   R   RX   t   nt   l(    (    s(   lib/python2.7/site-packages/boto/auth.pyt   canonical_headers  s    9c         C   sR   |  j  |  } |  j |  } d j | j | j d | d | j g  } | | f S(   s   
        Return the canonical StringToSign as well as a dict
        containing the original version of all headers that
        were included in the StringToSign.
        s   
t    (   RX   R`   R]   R?   R@   t   body(   R   R5   RX   R`   R&   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR&     s    c         K   s  d | j  k r | j  d =n  t d t  | j  d <|  j j rT |  j j | j  d <n  |  j |  \ } } t j j d |  t	 | j
 d   j   } |  j |  } d |  j j } | d |  j   7} | d	 d
 j |  7} | d | 7} | | j  d <d S(   s   
        Add AWS3 authentication to a request.

        :type req: :class`boto.connection.HTTPRequest`
        :param req: The HTTPRequest object.
        s   X-Amzn-AuthorizationR<   s
   X-Amz-Dates   X-Amz-Security-Tokens   StringToSign:
%ss   utf-8s   AWS3 AWSAccessKeyId=%s,s   Algorithm=%s,s   SignedHeaders=%s,t   ;s   Signature=%sN(   R>   R    RA   R   RB   R&   R   RF   RG   R   R   R#   R(   R
   R    R]   (   R   t   reqR6   R&   RX   t
   hash_valueRJ   RR   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR7   ,  s    	(	   R/   R0   R1   R8   R   RX   R`   R&   R7   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyRS      s   					t   HmacAuthV4Handlerc           B   s   e  Z d  Z d g Z d d d  Z e d  Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s:   
    Implements the new Version 4 HMAC authorization.
    s   hmac-v4c         C   sB   t  j |  | | |  t j |  | | |  | |  _ | |  _ d  S(   N(   R   R   R	   t   service_namet   region_name(   R   R   R   R   Rg   Rh   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR   L  s    	c         C   sv   t  | t  s! | j d  } n  | rN t j | | j d  t  j   } n$ t j | | j d  t  j   } | S(   Ns   utf-8(   t
   isinstancet   bytesR   R   R   R   t	   hexdigestR#   (   R   RI   t   msgt   hext   sig(    (    s(   lib/python2.7/site-packages/boto/auth.pyt   _signV  s    '$c         C   s   |  j  |  j |  } | j j d  r7 | j d } n  i | d 6} xf | j j   D]U \ } } | j   } | j d  rT t | t  r | j	 d  } n  | | | <qT qT W| S(   sk   
        Select the headers from the request that need to be included
        in the StringToSign.
        RT   s   x-amzs   utf-8(
   t   host_headerR   R>   t   getRU   RV   RW   Ri   Rj   R$   (   R   R5   t   host_header_valueRX   RY   RZ   R[   (    (    s(   lib/python2.7/site-packages/boto/auth.pyRX   `  s    c         C   sO   | j  } | j d k } | d k r+ | s= | d k rA | rA | Sd | | f S(   Nt   httpsiP   i  s   %s:%s(   t   portt   protocol(   R   R   R5   Rt   t   secure(    (    s(   lib/python2.7/site-packages/boto/auth.pyRp   q  s
    	%c         C   s   t  | j j    } g  } x` | D]X } t j j | j |  } | j t j j	 | d d d t j j	 | d d  q" Wd j
 |  S(   Nt   safeRa   t   =s   -_~t   &(   R\   t   paramst   keysR   RD   t   get_utf8_valuet   appendR   t   parset   quoteR]   (   R   R5   t   parameter_namest   pairst   pnamet   pval(    (    s(   lib/python2.7/site-packages/boto/auth.pyt   query_stringx  s    c         C   s   | j  d k r d Sg  } xk t | j  D]Z } t j j | j |  } | j d t j j	 | d d t j j	 | d d f  q) Wd j
 |  S(   Nt   POSTRa   s   %s=%sRw   s   -_.~Ry   (   R?   R\   Rz   R   RD   R|   R}   R   R~   R   R]   (   R   R5   R_   t   paramRZ   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt   canonical_query_string  s    !c         C   s   g  } x} | D]u } | j    j   } t | |  } d | k rP | j   } n d j | j   j    } | j d | | f  q Wd j t |   S(   s  
        Return the headers that need to be included in the StringToSign
        in their canonical form by converting all header keys to lower
        case, sorting them in alphabetical order and then joining
        them into a string, separated by newlines.
        t   "t    s   %s:%ss   
(   RV   R%   t   strR]   t   splitR}   R\   (   R   RX   t	   canonicalt   headert   c_namet	   raw_valuet   c_value(    (    s(   lib/python2.7/site-packages/boto/auth.pyR`     s    c         C   sB   g  | D] } d | j    j   ^ q } t |  } d j |  S(   Ns   %sRc   (   RV   R%   R\   R]   (   R   RX   R^   R_   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt   signed_headers  s    )c         C   sh   | j  } t j |  j d d  } t j j |  } t |  d k rd | j d  rd | d 7} n  | S(   Ns   \t   /i   (	   R@   t	   posixpatht   normpatht   replaceR   R~   R   t   lent   endswith(   R   R5   t   patht
   normalizedt   encoded(    (    s(   lib/python2.7/site-packages/boto/auth.pyt   canonical_uri  s    	!c         C   sr   | j  } t | d  rA t | d  rA t j j | d t d St | t  sb | j d  } n  t |  j	   S(   Nt   seekt   readt   hash_algorithmi    s   utf-8(
   Rb   t   hasattrR   RD   t   compute_hashR   Ri   Rj   R   Rk   (   R   R5   Rb   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt   payload  s    	c         C   s   | j  j   g } | j |  j |   | j |  j |   |  j |  } | j |  j |  d  | j |  j |   | j |  j |   d j	 |  S(   Ns   
(
   R?   t   upperR}   R   R   RX   R`   R   R   R]   (   R   R5   t   crRX   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt   canonical_request  s    c         C   sY   |  j  j g } | j | j  | j | j  | j | j  | j d  d j |  S(   Nt   aws4_requestR   (   R   R
   R}   t	   timestampRh   Rg   R]   (   R   R5   t   scope(    (    s(   lib/python2.7/site-packages/boto/auth.pyR     s    c         C   s   | j  d  S(   Nt   .(   R   (   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt   split_host_parts  s    c         C   s   |  j  |  } |  j d  k	 r* |  j } n] t |  d k r} | d d k rU d } q t |  d k rp d } q | d } n
 | d } | S(   Ni   s   us-govs   us-gov-west-1i   s	   us-east-1i    (   R   Rh   R   R   (   R   R   t   partsRh   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt   determine_region_name  s    		
c         C   s8   |  j  |  } |  j d  k	 r* |  j } n
 | d } | S(   Ni    (   R   Rg   R   (   R   R   R   Rg   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt   determine_service_name  s
    
c         C   s   g  } | j  d d d !| _ | j | j  |  j | j  } |  j | j  } | | _ | | _ | j | j  | j | j  | j d  d j |  S(   Ns
   X-Amz-Datei    i   R   R   (	   R>   R   R}   R   R   R   Rg   Rh   R]   (   R   R5   R   Rh   Rg   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt   credential_scope  s    		c         C   sb   d g } | j  | j d  | j  |  j |   | j  t | j d   j    d j |  S(   s   
        Return the canonical StringToSign as well as a dict
        containing the original version of all headers that
        were included in the StringToSign.
        s   AWS4-HMAC-SHA256s
   X-Amz-Dates   utf-8s   
(   R}   R>   R   R   R   Rk   R]   (   R   R5   R   t   sts(    (    s(   lib/python2.7/site-packages/boto/auth.pyR&     s
    	"c         C   s   |  j  j } |  j d | j d  | j  } |  j | | j  } |  j | | j  } |  j | d  } |  j | | d t S(   Nt   AWS4s   utf-8R   Rm   (   R   R   Ro   R   R   Rh   Rg   RA   (   R   R5   R&   RI   t   k_datet   k_regiont	   k_servicet	   k_signing(    (    s(   lib/python2.7/site-packages/boto/auth.pyt	   signature  s    c         K   s  d | j  k r | j  d =n  t j j   } | j d  | j  d <|  j j rc |  j j | j  d <n  |  j |  } | } d | k r |  j | d  } n  | r | j d k r | | _ d | j  d <t	 t
 | j   | j  d	 <n6 | j j d
  d | _ | r| j d
 | | _ n  |  j |  } t j j d |  |  j | |  } t j j d |  |  j | |  } t j j d |  |  j |  }	 d |  j |  g }
 |
 j d |  j |	   |
 j d |  d j |
  | j  d <d S(   s   
        Add AWS4 authentication to a request.

        :type req: :class`boto.connection.HTTPRequest`
        :param req: The HTTPRequest object.
        s   X-Amzn-Authorizations   %Y%m%dT%H%M%SZs
   X-Amz-Dates   X-Amz-Security-Tokent   unmangled_reqR   s0   application/x-www-form-urlencoded; charset=UTF-8s   Content-Types   Content-Lengtht   ?i    s   CanonicalRequest:
%ss   StringToSign:
%ss   Signature:
%ss   AWS4-HMAC-SHA256 Credential=%ss   SignedHeaders=%ss   Signature=%st   ,R=   N(   R>   t   datetimet   utcnowt   strftimeR   RB   R   R?   Rb   R   R   R   R   R   R   RF   RG   R&   R   RX   R   R}   R   R]   (   R   Rd   R6   t   nowt   qst
   qs_to_postR   R&   R   RX   R_   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR7     s8    		N(   R/   R0   R1   R8   R   R   t   FalseRo   RX   Rp   R   R   R`   R   R   R   R   R   R   R   R   R   R&   R   R7   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyRf   E  s*   		
										
								t   S3HmacAuthV4Handlerc           B   s   e  Z d  Z d g Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z d d  Z RS(   sN   
    Implements a variant of Version 4 HMAC authorization specific to S3.
    s
   hmac-v4-s3c         O   s>   t  t |   j | |   |  j r: |  j |  j  |  _ n  d  S(   N(   R4   R   R   Rh   t   clean_region_name(   R   t   argsR6   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR   O  s    	c         C   s   | j  d  r | d S| S(   Ns   s3-i   (   RW   (   R   Rh   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR   U  s    c         C   sF   t  j j | j  } t  j j | j  } t  j j | d d } | S(   NRw   s   /~(   R   R~   R   R   t   unquoteR   (   R   R5   R   t   unquotedR   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR   [  s    c         C   s   g  } xk t  | j  D]Z } t j j | j |  } | j d t j j | d d t j j | d d f  q Wd j	 |  S(   Ns   %s=%sRw   s   -_.~Ry   (
   R\   Rz   R   RD   R|   R}   R   R~   R   R]   (   R   R5   R_   R   RZ   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR   e  s    !c         C   sU   | j  } | j d k } | d k r+ | s= | d k rD | rD | j Sd | j | f S(   NRs   iP   i  s   %s:%s(   Rt   Ru   R   (   R   R   R5   Rt   Rv   (    (    s(   lib/python2.7/site-packages/boto/auth.pyRp   p  s
    	%c         C   sk   |  j  |  j |  } i | d 6} xB | j j   D]1 \ } } | j   } | d k r2 | | | <q2 q2 W| S(   sk   
        Select the headers from the request that need to be included
        in the StringToSign.
        RT   t   authorization(   R   (   Rp   R   R>   RU   RV   (   R   R5   Rr   RX   RY   RZ   R[   (    (    s(   lib/python2.7/site-packages/boto/auth.pyRX   w  s    c         C   s   |  j  |  } |  j d  k	 r* |  j } n t |  d k rg |  j | d  } | d k r d } q n x~ t t |   D]j \ } } | j   } | d k r | | } | d k r d } n  Pqz | j d  rz |  j |  } Pqz qz W| S(   Ni   i    R:   s	   us-east-1t	   amazonawss   s3-(	   R   Rh   R   R   R   t	   enumeratet   reversedRV   RW   (   R   R   R   Rh   t   offsett   part(    (    s(   lib/python2.7/site-packages/boto/auth.pyR     s$    	c         C   s   d S(   NR:   (    (   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR     s    c   	      C   s   t  j  |  } t j j | j  } | j | _ | j d k rK i  | _ n | j j    } | | _ | j } t	 | d t
 } xU | j   D]G \ } } t | t t f  r t |  d k r | d | | <q q q W| j j |  | S(   s|   
        Returns a copy of the request object with fixed ``auth_path/params``
        attributes from the original.
        t   keep_blank_valuesi   i    N(   R)   R   R~   R   R@   R   Rz   R   t   queryR   RA   RU   Ri   t   listt   tupleR   R"   (	   R   Rd   t   modified_reqt   parsed_patht   copy_paramst   raw_qst   existing_qsRI   RZ   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt   mangle_path_and_params  s"    			c         C   s3   | j  j d  r | j  d St t |   j |  S(   Ns   x-amz-content-sha256(   R>   Rq   R4   R   R   (   R   R5   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR     s    c         K   s   d | j  k rS d | j  k r: | j  j d  | j  d <qS |  j |  | j  d <n  |  j |  } t t |   j | d | | S(   Ns   x-amz-content-sha256t   _sha256R   (   R>   t   popR   R   R4   R   R7   (   R   Rd   R6   t   updated_req(    (    s(   lib/python2.7/site-packages/boto/auth.pyR7     s    c         C   s  | d k r' t j j   j d  } n  |  j | j  } |  j | j  } i d d 6d |  j j | d  | | f d 6| d 6| d 6d	 d
 6} |  j j	 r |  j j	 | d <n  |  j
 |  } t g  | D] } d | j   j   ^ q  }	 d j |	  | d
 <| j j |  |  j |  }
 d j |
 j d  d   d }
 | | j d <|  j | |
  } |  j | |  } | | j d <d | j | j | j t j j | j  f S(   s   
        Presign a request using SigV4 query params. Takes in an HTTP request
        and an expiration time in seconds and returns a URL.

        http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
        s   %Y%m%dT%H%M%SZs   AWS4-HMAC-SHA256s   X-Amz-Algorithms   %s/%s/%s/%s/aws4_requesti   s   X-Amz-Credentials
   X-Amz-Dates   X-Amz-ExpiresR   s   X-Amz-SignedHeaderss   X-Amz-Security-Tokens   %sRc   s   
is   
UNSIGNED-PAYLOADs   X-Amz-Signatures   %s://%s%s?%sN(   R   R   R   R   R   R   R   R   R
   RB   RX   R\   RV   R%   R]   Rz   R"   R   R   R>   R&   R   Ru   R   R   R~   t	   urlencode(   R   Rd   t   expirest   iso_datet   regiont   serviceRz   RX   R^   R_   R   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt   presign  s8    	
/ N(   R/   R0   R1   R8   R   R   R   R   Rp   RX   R   R   R   R   R7   R   R   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyR   I  s   				
				-		*		t   STSAnonHandlerc           B   s2   e  Z d  Z d g Z d   Z d   Z d   Z RS(   s   
    Provides pure query construction (no actual signing).

    Used for making anonymous STS request for operations like
    ``assume_role_with_web_identity``.
    s   sts-anonc         C   s   t  j j |  S(   N(   R   R~   R   (   R   RZ   (    (    s(   lib/python2.7/site-packages/boto/auth.pyt   _escape_value4  s    c         C   s   t  | j    } | j d d    g  } xK | D]C } t j j | |  } | j | d |  j | j d    q2 Wd j	 |  S(   NRI   c         S   s
   |  j    S(   N(   RV   (   t   x(    (    s(   lib/python2.7/site-packages/boto/auth.pyt   <lambda>=  s    Rx   s   utf-8Ry   (
   R   R{   t   sortR   RD   R|   R}   R   R$   R]   (   R   Rz   R{   R   RI   t   val(    (    s(   lib/python2.7/site-packages/boto/auth.pyt   _build_query_string;  s    +c         K   sF   | j  } |  j | j  } t j j d |  d | d <| | _ d  S(   Ns   query_string in body: %ss!   application/x-www-form-urlencodeds   Content-Type(   R>   R   Rz   R   RF   RG   Rb   (   R   R5   R6   R>   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR7   D  s    	
(   R/   R0   R1   R8   R   R   R7   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyR   *  s
   				t   QuerySignatureHelperc           B   s   e  Z d  Z d   Z RS(   sy   
    Helper for Query signature based Auth handler.

    Concrete sub class need to implement _calc_sigature method.
    c         K   s)  | j  } | j } |  j j | d <|  j | d <t j j   | d <|  j | j | j	 | j
 | j  \ } } t j j d | | f  | j	 d k r d | d <| d t j j |  | _ t t | j   | j  d	 <nJ d
 | _ | j j d  d | _ | j d | d t j j |  | _ d  S(   Nt   AWSAccessKeyIdt   SignatureVersiont	   Timestamps   query_string: %s Signature: %sR   s0   application/x-www-form-urlencoded; charset=UTF-8s   Content-Types   &Signature=s   Content-LengthRa   R   i    (   R>   Rz   R   R
   R   R   RD   t   get_tst   _calc_signatureR?   R@   R   RF   RG   R   R~   t
   quote_plusRb   R   R   R   R   (   R   R5   R6   R>   Rz   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR7   W  s     		
	(   R/   R0   R1   R7   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyR   P  s   t   QuerySignatureV0AuthHandlerc           B   s&   e  Z d  Z d Z d g Z d   Z RS(   s   Provides Signature V0 Signingi    s   sign-v0c   
      G   s   t  j j d  |  j   } | d | d } | j | j d   | j   } | j d d    g  } xE | D]= } t  j j	 | |  } | j
 | d t j j |   qp Wd j |  }	 |	 t j | j    f S(	   Ns   using _calc_signature_0t   ActionR   s   utf-8t   cmpc         S   s   t  |  j   | j    S(   N(   R   RV   (   R   t   y(    (    s(   lib/python2.7/site-packages/boto/auth.pyR   z  s    Rx   Ry   (   R   RF   RG   R!   R"   R   R{   R   RD   R|   R}   R   R~   R   R]   t   base64t	   b64encodeR#   (
   R   Rz   R   R   RR   R{   R   RI   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR   t  s    %(   R/   R0   R1   R   R8   R   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyR   n  s   	t   QuerySignatureV1AuthHandlerc           B   s2   e  Z d  Z d Z d d g Z d   Z d   Z RS(   s5   
    Provides Query Signature V1 Authentication.
    i   s   sign-v1t   mturkc         O   s3   t  j |  | |  t j |  | |  d  |  _ d  S(   N(   R   R   R   R   R   (   R   R   t   kw(    (    s(   lib/python2.7/site-packages/boto/auth.pyR     s    c   	      G   s   t  j j d  |  j   } t | j    } | j d d    g  } xh | D]` } | j | j d   t  j	 j
 | |  } | j |  | j | d t j j |   qN Wd j |  } | t j | j    f S(   Ns   using _calc_signature_1RI   c         S   s
   |  j    S(   N(   RV   (   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR     s    s   utf-8Rx   Ry   (   R   RF   RG   R!   R   R{   R   R"   R   RD   R|   R}   R   R~   R   R]   R   R   R#   (	   R   Rz   R   R   R{   R   RI   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR     s    %(   R/   R0   R1   R   R8   R   R   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyR     s
   	t   QuerySignatureV2AuthHandlerc           B   sG   e  Z d  Z d Z d d d d d d d d d	 d
 d d g Z d   Z RS(   s+   Provides Query Signature V2 Authentication.i   s   sign-v2t   ec2t   emrt   fpst   ecst   sdbt   iamt   rdst   snst   sqst   cloudformationc         C   s  t  j j d  d | | j   | f } |  j   } |  j   | d <|  j j rd |  j j | d <n  t | j	    } g  } x] | D]U }	 t  j
 j | |	  }
 | j t j j |	 d d d t j j |
 d d  q Wd	 j |  } t  j j d
 |  | | 7} t  j j d |  | j | j d   t j | j    } t  j j d t |   t  j j d |  | | f S(   Ns   using _calc_signature_2s	   %s
%s
%s
t   SignatureMethodt   SecurityTokenRw   Ra   Rx   s   -_~Ry   s   query string: %ss   string_to_sign: %ss   utf-8s   len(b64)=%ds   base64 encoded digest: %s(   R   RF   RG   RV   R!   R    R   RB   R\   R{   RD   R|   R}   R   R~   R   R]   R"   R   R   R   R#   R   (   R   Rz   t   verbR   t   server_nameR&   R   R{   R   RI   R   R   t   b64(    (    s(   lib/python2.7/site-packages/boto/auth.pyR     s*    
(   R/   R0   R1   R   R8   R   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyR     s
   t   POSTPathQSV2AuthHandlerc           B   s    e  Z d  Z d g Z d   Z RS(   s   
    Query Signature V2 Authentication relocating signed query
    into the path and allowing POST requests with Content-Types.
    t   mwsc         K   s  |  j  j | j d <|  j | j d <t j j   | j d <|  j | j | j | j	 | j
  \ } } t j j d | | f  | j d k r t t | j   | j d <| j j d d  | j d <n	 d	 | _ | j j d
  d | _ | j d
 | d t j j |  | _ d  S(   NR   R   R   s   query_string: %s Signature: %sR   s   Content-Lengths   Content-Types
   text/plainRa   R   i    s   &Signature=(   R   R
   Rz   R   R   RD   R   R   R?   R@   R   RF   RG   R   R   Rb   R>   Rq   R   R   R   R~   R   (   R   Rd   R6   R   R   (    (    s(   lib/python2.7/site-packages/boto/auth.pyR7     s    	(   R/   R0   R1   R8   R7   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyR    s   	c   	      C   s   g  } t  j j t |  } xE | D]= } y | j | |  | |   Wq" t  j j k
 r^ q" Xq" W| s | } g  | D] } | j ^ qv } t  j j	 d t
 |  t |  f   n  | d S(   s  Finds an AuthHandler that is ready to authenticate.

    Lists through all the registered AuthHandlers to find one that is willing
    to handle for the requested capabilities, config and provider.

    :type host: string
    :param host: The name of the host

    :type config:
    :param config:

    :type provider:
    :param provider:

    Returns:
        An implementation of AuthHandler.

    Raises:
        boto.exception.NoAuthHandlerFound
    sY   No handler was ready to authenticate. %d handlers were checked. %s Check your credentialsi(   R   t   plugint
   get_pluginR   R}   R   R   R/   t	   exceptiont   NoAuthHandlerFoundR   R   (	   R   R   R   t   requested_capabilityt   ready_handlerst   auth_handlerst   handlert   checked_handlerst   names(    (    s(   lib/python2.7/site-packages/boto/auth.pyt   get_auth_handler  s    	c            s     f d   } | S(   Nc            s   t  j j d t  r d g St j j d d t  r; d g St |  d  r t |  j d d  r x* t	 D] } | |  j j
 k rf d g Sqf Wq n    |   S(   Nt   EC2_USE_SIGV4s   hmac-v4R   s	   use-sigv4R   t   endpointRa   (   t   ost   environRq   R   R   R   R   t   getattrR   t   SIGV4_DETECTR  (   R   t   test(   t   func(    s(   lib/python2.7/site-packages/boto/auth.pyt   _wrapper  s    (    (   R  R  (    (   R  s(   lib/python2.7/site-packages/boto/auth.pyt   detect_potential_sigv4  s    c            s     f d   } | S(   Nc            sU  t  j j d t  r d g St j j d d t  r; d g St   d  sT     Sx$ t D] } |   j k r[ d g Sq[ W  j }   j j	 d  s   j j	 d  r d | } n  t
 |  j } | j d  p | j d	  s     S| j d
  r    St   f d   t D  r,    St   d  rN  j rN    Sd g S(   Nt   S3_USE_SIGV4s
   hmac-v4-s3R:   s	   use-sigv4R   s   http://s   https://s   amazonaws.coms   amazonaws.com.cns   s3.amazonaws.comc         3   s   |  ] } |   j  k Vq d  S(   N(   R   (   t   .0R  (   R   (    s(   lib/python2.7/site-packages/boto/auth.pys	   <genexpr>A  s    R3   (   R  R  Rq   R   R   R   R   R  R   RW   R   t   netlocR   t   anyt   S3_AUTH_DETECTR3   (   R   R  R   R  (   R  (   R   s(   lib/python2.7/site-packages/boto/auth.pyR  "  s0    
	



(    (   R  R  (    (   R  s(   lib/python2.7/site-packages/boto/auth.pyt   detect_potential_s3sigv4!  s    )(/   R1   R   R   t   boto.auth_handlert   boto.exceptiont   boto.plugint
   boto.utilsR)   R   t   email.utilsR    R   R  R   t   boto.compatR   R   R   R   R   R   t   hashlibR   R   R   t   ImportErrorR   R  R  t   objectR	   R2   R9   RM   RO   RS   Rf   R   R   R   R   R   R   R  R  R  R   (    (    (    s(   lib/python2.7/site-packages/boto/auth.pyt   <module>   s   "
		1#K &2	