
    Ϫf                          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e      Z G d	 d
e      Z G d de      Z G d de      Z G d de      ZdZd Zd Zd Z G d d      Zy)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                   V    e Zd ZdZ ed      Z ed      Z ed      Z ed      Zd Z	y)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                      y)z}
        Return an L{twisted.cred.checkers.ICredentialsChecker} provider using the supplied
        argument string.
        N )	argstrings    6/usr/lib/python3/dist-packages/twisted/cred/strcred.pygenerateCheckerzICheckerFactory.generateChecker2   s        N)
__name__
__module____qualname____doc__r   authTypeauthHelpargStringFormatcredentialInterfacesr   r   r   r   r   r      sG     KLH	;H
  GO %Jr   r   c                       e Zd ZdZy)StrcredExceptionz+
    Base exception class for strcred.
    Nr   r   r   r   r   r   r   r   r   9   s    r   r   c                       e Zd ZdZy)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                       e Zd ZdZy)InvalidAuthArgumentStringzl
    Raised by an authentication plugin when the argument string
    provided is formatted incorrectly.
    Nr   r   r   r   r!   r!   F   r   r   r!   c                       e Zd ZdZy)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   r#   z<WARNING: This authType is not supported by this application.c                       t        t              S )z=
    Find all objects that implement L{ICheckerFactory}.
    )r   r   r   r   r   findCheckerFactoriesr%   Y   s     o&&r   c                 \    t               D ]  }|j                  | k(  s|c S  t        |       )zJ
    Find the first checker factory that supports the given authType.
    )r%   r   r   )r   factorys     r   findCheckerFactoryr(   `   s6     () x'N (
##r   c                 r    d| v r| j                  dd      \  }}n| }d}t        |      j                  |      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   s      r   makeCheckerr/   j   sC     k)//Q7)	h'77	BBr   c                   t    e Zd ZU dZdZeeee         e	d<   e
j                  Zd Zd Zd Zd Zd Zd	 Zd
 Zy)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                 >    | j                   du xs || j                   v S )zR
        Returns whether a particular credentials interface is supported.
        N)r2   )self	interfaces     r   supportsInterfacez!AuthOptionMixin.supportsInterface   s%     ''4/X9@X@X3XXr   c                 L    |j                   D ]  }| j                  |      s y y)z
        Returns whether a checker factory will provide at least one of
        the credentials interfaces that we care about.
        TF)r   r6   r4   r'   r5   s      r   supportsCheckerFactoryz&AuthOptionMixin.supportsCheckerFactory   s/    
 !55 	I%%i0	 r   c                 r   g }| j                   |j                  }n4|j                  D ]%  }| j                  |      s|j                  |       ' |st	        |j                        d| vri | d<   d| vrg | d<   | d   j                  |       |D ]&  }| d   j                  |g       j                  |       ( y)zM
        Supply a supplied credentials checker to the Options class.
        NcredInterfacescredCheckers)r2   r   r6   appendr#   
setdefault)r4   checker	supportedr5   s       r   
addCheckerzAuthOptionMixin.addChecker   s    
 	##+44I$99 0	)))4$$Y/0 '(D(DEE4'%'D!"%#%D ^##G," 	MI!"--i<CCGL	Mr   c                 P   	 | j                  t        |             y# t        $ r*}t        j                  d|j
                  d   z        d}~wt        $ r*}t        j                  d|j
                  d   z        d}~wt        $ r}t        j                  d|z        d}~ww xY w)zB
        Specify an authentication method for the server.
        zAuth plugin not supported: %sr   NzAuth plugin not recognized: %szUnexpected error: %s)rA   r/   r#   r	   
UsageErrorargsr   	Exception)r4   r.   es      r   opt_authzAuthOptionMixin.opt_auth   s    	?OOK45$ 	P""#BQVVAY#NOO 	Q""#CaffQi#OPP 	?""#9A#=>>	?s,    	B%%AB%%A<<B%B  B%c              #   z   K   t               D ]*  }|j                  D ]  }| j                  |      s|  * , yw)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   r6   r8   s      r   _checkerFactoriesForOptHelpAuthz/AuthOptionMixin._checkerFactoriesForOptHelpAuth   sE     
 ,- 	G$99 	)))4!M	s   .;
;c                    | j                   j                  d       | j                   j                  d       | j                   j                  d       d}| j                         D ]0  }t        |j                        |kD  st        |j                        }2 d|z  }| j                   j                  |dz         | j                   j                  |dz         | j                         D ]6  }| j                   j                  ||j                  |j
                  fz         8 | j                   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writerI   lenr   r   
SystemExit)r4   firstLengthr'   formatStrings       r   opt_help_authzAuthOptionMixin.opt_help_auth   s    	DENOd#;;= 	4G7##${2!'"2"23	4 );6l-MMNl-MMN;;= 	GOO!! 0 0'2I2IJJ	 	d#mr   c                    	 t        |      }| j                  j                  d|z         | j                  j                  d|j                  z         | j                  j                  d       |j                  j                         j                         D ].  }| j                  j                  d|j                         z         0 | j                  j                  d       | j                  |      s=| j                  j                  dt        z         | j                  j                  d       t        d      # t        $ r t        j                  d|z        w xY w)zA
        Show help for a particular authentication type.
        zInvalid auth type: %szUsage: --auth %s[:ArgString]
zArgString format: %s
rK   z  %s
r   )r(   r   r	   rC   rM   rN   r   r   strip
splitlinesrstripr9   notSupportedWarningrP   )r4   r   cflines       r   opt_help_auth_typez"AuthOptionMixin.opt_help_auth_type   s   	G#H-B 	>IJ69K9KKLd#KK%%'224 	<DOO!!(T[[]":;	<d#**2.OO!!(-@"@AOO!!$'m  	G""#:X#EFF	Gs   D; ;"E)r   r   r   r   r2   r   r   r   r   __annotations__sysstdoutrM   r6   r9   rA   rG   rI   rS   r[   r   r   r   r1   r1   x   sR    0 @D(4	?";<CJYM.?	,r   r1   )r   r]   typingr   r   r   zope.interfacer   r   twisted.pluginr   twisted.pythonr	   r   rE   r   r   r!   r#   rX   r%   r(   r/   r1   r   r   r   <module>rc      s     + + / %  i <y &  0 ,  X '$CB Br   