U
    W[v!                     @   s   d Z ddlmZ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 ddlmZmZ dd	lmZ dd
lmZ eeG dd deZeeG dd deZdS )a  
A guard implementation which supports HTTP header-based authentication
schemes.

If no I{Authorization} header is supplied, an anonymous login will be
attempted by using a L{Anonymous} credentials object.  If such a header is
supplied and does not contain allowed credentials, or if anonymous login is
denied, a 401 will be sent in the response along with I{WWW-Authenticate}
headers for each of the allowed authentication schemes.
    )absolute_importdivision)error)	Anonymous)unicode)proxyForInterface)util)	ErrorPage	IResource)Logger)implementerc                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
UnauthorizedResourcez6
    Simple IResource to escape Resource dispatch
    Tc                 C   s
   || _ d S N)_credentialFactories)selfZ	factories r   ;/usr/lib/python3/dist-packages/twisted/web/_auth/wrapper.py__init__%   s    zUnauthorizedResource.__init__c                    sf   dd   fdd}dd | d | jD ]$}||}|jd||j| q.|jd	krbd
S dS )z=
        Send www-authenticate headers to the client
        c                 S   s   t | tr| dS | S )Nascii)
isinstancer   encodesr   r   r   ensureBytes-   s    z0UnauthorizedResource.render.<locals>.ensureBytesc                    sP   g }|  D ].\}} |} |}||d |  qd| d|gS )N   =    s   , )itemsappendjoin)scheme	challengelkvr   quoteStringr   r   generateWWWAuthenticate0   s    z<UnauthorizedResource.render.<locals>.generateWWWAuthenticatec                 S   s   d|  dd dd d S )N   "   \s   \\s   \")replacer   r   r   r   r%   8   s    z0UnauthorizedResource.render.<locals>.quoteStringi  s   www-authenticates   HEAD    s   Unauthorized)ZsetResponseCoder   ZgetChallengeZresponseHeadersZaddRawHeaderr   method)r   requestr&   factr    r   r$   r   render)   s    




zUnauthorizedResource.renderc                 C   s   | S )z+
        Disable resource dispatch
        r   r   pathr,   r   r   r   getChildWithDefaultF   s    z(UnauthorizedResource.getChildWithDefaultN)__name__
__module____qualname____doc__isLeafr   r.   r1   r   r   r   r   r      s
   r   c                   @   sZ   e Zd ZdZdZe Zdd Zdd Zdd Z	d	d
 Z
dd Zdd Zdd Zdd ZdS )HTTPAuthSessionWrapperaa  
    Wrap a portal, enforcing supported header-based authentication schemes.

    @ivar _portal: The L{Portal} which will be used to retrieve L{IResource}
        avatars.

    @ivar _credentialFactories: A list of L{ICredentialFactory} providers which
        will be used to decode I{Authorization} headers into L{ICredentials}
        providers.
    Fc                 C   s   || _ || _dS )a[  
        Initialize a session wrapper

        @type portal: C{Portal}
        @param portal: The portal that will authenticate the remote client

        @type credentialFactories: C{Iterable}
        @param credentialFactories: The portal that will authenticate the
            remote client based on one submitted C{ICredentialFactory}
        N)_portalr   )r   ZportalZcredentialFactoriesr   r   r   r   ]   s    zHTTPAuthSessionWrapper.__init__c                 C   s   | d}|s t| t S | |\}}|dkr@t| jS z|||}W nB t	j
k
rp   t| j Y S    | jd tddd Y S X t| |S dS )a  
        Get the L{IResource} which the given request is authorized to receive.
        If the proper authorization headers are present, the resource will be
        requested from the portal.  If not, an anonymous login attempt will be
        made.
        s   authorizationNz+Unexpected failure from credentials factory  )Z	getHeaderr   ZDeferredResource_loginr   _selectParseHeaderr   r   decoder   LoginFailed_logfailurer	   )r   r,   Z
authheaderfactoryZ
respStringcredentialsr   r   r   _authorizedResourcel   s    

