165 lines
4.8 KiB
JavaScript
165 lines
4.8 KiB
JavaScript
/*
|
|
* 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, timeString } from "/global.js";
|
|
|
|
(function()
|
|
{
|
|
// Element names specific to this page
|
|
elements.pwReset = "pw-reset";
|
|
elements.tfa = "tfa";
|
|
elements.tfaDisable = "tfa-disable";
|
|
elements.tfaEnable = "tfa-enable";
|
|
elements.tfaStatus = "tfa-status";
|
|
elements.trusted = "trusted";
|
|
elements.untrust = "untrust";
|
|
|
|
// Data received from API calls
|
|
var data = {};
|
|
|
|
// Static references to UI elements
|
|
var ui = {};
|
|
ui.pwReset = {};
|
|
ui.tfa = [];
|
|
ui.tfaDisable = {};
|
|
ui.tfaEnable = {};
|
|
ui.tfaStatus = {};
|
|
ui.trusted = {};
|
|
|
|
// Creates a row for the trusted devices list
|
|
var createTrustedRow = function(device)
|
|
{
|
|
var row = document.createElement("div");
|
|
row.id = elements.trusted + "-" + device.id;
|
|
var br = document.createElement("br");
|
|
row.appendChild(br);
|
|
|
|
var userAgent = document.createElement("span");
|
|
userAgent.innerHTML = device.user_agent;
|
|
row.appendChild(userAgent);
|
|
row.appendChild(br.cloneNode(true));
|
|
|
|
var lastAuth = document.createElement("span");
|
|
var now = new Date();
|
|
var authDt = new Date(device.last_authenticated + "Z");
|
|
lastAuth.innerHTML = "Last authenticated " + timeString(now - authDt, true) + " from " + device.last_remote_addr;
|
|
row.appendChild(lastAuth);
|
|
row.appendChild(br.cloneNode(true));
|
|
|
|
var expiry = document.createElement("span");
|
|
var expireDt = new Date(device.expiry + "Z");
|
|
expiry.innerHTML = "Expires in " + timeString(expireDt - now, false) + " - ";
|
|
row.appendChild(expiry);
|
|
|
|
var untrust = document.createElement("a");
|
|
untrust.id = elements.untrust + "-" + device.id;
|
|
untrust.href = "#";
|
|
untrust.innerHTML = "untrust";
|
|
untrust.addEventListener("click", handleUntrust);
|
|
row.appendChild(untrust);
|
|
|
|
return row;
|
|
};
|
|
|
|
// Callback for profile API call
|
|
var displayProfile = function(response)
|
|
{
|
|
ui.pwReset.href += response.username;
|
|
if (response.two_factor_auth) {
|
|
apiGet("/profile/devices", displayTrusted, null);
|
|
ui.tfaStatus.innerHTML = "Enabled";
|
|
ui.tfaEnable.innerHTML = "Reset Two-Factor Authentication";
|
|
for (var i = 0; i < ui.tfa.length; i++) {
|
|
if (ui.tfa[i].tagName == "TR")
|
|
ui.tfa[i].style.display = "table-row";
|
|
else
|
|
ui.tfa[i].style.display = "initial";
|
|
}
|
|
} else {
|
|
ui.tfaStatus.innerHTML = "Disabled";
|
|
}
|
|
|
|
ui.tfaEnable.disabled = false;
|
|
};
|
|
|
|
// Callback for trusted devices API call
|
|
var displayTrusted = function(response)
|
|
{
|
|
for (var i = 0; i < response.data.length; i++)
|
|
ui.trusted.appendChild(createTrustedRow(response.data[i]));
|
|
};
|
|
|
|
// Click handler for disable 2FA link
|
|
var handleDisableTFA = function(event)
|
|
{
|
|
if (!confirm("Are you sure you want to disable two-factor authentication?"))
|
|
return;
|
|
|
|
apiPost("/profile/tfa-disable", {}, function(response)
|
|
{
|
|
location.reload();
|
|
});
|
|
};
|
|
|
|
// Click handler for enable/reset 2FA button
|
|
var handleEnableTFA = function(event)
|
|
{
|
|
if (event.currentTarget.disabled)
|
|
return;
|
|
|
|
location.href = "/profile/twofactor";
|
|
};
|
|
|
|
// Click handler for untrust links
|
|
var handleUntrust = function(event)
|
|
{
|
|
var deviceID = parseInt(event.currentTarget.id.split("-")[1]);
|
|
apiDelete("/profile/devices/" + deviceID, function(response)
|
|
{
|
|
var row = document.getElementById(elements.trusted + "-" + deviceID);
|
|
row.remove();
|
|
});
|
|
};
|
|
|
|
// Initial setup
|
|
var setup = function()
|
|
{
|
|
// Parse URL parameters
|
|
data.params = parseParams();
|
|
|
|
setupHeader();
|
|
|
|
// Get element references
|
|
ui.pwReset = document.getElementById(elements.pwReset);
|
|
ui.tfa = document.getElementsByClassName(elements.tfa);
|
|
ui.tfaDisable = document.getElementById(elements.tfaDisable);
|
|
ui.tfaEnable = document.getElementById(elements.tfaEnable);
|
|
ui.tfaStatus = document.getElementById(elements.tfaStatus);
|
|
ui.trusted = document.getElementById(elements.trusted);
|
|
|
|
// Register event handlers
|
|
ui.tfaDisable.addEventListener("click", handleDisableTFA);
|
|
ui.tfaEnable.addEventListener("click", handleEnableTFA);
|
|
|
|
// Get data from API
|
|
apiGet("/profile", displayProfile, null);
|
|
};
|
|
|
|
// Attach onload handler
|
|
window.addEventListener("load", setup);
|
|
})();
|