Committing updates.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
|
||||
<html lang="ru" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
|
||||
<head>
|
||||
<!-- Required meta tags -->
|
||||
<meta charset="utf-8">
|
||||
@@ -44,7 +44,6 @@
|
||||
<a class="nav-link dropdown-toggle" href="#" id="btnSettings" data-toggle="dropdown" role="tab" aria-haspopup="true" aria-expanded="false">Other</a>
|
||||
<div class="dropdown-menu" aria-labelledby="other">
|
||||
<a class="dropdown-item" data-toggle="tab" href="#tab-config">Settings</a>
|
||||
<a class="dropdown-item" onclick="SignOut()">Log out</a>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -621,159 +620,6 @@
|
||||
</div>
|
||||
`;
|
||||
|
||||
let token;
|
||||
|
||||
function parseJWT(token) {
|
||||
try {
|
||||
// Get Token Header
|
||||
const base64HeaderUrl = token.split('.')[0];
|
||||
const base64Header = base64HeaderUrl.replace('-', '+').replace('_', '/');
|
||||
const headerData = JSON.parse(window.atob(base64Header));
|
||||
|
||||
// Get Token payload and date's
|
||||
const base64Url = token.split('.')[1];
|
||||
const base64 = base64Url.replace('-', '+').replace('_', '/');
|
||||
const dataJWT = JSON.parse(window.atob(base64));
|
||||
|
||||
dataJWT.header = headerData;
|
||||
|
||||
// TODO: add expiration at check ...
|
||||
|
||||
return dataJWT;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class Token {
|
||||
|
||||
constructor(clientId, accessName, refreshName) {
|
||||
this.clientId = clientId;
|
||||
this.accessName = accessName;
|
||||
this.refreshName = refreshName;
|
||||
}
|
||||
|
||||
save(accessToken, refreshToken) {
|
||||
let success = false;
|
||||
|
||||
if (!empty(accessToken)) {
|
||||
localStorage.setItem(this.accessName, accessToken);
|
||||
success = true;
|
||||
}
|
||||
|
||||
if (!empty(refreshToken)) {
|
||||
localStorage.setItem(this.refreshName, refreshToken);
|
||||
success = true;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
getAccess() {
|
||||
return localStorage.getItem(this.accessName);
|
||||
}
|
||||
|
||||
getRefresh() {
|
||||
return localStorage.getItem(this.refreshName);
|
||||
}
|
||||
|
||||
clear() {
|
||||
localStorage.removeItem(this.accessName);
|
||||
localStorage.removeItem(this.refreshName);
|
||||
}
|
||||
|
||||
parse(hash) {
|
||||
if (empty(hash))
|
||||
return false;
|
||||
|
||||
const search = hash.substr(1);
|
||||
|
||||
let params = new URLSearchParams(search);
|
||||
|
||||
let access_token = params.get("access_token");
|
||||
let refresh_token = decodeURIComponent(params.get("refresh_token"));
|
||||
|
||||
return this.save(access_token, refresh_token);
|
||||
}
|
||||
|
||||
async new() {
|
||||
const body = {
|
||||
client_id: this.clientId,
|
||||
grant_type: 'client_credentials',
|
||||
}
|
||||
|
||||
return await this.fetchToken(body);
|
||||
}
|
||||
|
||||
async refresh() {
|
||||
const refreshToken = this.getRefresh();
|
||||
|
||||
if (empty(refreshToken))
|
||||
return false;
|
||||
|
||||
const body = {
|
||||
client_id: this.clientId,
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token: refreshToken
|
||||
}
|
||||
|
||||
return await this.fetchToken(body);
|
||||
}
|
||||
|
||||
async fetchToken(body) {
|
||||
const server = $("input[id='serverURL']").val();
|
||||
|
||||
const response = await fetch(`${server}/oauth2/token`, {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify(body),
|
||||
mode: "cors",
|
||||
cache: "no-store",
|
||||
credentials: 'omit'
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
this.clear();
|
||||
throw new Error(`Fetch token failed: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
return this.save(json['access_token'], json['refresh_token']);
|
||||
}
|
||||
|
||||
expired(accessToken) {
|
||||
if (!empty(accessToken)) {
|
||||
const jwtDecoded = parseJWT(accessToken);
|
||||
if (jwtDecoded) {
|
||||
let now = new Date();
|
||||
let exp = new Date(jwtDecoded.exp * 1000);
|
||||
return now >= exp;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async update() {
|
||||
const accessToken = this.getAccess();
|
||||
if (empty(accessToken))
|
||||
return await this.new();
|
||||
if (this.expired(accessToken)) {
|
||||
return await this.refresh();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
async validate() {
|
||||
const accessToken = this.getAccess();
|
||||
if (empty(accessToken))
|
||||
return false;
|
||||
if (this.expired(accessToken)) {
|
||||
return await this.refresh();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
async function CheckResponse(json, status) {
|
||||
let error;
|
||||
let message;
|
||||
@@ -789,9 +635,6 @@
|
||||
}
|
||||
|
||||
if (status === 400) {
|
||||
if (typeof error === 'string') {
|
||||
token.clear();
|
||||
}
|
||||
return undefined;
|
||||
} else if (status === 401) {
|
||||
//location = '/welcome/';
|
||||
@@ -841,35 +684,6 @@
|
||||
document.location.replace(location);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {json}
|
||||
*/
|
||||
async function TokenFetch(path, method = "GET", body = null, initHeaders = {}) {
|
||||
|
||||
if (await token.validate()) {
|
||||
let headers = new Headers(initHeaders);
|
||||
headers.append('Authorization', `Bearer ${token.getAccess()}`)
|
||||
return AsyncFetch(path, method, body, headers);
|
||||
}
|
||||
|
||||
Welcome();
|
||||
}
|
||||
|
||||
function Welcome() {
|
||||
localStorage.removeItem('Session');
|
||||
localStorage.removeItem('Secret');
|
||||
|
||||
token.clear();
|
||||
|
||||
window.location.replace("/welcome/");
|
||||
}
|
||||
|
||||
function SignOut() {
|
||||
const api = $("input[id='apiURL']").val();
|
||||
TokenFetch(api + '/sign/out');
|
||||
Welcome();
|
||||
}
|
||||
|
||||
function empty(e) {
|
||||
switch (e) {
|
||||
case "":
|
||||
@@ -1108,7 +922,7 @@
|
||||
const request = $("#action-help");
|
||||
request.html(spinner);
|
||||
|
||||
TokenFetch(api + '/help' + settings)
|
||||
AsyncFetch(api + '/help' + settings)
|
||||
.then(json => request.html(JsonToPayload(json)))
|
||||
.catch(reason => request.html(reason.message));
|
||||
}
|
||||
@@ -1130,7 +944,7 @@
|
||||
const request = $("#action-status");
|
||||
request.html(spinner);
|
||||
|
||||
TokenFetch(api + '/account/status' + settings)
|
||||
AsyncFetch(api + '/account/status' + settings)
|
||||
.then(json => request.html(JsonToPayload(json)))
|
||||
.catch(reason => request.html(reason.message));
|
||||
}
|
||||
@@ -1177,7 +991,7 @@
|
||||
const request = $("#action-new");
|
||||
request.html(spinner);
|
||||
|
||||
TokenFetch(api + '/account/new' + settings, 'POST', formData, {'Content-Type': 'multipart/form-data'})
|
||||
AsyncFetch(api + '/account/new' + settings, 'POST', formData, {'Content-Type': 'multipart/form-data'})
|
||||
.then(json => request.html(JsonToPayload(json)))
|
||||
.catch(reason => request.html(reason.message));
|
||||
}
|
||||
@@ -1211,7 +1025,7 @@
|
||||
const request = $("#action-add");
|
||||
request.html(spinner);
|
||||
|
||||
TokenFetch(api + '/account/add' + settings, 'POST', formData, {'Content-Type': 'multipart/form-data'})
|
||||
AsyncFetch(api + '/account/add' + settings, 'POST', formData, {'Content-Type': 'multipart/form-data'})
|
||||
.then(json => request.html(JsonToPayload(json)))
|
||||
.catch(reason => request.html(reason.message));
|
||||
}
|
||||
@@ -1256,7 +1070,7 @@
|
||||
const request = $("#action-update");
|
||||
request.html(spinner);
|
||||
|
||||
TokenFetch(api + '/account/update' + settings, 'POST', formData, {'Content-Type': 'multipart/form-data'})
|
||||
AsyncFetch(api + '/account/update' + settings, 'POST', formData, {'Content-Type': 'multipart/form-data'})
|
||||
.then(json => request.html(JsonToPayload(json)))
|
||||
.catch(reason => request.html(reason.message));
|
||||
}
|
||||
@@ -1287,7 +1101,7 @@
|
||||
const request = $("#action-delete");
|
||||
request.html(spinner);
|
||||
|
||||
TokenFetch(api + '/account/delete' + settings, 'POST', formData, {'Content-Type': 'multipart/form-data'})
|
||||
AsyncFetch(api + '/account/delete' + settings, 'POST', formData, {'Content-Type': 'multipart/form-data'})
|
||||
.then(json => request.html(JsonToPayload(json)))
|
||||
.catch(reason => request.html(reason.message));
|
||||
}
|
||||
@@ -1473,7 +1287,7 @@
|
||||
let execute = $("button[id='btnDealForm']");
|
||||
execute.prop('disabled', true);
|
||||
|
||||
TokenFetch(api + '/deal/' + formOrder.val() + settings, 'POST', formData, {'Content-Type': 'multipart/form-data'})
|
||||
AsyncFetch(api + '/deal/' + formOrder.val() + settings, 'POST', formData, {'Content-Type': 'multipart/form-data'})
|
||||
.then(function(json) {
|
||||
let html;
|
||||
let payload;
|
||||
@@ -1522,7 +1336,7 @@
|
||||
let execute = $("button[id='btnDealJSON']");
|
||||
execute.prop('disabled', true);
|
||||
|
||||
TokenFetch(api + '/deal' + settings, 'POST', dealJSON.val(), {'Content-Type': 'application/json'})
|
||||
AsyncFetch(api + '/deal' + settings, 'POST', dealJSON.val(), {'Content-Type': 'application/json'})
|
||||
.then(function(json) {
|
||||
let html;
|
||||
let payload;
|
||||
@@ -1571,7 +1385,7 @@
|
||||
let execute = $("button[id='btnDealYAML']");
|
||||
execute.prop('disabled', true);
|
||||
|
||||
TokenFetch(api + '/deal' + settings, 'POST', dealYAML.val(), {'Content-Type': 'text/plain'})
|
||||
AsyncFetch(api + '/deal' + settings, 'POST', dealYAML.val(), {'Content-Type': 'text/plain'})
|
||||
.then(function(json) {
|
||||
let html;
|
||||
let payload;
|
||||
@@ -1613,7 +1427,7 @@
|
||||
let execute = $("button[id='btnCheckSignature']");
|
||||
execute.prop('disabled', true);
|
||||
|
||||
TokenFetch(api + '/signature', 'POST', JSON.stringify(json), {'Content-Type': 'application/json'})
|
||||
AsyncFetch(api + '/signature', 'POST', JSON.stringify(json), {'Content-Type': 'application/json'})
|
||||
.then(function(json) {
|
||||
let html;
|
||||
|
||||
@@ -1654,7 +1468,7 @@
|
||||
let execute = $("button[id='btnAccountGet']");
|
||||
execute.prop('disabled', true);
|
||||
|
||||
TokenFetch(`${server}${api}/account/get${params}`)
|
||||
AsyncFetch(`${server}${api}/account/get${params}`)
|
||||
.then(function(json) {
|
||||
let html;
|
||||
|
||||
@@ -1694,7 +1508,7 @@
|
||||
let execute = $("button[id='btnAccountList']");
|
||||
execute.prop('disabled', true);
|
||||
|
||||
TokenFetch(`${server}${api}/account/list${params}`)
|
||||
AsyncFetch(`${server}${api}/account/list${params}`)
|
||||
.then(function(json) {
|
||||
let html;
|
||||
|
||||
@@ -1733,7 +1547,7 @@
|
||||
const request = $("#action-deal-status");
|
||||
request.html(spinner);
|
||||
|
||||
TokenFetch(`${api}/deal/status${settings}`)
|
||||
AsyncFetch(`${api}/deal/status${settings}`)
|
||||
.then(json => request.html(PayloadToHTML(JsonToPayload(json))))
|
||||
.catch(reason => request.html(reason.message));
|
||||
}
|
||||
@@ -1751,7 +1565,7 @@
|
||||
const request = $("#action-deal-feedback");
|
||||
request.html(spinner);
|
||||
|
||||
TokenFetch(`${api}/deal/feedback${settings}`)
|
||||
AsyncFetch(`${api}/deal/feedback${settings}`)
|
||||
.then(json => request.html(PayloadToHTML(JsonToPayload(json))))
|
||||
.catch(reason => request.html(reason.message));
|
||||
}
|
||||
@@ -1769,7 +1583,7 @@
|
||||
const request = $("#action-deal-cancel");
|
||||
request.html(spinner);
|
||||
|
||||
TokenFetch(`${api}/deal/cancel${settings}`)
|
||||
AsyncFetch(`${api}/deal/cancel${settings}`)
|
||||
.then(json => request.html(PayloadToHTML(JsonToPayload(json))))
|
||||
.catch(reason => request.html(reason.message));
|
||||
}
|
||||
@@ -1789,7 +1603,7 @@
|
||||
let execute = $("button[id='btnDealGet']");
|
||||
execute.prop('disabled', true);
|
||||
|
||||
TokenFetch(`${server}${api}/deal/get${params}`)
|
||||
AsyncFetch(`${server}${api}/deal/get${params}`)
|
||||
.then(function(json) {
|
||||
let html;
|
||||
|
||||
@@ -1829,7 +1643,7 @@
|
||||
let execute = $("button[id='btnDealList']");
|
||||
execute.prop('disabled', true);
|
||||
|
||||
TokenFetch(`${server}${api}/deal/list?${params}`)
|
||||
AsyncFetch(`${server}${api}/deal/list?${params}`)
|
||||
.then(function(json) {
|
||||
let html;
|
||||
|
||||
@@ -1875,12 +1689,6 @@
|
||||
})();
|
||||
|
||||
$(document).ready(function() {
|
||||
token = new Token($("meta[name='web-client_id']").attr("content"), 'accessToken', 'refreshToken');
|
||||
|
||||
if (token.parse(document.location.hash)) {
|
||||
window.location.hash = '#';
|
||||
}
|
||||
|
||||
actionHelp();
|
||||
actionDeal();
|
||||
});
|
||||
|
||||
@@ -104,6 +104,9 @@
|
||||
SendForm(params, oauth2Endpoint);
|
||||
}
|
||||
|
||||
$(document).ready(function() {
|
||||
document.location.replace('/dashboard');
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user