1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
function fillTemplate(templateId, values) {
var tpl = document.getElementById(templateId).text;
Object.keys(values).forEach(function(key) {
tpl = tpl.replace(new RegExp("\\[\\[ \\." + key + " \\]\\]", "g"), values[key]);
});
var out = [];
tpl.split("\n").forEach(function(line) {
line = line.replace(/^\s+/, "").replace(/\s+$/, "");
if (line !== "") {
out.push(line);
}
});
return out.join("\n");
}
function getJSON(response) {
if (!response.ok) {
document.getElementById("api-error").style.display = "block";
throw new Error("Error loading api");
}
return response.json();
}
function accountTableLinkClick(event) {
event.preventDefault();
var thisRow = event.target.parentElement.parentElement;
var account = thisRow.dataset["accountName"];
var oldText = event.target.text;
var existingTr = document.getElementById("credentials-for-" + account);
if (existingTr) {
existingTr.remove();
}
event.target.text = "Loading...";
fetch(thisRow.dataset["globalCredentialEndpoint"], {
"headers": {
"Accept": event.target.dataset["contentType"]
}
}).then(r => r.text()).then(function(text) {
var newTr = fillTemplate("credential_row_template", {
"Account": account,
"Content": text,
});
event.target.text = oldText;
thisRow.insertAdjacentHTML("afterend", newTr);
thisRow.nextElementSibling.getElementsByTagName("button")[0].addEventListener("click", function(event) {
event.target.parentNode.parentNode.remove();
});
});
return false;
}
function populateAccountTable() {
fetch("/api/account").then(getJSON).then(function(response) {
response.forEach(populateAccountRow);
});
}
function populateAccountRow(row) {
var out = fillTemplate("account_row_template", row);
document.querySelector("#account-table tr").insertAdjacentHTML("afterend", out);
document.querySelectorAll(".account-row a[data-content-type]").forEach(function(e) {
e.addEventListener("click", accountTableLinkClick);
});
}
function getCookie(name) {
return document.cookie.match(new RegExp(name + "=\"?([^;\"]*)\"?;?"))[1];
}
function parseJWT(token) {
return JSON.parse(atob(token.split(".")[1]));
}
function parseJWTExpires(token) {
return new Date(parseJWT(token)["exp"] * 1000);
}
function isAdmin(token) {
return parseJWT(token)["admin"];
}
function populateAPIKey() {
document.querySelector("#api-key textarea").innerText = getCookie("github-token");
document.querySelector("#session-expires").innerText = parseJWTExpires(getCookie("github-token"));
}
function setAdminClass() {
if (isAdmin(getCookie("github-token"))) {
document.body.classList.add("isAdmin");
}
}
|