ó
ú2ec           @   sē   d  Z  d d l m Z d d l Z d d l m Z d d l Td e f d     YZ d e f d	     YZ	 e	 Z
 d g Z d
   Z d e k rŪ d d l Z e j d e j  n  d S(   s  
Basic HTTP/1.0 Authentication

This module implements ``Basic`` authentication as described in
HTTP/1.0 specification [1]_ .  Do not use this module unless you
are using SSL or need to work with very out-dated clients, instead
use ``digest`` authentication.

>>> from paste.wsgilib import dump_environ
>>> from paste.httpserver import serve
>>> # from paste.auth.basic import AuthBasicHandler
>>> realm = 'Test Realm'
>>> def authfunc(environ, username, password):
...     return username == password
>>> serve(AuthBasicHandler(dump_environ, realm, authfunc))
serving on...

.. [1] http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html#BasicAA
iĸĸĸĸ(   t	   b64decodeN(   t   HTTPUnauthorized(   t   *t   AuthBasicAuthenticatorc           B   s5   e  Z d  Z d Z d   Z d   Z d   Z e Z RS(   s5   
    implements ``Basic`` authentication details
    t   basicc         C   s   | |  _  | |  _ d  S(   N(   t   realmt   authfunc(   t   selfR   R   (    (    s:   /usr/local/lib/python2.7/dist-packages/paste/auth/basic.pyt   __init__"   s    	c         C   s#   t  j d |  j  } t d |  S(   Ns   Basic realm="%s"t   headers(   t   WWW_AUTHENTICATEt   tuplesR   R   (   R   t   head(    (    s:   /usr/local/lib/python2.7/dist-packages/paste/auth/basic.pyt   build_authentication&   s    c         C   sŊ   t  |  } | s |  j   S| j d d  \ } } d | j   k rP |  j   St j t t j | j      } | j d d  \ } } |  j	 | | |  rĨ | S|  j   S(   Nt    i   R   t   :(
   t   AUTHORIZATIONR   t   splitt   lowert   sixt   ensure_textR    t   ensure_binaryt   stripR   (   R   t   environt   authorizationt   authmetht   autht   usernamet   password(    (    s:   /usr/local/lib/python2.7/dist-packages/paste/auth/basic.pyt   authenticate*   s    

$(   t   __name__t
   __module__t   __doc__t   typeR   R   R   t   __call__(    (    (    s:   /usr/local/lib/python2.7/dist-packages/paste/auth/basic.pyR      s   			t   AuthBasicHandlerc           B   s    e  Z d  Z d   Z d   Z RS(   s/  
    HTTP/1.0 ``Basic`` authentication middleware

    Parameters:

        ``application``

            The application object is called only upon successful
            authentication, and can assume ``environ['REMOTE_USER']``
            is set.  If the ``REMOTE_USER`` is already set, this
            middleware is simply pass-through.

        ``realm``

            This is a identifier for the authority that is requesting
            authorization.  It is shown to the user and should be unique
            within the domain it is being used.

        ``authfunc``

            This is a mandatory user-defined function which takes a
            ``environ``, ``username`` and ``password`` for its first
            three arguments.  It should return ``True`` if the user is
            authenticated.

    c         C   s   | |  _  t | |  |  _ d  S(   N(   t   applicationR   R   (   R   R$   R   R   (    (    s:   /usr/local/lib/python2.7/dist-packages/paste/auth/basic.pyR   T   s    	c         C   sv   t  |  } | sf |  j |  } t | t  rS t j | d  t  j | |  qf | j | |  Sn  |  j | |  S(   NR   (   t   REMOTE_USERR   t
   isinstancet   strt	   AUTH_TYPEt   updatet   wsgi_applicationR$   (   R   R   t   start_responseR   t   result(    (    s:   /usr/local/lib/python2.7/dist-packages/paste/auth/basic.pyR"   X   s    (   R   R   R    R   R"   (    (    (    s:   /usr/local/lib/python2.7/dist-packages/paste/auth/basic.pyR#   9   s   	c         K   sV   d d l  m } d d l } | |  } t | | j  sF t d   t |  | |  S(   sĖ   
    Grant access via basic authentication

    Config looks like this::

      [filter:grant]
      use = egg:Paste#auth_basic
      realm=myrealm
      authfunc=somepackage.somemodule:somefunction

    iĸĸĸĸ(   t   eval_importNs#   authfunc must resolve to a function(   t   paste.util.import_stringR-   t   typesR&   t   FunctionTypet   AssertionErrorR#   (   t   appt   global_confR   R   t   kwR-   R/   (    (    s:   /usr/local/lib/python2.7/dist-packages/paste/auth/basic.pyt
   make_basicg   s
    t   __main__t   optionflags(   R    t   base64R    R   t   paste.httpexceptionsR   t   paste.httpheaderst   objectR   R#   t
   middlewaret   __all__R5   R   t   doctestt   testmodt   ELLIPSIS(    (    (    s:   /usr/local/lib/python2.7/dist-packages/paste/auth/basic.pyt   <module>   s   
*		