Skip to content

Commit 7cfb3aa

Browse files
author
Alexander Birkner
committed
Added SSH Key Management
1 parent 0d6d267 commit 7cfb3aa

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

lib/Nitrapi/SSHKeys/Manager.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Nitrapi\SSHKeys;
4+
5+
use Nitrapi\Nitrapi;
6+
7+
class Manager
8+
{
9+
/**
10+
* @var Nitrapi
11+
*/
12+
private $api;
13+
14+
public function __construct(Nitrapi $api)
15+
{
16+
$this->api = $api;
17+
}
18+
19+
/**
20+
* Returns all your SSH Public Keys
21+
*
22+
* @return array
23+
*/
24+
public function getPublicKeys()
25+
{
26+
$url = "user/ssh_keys";
27+
$keys = [];
28+
foreach ($this->api->dataGet($url)['keys'] as $key) {
29+
$keys[] = new SSHKey($this->api, $key);
30+
}
31+
return $keys;
32+
}
33+
34+
/**
35+
* Uploads a new SSH Public Key
36+
*
37+
* @param $key
38+
* @param bool $enabled
39+
* @return bool
40+
*/
41+
public function uploadPublicKey($key, $enabled = true)
42+
{
43+
$url = "user/ssh_keys";
44+
$this->api->dataPost($url, [
45+
'key' => $key,
46+
'enabled' => ($enabled ? 'true' : 'false')
47+
]);
48+
49+
return true;
50+
}
51+
52+
}

lib/Nitrapi/SSHKeys/SSHKey.php

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Nitrapi\SSHKeys;
4+
5+
use Nitrapi\Nitrapi;
6+
7+
class SSHKey {
8+
9+
/**
10+
* @var Nitrapi
11+
*/
12+
private $api;
13+
14+
/**
15+
* @var array
16+
*/
17+
private $data;
18+
19+
public function __construct(Nitrapi $api, array $data) {
20+
$this->api = $api;
21+
$this->data = $data;
22+
$this->data['full_public_key'] = $this->data['type'] . ' ' . $this->data['public_key'] . ' ' . $this->data['comment'];
23+
}
24+
25+
/**
26+
* Returns the full SSH public key
27+
*
28+
* @return string
29+
*/
30+
public function getPublicKey()
31+
{
32+
return $this->data['full_public_key'];
33+
}
34+
35+
/**
36+
* Updates the existing SSH public key
37+
*
38+
* @return SSHKey
39+
*/
40+
public function setPublicKey($key)
41+
{
42+
$this->data['full_public_key'] = $key;
43+
$this->doUpdate();
44+
return $this;
45+
}
46+
47+
/**
48+
* Returns true if the key is enabled
49+
*
50+
* @return bool
51+
*/
52+
public function isEnabled()
53+
{
54+
return (bool)$this->data['enabled'];
55+
}
56+
57+
/**
58+
* Returns true if the key is enabled
59+
*
60+
* @return SSHKey
61+
*/
62+
public function setEnabled($enabled = true)
63+
{
64+
$this->data['enabled'] = $enabled;
65+
$this->doUpdate();
66+
return $this;
67+
}
68+
69+
/**
70+
* Deletes this SSH public key
71+
*
72+
* @return bool
73+
*/
74+
public function doDelete()
75+
{
76+
$url = "user/ssh_keys/" . $this->data['id'];
77+
$this->api->dataDelete($url);
78+
79+
return true;
80+
}
81+
82+
/**
83+
* Updates this SSH public key in database
84+
*
85+
* @return $this
86+
*/
87+
private function doUpdate()
88+
{
89+
$url = "user/ssh_keys/" . $this->data['id'];
90+
$this->api->dataPost($url, [
91+
'key' => $this->getPublicKey(),
92+
'enabled' => ($this->isEnabled() ? 'true' : 'false')
93+
]);
94+
95+
return $this;
96+
}
97+
98+
}

0 commit comments

Comments
 (0)