lmc/account/oauth_apps/oauth_apps.js

164 lines
4.7 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, apiPost, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.appImg = "app-img";
elements.lmcRow = "lmc-tr1";
elements.lmcRowAlt = "lmc-tr2";
elements.oauthApps = "oauth-apps";
// Data received from API calls
var data = {};
data.apps = [];
// Static references to UI elements
var ui = {};
ui.oauthApps = {};
// 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");
label.innerHTML = app.label;
row.appendChild(label);
var access = document.createElement("td");
if (app.public)
access.innerHTML = "Public";
else
access.innerHTML = "Private";
row.appendChild(access);
var id = document.createElement("td");
id.innerHTML = app.id;
row.appendChild(id);
var callback = document.createElement("td");
callback.innerHTML = app.redirect_uri;
row.appendChild(callback);
var options = document.createElement("td");
var editLink = document.createElement("a");
editLink.href = "/account/oauth_apps/app?aid=" + app.id;
editLink.innerHTML = "Edit";
var separator = document.createElement("span");
separator.innerHTML = " | ";
var resetLink = document.createElement("a");
resetLink.id = "reset-app-" + app.id;
resetLink.href = "#";
resetLink.innerHTML = "Reset Secret";
resetLink.addEventListener("click", resetApp);
var removeLink = document.createElement("a");
removeLink.id = "remove-app-" + app.id;
removeLink.href = "#";
removeLink.innerHTML = "Remove";
removeLink.addEventListener("click", removeApp);
options.appendChild(editLink);
options.appendChild(separator);
options.appendChild(resetLink);
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("/account/oauth-clients?page=" + (response.page + 1), displayApps, null);
return;
}
// Redirect to add page if there are no OAuth apps
if (!data.apps.length)
location.href = "/account/oauth_apps/app?aid=0";
// Add apps to table
for (var i = 0; i < data.apps.length; i++)
ui.oauthApps.appendChild(createAppRow(data.apps[i], i % 2));
};
// Handle removing an OAuth app
var removeApp = function(event)
{
var id = event.currentTarget.id.split("-")[2];
if (!confirm("Are you sure you want to permanently delete this app?"))
return;
apiDelete("/account/oauth-clients/" + id, function(response)
{
location.reload();
});
};
// Handle resetting an OAuth app secret
var resetApp = function(event)
{
var id = event.currentTarget.id.split("-")[2];
if (!confirm("Are you sure you want to permanently reset the secret for this app?"))
return;
apiPost("/account/oauth-clients/" + id + "/reset-secret", {}, function(response)
{
alert("Here is your client secret! Store it securely, as it won't be shown again.\n" + response.secret);
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.oauthApps = document.getElementById(elements.oauthApps);
// Get data from API
apiGet("/account/oauth-clients", displayApps, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();