Skip to content

Commit

Permalink
PhpsecServer: add support for using a RSA key (#137)
Browse files Browse the repository at this point in the history
keys generated by `ssh-keygen -t rsa` are working (with and without passphrase)
  • Loading branch information
BigMichi1 committed Dec 8, 2020
1 parent 74b82ac commit d0c5398
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Deployment/PhpsecServer.php
Expand Up @@ -4,6 +4,7 @@

namespace Deployment;

use phpseclib\Crypt\RSA;
use phpseclib\Net\SFTP;

class PhpsecServer implements Server
Expand Down Expand Up @@ -52,8 +53,21 @@ public function connect(): void
$this->sftp->disconnect(); // @ may fail
}
$sftp = new SFTP($this->url['host'], $this->url['port'] ?? 22);
if (!$sftp->login(urldecode($this->url['user']), urldecode($this->url['pass']))) {
exit('Login Failed');
if ($this->privateKey) {
$rsa = new RSA();
if ($this->passPhrase) {
$rsa->setPassword($this->passPhrase);
}
if (!$rsa->loadKey(file_get_contents($this->privateKey))) {
exit('Loading private key failed');
}
if (!$sftp->login(urldecode($this->url['user']), $rsa)) {
exit('Login Failed');
}
} else {
if (!$sftp->login(urldecode($this->url['user']), urldecode($this->url['pass']))) {
exit('Login Failed');
}
}
$this->sftp = $sftp;
}
Expand Down

0 comments on commit d0c5398

Please sign in to comment.