U
    
W[Á   ã                   @   sÂ   d Z ddlmZmZ ddlZddlmZmZ ddlm	Z	 ddl
mZ G dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdZdd„ Zdd„ Zdd„ ZG dd„ dƒZdS )z¨
Support for resolving command-line strings that represent different
checkers available to cred.

Examples:
 - passwd:/etc/passwd
 - memory:admin:asdf:user:lkj
 - unix
é    )Úabsolute_importÚdivisionN)Ú	InterfaceÚ	Attribute)Ú
getPlugins)Úusagec                   @   s8   e Zd ZdZedƒZedƒZedƒZedƒZdd„ Z	dS )	ÚICheckerFactoryzš
    A factory for objects which provide
    L{twisted.cred.checkers.ICredentialsChecker}.

    It's implemented by twistd plugins creating checkers.
    z0A tag that identifies the authentication method.zmA detailed (potentially multi-line) description of precisely what functionality this CheckerFactory provides.z=A short (one-line) description of the argument string format.z@A list of credentials interfaces that this factory will support.c                 C   s   dS )z}
        Return an L{twisted.cred.checkers.ICredentialsChecker} provider using the supplied
        argument string.
        N© )Ú	argstringr	   r	   ú6/usr/lib/python3/dist-packages/twisted/cred/strcred.pyÚgenerateChecker5   s    zICheckerFactory.generateCheckerN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚauthTypeÚauthHelpÚargStringFormatÚcredentialInterfacesr   r	   r	   r	   r   r      s   ÿÿÿÿr   c                   @   s   e Zd ZdZdS )ÚStrcredExceptionz+
    Base exception class for strcred.
    N©r   r   r   r   r	   r	   r	   r   r   =   s   r   c                   @   s   e Zd ZdZdS )ÚInvalidAuthTypezv
    Raised when a user provides an invalid identifier for the
    authentication plugin (known as the authType).
    Nr   r	   r	   r	   r   r   D   s   r   c                   @   s   e Zd ZdZdS )ÚInvalidAuthArgumentStringzl
    Raised by an authentication plugin when the argument string
    provided is formatted incorrectly.
    Nr   r	   r	   r	   r   r   L   s   r   c                   @   s   e Zd ZdZdS )ÚUnsupportedInterfacesz“
    Raised when an application is given a checker to use that does not
    provide any of the application's supported credentials interfaces.
    Nr   r	   r	   r	   r   r   T   s   r   z<WARNING: This authType is not supported by this application.c                   C   s   t tƒS )z=
    Find all objects that implement L{ICheckerFactory}.
    )r   r   r	   r	   r	   r   ÚfindCheckerFactoriesc   s    r   c                 C   s*   t ƒ D ]}|j| kr|  S qt| ƒ‚dS )zJ
    Find the first checker factory that supports the given authType.
    N)r   r   r   )r   Úfactoryr	   r	   r   ÚfindCheckerFactoryk   s    


r   c                 C   s0   d| kr|   dd¡\}}n| }d}t|ƒ |¡S )z¤
    Returns an L{twisted.cred.checkers.ICredentialsChecker} based on the
    contents of a descriptive string. Similar to
    L{twisted.application.strports}.
    ú:é   Ú )Úsplitr   r   )Údescriptionr   r
   r	   r	   r   ÚmakeCheckerv   s
    r"   c                   @   sR   e Zd ZdZdZejZdd„ Zdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zdd„ ZdS )ÚAuthOptionMixinak  
    Defines helper methods that can be added on to any
    L{usage.Options} subclass that needs authentication.

    This mixin implements three new options methods:

    The opt_auth method (--auth) will write two new values to the
    'self' dictionary: C{credInterfaces} (a dict of lists) and
    C{credCheckers} (a list).

    The opt_help_auth method (--help-auth) will search for all
    available checker plugins and list them for the user; it will exit
    when finished.

    The opt_help_auth_type method (--help-auth-type) will display
    detailed help for a particular checker plugin.

    @cvar supportedInterfaces: An iterable object that returns
       credential interfaces which this application is able to support.

    @cvar authOutput: A writeable object to which this options class
        will send all help-related output. Default: L{sys.stdout}
    Nc                 C   s   | j dkp|| j kS )zR
        Returns whether a particular credentials interface is supported.
        N)ÚsupportedInterfaces)ÚselfÚ	interfacer	   r	   r   ÚsupportsInterface¢   s    
