def _auth(<EOL>self,<EOL>username,<EOL>password,<EOL>pkey,<EOL>key_filenames,<EOL>allow_agent,<EOL>look_for_keys,<EOL>gss_auth,<EOL>gss_kex,<EOL>gss_deleg_creds,<EOL>gss_host,<EOL>passphrase,<EOL>):
saved_exception = None<EOL>two_factor = False<EOL>allowed_types = set()<EOL>two_factor_types = {"<STR_LIT>", "<STR_LIT:password>"}<EOL>if passphrase is None and password is not None:<EOL><INDENT>passphrase = password<EOL><DEDENT>if gss_kex and self._transport.gss_kex_used:<EOL><INDENT>try:<EOL><INDENT>self._transport.auth_gssapi_keyex(username)<EOL>return<EOL><DEDENT>except Exception as e:<EOL><INDENT>saved_exception = e<EOL><DEDENT><DEDENT>if gss_auth:<EOL><INDENT>try:<EOL><INDENT>return self._transport.auth_gssapi_with_mic(<EOL>username, gss_host, gss_deleg_creds<EOL>)<EOL><DEDENT>except Exception as e:<EOL><INDENT>saved_exception = e<EOL><DEDENT><DEDENT>if pkey is not None:<EOL><INDENT>try:<EOL><INDENT>self._log(<EOL>DEBUG,<EOL>"<STR_LIT>".format(<EOL>hexlify(pkey.get_fingerprint())<EOL>),<EOL>)<EOL>allowed_types = set(<EOL>self._transport.auth_publickey(username, pkey)<EOL>)<EOL>two_factor = allowed_types & two_factor_types<EOL>if not two_factor:<EOL><INDENT>return<EOL><DEDENT><DEDENT>except SSHException as e:<EOL><INDENT>saved_exception = e<EOL><DEDENT><DEDENT>if not two_factor:<EOL><INDENT>for key_filename in key_filenames:<EOL><INDENT>for pkey_class in (RSAKey, DSSKey, ECDSAKey, Ed25519Key):<EOL><INDENT>try:<EOL><INDENT>key = self._key_from_filepath(<EOL>key_filename, pkey_class, passphrase<EOL>)<EOL>allowed_types = set(<EOL>self._transport.auth_publickey(username, key)<EOL>)<EOL>two_factor = allowed_types & two_factor_types<EOL>if not two_factor:<EOL><INDENT>return<EOL><DEDENT>break<EOL><DEDENT>except SSHException as e:<EOL><INDENT>saved_exception = e<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if not two_factor and allow_agent:<EOL><INDENT>if self._agent is None:<EOL><INDENT>self._agent = Agent()<EOL><DEDENT>for key in self._agent.get_keys():<EOL><INDENT>try:<EOL><INDENT>id_ = hexlify(key.get_fingerprint())<EOL>self._log(DEBUG, "<STR_LIT>".format(id_))<EOL>allowed_types = set(<EOL>self._transport.auth_publickey(username, key)<EOL>)<EOL>two_factor = allowed_types & two_factor_types<EOL>if not two_factor:<EOL><INDENT>return<EOL><DEDENT>break<EOL><DEDENT>except SSHException as e:<EOL><INDENT>saved_exception = e<EOL><DEDENT><DEDENT><DEDENT>if not two_factor:<EOL><INDENT>keyfiles = []<EOL>for keytype, name in [<EOL>(RSAKey, "<STR_LIT>"),<EOL>(DSSKey, "<STR_LIT>"),<EOL>(ECDSAKey, "<STR_LIT>"),<EOL>(Ed25519Key, "<STR_LIT>"),<EOL>]:<EOL><INDENT>for directory in ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>full_path = os.path.expanduser(<EOL>"<STR_LIT>".format(directory, name)<EOL>)<EOL>if os.path.isfile(full_path):<EOL><INDENT>keyfiles.append((keytype, full_path))<EOL>if os.path.isfile(full_path + "<STR_LIT>"):<EOL><INDENT>keyfiles.append((keytype, full_path + "<STR_LIT>"))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if not look_for_keys:<EOL><INDENT>keyfiles = []<EOL><DEDENT>for pkey_class, filename in keyfiles:<EOL><INDENT>try:<EOL><INDENT>key = self._key_from_filepath(<EOL>filename, pkey_class, passphrase<EOL>)<EOL>allowed_types = set(<EOL>self._transport.auth_publickey(username, key)<EOL>)<EOL>two_factor = allowed_types & two_factor_types<EOL>if not two_factor:<EOL><INDENT>return<EOL><DEDENT>break<EOL><DEDENT>except (SSHException, IOError) as e:<EOL><INDENT>saved_exception = e<EOL><DEDENT><DEDENT><DEDENT>if password is not None:<EOL><INDENT>try:<EOL><INDENT>self._transport.auth_password(username, password)<EOL>return<EOL><DEDENT>except SSHException as e:<EOL><INDENT>saved_exception = e<EOL><DEDENT><DEDENT>elif two_factor:<EOL><INDENT>try:<EOL><INDENT>self._transport.auth_interactive_dumb(username)<EOL>return<EOL><DEDENT>except SSHException as e:<EOL><INDENT>saved_exception = e<EOL><DEDENT><DEDENT>if saved_exception is not None:<EOL><INDENT>raise saved_exception<EOL><DEDENT>raise SSHException("<STR_LIT>")<EOL>
Try, in order:

    - The key(s) passed in, if one was passed in.
    - Any key we can find through an SSH agent (if allowed).
    - Any "id_rsa", "id_dsa" or "id_ecdsa" key discoverable in ~/.ssh/
      (if allowed).
    - Plain username/password auth, if a password was given.

(The password might be needed to unlock a private key [if 'passphrase'
isn't also given], or for two-factor authentication [for which it is
required].)