@@ -18,6 +18,7 @@ class SSHConnection
18
18
private $ username ;
19
19
private $ password ;
20
20
private $ privateKeyPath ;
21
+ private $ privateKeyContents ;
21
22
private $ timeout ;
22
23
private $ connected = false ;
23
24
private $ ssh ;
@@ -52,6 +53,12 @@ public function withPrivateKey(string $privateKeyPath): self
52
53
return $ this ;
53
54
}
54
55
56
+ public function withPrivateKeyString (string $ privateKeyContents ): self
57
+ {
58
+ $ this ->privateKeyContents = $ privateKeyContents ;
59
+ return $ this ;
60
+ }
61
+
55
62
public function timeout (int $ timeout ): self
56
63
{
57
64
$ this ->timeout = $ timeout ;
@@ -68,8 +75,8 @@ private function sanityCheck()
68
75
throw new InvalidArgumentException ('Username not specified. ' );
69
76
}
70
77
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. ' );
73
80
}
74
81
}
75
82
@@ -83,9 +90,15 @@ public function connect(): self
83
90
throw new RuntimeException ('Error connecting to server. ' );
84
91
}
85
92
86
- if ($ this ->privateKeyPath ) {
93
+ if ($ this ->privateKeyPath || $ this -> privateKeyContents ) {
87
94
$ 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
+
89
102
$ authenticated = $ this ->ssh ->login ($ this ->username , $ key );
90
103
if (!$ authenticated ) {
91
104
throw new RuntimeException ('Error authenticating with public-private key pair. ' );
0 commit comments