U
    
W[$                     @   s   d dl mZmZ d dlZd dlmZmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZ G dd	 d	eZd
ZeeG dd dZeeG dd deZeeG dd dZeZdS )    )divisionabsolute_importN)implementer	Interface	Attribute)Logger)defer)failure)errorcredentialsc                   @   s    e Zd ZdZedZdd ZdS )ICredentialsCheckerzB
    An object that can check sub-interfaces of ICredentials.
    zKA list of sub-interfaces of ICredentials which specifies which I may check.c                 C   s   dS )a  
        @param credentials: something which implements one of the interfaces in
        self.credentialInterfaces.

        @return: a Deferred which will fire a string which identifies an
        avatar, an empty tuple to specify an authenticated anonymous user
        (provided as checkers.ANONYMOUS) or fire a Failure(UnauthorizedLogin).
        Alternatively, return the result itself.

        @see: L{twisted.cred.credentials}
        N )r   r   r   7/usr/lib/python3/dist-packages/twisted/cred/checkers.pyrequestAvatarId   s    z#ICredentialsChecker.requestAvatarIdN)__name__
__module____qualname____doc__r   credentialInterfacesr   r   r   r   r   r      s
   r   r   c                   @   s   e Zd ZejfZdd ZdS )AllowAnonymousAccessc                 C   s
   t tS N)r   succeed	ANONYMOUSselfr   r   r   r   r   :   s    z$AllowAnonymousAccess.requestAvatarIdN)r   r   r   r   Z
IAnonymousr   r   r   r   r   r   r   6   s   r   c                   @   s<   e Zd ZdZejejfZdd Zdd Z	dd Z
dd	 Zd
S )'InMemoryUsernamePasswordDatabaseDontUseak  
    An extremely simple credentials checker.

    This is only of use in one-off test programs or examples which don't
    want to focus too much on how credentials are verified.

    You really don't want to use this for anything else.  It is, at best, a
    toy.  If you need a simple credentials checker for a real application,
    see L{FilePasswordDB}.
    c                 K   s   dd |  D | _d S )Nc                 S   s   i | ]\}}| d |qS )ascii)encode).0xyr   r   r   
<dictcomp>O   s      zDInMemoryUsernamePasswordDatabaseDontUse.__init__.<locals>.<dictcomp>)itemsusers)r   r#   r   r   r   __init__N   s    z0InMemoryUsernamePasswordDatabaseDontUse.__init__c                 C   s   || j |< d S r   )r#   )r   usernamepasswordr   r   r   addUserR   s    z/InMemoryUsernamePasswordDatabaseDontUse.addUserc                 C   s   |r|S t t S d S r   r	   ZFailurer
   UnauthorizedLoginr   Zmatchedr%   r   r   r   _cbPasswordMatchV   s    z8InMemoryUsernamePasswordDatabaseDontUse._cbPasswordMatchc                 C   s@   |j | jkr.t|j| j|j  | j|j S tt	 S d S r   )
r%   r#   r   maybeDeferredcheckPasswordaddCallbackr+   failr
   r)   r   r   r   r   r   ]   s    
 z7InMemoryUsernamePasswordDatabaseDontUse.requestAvatarIdN)r   r   r   r   r   IUsernamePasswordIUsernameHashedPasswordr   r$   r'   r+   r   r   r   r   r   r   ?   s   
r   c                   @   sT   e Zd ZdZdZdZdZe Zddd	Z	d
d Z
dd Zdd Zdd Zdd ZdS )FilePasswordDBat  
    A file-based, text-based username/password database.

    Records in the datafile for this class are delimited by a particular
    string.  The username appears in a fixed field of the columns delimited
    by this string, as does the password.  Both fields are specifiable.  If
    the passwords are not stored plaintext, a hash function must be supplied
    to convert plaintext passwords to the form stored on disk and this
    CredentialsChecker will only be able to check IUsernamePassword
    credentials.  If the passwords are stored plaintext,
    IUsernameHashedPassword credentials will be checkable as well.
    FNr      :   Tc                 C   sR   || _ || _|| _|| _|| _|| _|| _| jdkrDtjtj	f| _
