Implemented user profile settings, OAuth apps, and maintenance windows. Other minor fixes/improvements

This commit is contained in:
2021-03-11 10:37:07 -05:00
parent a020009c47
commit c58e2fc545
48 changed files with 3152 additions and 36 deletions

57
profile/api/add/add.css Normal file
View File

@ -0,0 +1,57 @@
/*
* 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 url('/global.css');
#add {
padding: 15px 15px 15px;
}
.lmc-table>tbody:not(.lmc-tbody-head)>tr>td:first-of-type {
font-weight: bold;
text-align: right;
}
#scopes-table {
border-bottom: 1px solid #4C4C4C;
border-collapse: separate;
border-left: 1px solid #B2B2B2;
border-right: 1px solid #4C4C4C;
border-spacing: 2px;
border-top: 1px solid #B2B2B2;
margin-bottom: 30px;
}
#scopes-table label {
display: block;
}
#scopes-table tbody td {
text-align: center;
}
#scopes-table tbody td:first-of-type {
text-align: left;
}
#scopes-table td {
border-bottom: 1px solid #B2B2B2;
border-left: 1px solid #4C4C4C;
border-right: 1px solid #B2B2B2;
border-top: 1px solid #4C4C4C;
padding: 2px;
}

191
profile/api/add/add.js Normal file
View File

@ -0,0 +1,191 @@
/*
* 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, apiPost, oauthScopes, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.addButton = "add-button";
elements.expiry = "expiry";
elements.label = "label";
elements.scopeBox = "scope-box";
elements.scopes = "scopes";
elements.selectNone = "select-none";
elements.selectRo = "select-read_only";
elements.selectRw = "select-read_write";
// Data received from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.addButton = {};
ui.expiry = {};
ui.label = {};
ui.scopes = {};
ui.selectNone = {};
ui.selectRo = {};
ui.selectRw = {};
// Display OAuth scopes in scope selector
var displayScopes = function()
{
for (var scope in oauthScopes) {
var row = document.createElement("tr");
var label = document.createElement("td");
label.innerHTML = oauthScopes[scope];
row.appendChild(label);
var none = document.createElement("td");
var noneLabel = document.createElement("label");
noneLabel.htmlFor = "scope-" + scope + "-none";
var noneBox = document.createElement("input");
noneBox.id = "scope-" + scope + "-none";
noneBox.className = elements.scopeBox;
noneBox.type = "radio";
noneBox.name = "scope-" + scope;
noneBox.checked = true;
noneBox.addEventListener("input", handleSelectScope);
noneLabel.appendChild(noneBox);
none.appendChild(noneLabel);
row.appendChild(none);
var ro = document.createElement("td");
var roLabel = document.createElement("label");
roLabel.htmlFor = "scope-" + scope + "-read_only";
var roBox = document.createElement("input");
roBox.id = "scope-" + scope + "-read_only";
roBox.className = elements.scopeBox;
roBox.type = "radio";
roBox.name = "scope-" + scope;
roBox.addEventListener("input", handleSelectScope);
roLabel.appendChild(roBox);
ro.appendChild(roLabel);
row.appendChild(ro);
var rw = document.createElement("td");
var rwLabel = document.createElement("label");
rwLabel.htmlFor = "scope-" + scope + "-read_write";
var rwBox = document.createElement("input");
rwBox.id = "scope-" + scope + "-read_write";
rwBox.className = elements.scopeBox;
rwBox.type = "radio";
rwBox.name = "scope-" + scope;
rwBox.addEventListener("input", handleSelectScope);
rwLabel.appendChild(rwBox);
rw.appendChild(rwLabel);
row.appendChild(rw);
ui.scopes.appendChild(row);
}
};
// Click handler for add button
var handleAdd = function(event)
{
var req = {
"label": ui.label.value
};
var expiry = parseInt(ui.expiry.value);
if (expiry) {
var expireDt = new Date(Date.now() + expiry).toISOString();
req.expiry = expireDt.slice(0, expireDt.indexOf("."));
}
if (ui.selectRw.checked) {
req.scopes = "*";
} else {
req.scopes = [];
var buttons = document.getElementsByClassName(elements.scopeBox);
for (var i = 0; i < buttons.length; i++) {
if (!buttons[i].checked)
continue;
var attrs = buttons[i].id.split("-");
if (attrs[2] == "none")
continue;
req.scopes.push(attrs[1] + ":" + attrs[2]);
}
req.scopes = req.scopes.join(" ");
if (!req.scopes.length) {
alert("You must select some permissions.");
return;
}
}
apiPost("/profile/tokens", req, function(response)
{
alert("Your API token has been created. Store this secret - it won't be shown again.\n" + response.token);
location.href = "/profile/api";
});
};
// Handler for the "select all" row
var handleSelectAll = function(event)
{
var column = event.currentTarget.id.split("-")[1];
var buttons = document.getElementsByClassName(elements.scopeBox);
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].id.split("-")[2] == column)
buttons[i].checked = true;
}
};
// Handler for individual scope selectors
var handleSelectScope = function(event)
{
ui.selectNone.checked = false;
ui.selectRo.checked = false;
ui.selectRw.checked = false;
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.addButton = document.getElementById(elements.addButton);
ui.expiry = document.getElementById(elements.expiry);
ui.label = document.getElementById(elements.label);
ui.scopes = document.getElementById(elements.scopes);
ui.selectNone = document.getElementById(elements.selectNone);
ui.selectRo = document.getElementById(elements.selectRo);
ui.selectRw = document.getElementById(elements.selectRw);
// Add scopes to tables
displayScopes();
ui.selectNone.checked = false;
ui.selectRo.checked = false;
ui.selectRw.checked = false;
// Register event handlers
ui.addButton.addEventListener("click", handleAdd);
ui.selectNone.addEventListener("change", handleSelectAll);
ui.selectRo.addEventListener("change", handleSelectAll);
ui.selectRw.addEventListener("change", handleSelectAll);
};
// Attach onload handler
window.addEventListener("load", setup);
})();

View File

@ -0,0 +1,93 @@
<!--
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/>.
-->
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LMC - My Profile // Add API Key</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<link rel="stylesheet" type="text/css" href="add.css" />
<script src="add.js" type="module"></script>
</head>
<body>
<!--#include virtual="/include/header.html"-->
<!--#include virtual="/include/profile_subnav.html"-->
<div id="main-content" class="wrapper">
<div id="add">
<table class="lmc-table">
<thead>
<tr>
<td colspan="2">Add an SSH Key</td>
</tr>
</thead>
<tbody>
<tr class="lmc-tr3">
<td>Label</td>
<td><input id="label" type="text" size="30" /></td>
</tr>
<tr class="lmc-tr3">
<td>Expires</td>
<td>
<select id="expiry">
<!-- The values are milliseconds, in case you were wondering -->
<option value="0">Never</option>
<option value="3600000">1 hour</option>
<option value="21600000">6 hours</option>
<option value="43200000">12 hours</option>
<option value="86400000">1 day</option>
<option value="172800000">2 days</option>
<option value="604800000">1 week</option>
<option value="1209600000">2 weeks</option>
<option value="2419200000">4 weeks</option>
<option value="15768000000">6 months</option>
<option value="31536000000">12 months</option>
</select>
</td>
</tr>
<tr class="lmc-tr3">
<td>Scopes</td>
<td>
<table id="scopes-table">
<thead>
<tr>
<td>Access</td>
<td>None</td>
<td>Read Only</td>
<td>Read/Write</td>
</tr>
</thead>
<tbody id="scopes">
<tr>
<td><strong>Select All</strong></td>
<td><label for="select-none"><input id="select-none" type="radio" name="select-all" /></label></td>
<td><label for="select-read_only"><input id="select-read_only" type="radio" name="select-all" /></label></td>
<td><label for="select-read_write"><input id="select-read_write" type="radio" name="select-all" /></label></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr class="lmc-tr3">
<td></td>
<td><button id="add-button" type="button">Create API Key</button></td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>

41
profile/api/api.css Normal file
View File

@ -0,0 +1,41 @@
/*
* 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 url('/global.css');
#api {
padding: 15px 15px 15px;
}
.app-img {
height: 36px;
vertical-align: middle;
width: 36px;
}
.hover-info {
cursor: help;
text-decoration: dotted underline;
}
td:nth-of-type(5) {
text-align: center;
}
thead span.info {
font-size: 12px;
}

327
profile/api/api.js Normal file
View File

@ -0,0 +1,327 @@
/*
* 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);
})();

67
profile/api/index.shtml Normal file
View File

@ -0,0 +1,67 @@
<!--
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/>.
-->
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LMC - My Profile // API Keys</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<link rel="stylesheet" type="text/css" href="api.css" />
<script src="api.js" type="module"></script>
</head>
<body>
<!--#include virtual="/include/header.html"-->
<!--#include virtual="/include/profile_subnav.html"-->
<div id="main-content" class="wrapper">
<div id="api">
<table class="lmc-table">
<thead>
<tr>
<td colspan="5">API Keys</td>
</tr>
<tr>
<td>Label</td>
<td>Key <span class="info">(prefix only)</span></td>
<td>Created</td>
<td>Expires</td>
<td>Options</td>
</tr>
</thead>
<tbody id="api-keys"></tbody>
</table>
<p class="sub-links">
<a href="/profile/api/add">Add an API Key</a>
</p>
<table class="lmc-table">
<thead>
<tr>
<td colspan="5">Authorized Apps</td>
</tr>
<tr>
<td></td>
<td>App</td>
<td>Created</td>
<td>Expires</td>
<td>Options</td>
</tr>
</thead>
<tbody id="authorized-apps"></tbody>
</table>
</div>
</div>
</body>
</html>