o
    g,                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ G dd d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edddgZG dd deZG dd deZG dd dZdS )z
Modern, adaptable authentication machinery.

Replaces certain parts of `.SSHClient`. For a concrete implementation, see the
``OpenSSHAuthStrategy`` class in `Fabric <https://fabfile.org>`_.
    )
namedtuple   )AgentKey)
get_logger)AuthenticationExceptionc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )
AuthSourcez
    Some SSH authentication source, such as a password, private key, or agent.

    See subclasses in this module for concrete implementations.

    All implementations must accept at least a ``username`` (``str``) kwarg.
    c                 C   
   || _ d S Nusername)selfr    r   P/var/www/html/api-tag/env/lib/python3.10/site-packages/paramiko/auth_strategy.py__init__      
zAuthSource.__init__c                 K   s0   dd |  D }d|}| jj d| dS )Nc                 S   s   g | ]\}}| d |qS )=r   ).0kvr   r   r   
<listcomp>   s    z$AuthSource._repr.<locals>.<listcomp>z, ())itemsjoin	__class____name__)r   kwargspairsjoinedr   r   r   _repr   s   
zAuthSource._reprc                 C   s   |   S r	   )r   r   r   r   r   __repr__"   s   zAuthSource.__repr__c                 C      t )z)
        Perform authentication.
        NotImplementedErrorr   	transportr   r   r   authenticate%   s   zAuthSource.authenticateN)r   
__module____qualname____doc__r   r   r!   r'   r   r   r   r   r      s    r   c                   @      e Zd ZdZdd ZdS )NoneAuthzS
    Auth type "none", ie https://www.rfc-editor.org/rfc/rfc4252#section-5.2 .
    c                 C   s   | | jS r	   )	auth_noner   r%   r   r   r   r'   1   s   zNoneAuth.authenticateNr   r(   r)   r*   r'   r   r   r   r   r,   ,   s    r,   c                       s4   e Zd ZdZ fddZ fddZdd Z  ZS )Passworda  
    Password authentication.

    :param callable password_getter:
        A lazy callable that should return a `str` password value at
        authentication time, such as a `functools.partial` wrapping
        `getpass.getpass`, an API call to a secrets store, or similar.

        If you already know the password at instantiation time, you should
        simply use something like ``lambda: "my literal"`` (for a literal, but
        also, shame on you!) or ``lambda: variable_name`` (for something stored
        in a variable).
    c                       t  j|d || _d S Nr
   )superr   password_getter)r   r   r3   r   r   r   r   D   s   
zPassword.__init__c                    s   t  j| jdS )N)user)r2   r   r   r    r4   r   r   r!   H   s   zPassword.__repr__c                 C   s   |   }|| j|S r	   )r3   auth_passwordr   )r   r&   passwordr   r   r   r'   M   s   zPassword.authenticate)r   r(   r)   r*   r   r!   r'   __classcell__r   r   r4   r   r/   5   s
    r/   c                   @   r+   )
PrivateKeya  
    Essentially a mixin for private keys.

    Knows how to auth, but leaves key material discovery/loading/decryption to
    subclasses.

    Subclasses **must** ensure that they've set ``self.pkey`` to a decrypted
    `.PKey` instance before calling ``super().authenticate``; typically
    either in their ``__init__``, or in an overridden ``authenticate`` prior to
    its `super` call.
    c                 C   s   | | j| jS r	   )auth_publickeyr   pkeyr%   r   r   r   r'   e   s   zPrivateKey.authenticateNr.   r   r   r   r   r9   X   s    r9   c                       s,   e Zd ZdZ fddZ fddZ  ZS )InMemoryPrivateKeyz1
    An in-memory, decrypted `.PKey` object.
    c                    r0   r1   )r2   r   r;   )r   r   r;   r4   r   r   r   n   s   
zInMemoryPrivateKey.__init__c                    s(   t  j| jd}t| jtr|d7 }|S )N)r;   z [agent])r2   r   r;   
isinstancer   )r   repr4   r   r   r!   s   s   zInMemoryPrivateKey.__repr__r   r(   r)   r*   r   r!   r8   r   r   r4   r   r<   i   s    r<   c                       (   e Zd ZdZ fddZdd Z  ZS )OnDiskPrivateKeya  
    Some on-disk private key that needs opening and possibly decrypting.

    :param str source:
        String tracking where this key's path was specified; should be one of
        ``"ssh-config"``, ``"python-config"``, or ``"implicit-home"``.
    :param Path path:
        The filesystem path this key was loaded from.
    :param PKey pkey:
        The `PKey` object this auth source uses/represents.
    c                    s>   t  j|d || _d}||vrtd||| _|| _d S )Nr
   )z