n
tjf| _
dS )a  
        @type filename: C{str}
        @param filename: The name of the file from which to read username and
        password information.

        @type delim: C{str}
        @param delim: The field delimiter used in the file.

        @type usernameField: C{int}
        @param usernameField: The index of the username after splitting a
        line on the delimiter.

        @type passwordField: C{int}
        @param passwordField: The index of the password after splitting a
        line on the delimiter.

        @type caseSensitive: C{bool}
        @param caseSensitive: If true, consider the case of the username when
        performing a lookup.  Ignore it otherwise.

        @type hash: Three-argument callable or L{None}
        @param hash: A function used to transform the plaintext password
        received over the network to a format suitable for comparison
        against the version stored on disk.  The arguments to the callable
        are the username, the network-supplied password, and the in-file
        version of the password.  If the return value compares equal to the
        version stored on disk, the credentials are accepted.

        @type cache: C{bool}
        @param cache: If true, maintain an in-memory cache of the
        contents of the password file.  On lookups, the mtime of the
        file will be checked, and the file will only be re-parsed if
        the mtime is newer than when the cache was generated.
        N)filenamedelimufieldpfieldcaseSensitivehashcacher   r0   r1   r   )r   r5   r6   ZusernameFieldZpasswordFieldr9   r:   r;   r   r   r   r$   |   s    $
zFilePasswordDB.__init__c              	   C   s:   t t| }dD ]$}z
||= W q tk
r2   Y qX q|S )N)
_credCache_cacheTimestamp)dictvarsKeyError)r   dkr   r   r   __getstate__   s    
zFilePasswordDB.__getstate__c                 C   s   |r|S t t S d S r   r(   r*   r   r   r   r+      s    zFilePasswordDB._cbPasswordMatchc              
   c   s   zt | jdz}|D ]n}| }|| j}| jt|ks| jt|krJq| jrh|| j || j fV  q|| j 	 || j fV  qW 5 Q R X W n: t
k
r } z| jjd|d t W 5 d}~X Y nX dS )a   
        Loads the credentials from the configured file.

        @return: An iterable of C{username, password} couples.
        @rtype: C{iterable}

        @raise UnauthorizedLogin: when failing to read the credentials from the
            file.
        rbz$Unable to load credentials db: {e!r})eN)openr5   rstripsplitr6   r7   lenr8   r9   lowerIOError_logr
   r)   )r   flinepartsrE   r   r   r   _loadCredentials   s    
*zFilePasswordDB._loadCredentialsc                 C   s   | j s| }| jr^| jd ks2tj| j| jkrPtj| j| _t	| 
 | _|| j| fS | 
 D ]\}}||krf||f  S qft|d S r   )r9   rJ   r;   r<   ospathgetmtimer5   r=   r>   rP   r@   )r   r%   upr   r   r   getUser   s    zFilePasswordDB.getUserc                 C   s   z|  |j\}}W n" tk
r6   tt  Y S X t|d }| j	r|d k	rv| 	|j|j
|}||krvt|S tt S t|j|| j|S d S r   )rV   r%   r@   r   r/   r
   r)   r   r0   r:   r&   r   r,   r-   r.   r+   )r   crT   rU   Zuphr   r   r   r      s    
 zFilePasswordDB.requestAvatarId)r3   r   r4   TNF)r   r   r   r   r;   r<   r=   r   rL   r$   rC   r+   rP   rV   r   r   r   r   r   r2   h   s         
;
r2   )Z
__future__r   r   rQ   Zzope.interfacer   r   r   Ztwisted.loggerr   Ztwisted.internetr   Ztwisted.pythonr	   Ztwisted.credr
   r   r   r   r   objectr   r2   ZOnDiskUsernamePasswordDatabaser   r   r   r   <module>   s    "(  