ó
ù¢TQc           @  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 m 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 m Z e j d
 ƒ Z d Z d Z d Z d Z d Z d „  Z d „  Z d „  Z d „  Z d e f d „  ƒ  YZ  d S(   u’   
Cross Site Request Forgery Middleware.

This module provides a middleware that implements protection
against request forgeries from other sites.
iÿÿÿÿ(   t   unicode_literalsN(   t   settings(   t   get_callable(   t   patch_vary_headers(   t
   force_text(   t   same_origin(   t   constant_time_comparet   get_random_stringu   django.requestu%   Referer checking failed - no Referer.u/   Referer checking failed - %s does not match %s.u   CSRF cookie not set.u    CSRF token missing or incorrect.i    c           C  s   t  t j ƒ S(   u9   
    Returns the view to be used for CSRF rejections
    (   R   R   t   CSRF_FAILURE_VIEW(    (    (    s/   ../Django//lib/python/django/middleware/csrf.pyt   _get_failure_view   s    c           C  s
   t  t ƒ S(   N(   R   t   CSRF_KEY_LENGTH(    (    (    s/   ../Django//lib/python/django/middleware/csrf.pyt   _get_new_csrf_key&   s    c         C  s    t  |  j d <|  j j d d ƒ S(   u‰  
    Returns the CSRF token required for a POST form. The token is an
    alphanumeric value.

    A side effect of calling this function is to make 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.
    u   CSRF_COOKIE_USEDu   CSRF_COOKIEN(   t   Truet   METAt   gett   None(   t   request(    (    s/   ../Django//lib/python/django/middleware/csrf.pyt	   get_token*   s    
c         C  sK   t  |  ƒ t k r t ƒ  St j d d t |  ƒ ƒ }  |  d k rG t ƒ  S|  S(   Nu   [^a-zA-Z0-9]+u    (   t   lenR
   R   t   ret   subR   (   t   token(    (    s/   ../Django//lib/python/django/middleware/csrf.pyt   _sanitize_token8   s    t   CsrfViewMiddlewarec           B  s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   uú   
    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         C  s   t  | _ d  S(   N(   R   t   csrf_processing_doneR   (   t   selfR   (    (    s/   ../Django//lib/python/django/middleware/csrf.pyt   _acceptN   s    	c         C  s   t  ƒ  | d | ƒS(   Nt   reason(   R	   (   R   R   R   (    (    s/   ../Django//lib/python/django/middleware/csrf.pyt   _rejectU   s    c   
      C  sk  t  | d t ƒ r d  Sy' t | j t j ƒ } | | j d <Wn' t k
 rf d  } t	 ƒ  | j d <n Xt  | d t ƒ r} d  S| j
 d k r^t  | d t ƒ r« |  j | ƒ S| j ƒ  r{| j j d	 ƒ } | d  k rt j d
 t | j d i d d 6| d 6ƒ|  j | t ƒ Sd | j ƒ  } t | | ƒ s{t | | f } t j d
 | | j d i d d 6| d 6ƒ|  j | | ƒ Sn  | d  k rÁt j d
 t | j d i d d 6| d 6ƒ|  j | t ƒ Sd }	 | j
 d k rî| j j d d ƒ }	 n  |	 d k r| j j d d ƒ }	 n  t |	 | ƒ s^t j d
 t | j d i d d 6| d 6ƒ|  j | t ƒ Sn  |  j | ƒ S(   Nu   csrf_processing_doneu   CSRF_COOKIEu   csrf_exemptu   GETu   HEADu   OPTIONSu   TRACEu   _dont_enforce_csrf_checksu   HTTP_REFERERu   Forbidden (%s): %st   extrai“  u   status_codeu   requestu   https://%s/u    u   POSTu   csrfmiddlewaretokenu   HTTP_X_CSRFTOKEN(   u   GETu   HEADu   OPTIONSu   TRACE(   t   getattrt   FalseR   R   t   COOKIESR   t   CSRF_COOKIE_NAMER   t   KeyErrorR   t   methodR   t	   is_secureR   t   loggert   warningt   REASON_NO_REFERERt   pathR   t   get_hostR   t   REASON_BAD_REFERERt   REASON_NO_CSRF_COOKIEt   POSTR   t   REASON_BAD_TOKEN(
   R   R   t   callbackt   callback_argst   callback_kwargst
   csrf_tokent   referert   good_refererR   t   request_csrf_token(    (    s/   ../Django//lib/python/django/middleware/csrf.pyt   process_viewX   sf    			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 d
 t j	 d t j
 ƒt | d ƒ t | _ | S(   Nu   csrf_processing_doneu   CSRF_COOKIEu   CSRF_COOKIE_USEDt   max_agei<   i   i   i4   t   domainR(   t   secureu   Cookiei  i€Q i€:	 i âß(   u   Cookie(   R   R   R   R   R   t
   set_cookieR   R!   t   CSRF_COOKIE_DOMAINt   CSRF_COOKIE_PATHt   CSRF_COOKIE_SECURER   R   R   (   R   R   t   response(    (    s/   ../Django//lib/python/django/middleware/csrf.pyt   process_responseÁ   s    		
	(   t   __name__t
   __module__t   __doc__R   R   R5   R>   (    (    (    s/   ../Django//lib/python/django/middleware/csrf.pyR   C   s
   			i(!   RA   t
   __future__R    t   hashlibt   loggingR   t   randomt   django.confR   t   django.core.urlresolversR   t   django.utils.cacheR   t   django.utils.encodingR   t   django.utils.httpR   t   django.utils.cryptoR   R   t	   getLoggerR%   R'   R*   R+   R-   R
   R	   R   R   R   t   objectR   (    (    (    s/   ../Django//lib/python/django/middleware/csrf.pyt   <module>   s,   				