def format_openssh_pubkey(key: PKey) -> str: """Format the given ``key`` to an OpenSSH public key line, used by :file:`authorized_keys`, :file:`id_rsa.pub`, etc. :param key: the key object to format :type key: :class:`paramiko.pkey.PKey` :return: a formatted openssh public key line :rtype: :class:`str` """ return '{} {} '.format(key.get_name(), key.get_base64())
def check_auth_publickey(self, username: Text, key: PKey) -> int: ssh_pub_key = SSHKey(f"{key.get_name()} {key.get_base64()}") ssh_pub_key.parse() logging.debug("check_auth_publickey: username=%s, key=%s %s %sbits", username, key.get_name(), ssh_pub_key.hash_sha256(), ssh_pub_key.bits) if self.session.session_log_dir: os.makedirs(self.session.session_log_dir, exist_ok=True) pubkeyfile_path = os.path.join(self.session.session_log_dir, 'publickeys') with open(pubkeyfile_path, 'a+') as pubkeyfile: pubkeyfile.write( f"{key.get_name()} {key.get_base64()} saved-from-auth-publickey\n" ) if self.args.disable_pubkey_auth: logging.debug( "Publickey login attempt, but publickey auth was disabled!") return paramiko.common.AUTH_FAILED if self.args.accept_first_publickey: logging.debug('host probing disabled - first key accepted') if self.args.disallow_publickey_auth: logging.debug( 'ignoring argument --disallow-publickey-auth, first key still accepted' ) self.session.authenticator.authenticate(username, key=None) self.session.accepted_key = key return paramiko.common.AUTH_SUCCESSFUL auth_result: int = self.session.authenticator.authenticate(username, key=key) if auth_result == paramiko.common.AUTH_SUCCESSFUL: self.session.accepted_key = key if self.session.accepted_key is not None and self.args.enable_trivial_auth: logging.debug("found valid key for trivial authentication") return paramiko.common.AUTH_FAILED if self.args.disallow_publickey_auth: return paramiko.common.AUTH_FAILED return auth_result
def _get_key_params(self, public_key: PKey) -> Tuple[str, str]: return public_key.get_name(), get_key_fingerprint(public_key, '')