ÿz!AuthOptionMixin.supportsInterfacec                 C   s    |j D ]}|  |¡r dS qdS )z‡
        Returns whether a checker factory will provide at least one of
        the credentials interfaces that we care about.
        TF)r   r'   ©r%   r   r&   r	   r	   r   ÚsupportsCheckerFactoryª   s    

z&AuthOptionMixin.supportsCheckerFactoryc                 C   s–   g }| j dkr|j}n |jD ]}|  |¡r| |¡ q|sDt|jƒ‚d| krTi | d< d| krdg | d< | d  |¡ |D ]}| d  |g ¡ |¡ qvdS )zM
        Supply a supplied credentials checker to the Options class.
        NZcredInterfacesZcredCheckers)r$   r   r'   Úappendr   Ú
setdefault)r%   ZcheckerZ	supportedr&   r	   r	   r   Ú
addCheckerµ   s    



zAuthOptionMixin.addCheckerc              
   C   s°   z|   t|ƒ¡ W n˜ tk
rH } zt d|jd  ¡‚W 5 d}~X Y nd tk
r| } zt d|jd  ¡‚W 5 d}~X Y n0 tk
rª } zt d| ¡‚W 5 d}~X Y nX dS )zB
        Specify an authentication method for the server.
        zAuth plugin not supported: %sr   NzAuth plugin not recognized: %szUnexpected error: %s)r,   r"   r   r   Ú
UsageErrorÚargsr   Ú	Exception)r%   r!   Úer	   r	   r   Úopt_authÍ   s    ÿÿzAuthOptionMixin.opt_authc                 c   s0   t ƒ D ]$}|jD ]}|  |¡r|V   qqqdS )zŒ
        Return a list of which authTypes will be displayed by --help-auth.
        This makes it a lot easier to test this module.
        N)r   r   r'   r(   r	   r	   r   Ú_checkerFactoriesForOptHelpAuthÝ   s
    


z/AuthOptionMixin._checkerFactoriesForOptHelpAuthc                 C   s´   | j  d¡ | j  d¡ | j  d¡ d}|  ¡ D ]}t|jƒ|kr0t|jƒ}q0d| }| j  |d ¡ | j  |d ¡ |  ¡ D ]}| j  ||j|jf ¡ q~| j  d¡ tdƒ‚dS )	z<
        Show all authentication methods available.
        z#Usage: --auth AuthType[:ArgString]
z-For detailed help: --help-auth-type AuthType
Ú
r   z  %%-%is	%%s
)ZAuthTypezArgString format)z========z================N)Ú
authOutputÚwriter2   Úlenr   r   Ú
SystemExit)r%   ZfirstLengthr   ZformatStringr	   r	   r   Úopt_help_authé   s     ÿzAuthOptionMixin.opt_help_authc                 C   sÄ   zt |ƒ}W n" tk
r.   t d| ¡‚Y nX | j d| ¡ | j d|j ¡ | j d¡ |j ¡  	¡ D ]}| j d| 
¡  ¡ ql| j d¡ |  |¡s¸| j dt ¡ | j d¡ tdƒ‚dS )zA
        Show help for a particular authentication type.
        zInvalid auth type: %szUsage: --auth %s[:ArgString]
zArgString format: %s
r3   z  %s
r   N)r   r   r   r-   r4   r5   r   r   ÚstripÚ
splitlinesÚrstripr)   ÚnotSupportedWarningr7   )r%   r   ZcfÚliner	   r	   r   Úopt_help_auth_typeÿ   s    
z"AuthOptionMixin.opt_help_auth_type)r   r   r   r   r$   ÚsysÚstdoutr4   r'   r)   r,   r1   r2   r8   r>   r	   r	   r	   r   r#   …   s   r#   )r   Z
__future__r   r   r?   Zzope.interfacer   r   Ztwisted.pluginr   Ztwisted.pythonr   r   r/   r   r   r   r   r<   r   r   r"   r#   r	   r	   r	   r   Ú<module>   s   
!
