From 7cfb3aaa2487e81c723b65856f694b2990b602c1 Mon Sep 17 00:00:00 2001 From: Alexander Birkner Date: Fri, 2 Dec 2016 18:26:34 +0100 Subject: [PATCH] Added SSH Key Management --- lib/Nitrapi/SSHKeys/Manager.php | 52 +++++++++++++++++ lib/Nitrapi/SSHKeys/SSHKey.php | 98 +++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 lib/Nitrapi/SSHKeys/Manager.php create mode 100644 lib/Nitrapi/SSHKeys/SSHKey.php diff --git a/lib/Nitrapi/SSHKeys/Manager.php b/lib/Nitrapi/SSHKeys/Manager.php new file mode 100644 index 0000000..b3c7f9c --- /dev/null +++ b/lib/Nitrapi/SSHKeys/Manager.php @@ -0,0 +1,52 @@ +api = $api; + } + + /** + * Returns all your SSH Public Keys + * + * @return array + */ + public function getPublicKeys() + { + $url = "user/ssh_keys"; + $keys = []; + foreach ($this->api->dataGet($url)['keys'] as $key) { + $keys[] = new SSHKey($this->api, $key); + } + return $keys; + } + + /** + * Uploads a new SSH Public Key + * + * @param $key + * @param bool $enabled + * @return bool + */ + public function uploadPublicKey($key, $enabled = true) + { + $url = "user/ssh_keys"; + $this->api->dataPost($url, [ + 'key' => $key, + 'enabled' => ($enabled ? 'true' : 'false') + ]); + + return true; + } + +} \ No newline at end of file diff --git a/lib/Nitrapi/SSHKeys/SSHKey.php b/lib/Nitrapi/SSHKeys/SSHKey.php new file mode 100644 index 0000000..46a2666 --- /dev/null +++ b/lib/Nitrapi/SSHKeys/SSHKey.php @@ -0,0 +1,98 @@ +api = $api; + $this->data = $data; + $this->data['full_public_key'] = $this->data['type'] . ' ' . $this->data['public_key'] . ' ' . $this->data['comment']; + } + + /** + * Returns the full SSH public key + * + * @return string + */ + public function getPublicKey() + { + return $this->data['full_public_key']; + } + + /** + * Updates the existing SSH public key + * + * @return SSHKey + */ + public function setPublicKey($key) + { + $this->data['full_public_key'] = $key; + $this->doUpdate(); + return $this; + } + + /** + * Returns true if the key is enabled + * + * @return bool + */ + public function isEnabled() + { + return (bool)$this->data['enabled']; + } + + /** + * Returns true if the key is enabled + * + * @return SSHKey + */ + public function setEnabled($enabled = true) + { + $this->data['enabled'] = $enabled; + $this->doUpdate(); + return $this; + } + + /** + * Deletes this SSH public key + * + * @return bool + */ + public function doDelete() + { + $url = "user/ssh_keys/" . $this->data['id']; + $this->api->dataDelete($url); + + return true; + } + + /** + * Updates this SSH public key in database + * + * @return $this + */ + private function doUpdate() + { + $url = "user/ssh_keys/" . $this->data['id']; + $this->api->dataPost($url, [ + 'key' => $this->getPublicKey(), + 'enabled' => ($this->isEnabled() ? 'true' : 'false') + ]); + + return $this; + } + +} \ No newline at end of file