lmc/account/oauth_apps/app/app.js

138 lines
3.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, apiGet, apiPost, apiPut, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.callback = "callback";
elements.label = "label";
elements.pub = "public";
elements.saveButton = "save-button";
elements.thumbnail = "thumbnail";
// Data received from API calls
var data = {};
data.app = {};
// Static references to UI elements
var ui = {};
ui.callback = {};
ui.label = {};
ui.pub = {};
ui.saveButton = {};
ui.thumbnail = {};
// Temporary state
var state = {};
state.sentApp = false;
state.sentThumb = false;
// Callback for app details API call
var displayApp = function(response)
{
data.app = response;
ui.label.value = data.app.label;
ui.callback.value = data.app.redirect_uri;
ui.pub.checked = data.app.public;
ui.saveButton.disabled = false;
};
// Click handler for save button
var handleSave = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"label": ui.label.value,
"redirect_uri": ui.callback.value
};
if (data.params.aid != "0") {
if (ui.thumbnail.files.length && ui.thumbnail.files[0].type.startsWith("image/")) {
apiPut("/account/oauth-clients/" + data.params.aid + "/thumbnail", ui.thumbnail.files[0], function(response)
{
state.sentThumb = true;
if (state.sentApp)
location.href = "/account/oauth_apps";
});
} else {
state.sentThumb = true;
}
apiPut("/account/oauth-clients/" + data.params.aid, req, function(response)
{
state.sentApp = true;
if (state.sentThumb)
location.href = "/account/oauth_apps";
});
} else {
req.public = ui.pub.checked;
apiPost("/account/oauth-clients", req, function(response)
{
alert("Here is your client secret! Store it securely, as it won't be shown again.\n" + response.secret);
if (ui.thumbnail.files.length && ui.thumbnail.files[0].type.startsWith("image/")) {
apiPut("/account/oauth-clients/" + response.id + "/thumbnail", ui.thumbnail.files[0], function(response)
{
location.href = "/account/oauth_apps";
});
} else {
location.href = "/account/oauth_apps";
}
});
}
ui.saveButton.disabled = true;
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
// Assume 0 if no app ID supplied
if (!data.params.aid)
data.params.aid = "0";
setupHeader();
// Get element references
ui.callback = document.getElementById(elements.callback);
ui.label = document.getElementById(elements.label);
ui.pub = document.getElementById(elements.pub);
ui.saveButton = document.getElementById(elements.saveButton);
ui.thumbnail = document.getElementById(elements.thumbnail);
// Fetch app details if specified
if (data.params.aid != "0") {
apiGet("/account/oauth-clients/" + data.params.aid, displayApp, null);
} else {
ui.pub.disabled = false;
ui.saveButton.disabled = false;
}
// Register event handlers
ui.saveButton.addEventListener("click", handleSave);
};
// Attach onload handler
window.addEventListener("load", setup);
})();