lmc/profile/ssh/ssh.js

121 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

/*
* This file is part of Linode Manager Classic.
*
* Linode Manager Classic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Linode Manager Classic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Linode Manager Classic. If not, see <https://www.gnu.org/licenses/>.
*/
import { settings, elements, apiGet, md5, parseParams, setupHeader, timeString } from "/global.js";
(function()
{
// Element names specific to this page
elements.hoverInfo = "hover-info";
elements.lmcRow = "lmc-tr1";
elements.lmcRowAlt = "lmc-tr2";
elements.sshKeys = "ssh-keys";
// Data received from API calls
var data = {};
data.keys = [];
// Static references to UI elements
var ui = {};
ui.sshKeys = {};
// Creates a row in the SSH keys table
var createKeyRow = function(key, alt)
{
var row = document.createElement("tr");
if (alt)
row.className = elements.lmcRowAlt;
else
row.className = elements.lmcRow;
var label = document.createElement("td");
label.innerHTML = key.label;
row.appendChild(label);
var keyInfo = document.createElement("td");
var start = document.createElement("span");
start.className = elements.hoverInfo;
start.title = key.ssh_key;
start.innerHTML = key.ssh_key.substring(0, 26);
keyInfo.appendChild(start);
var br = document.createElement("br");
keyInfo.appendChild(br);
var fingerprint = document.createElement("span");
try {
fingerprint.innerHTML = "Fingerprint: " + md5(atob(key.ssh_key.split(" ")[1]), true);
} catch (error) {
fingerprint.innerHTML = "Invalid key";
}
keyInfo.appendChild(fingerprint);
row.appendChild(keyInfo);
var created = document.createElement("td");
var now = new Date();
var createDate = new Date(key.created + "Z");
created.innerHTML = timeString(now - createDate, true);
row.appendChild(created);
var options = document.createElement("td");
var removeLink = document.createElement("a");
removeLink.href = "/profile/ssh/remove?kid=" + key.id;
removeLink.innerHTML = "Remove";
options.appendChild(removeLink);
row.appendChild(options);
return row;
};
// Callback for ssh keys API call
var displayKeys = function(response)
{
data.keys = data.keys.concat(response.data);
// Request the next page if there are more
if (response.page != response.pages) {
apiGet("/profile/sshkeys?page=" + (response.page + 1), displayKeys, null);
return;
}
// Redirect to add page if there are no keys
if (!data.keys.length)
location.href = "/profile/ssh/add";
// Add keys to table
for (var i = 0; i < data.keys.length; i++)
ui.sshKeys.appendChild(createKeyRow(data.keys[i], i % 2));
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.sshKeys = document.getElementById(elements.sshKeys);
// Get data from API
apiGet("/profile/sshkeys", displayKeys, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();