o bI+@sdZddlZddlmZmZmZddlmZmZddl m Z ddl m Z ddl mZGdd d eZd ZeeGd d d ZeeGd ddZeeGdddZeZdS)zf Basic credential checkers @var ANONYMOUS: An empty tuple used to represent the anonymous avatar ID. N) Attribute Interface implementer) credentialserror)defer)Logger)failurec@s eZdZdZedZddZdS)ICredentialsCheckerzE An object that can check sub-interfaces of L{ICredentials}. zNA list of sub-interfaces of L{ICredentials} which specifies which I may check.cCsdS)a Validate credentials and produce an avatar ID. @param credentials: something which implements one of the interfaces in C{credentialInterfaces}. @return: a L{Deferred} which will fire with a L{bytes} that identifies an avatar, an empty tuple to specify an authenticated anonymous user (provided as L{twisted.cred.checkers.ANONYMOUS}) or fail with L{UnauthorizedLogin}. Alternatively, return the result itself. @see: L{twisted.cred.credentials} N)rr r 7/usr/lib/python3/dist-packages/twisted/cred/checkers.pyrequestAvatarId sz#ICredentialsChecker.requestAvatarIdN)__name__ __module__ __qualname____doc__rcredentialInterfacesr r r r r r s  r r c@s eZdZdZejfZddZdS)AllowAnonymousAccessz A credentials checker that unconditionally grants anonymous access. @cvar credentialInterfaces: Tuple containing L{IAnonymous}. cCs ttS)z Succeed with the L{ANONYMOUS} avatar ID. @return: L{Deferred} that fires with L{twisted.cred.checkers.ANONYMOUS} )rsucceed ANONYMOUSselfrr r r r Gs z$AllowAnonymousAccess.requestAvatarIdN)rrrrr IAnonymousrr r r r r r=s rc@s<eZdZdZejejfZddZddZ ddZ dd Z d S) 'InMemoryUsernamePasswordDatabaseDontUsea9 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}. @cvar credentialInterfaces: Tuple of L{IUsernamePassword} and L{IUsernameHashedPassword}. @ivar users: Mapping of usernames to passwords. @type users: L{dict} mapping L{bytes} to L{bytes} cKsdd|D|_dS)a Initialize the in-memory database. For example:: db = InMemoryUsernamePasswordDatabaseDontUse( user1=b'sesame', user2=b'hunter2', ) @param users: Usernames and passwords to seed the database with. Each username given as a keyword is encoded to L{bytes} as ASCII. Passwords must be given as L{bytes}. @type users: L{dict} of L{str} to L{bytes} cSsi|] \}}|d|qS)ascii)encode).0xyr r r xszDInMemoryUsernamePasswordDatabaseDontUse.__init__..N)itemsusers)rr!r r r __init__hsz0InMemoryUsernamePasswordDatabaseDontUse.__init__cCs||j|<dS)z Set a user's password. @param username: Name of the user. @type username: L{bytes} @param password: Password to associate with the username. @type password: L{bytes} N)r!)rusernamepasswordr r r addUserzs z/InMemoryUsernamePasswordDatabaseDontUse.addUsercC|r|SttSNr FailurerUnauthorizedLoginrmatchedr#r r r _cbPasswordMatchz8InMemoryUsernamePasswordDatabaseDontUse._cbPasswordMatchcCs<|j|jvrt|j|j|j|j|jStt Sr') r#r!r maybeDeferred checkPassword addCallbackr-failrr*rr r r r s  z7InMemoryUsernamePasswordDatabaseDontUse.requestAvatarIdN) rrrrrIUsernamePasswordIUsernameHashedPasswordrr"r%r-r r r r r rPs rc@s`eZdZdZdZdZdZeZ      ddd Z d d Z d d Z ddZ ddZ ddZdS)FilePasswordDBaz 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 L{IUsernamePassword} credentials. If the passwords are stored plaintext, L{IUsernameHashedPassword} credentials will be checkable as well. FNr:TcCsT||_||_||_||_||_||_||_|jdur#tjtj f|_ dStjf|_ dS)a @type filename: L{str} @param filename: The name of the file from which to read username and password information. @type delim: L{bytes} @param delim: The field delimiter used in the file. @type usernameField: L{int} @param usernameField: The index of the username after splitting a line on the delimiter. @type passwordField: L{int} @param passwordField: The index of the password after splitting a line on the delimiter. @type caseSensitive: L{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: L{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) filenamedelimufieldpfield caseSensitivehashcacherr3r4r)rr8r9 usernameField passwordFieldr<r=r>r r r r"s,  zFilePasswordDB.__init__c Cs6tt|}dD]}z||=WqtyYqw|S)N) _credCache_cacheTimestamp)dictvarsKeyError)rdkr r r __getstate__s   zFilePasswordDB.__getstate__cCr&r'r(r+r r r r-r.zFilePasswordDB._cbPasswordMatchc cszUt|jdD}|D]7}|}||j}|jt|ks%|jt|kr&q |jr5||j||jfVq ||j ||jfVq WdWdS1sOwYWdSt yn}z |j j d|dt d}~ww)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. rbNz$Unable to load credentials db: {e!r})e)openr8rstripsplitr9r:lenr;r<lowerOSError_logrr*)rflinepartsrJr r r _loadCredentialss$  & zFilePasswordDB._loadCredentialscCs|js|}|jr/|jdustj|j|jkr(tj|j|_t | |_||j|fS| D]\}}||krA||fSq3t |)a Look up the credentials for a username. @param username: The username to look up. @type username: L{bytes} @returns: Two-tuple of the canonicalicalized username (i.e. lowercase if the database is not case sensitive) and the associated password value, both L{bytes}. @rtype: L{tuple} @raises KeyError: When lookup of the username fails. N) r<rOr>rAospathgetmtimer8rBrCrUrE)rr#upr r r getUsers  zFilePasswordDB.getUsercCsz ||j\}}WntyttYSwt|d}|j rA|dur:| |j|j |}||kr:t |SttSt |j ||j|Sr')r[r#rErr2rr*rr3r=r$rr/r0r1r-)rcrYrZuphr r r r 1s   zFilePasswordDB.requestAvatarId)r6rr7TNF)rrrrr>rArBrrQr"rHr-rUr[r r r r r r5s$  @  r5)rrVzope.interfacerrr twisted.credrrtwisted.internetrtwisted.loggerrtwisted.pythonr r rrrr5OnDiskUsernamePasswordDatabaser r r r s"   $D1