/
main.js
158 lines (140 loc) · 4.78 KB
/
main.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function addRow() {
var key = document.getElementById("key");
var value = document.getElementById("value");
if (!key.value || !value.value) {
alert("Neither 'Key' nor 'Value' can be null!");
return
}
if (!key.value.trim() || !value.value.trim()) {
alert("Neither 'Key' nor 'Value' can be blank white spaces!");
return
}
var table = document.getElementById("tableData");
// Check for duplicate Key before adding it to the table memory
for (let i = 0; i < table.rows.length; i++)
{
let existing = table.rows[i].cells[1].innerHTML;
if (key.value === existing) {
alert(`Key '${key.value}' already exists!`)
return
}
}
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
row.insertCell(0).innerHTML= '<input type="button" value = "Delete" onClick="Javacsript:deleteRow(this)">';
row.insertCell(1).innerHTML= key.value;
if (key.value === "password") {
var hide_value = `<input type="password" value='${value.value}' id="hide"><input type="checkbox" onclick="hide()">Show`
} else {
var hide_value = value.value;
}
row.insertCell(2).innerHTML = hide_value;
}
function hide() {
var x = document.getElementById("hide");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
function deleteRow(obj) {
var index = obj.parentNode.parentNode.rowIndex;
var table = document.getElementById("tableData");
table.deleteRow(index);
}
function tableCells(t){
if(t.cells) return t.cells; // use internal routine when supported
for(var a=[], r=t.rows, y=0, c, x; t=r[y++];){
for(c=t.cells, x=0; t=c[x++]; a.push(t));
}
return a;
}
function addTable(e) {
e.preventDefault();
var secret = $("#secret").val();
if (!secret) {
alert("Secret cannot be empty!");
return
}
if (!secret.trim()) {
alert("Secret cannot be a blank white space!");
return
}
var table = document.getElementById("tableData");
// for (var i = 0, row; row = table.rows[i]; i++) {
// console.log(row.innerHTML);
// for (var j = 0, col; col = row.cells[j]; j++) {
// console.log(col.innerHTML);
// }
// }
var array = [];
for (var i = 3, cell; cell = tableCells(table)[i]; i++) {
if (i === 3 || i % 3 == 0 ) { continue; }
if (cell.innerHTML.startsWith('<input type="password" value=')) {
array.push(document.getElementById("hide").value);
} else {
array.push(cell.innerHTML);
}
}
if (array.length < 2) {
alert("At least one Key-Value pair is required to build the payload!");
return
}
var dict = {}
for (var j = 0; j < array.length - 1; j++) {
if (j === 0 || j % 2 == 0 ) {
// Check for duplicate Key in the dict when submitted
if (!(array[j] in dict)){
dict[array[j]] = array[j+1]
} else {
alert(`Key '${array[j]}' already exists.`);
return
}
}
}
tokenize(secret, dict);
}
function load() {
console.log("Page load finished");
}
function base64url(source) {
// Encode in classical base64
encodedSource = CryptoJS.enc.Base64.stringify(source);
// Remove padding equal characters
encodedSource = encodedSource.replace(/=+$/, '');
// Replace characters according to base64url specifications
encodedSource = encodedSource.replace(/\+/g, '-');
encodedSource = encodedSource.replace(/\//g, '_');
return encodedSource;
}
function tokenize(secret, payload) {
var header = {
"alg": "HS256",
"typ": "JWT"
};
var stringifiedHeader = CryptoJS.enc.Utf8.parse(JSON.stringify(header));
var encodedHeader = base64url(stringifiedHeader);
var stringifiedData = CryptoJS.enc.Utf8.parse(JSON.stringify(payload));
var encodedData = base64url(stringifiedData);
var token = encodedHeader + "." + encodedData;
var signature = CryptoJS.HmacSHA256(token, secret);
signature = base64url(signature);
var signedToken = token + "." + signature;
document.getElementById("encoded").innerHTML = signedToken;
}
function parseJWT (e) {
e.preventDefault();
var token = $("#encoded_token").val().trim();
if (!token) {
alert("Token cannot be null!");
return
}
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(window.atob(base64).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
document.getElementById("decoded").innerHTML = jsonPayload;
// console.log(JSON.parse(jsonPayload));
}