ó
TR¹Nc           @   s/  d  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
 m Z d d l m Z e j d e j ƒ Z d Z e e d ƒ r¶ e j ƒ  j Z n	 e j Z d Z d „  Z d „  Z d „  Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s’   
Cross Site Request Forgery Middleware.

This module provides a middleware that implements protection
against request forgeries from other sites.
iÿÿÿÿN(   t   settings(   t   get_callable(   t   patch_vary_headers(   t   md5_constructor(   t	   mark_safes7   (<form\W[^>]*\bmethod\s*=\s*(\'|"|)POST(\'|"|)\b[^>]*>)s	   text/htmls   application/xhtml+xmlt   SystemRandoml            c           C   s   t  t j ƒ S(   s9   
    Returns the view to be used for CSRF rejections
    (   R   R    t   CSRF_FAILURE_VIEW(    (    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyt   _get_failure_view   s    c           C   s&   t  d t d t ƒ t j f ƒ j ƒ  S(   Ns   %s%si    (   R   t	   randranget   _MAX_CSRF_KEYR    t
   SECRET_KEYt	   hexdigest(    (    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyt   _get_new_csrf_key$   s    c         C   s   t  t j |  ƒ j ƒ  S(   N(   R   R    R
   R   (   t
   session_id(    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyt   _make_legacy_session_token(   s    c         C   s    t  |  j d <|  j j d d ƒ S(   si  
    Returns the the CSRF token required for a POST form.

    A side effect of calling this function is to make the the csrf_protect
    decorator and the CsrfViewMiddleware add a CSRF cookie and a 'Vary: Cookie'
    header to the outgoing response.  For this reason, you may need to use this
    function lazily, as is done by the csrf context processor.
    t   CSRF_COOKIE_USEDt   CSRF_COOKIEN(   t   Truet   METAt   gett   None(   t   request(    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyt	   get_token+   s    	t   CsrfViewMiddlewarec           B   s    e  Z d  Z d „  Z d „  Z RS(   sú   
    Middleware that requires a present and correct csrfmiddlewaretoken
    for POST requests that have a CSRF cookie, and sets an outgoing
    CSRF cookie.

    This middleware should be used in conjunction with the csrf_token template
    tag.
    c            sÓ  t  | d t ƒ r d  St  ˆ  d t ƒ r, d  S‡  f d †  } ‡  f d †  } y! ˆ  j t j ˆ  j d <t } Wn' t k
 r” t ƒ  ˆ  j d <t	 } n Xˆ  j
 d k rÌt  ˆ  d t ƒ r½ | ƒ  Sˆ  j ƒ  rÐ | ƒ  Sˆ  j ƒ  r:ˆ  j j d ƒ } | d  k r| d	 ƒ Sd
 ˆ  j ƒ  }	 | j |	 ƒ s:| d | |	 f ƒ Sn  | r~y  ˆ  j t j }
 t |
 ƒ } Wq‹t k
 rz| d ƒ SXn ˆ  j d } ˆ  j j d d  ƒ } | | k rÌ| r¼| d ƒ S| d ƒ SqÌn  | ƒ  S(   Nt   csrf_exemptt   csrf_processing_donec            s   t  ƒ  ˆ  d |  ƒS(   Nt   reason(   R   (   t   s(   R   (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyt   <lambda>G   s    c              s   t  ˆ  _ d  S(   N(   R   R   R   (    (   R   (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyt   acceptH   s    	R   t   POSTt   _dont_enforce_csrf_checkst   HTTP_REFERERs%   Referer checking failed - no Referer.s   https://%s/s/   Referer checking failed - %s does not match %s.s   No CSRF or session cookie.t   csrfmiddlewaretokens   CSRF cookie not set.s    CSRF token missing or incorrect.(   t   getattrt   FalseR   t   COOKIESR    t   CSRF_COOKIE_NAMER   t   KeyErrorR   R   t   methodt   is_ajaxt	   is_secureR   t   get_hostt
   startswitht   SESSION_COOKIE_NAMER   R   (   t   selfR   t   callbackt   callback_argst   callback_kwargst   rejectR   t   cookie_is_newt   referert   good_refererR   t
   csrf_tokent   request_csrf_token(    (   R   sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyt   process_view@   sL    




c         C   sŽ   t  | d t ƒ r | S| j j d ƒ d  k r2 | S| j j d t ƒ sK | S| j t j | j d d d d	 t j ƒt	 | d ƒ t
 | _ | S(   NR   R   R   t   max_agei<   i   i   i4   t   domaint   Cookiei  i€Q i€:	 i âß(   s   Cookie(   R"   R#   R   R   R   t
   set_cookieR    R%   t   CSRF_COOKIE_DOMAINR   R   R   (   R-   R   t   response(    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyt   process_response¦   s    
	(   t   __name__t
   __module__t   __doc__R7   R>   (    (    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyR   7   s   	ft   CsrfResponseMiddlewarec           B   s    e  Z d  Z d „  Z d „  Z RS(   s$  
    DEPRECATED
    Middleware that post-processes a response to add a csrfmiddlewaretoken.

    This exists for backwards compatibility and as an interim measure until
    applications are converted to using use the csrf_token template tag
    instead. It will be removed in Django 1.4.
    c         C   s    d d  l  } | j d t ƒ d  S(   Niÿÿÿÿs‡   CsrfResponseMiddleware and CsrfMiddleware are deprecated; use CsrfViewMiddleware and the template tag instead (see CSRF documentation).(   t   warningst   warnt   PendingDeprecationWarning(   R-   RC   (    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyt   __init__Å   s    c            sÄ   t  | d t ƒ r | S| d j d ƒ d t k rÀ t | ƒ ‰  ˆ  d  k rO | St j d
 t j d ƒ ƒ ‰ ‡ ‡  f d †  } t	 j
 | | j ƒ \ | _ } | d k rÀ t | d ƒ | d	 =qÀ n  | S(   NR   s   Content-Typet   ;i    s   id='csrfmiddlewaretoken't    c            s.   t  |  j ƒ  d d ˆ  j ƒ  d ˆ d ƒ S(   s=   Returns the matched <form> tag plus the added <input> elements   <div style='display:none;'>s   <input type='hidden' s#    name='csrfmiddlewaretoken' value='s
   ' /></div>(   R   t   groupt   next(   t   match(   t   idattributesR5   (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyt   add_csrf_fieldÚ   s    #R:   t   ETag(   s   id='csrfmiddlewaretoken'(   s   Cookie(   R"   R#   t   splitt   _HTML_TYPESR   R   t	   itertoolst   chaint   repeatt   _POST_FORM_REt   subnt   contentR   (   R-   R   R=   RM   t   n(    (   R5   RL   sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyR>   Ì   s    	(   R?   R@   RA   RF   R>   (    (    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyRB   ¼   s   	t   CsrfMiddlewarec           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s¶  
    Django middleware that adds protection against Cross Site
    Request Forgeries by adding hidden form fields to POST forms and
    checking requests for the correct value.

    CsrfMiddleware uses two middleware, CsrfViewMiddleware and
    CsrfResponseMiddleware, which can be used independently.  It is recommended
    to use only CsrfViewMiddleware and use the csrf_token template tag in
    templates for inserting the token.
    c         C   s   t  ƒ  |  _ t ƒ  |  _ d  S(   N(   RB   t   response_middlewareR   t   view_middleware(   R-   (    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyRF   û   s    c         C   s(   |  j  j | | ƒ } |  j j | | ƒ S(   N(   RY   R>   RZ   (   R-   R   t   respt   resp2(    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyR>   ÿ   s    c         C   s   |  j  j | | | | ƒ S(   N(   RZ   R7   (   R-   R   R.   R/   R0   (    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyR7     s    (   R?   R@   RA   RF   R>   R7   (    (    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyRX   î   s   
		(   s	   text/htmls   application/xhtml+xml(   RA   RQ   t   ret   randomt   django.confR    t   django.core.urlresolversR   t   django.utils.cacheR   t   django.utils.hashcompatR   t   django.utils.safestringR   t   compilet
   IGNORECASERT   RP   t   hasattrR   R   R	   R   R   R   R   t   objectR   RB   RX   (    (    (    sC   /home/panlixing/Python_Projects/gaeseries/django/middleware/csrf.pyt   <module>   s*   					…2