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    	
