@@ -18,6 +18,7 @@ class SSHConnection
1818    private  $ username ;
1919    private  $ password ;
2020    private  $ privateKeyPath ;
21+     private  $ privateKeyContents ;
2122    private  $ timeout ;
2223    private  $ connected  = false ;
2324    private  $ ssh ;
@@ -52,6 +53,12 @@ public function withPrivateKey(string $privateKeyPath): self
5253        return  $ this  ;
5354    }
5455
56+     public  function  withPrivateKeyString (string  $ privateKeyContents ): self 
57+     {
58+         $ this  ->privateKeyContents  = $ privateKeyContents ;
59+         return  $ this  ;
60+     }
61+ 
5562    public  function  timeout (int  $ timeout ): self 
5663    {
5764        $ this  ->timeout  = $ timeout ;
@@ -68,8 +75,8 @@ private function sanityCheck()
6875            throw  new  InvalidArgumentException ('Username not specified. ' );
6976        }
7077
71-         if  (!$ this  ->password  && ( !$ this  ->privateKeyPath ) ) {
72-             throw  new  InvalidArgumentException ('No password or private key path  specified. ' );
78+         if  (!$ this  ->password  && !$ this  ->privateKeyPath  && ! $ this -> privateKeyContents ) {
79+             throw  new  InvalidArgumentException ('No password or private key specified. ' );
7380        }
7481    }
7582
@@ -83,9 +90,15 @@ public function connect(): self
8390            throw  new  RuntimeException ('Error connecting to server. ' );
8491        }
8592
86-         if  ($ this  ->privateKeyPath ) {
93+         if  ($ this  ->privateKeyPath  ||  $ this -> privateKeyContents ) {
8794            $ key  = new  RSA ();
88-             $ key ->loadKey (file_get_contents ($ this  ->privateKeyPath ));
95+ 
96+             if  ($ this  ->privateKeyPath ) {
97+                 $ key ->loadKey (file_get_contents ($ this  ->privateKeyPath ));
98+             } else  if  ($ this  ->privateKeyContents ) {
99+                 $ key ->loadKey ($ this  ->privateKeyContents );
100+             }
101+ 
89102            $ authenticated  = $ this  ->ssh ->login ($ this  ->username , $ key );
90103            if  (!$ authenticated ) {
91104                throw  new  RuntimeException ('Error authenticating with public-private key pair. ' );
0 commit comments