From 34de195afd9a8099ec0f8f66986f1c8d932fcfe9 Mon Sep 17 00:00:00 2001 From: Frederic Ollivier <3798489+fredol@users.noreply.github.com> Date: Mon, 27 Jun 2022 16:48:27 +0200 Subject: [PATCH] Update SftpConnectionProvider.php 6a19b50c(Fix a logic error on private key file) --- src/PhpseclibV3/SftpConnectionProvider.php | 23 +++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/PhpseclibV3/SftpConnectionProvider.php b/src/PhpseclibV3/SftpConnectionProvider.php index 5cd3f5f8d..996261cdd 100644 --- a/src/PhpseclibV3/SftpConnectionProvider.php +++ b/src/PhpseclibV3/SftpConnectionProvider.php @@ -228,21 +228,26 @@ private function authenticateWithPrivateKey(SFTP $connection): void private function loadPrivateKey(): AsymmetricKey { - if ("---" !== substr($this->privateKey, 0, 3) && is_file($this->privateKey)) { + if (is_file($this->privateKey)) { $this->privateKey = file_get_contents($this->privateKey); - } + } else + throw new UnableToLoadPrivateKey(); - try { - if ($this->passphrase !== null) { - return PublicKeyLoader::load($this->privateKey, $this->passphrase); - } + if (str_starts_with($this->privateKey, "---")) { + try { + if ($this->passphrase !== null) { + return PublicKeyLoader::load($this->privateKey, $this->passphrase); + } - return PublicKeyLoader::load($this->privateKey); - } catch (NoKeyLoadedException $exception) { + return PublicKeyLoader::load($this->privateKey); + } catch (NoKeyLoadedException $exception) { + throw new UnableToLoadPrivateKey(); + } + } else throw new UnableToLoadPrivateKey(); - } } + private function authenticateWithAgent(SFTP $connection): void { $agent = new Agent();