lmc/profile/twofactor/twofactor.js

96 lines
2.8 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, apiPost, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.confirmButton = "confirm-button";
elements.qrCode = "qr-code-img";
elements.subnav = "subnav-link";
elements.subnavActive = "subnav-link-active";
elements.tfaSecret = "tfa-secret";
elements.tfaToken = "tfa-token";
// Data received from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.confirmButton = {};
ui.qrCode = {};
ui.tfaSecret = {};
ui.tfaToken = {};
// Callback for TFA API call
var displaySecret = function(response)
{
ui.tfaSecret.innerHTML = response.secret;
ui.confirmButton.disabled = false;
};
// Click handler for confirm button
var handleConfirm = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"tfa_code": ui.tfaToken.value
};
apiPost("/profile/tfa-enable-confirm", req, function(response) {
alert("Your emergency scratch code is: " + response.scratch + "\nRecord this code and store it in a safe place. You will not be able to view it again!");
location.href = "/profile/auth";
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Highlight the auth subnav link
var subnavLinks = document.getElementsByClassName(elements.subnav);
for (var i = 0; i < subnavLinks.length; i++) {
if (subnavLinks[i].pathname == "/profile/auth")
subnavLinks[i].className = elements.subnav + " " + elements.subnavActive;
else
subnavLinks[i].className = elements.subnav;
}
// Get element references
ui.confirmButton = document.getElementById(elements.confirmButton);
ui.qrCode = document.getElementById(elements.qrCode);
ui.tfaSecret = document.getElementById(elements.tfaSecret);
ui.tfaToken = document.getElementById(elements.tfaToken);
// Register event handlers
ui.confirmButton.addEventListener("click", handleConfirm);
// Get data from API
apiPost("/profile/tfa-enable", {}, displaySecret);
};
// Attach onload handler
window.addEventListener("load", setup);
})();