o
    ¯b’   ã                   @   sÆ   d Z ddlZddlmZmZm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
é    N)ÚOptionalÚSequenceÚType)Ú	AttributeÚ	Interface)Ú
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ÚgenerateChecker2   s    zICheckerFactory.generateCheckerN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚauthTypeÚauthHelpÚargStringFormatÚcredentialInterfacesr   r
   r
   r
   r   r	      s    ÿÿÿr	   c                   @   ó   e Zd ZdZdS )ÚStrcredExceptionz+
    Base exception class for strcred.
    N©r   r   r   r   r
   r
   r
   r   r   9   ó    r   c                   @   r   )ÚInvalidAuthTypezv
    Raised when a user provides an invalid identifier for the
    authentication plugin (known as the authType).
    Nr   r
   r
   r
   r   r   ?   r   r   c                   @   r   )ÚInvalidAuthArgumentStringzl
    Raised by an authentication plugin when the argument string
    provided is formatted incorrectly.
    Nr   r
   r
   r
   r   r   F   r   r   c                   @   r   )Ú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   M   r   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   ÚfindCheckerFactoriesY   s   r   c                 C   s&   t ƒ D ]}|j| kr|  S qt| ƒ‚)zJ
    Find the first checker factory that supports the given authType.
    )r   r   r   )r   Úfactoryr
   r
   r   ÚfindCheckerFactory`   s
   

ÿr   c                 C   s0   d| v 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   ÚmakeCheckerj   s
   r%   c                   @   sh   e Zd ZU dZdZeeee   e	d< 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}
    NÚsupportedInterfacesc                 C   s   | j du p	|| j v S )zR
        Returns whether a particular credentials interface is supported.
        N)r'   )Ú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u r|j}n|jD ]}|  |¡r| |¡ q|s"t|jƒ‚d| vr*i | d< d| vr2g | d< | d  |¡ |D ]}| d  |g ¡ |¡ q;dS )zM
        Supply a supplied credentials checker to the Options class.
        NÚcredInterfacesÚcredCheckers)r'   r   r*   Úappendr   Ú
setdefault)r(   ÚcheckerÚ	supportedr)   r
   r
   r   Ú
addChecker¤   s"   



€
ÿzAuthOptionMixin.addCheckerc              
   C   s�   z
|   t|ƒ¡ W dS  ty  } z
t d|jd  ¡‚d}~w ty5 } z
t d|jd  ¡‚d}~w tyG } zt d| ¡‚d}~ww )zB
        Specify an authentication method for the server.
        zAuth plugin not supported: %sr   NzAuth plugin not recognized: %szUnexpected error: %s)r3   r%   r   r   Ú
UsageErrorÚargsr   Ú	Exception)r(   r$   Úer
   r
   r   Úopt_auth»   s   €€€ÿzAuthOptionMixin.opt_authc                 c   s2   � t ƒ D ]}|jD ]}|  |¡r|V   n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r&t|jƒ}qd| }| j  |d ¡ | j  |d ¡ |  ¡ D ]}| j  ||j|jf ¡ q?| j  d¡ tdƒ‚)z<
        Show all authentication methods available.
        z#Usage: --auth AuthType[:ArgString]
z-For detailed help: --help-auth-type AuthType
Ú
r   z  %%-%is	%%s
)ÚAuthTypezArgString format)z========z================)Ú
authOutputÚwriter9   Úlenr   r   Ú
SystemExit)r(   ÚfirstLengthr   ÚformatStringr
   r
   r   Úopt_help_authÓ   s"   
€ÿzAuthOptionMixin.opt_help_authc                 C   sº   zt |ƒ}W n ty   t d| ¡‚w | j d| ¡ | j d|j ¡ | j d¡ |j ¡  	¡ D ]}| j d| 
¡  ¡ q3| j d¡ |  |¡sY| j dt ¡ | j d¡ tdƒ‚)zA
        Show help for a particular authentication type.
        zInvalid auth type: %szUsage: --auth %s[:ArgString]
zArgString format: %s
r:   z  %s
r   )r   r   r   r4   r<   r=   r   r   ÚstripÚ
splitlinesÚrstripr,   ÚnotSupportedWarningr?   )r(   r   ÚcfÚliner
   r
   r   Úopt_help_auth_typeé   s   ÿ
z"AuthOptionMixin.opt_help_auth_type)r   r   r   r   r'   r   r   r   r   Ú__annotations__ÚsysÚstdoutr<   r*   r,   r3   r8   r9   rB   rI   r
   r
   r
   r   r&   x   s   
 
r&   )r   rK   Útypingr   r   r   Úzope.interfacer   r   Útwisted.pluginr   Útwisted.pythonr   r	   r6   r   r   r   r   rF   r   r   r%   r&   r
   r
   r
   r   Ú<module>   s    	