z*HTTPAuthSessionWrapper._authorizedResourcec                 C   s   |  ||S )z
        Find the L{IResource} avatar suitable for the given request, if
        possible, and render it.  Otherwise, perhaps render an error page
        requiring authorization or describing an internal server failure.
        )rB   r.   r   r,   r   r   r   r.      s    zHTTPAuthSessionWrapper.renderc                 C   s   |j d|j  | |S )a\  
        Inspect the Authorization HTTP header, and return a deferred which,
        when fired after successful authentication, will return an authorized
        C{Avatar}. On authentication failure, an C{UnauthorizedResource} will
        be returned, essentially halting further dispatch on the wrapped
        resource and all children
        r   )ZpostpathinsertZprepathpoprB   r/   r   r   r   r1      s    
z*HTTPAuthSessionWrapper.getChildWithDefaultc                 C   s$   | j |dt}|| j| j |S )z
        Get the L{IResource} avatar for the given credentials.

        @return: A L{Deferred} which will be called back with an L{IResource}
            avatar or which will errback if authentication fails.
        N)r8   Zloginr
   ZaddCallbacks_loginSucceeded_loginFailed)r   rA   dr   r   r   r:      s    zHTTPAuthSessionWrapper._loginc                    s.   |\}}G  fdddt td  |S )z
        Handle login success by wrapping the resulting L{IResource} avatar
        so that the C{logout} callback will be invoked when rendering is
        complete.
        c                       s0   e Zd ZdZfddZ fddZ  ZS )z?HTTPAuthSessionWrapper._loginSucceeded.<locals>.ResourceWrappera  
            Wrap an L{IResource} so that whenever it or a child of it
            completes rendering, the cred logout hook will be invoked.

            An assumption is made here that exactly one L{IResource} from
            among C{avatar} and all of its children will be rendered.  If
            more than one is rendered, C{logout} will be invoked multiple
            times and probably earlier than desired.
            c                    s    | j ||S )z
                Pass through the lookup to the wrapped resource, wrapping
                the result in L{ResourceWrapper} to ensure C{logout} is
                called when rendering of the child is complete.
                )resourcer1   )r   namer,   )ResourceWrapperr   r   r1      s    zSHTTPAuthSessionWrapper._loginSucceeded.<locals>.ResourceWrapper.getChildWithDefaultc                    s&   |  fdd t | |S )z
                Hook into response generation so that when rendering has
                finished completely (with or without error), C{logout} is
                called.
                c                    s     S r   r   )Zign)logoutr   r   <lambda>   r*   zXHTTPAuthSessionWrapper._loginSucceeded.<locals>.ResourceWrapper.render.<locals>.<lambda>)ZnotifyFinishZaddBothsuperr.   rC   )rK   	__class__rL   r   r   r.      s    zFHTTPAuthSessionWrapper._loginSucceeded.<locals>.ResourceWrapper.render)r2   r3   r4   r5   r1   r.   __classcell__r   rK   rL   )rO   r   rK      s   	rK   rI   )r   r
   )r   argsZ	interfaceZavatarr   rQ   r   rF      s    
z&HTTPAuthSessionWrapper._loginSucceededc                 C   s:   | tjtjrt| jS | jjd|d tdddS dS )z
        Handle login failure by presenting either another challenge (for
        expected authentication/authorization-related failures) or a server
        error page (for anything else).
        zGHTTPAuthSessionWrapper.getChildWithDefault encountered unexpected error)r?   r9   N)	Zcheckr   ZUnauthorizedr=   r   r   r>   r?   r	   )r   resultr   r   r   rG      s    
z#HTTPAuthSessionWrapper._loginFailedc                 C   sJ   | d}|d  }| jD ](}|j|kr|d|dd f  S qdS )aV  
        Choose an C{ICredentialFactory} from C{_credentialFactories}
        suitable to use to decode the given I{Authenticate} header.

        @return: A two-tuple of a factory and the remaining portion of the
            header value to be decoded or a two-tuple of L{None} if no
            factory can decode the header value.
        r   r      N)NN)splitlowerr   r   r   )r   headerelementsr   r-   r   r   r   r;      s    	


z)HTTPAuthSessionWrapper._selectParseHeaderN)r2   r3   r4   r5   r6   r   r>   r   rB   r.   r1   r:   rF   rG   r;   r   r   r   r   r7   N   s   
	%r7   N)r5   Z
__future__r   r   Ztwisted.credr   Ztwisted.cred.credentialsr   Ztwisted.python.compatr   Ztwisted.python.componentsr   Ztwisted.webr   Ztwisted.web.resourcer	   r
   Ztwisted.loggerr   Zzope.interfacer   objectr   r7   r   r   r   r   <module>   s   0