ó
i4Vdc           @  s®  d  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 Z d d l Z d d l	 Z	 d d l
 m Z d d l m Z d d l m Z m Z d d l m Z d d l m Z m Z m Z d d	 l m Z e j d
 ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z d „  Z  d d „ Z! d e" f d „  ƒ  YZ# d d e# e% d „ Z& d d e# d d „ Z' d e" f d „  ƒ  YZ( d e( f d „  ƒ  YZ) d S(   u`  
Functions for creating and restoring url-safe signed JSON objects.

The format used looks like this:

>>> signing.dumps("hello")
'ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk'

There are two components here, separated by a ':'. The first component is a
URLsafe base64 encoded JSON of the object passed to dumps(). The second
component is a base64 encoded hmac/SHA1 hash of "$first_component:$secret"

signing.loads(s) checks the signature and returns the deserialized object.
If the signature fails, a BadSignature exception is raised.

>>> signing.loads("ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk")
u'hello'
>>> signing.loads("ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk-modified")
...
BadSignature: Signature failed: ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk-modified

You can optionally compress the JSON prior to base64 encoding it to save
space, using the compress=True argument. This checks if compression actually
helps and only applies compression if the result is a shorter string:

>>> signing.dumps(range(1, 20), compress=True)
'.eJwFwcERACAIwLCF-rCiILN47r-GyZVJsNgkxaFxoDgxcOHGxMKD_T7vhAml:1QaUaL:BA0thEZrp4FQVXIXuOvYJtLJSrQ'

The fact that the string is compressed is signalled by the prefixed '.' at the
start of the base64 JSON.

There are 65 url-safe characters: the 64 used by url-safe base64 and the ':'.
These functions make use of all of them.
iÿÿÿÿ(   t   unicode_literalsN(   t   settings(   t   baseconv(   t   constant_time_comparet   salted_hmac(   t   RemovedInDjango110Warning(   t   force_bytest	   force_strt
   force_text(   t   import_stringu   ^[A-z0-9-_=]*$t   BadSignaturec           B  s   e  Z d  Z RS(   u"   
    Signature does not match
    (   t   __name__t
   __module__t   __doc__(    (    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR
   8   s   t   SignatureExpiredc           B  s   e  Z d  Z RS(   u<   
    Signature timestamp is older than required max_age
    (   R   R   R   (    (    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR   ?   s   c         C  s   t  j |  ƒ j d ƒ S(   Nt   =(   t   base64t   urlsafe_b64encodet   strip(   t   s(    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyt
   b64_encodeF   s    c         C  s&   d t  |  ƒ d } t j |  | ƒ S(   NR   i   (   t   lenR   t   urlsafe_b64decode(   R   t   pad(    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyt
   b64_decodeJ   s    c         C  s   t  t |  | | ƒ j ƒ  ƒ S(   N(   R   R   t   digest(   t   saltt   valuet   key(    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyt   base64_hmacO   s    u%   django.core.signing.get_cookie_signerc         C  s2   t  t j ƒ } t t j ƒ } | d | d |  ƒS(   Ns   django.http.cookiesR   (   R	   R   t   SIGNING_BACKENDR   t
   SECRET_KEY(   R   t   SignerR   (    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyt   get_cookie_signerS   s    t   JSONSerializerc           B  s    e  Z d  Z d „  Z d „  Z RS(   uW   
    Simple wrapper around json to be used in signing.dumps and
    signing.loads.
    c         C  s   t  j | d d ƒj d ƒ S(   Nt
   separatorsu   ,u   :u   latin-1(   u   ,u   :(   t   jsont   dumpst   encode(   t   selft   obj(    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR%   ^   s    c         C  s   t  j | j d ƒ ƒ S(   Nu   latin-1(   R$   t   loadst   decode(   R'   t   data(    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR)   a   s    (   R   R   R   R%   R)   (    (    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR"   Y   s   	u   django.core.signingc   	      C  s“   | ƒ  j  |  ƒ } t } | r[ t j | ƒ } t | ƒ t | ƒ d k  r[ | } t } q[ n  t | ƒ } | rz d | } n  t | d | ƒj | ƒ S(   u‹  
    Returns URL-safe, sha1 signed base64 compressed JSON string. If key is
    None, settings.SECRET_KEY is used instead.

    If compress is True (not the default) checks if compressing using zlib can
    save some space. Prepends a '.' to signify compression. This is included
    in the signature, to protect against zip bombs.

    Salt can be used to namespace the hash, so that a signed string is
    only valid for a given namespace. Leaving this at the default
    value or re-using a salt value across different parts of your
    application without good cause is a security risk.

    The serializer is expected to return a bytestring.
    i   t   .R   (	   R%   t   Falset   zlibt   compressR   t   TrueR   t   TimestampSignert   sign(	   R(   R   R   t
   serializerR/   R+   t   is_compressedt
   compressedt   base64d(    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR%   e   s    c         C  s„   t  t | d | ƒj |  d | ƒƒ } t } | d  d k rP | d } t } n  t | ƒ } | rt t j | ƒ } n  | ƒ  j | ƒ S(   u}   
    Reverse of dumps(), raises BadSignature if signature fails.

    The serializer is expected to accept a bytestring.
    R   t   max_agei   R,   (	   R   R1   t   unsignR-   R0   R   R.   t
   decompressR)   (   R   R   R   R3   R7   R6   R9   R+   (    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR)   †   s    '
	R    c           B  s5   e  Z d d  d d „ Z d „  Z d „  Z d „  Z RS(   u   :c         C  sy   | p t  j |  _ t | ƒ |  _ t j |  j ƒ rJ t j d | t	 ƒ n  t | pl d |  j
 j |  j
 j f ƒ |  _ d  S(   NuJ   Unsafe Signer separator: %r (cannot be empty or consist of only A-z0-9-_=)u   %s.%s(   R   R   R   R   t   sept   _SEP_UNSAFEt   matcht   warningst   warnR   t	   __class__R   R   R   (   R'   R   R:   R   (    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyt   __init__œ   s    
	c         C  s&   t  |  j d | |  j ƒ } t | ƒ S(   Nu   signer(   R   R   R   R   (   R'   R   t	   signature(    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyRA   ¦   s    c         C  s/   t  | ƒ } t d ƒ | |  j |  j | ƒ f S(   Nu   %s%s%s(   R   t   strR:   RA   (   R'   R   (    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR2   «   s    c         C  s‚   t  | ƒ } |  j | k r1 t d |  j ƒ ‚ n  | j |  j d ƒ \ } } t | |  j | ƒ ƒ rn t | ƒ St d | ƒ ‚ d  S(   Nu   No "%s" found in valuei   u   Signature "%s" does not match(   R   R:   R
   t   rsplitR   RA   R   (   R'   t   signed_valueR   t   sig(    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR8   ¯   s    
N(   R   R   t   NoneR@   RA   R2   R8   (    (    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR    š   s   
		R1   c           B  s&   e  Z d  „  Z d „  Z d d „ Z RS(   c         C  s   t  j j t t j ƒ  ƒ ƒ S(   N(   R   t   base62R&   t   intt   time(   R'   (    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyt	   timestamp»   s    c         C  sD   t  | ƒ } t d ƒ | |  j |  j ƒ  f } t t |  ƒ j | ƒ S(   Nu   %s%s%s(   R   RB   R:   RJ   t   superR1   R2   (   R'   R   (    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR2   ¾   s    "c         C  s®   t  t |  ƒ j | ƒ } | j |  j d ƒ \ } } t j j | ƒ } | d k	 rª t	 | t
 j ƒ rr | j ƒ  } n  t j ƒ  | } | | k rª t d | | f ƒ ‚ qª n  | S(   uk   
        Retrieve original value and check it wasn't signed more
        than max_age seconds ago.
        i   u   Signature age %s > %s secondsN(   RK   R1   R8   RC   R:   R   RG   R*   RF   t
   isinstancet   datetimet	   timedeltat   total_secondsRI   R   (   R'   R   R7   t   resultRJ   t   age(    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR8   Ã   s    N(   R   R   RJ   R2   RF   R8   (    (    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyR1   ¹   s   		(*   R   t
   __future__R    R   RM   R$   t   reRI   R=   R.   t   django.confR   t   django.utilsR   t   django.utils.cryptoR   R   t   django.utils.deprecationR   t   django.utils.encodingR   R   R   t   django.utils.module_loadingR	   t   compileR;   t	   ExceptionR
   R   R   R   R   R!   t   objectR"   RF   R-   R%   R)   R    R1   (    (    (    sa   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/core/signing.pyt   <module>"   s4   			!