lmc/profile/api/api.js

328 lines
9.3 KiB
JavaScript
Raw 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, apiDelete, apiGet, apiPut, oauthScopes, parseParams, setupHeader, timeString } from "/global.js";
(function()
{
// Element names specific to this page
elements.apiKeys = "api-keys";
elements.appImg = "app-img";
elements.authorizedApps = "authorized-apps";
elements.hoverInfo = "hover-info";
elements.info = "info";
elements.lmcRow = "lmc-tr1";
elements.lmcRowAlt = "lmc-tr2";
// Data received from API calls
var data = {};
data.apps = [];
data.keys = [];
// Static references to UI elements
var ui = {};
ui.apiKeys = {};
ui.authorizedApps = {};
// Creates a row in the authorized apps table
var createAppRow = function(app, alt)
{
var row = document.createElement("tr");
if (alt)
row.className = elements.lmcRowAlt;
else
row.className = elements.lmcRow;
var thumb = document.createElement("td");
if (app.thumbnail_url && app.thumbnail_url.length) {
var thumbImg = document.createElement("img");
thumbImg.className = elements.appImg;
thumbImg.src = settings.apiURL + app.thumbnail_url;
thumbImg.alt = app.label;
thumb.appendChild(thumbImg);
}
row.appendChild(thumb);
var label = document.createElement("td");
if (app.website && app.website.length) {
var link = document.createElement("a");
link.href = app.website;
link.target = "_blank";
link.innerHTML = app.label;
label.appendChild(link);
} else {
label.innerHTML = app.label;
}
row.appendChild(label);
var created = document.createElement("td");
var created1 = document.createElement("span");
var now = new Date();
var createDt = new Date(app.created + "Z");
created1.innerHTML = timeString(now - createDt, true);
var created2 = document.createElement("span");
created2.className = elements.info;
created2.innerHTML = createDt.toLocaleString();
created.appendChild(created1);
created.appendChild(document.createElement("br"));
created.appendChild(created2);
row.appendChild(created);
var expires = document.createElement("td");
if (app.expiry) {
var expires1 = document.createElement("span");
var expireDt = new Date(app.expiry + "Z");
expires1.innerHTML = timeString(now - expireDt, true);
var expires2 = document.createElement("span");
expires2.className = elements.info;
expires2.innerHTML = expireDt.toLocaleString();
expires.appendChild(expires1);
expires.appendChild(document.createElement("br"));
expires.appendChild(expires2);
} else {
expires.innerHTML = "Never";
}
row.appendChild(expires);
var options = document.createElement("td");
var scopesLink = document.createElement("a");
scopesLink.id = "scopes-app-" + app.id;
scopesLink.href = "#";
scopesLink.innerHTML = "View Scopes";
scopesLink.addEventListener("click", viewScopes);
var separator = document.createElement("span");
separator.innerHTML = " | ";
var removeLink = document.createElement("a");
removeLink.id = "revoke-app-" + app.id;
removeLink.href = "#";
removeLink.innerHTML = "Revoke";
removeLink.addEventListener("click", revoke);
options.appendChild(scopesLink);
options.appendChild(separator);
options.appendChild(removeLink);
row.appendChild(options);
return row;
};
// Creates a row in the API 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 prefix = document.createElement("td");
prefix.innerHTML = key.token + "...";
row.appendChild(prefix);
var created = document.createElement("td");
var created1 = document.createElement("span");
var now = new Date();
var createDt = new Date(key.created + "Z");
created1.innerHTML = timeString(now - createDt, true);
var created2 = document.createElement("span");
created2.className = elements.info;
created2.innerHTML = createDt.toLocaleString();
created.appendChild(created1);
created.appendChild(document.createElement("br"));
created.appendChild(created2);
row.appendChild(created);
var expires = document.createElement("td");
if (key.expiry) {
var expires1 = document.createElement("span");
var expireDt = new Date(key.expiry + "Z");
expires1.innerHTML = timeString(now - expireDt, true);
var expires2 = document.createElement("span");
expires2.className = elements.info;
expires2.innerHTML = expireDt.toLocaleString();
expires.appendChild(expires1);
expires.appendChild(document.createElement("br"));
expires.appendChild(expires2);
} else {
expires.innerHTML = "Never";
}
row.appendChild(expires);
var options = document.createElement("td");
var scopesLink = document.createElement("a");
scopesLink.id = "scopes-key-" + key.id;
scopesLink.href = "#";
scopesLink.innerHTML = "View Scopes";
scopesLink.addEventListener("click", viewScopes);
var separator = document.createElement("span");
separator.innerHTML = " | ";
var renameLink = document.createElement("a");
renameLink.id = "rename-" + key.id;
renameLink.href = "#";
renameLink.innerHTML = "Rename";
renameLink.addEventListener("click", handleRename);
var removeLink = document.createElement("a");
removeLink.id = "revoke-key-" + key.id;
removeLink.href = "#";
removeLink.innerHTML = "Revoke";
removeLink.addEventListener("click", revoke);
options.appendChild(scopesLink);
options.appendChild(separator);
options.appendChild(renameLink);
options.appendChild(separator.cloneNode(true));
options.appendChild(removeLink);
row.appendChild(options);
return row;
};
// Callback for authorized apps API call
var displayApps = function(response)
{
data.apps = data.apps.concat(response.data);
// Request the next page if there are more
if (response.page != response.pages) {
apiGet("/profile/apps?page=" + (response.page + 1), displayApps, null);
return;
}
// Add apps to table
for (var i = 0; i < data.apps.length; i++)
ui.authorizedApps.appendChild(createAppRow(data.apps[i], i % 2));
};
// 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/tokens?page=" + (response.page + 1), displayKeys, null);
return;
}
// Add keys to table
for (var i = 0; i < data.keys.length; i++)
ui.apiKeys.appendChild(createKeyRow(data.keys[i], i % 2));
};
// Handler for renaming an API token
var handleRename = function(event)
{
var id = event.currentTarget.id.split("-")[1];
var name = prompt("Rename API token:");
if (!name)
return;
var req = {
"label": name
};
apiPut("/profile/tokens/" + id, req, function(response)
{
location.reload();
});
};
// Revokes an API key or app
var revoke = function(event)
{
var info = event.currentTarget.id.split("-");
var isApp = (info[1] == "app");
var id = parseInt(info[2]);
var message = "Are you sure you want to remove this API key?";
var url = "/profile/tokens/";
if (isApp) {
message = "Are you sure you want to revoke access for this app?";
url = "/profile/apps/";
}
if (!confirm(message))
return;
apiDelete(url + id, function(response)
{
location.reload();
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.apiKeys = document.getElementById(elements.apiKeys);
ui.authorizedApps = document.getElementById(elements.authorizedApps);
// Get data from API
apiGet("/profile/tokens", displayKeys, null);
apiGet("/profile/apps", displayApps, null);
};
// Display the scopes for a given app or key
var viewScopes = function(event)
{
var info = event.currentTarget.id.split("-");
var isApp = (info[1] == "app");
var id = parseInt(info[2]);
var searchArr = data.keys;
if (isApp)
searchArr = data.apps;
var scopeStr = "";
for (var i = 0; i < searchArr.length; i++) {
if (searchArr[i].id == id) {
scopeStr = searchArr[i].scopes;
break;
}
}
var scopes = [];
if (scopeStr == "*") {
for (var scope in oauthScopes)
scopes.push(scope + ":read_write");
} else {
scopes = scopeStr.split(" ");
}
var alertStr = "";
for (var i = 0; i < scopes.length; i++) {
var scopeInfo = scopes[i].split(":");
alertStr += oauthScopes[scopeInfo[0]] + " - ";
if (scopeInfo[1] == "read_write")
alertStr += "Read/Write\n";
else
alertStr += "Read Only\n";
}
alert(alertStr);
};
// Attach onload handler
window.addEventListener("load", setup);
})();