ssh-configzpython-configzimplicit-homez source argument must be one of: )r2   r   source
ValueErrorpathr;   )r   r   rB   rD   r;   allowedr4   r   r   r      s   
zOnDiskPrivateKey.__init__c                 C   s   | j | j| jt| jdS )N)keyrB   rD   )r   r;   rB   strrD   r    r   r   r   r!      s   zOnDiskPrivateKey.__repr__r?   r   r   r4   r   rA   |   s    
rA   SourceResultrB   resultc                       r@   )
AuthResulta  
    Represents a partial or complete SSH authentication attempt.

    This class conceptually extends `AuthStrategy` by pairing the former's
    authentication **sources** with the **results** of trying to authenticate
    with them.

    `AuthResult` is a (subclass of) `list` of `namedtuple`, which are of the
    form ``namedtuple('SourceResult', 'source', 'result')`` (where the
    ``source`` member is an `AuthSource` and the ``result`` member is either a
    return value from the relevant `.Transport` method, or an exception
    object).

    .. note::
        Transport auth method results are always themselves a ``list`` of "next
        allowable authentication methods".

        In the simple case of "you just authenticated successfully", it's an
        empty list; if your auth was rejected but you're allowed to try again,
        it will be a list of string method names like ``pubkey`` or
        ``password``.

        The ``__str__`` of this class represents the empty-list scenario as the
        word ``success``, which should make reading the result of an
        authentication session more obvious to humans.

    Instances also have a `strategy` attribute referencing the `AuthStrategy`
    which was attempted.
    c                    s   || _ t j|i | d S r	   )strategyr2   r   )r   rK   argsr   r4   r   r   r      s   zAuthResult.__init__c                 C   s   d dd | D S )N
c                 s   s&    | ]}|j  d |jpd V  qdS )z -> successN)rB   rI   )r   xr   r   r   	<genexpr>   s    
z%AuthResult.__str__.<locals>.<genexpr>)r   r    r   r   r   __str__   s   
zAuthResult.__str__)r   r(   r)   r*   r   rQ   r8   r   r   r4   r   rJ      s    rJ   c                   @   s    e Zd ZdZdd Zdd ZdS )AuthFailurea  
    Basic exception wrapping an `AuthResult` indicating overall auth failure.

    Note that `AuthFailure` descends from `AuthenticationException` but is
    generally "higher level"; the latter is now only raised by individual
    `AuthSource` attempts and should typically only be seen by users when
    encapsulated in this class. It subclasses `AuthenticationException`
    primarily for backwards compatibility reasons.
    c                 C   r   r	   rI   )r   rI   r   r   r   r      r   zAuthFailure.__init__c                 C   s   dt | j S )NrM   )rG   rI   r    r   r   r   rQ      s   zAuthFailure.__str__N)r   r(   r)   r*   r   rQ   r   r   r   r   rR      s    
rR   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	AuthStrategya   
    This class represents one or more attempts to auth with an SSH server.

    By default, subclasses must at least accept an ``ssh_config``
    (`.SSHConfig`) keyword argument, but may opt to accept more as needed for
    their particular strategy.
    c                 C   s   || _ tt| _d S r	   )
ssh_configr   r   log)r   rU   r   r   r   r      s   zAuthStrategy.__init__c                 C   r"   )a[  
        Generator yielding `AuthSource` instances, in the order to try.

        This is the primary override point for subclasses: you figure out what
        sources you need, and ``yield`` them.

        Subclasses _of_ subclasses may find themselves wanting to do things
        like filtering or discarding around a call to `super`.
        r#   r    r   r   r   get_sources   s   
zAuthStrategy.get_sourcesc                 C   s   d}t | d}|  D ]E}| jd|  z	||}d}W n$ tyC } z|}|jj}| jd| d|  W Y d}~nd}~ww |	t
|| |rP nq|sXt|d|S )	z
        Handles attempting `AuthSource` instances yielded from `get_sources`.

        You *normally* won't need to override this, but it's an option for
        advanced users.
        F)rK   zTrying TzAuthentication via z failed with NrS   )rJ   rW   rV   debugr'   	Exceptionr   r   infoappendrH   rR   )r   r&   	succeededoverall_resultrB   rI   esource_classr   r   r   r'     s,   


zAuthStrategy.authenticateN)r   r(   r)   r*   r   rW   r'   r   r   r   r   rT      s
    rT   N)r*   collectionsr   agentr   utilr   ssh_exceptionr   r   r,   r/   r9   r<   rA   rH   listrJ   rR   rT   r   r   r   r   <module>   s    	#!.