From 115f0257e5a914abc48bceb9c490e218c3a49525 Mon Sep 17 00:00:00 2001 From: daragh Date: Sat, 30 Dec 2023 01:47:41 +0000 Subject: [PATCH] feat : frontend for adding & deleting keys --- src/scripts/ssh.js | 127 ++++++++++++++++++++++++++++++++++++++++++++ src/ssh.html | 128 ++++++++++++++++++++------------------------- 2 files changed, 185 insertions(+), 70 deletions(-) create mode 100644 src/scripts/ssh.js diff --git a/src/scripts/ssh.js b/src/scripts/ssh.js new file mode 100644 index 0000000..b791691 --- /dev/null +++ b/src/scripts/ssh.js @@ -0,0 +1,127 @@ +let got_keys = false; + +function get_keys() { + console.log("get_keys running , got_keys = ", got_keys); + let username = document.getElementById("user").value; + let password = document.getElementById("pass").value; + let request = {auth: {user: username, pass: password}}; + + fetch('http://127.0.0.1:8087/ldap/ssh', { + method: 'POST', + body: JSON.stringify(request), + mode: 'cors', + }).then(res => { + if (res.status === 200) { + return res.json(); + } else { + document.getElementById('formStatus').innerHTML = "Failed to fetch"; + } + }).then(data => { + if (data.result === "success") { + document.getElementById('formStatus').innerHTML = "Success : Got keys"; + let keys = data.success; + document.getElementById('key_table').style.display = "table"; + got_keys = true; + keys.forEach(key => { + add_to_table(key); + }) + } else { + document.getElementById('formStatus').innerHTML = "Failed to get keys"; + } + }).catch(err => { + console.log(err); + }); +} + +function add_key() { + let username = document.getElementById("user").value; + let password = document.getElementById("pass").value; + let key = document.getElementById("key_input").value; + let request = {auth: {user: username, pass: password}, key: key}; + if (key === "") { + document.getElementById('formStatus').innerHTML = "Key cannot be empty"; + return; + } + fetch('http://127.0.0.1:8087/ldap/add', { + method: 'POST', + body: JSON.stringify(request), + mode: 'cors', + }).then(res => { + if (res.status === 200) { + return res.json(); + } else { + document.getElementById('formStatus').innerHTML = "Failed to fetch"; + } + + }).then(data => { + if (data.result === "success") { + document.getElementById('formStatus').innerHTML = "Success : Key added"; + document.getElementById('key_table').style.display = "table"; + add_to_table(key); + } else { + document.getElementById('formStatus').innerHTML = "Failed to add"; + } + }).catch(err => { + document.getElementById('formStatus').innerHTML = "Error "; + console.log(err); + }); +} + +function delete_key(row_idx) { + let table = document.getElementById("key_table"); + let key = table.rows[row_idx].cells[0].innerHTML; + let username = document.getElementById("user").value; + let password = document.getElementById("pass").value; + let request = {auth: {user: username, pass: password}, key: key}; + + fetch('http://127.0.0.1:8087/ldap/delete', { + method: 'DELETE', + body: JSON.stringify(request), + mode: 'cors', + }).then(res => { + if (res.status === 200) { + return res.json(); + } else { + document.getElementById('formStatus').innerHTML = "Failed to fetch"; + } + }).then(data => { + if (data.result === "success") { + table.deleteRow(row_idx); + document.getElementById('formStatus').innerHTML = "Success : Key deleted"; + } else { + document.getElementById('formStatus').innerHTML = "Failed to delete"; + } + }).catch(err => { + document.getElementById('formStatus').innerHTML = "Error "; + console.log(err); + }) +} + +function add_to_table(key) { + let table = document.getElementById("key_table"); + let row = table.insertRow(); + let cell1 = row.insertCell(0); + cell1.innerHTML = key; + let cell2 = row.insertCell(1); + + let button = document.createElement("button"); + button.innerHTML = "Delete"; + cell2.appendChild(button); + //cell2.appendChild(document.createElement("button")).innerHTML = "Delete"; + button.addEventListener("click", function () { + delete_key(row.rowIndex); + }); +} + +document.addEventListener("DOMContentLoaded", () => { + document.getElementById("get_keys").addEventListener("click", function () { + if (got_keys) { + console.log("got keys already"); + document.getElementById('formStatus').innerHTML = "Got keys already"; + } else { + get_keys(); + } + }); + document.getElementById("add_key_button").addEventListener("click", add_key); +}); + diff --git a/src/ssh.html b/src/ssh.html index 879ad51..be524f3 100644 --- a/src/ssh.html +++ b/src/ssh.html @@ -1,90 +1,78 @@ - + SSH - - -
- +

SSH

- Add / Remove SSH Keys from your account. -

- Enter Username and Password to see your current keys. + Add / Remove SSH Keys from your account.

- - - - - - - - - - - - -


+ + + + + + + + + + + + +


+ +

- - -
-
- + + +