2018-08-30 17:45:08 +00:00
|
|
|
// Generated by CoffeeScript 2.3.1
|
2017-06-14 18:40:12 +00:00
|
|
|
(function() {
|
2018-03-23 11:32:03 +00:00
|
|
|
var clipboard, createAlert, createOTPItem, currentTimeout, delay, fetchCodes, fetchInProgress, filterChange, initializeApplication, iterationCurrent, iterationNext, preFetch, refreshTimerProgress, serverConnectionError, tick, timeLeft, updateCodes, updatePreFetch;
|
2017-06-14 18:40:12 +00:00
|
|
|
|
|
|
|
currentTimeout = 0;
|
|
|
|
|
2018-03-23 11:32:03 +00:00
|
|
|
clipboard = null;
|
|
|
|
|
|
|
|
preFetch = null;
|
|
|
|
|
|
|
|
fetchInProgress = false;
|
|
|
|
|
|
|
|
serverConnectionError = false;
|
|
|
|
|
|
|
|
iterationCurrent = 'current';
|
|
|
|
|
|
|
|
iterationNext = 'next';
|
2017-06-14 18:40:12 +00:00
|
|
|
|
2018-08-30 17:45:08 +00:00
|
|
|
// document-ready function to start Javascript processing
|
2017-06-14 18:40:12 +00:00
|
|
|
$(function() {
|
|
|
|
if ($('body').hasClass('state-signedin')) {
|
|
|
|
return initializeApplication();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-30 17:45:08 +00:00
|
|
|
// createOTPItem generates code entries from the JSON objects passed from the backend
|
2017-06-14 18:40:12 +00:00
|
|
|
createOTPItem = function(item) {
|
|
|
|
var otpItem, tpl;
|
2017-06-14 21:15:15 +00:00
|
|
|
tpl = $('#tpl-otp-item').html();
|
2017-06-14 18:40:12 +00:00
|
|
|
otpItem = $(tpl);
|
2018-08-30 17:45:08 +00:00
|
|
|
otpItem.find('.badge').text(item.code.replace(/^([0-9]{3})([0-9]{3})$/, '$1 $2').replace(/^([0-9]{2})([0-9]{3})([0-9]{3})$/, '$1 $2 $3'));
|
2017-06-14 18:40:12 +00:00
|
|
|
otpItem.find('.title').text(item.name);
|
2018-08-30 17:45:08 +00:00
|
|
|
otpItem.find('i.fa').addClass(`fa-${item.icon}`);
|
2017-06-14 18:40:12 +00:00
|
|
|
return otpItem.appendTo($('#keylist'));
|
|
|
|
};
|
|
|
|
|
2018-08-30 17:45:08 +00:00
|
|
|
// createAlert adds a colored message at the top of the list
|
|
|
|
// type = success / info / warning / danger
|
2017-06-14 21:15:15 +00:00
|
|
|
createAlert = function(type, keyword, message, timeout) {
|
|
|
|
var alrt, tpl;
|
|
|
|
tpl = $('#tpl-message').html();
|
|
|
|
alrt = $(tpl);
|
2018-08-30 17:45:08 +00:00
|
|
|
alrt.find('.alert').addClass(`alert-${type}`);
|
2018-03-23 18:06:48 +00:00
|
|
|
alrt.find('.alert').find('.keyword').text(keyword);
|
|
|
|
alrt.find('.alert').find('.message').text(message);
|
2017-06-14 21:15:15 +00:00
|
|
|
alrt.appendTo($('#messagecontainer'));
|
|
|
|
if (timeout > 0) {
|
|
|
|
return delay(timeout, function() {
|
|
|
|
return alrt.remove();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-30 17:45:08 +00:00
|
|
|
// delay is a convenience wrapper to swap parameters of setTimeout
|
2017-06-14 18:40:12 +00:00
|
|
|
delay = function(delayMSecs, fkt) {
|
|
|
|
return window.setTimeout(fkt, delayMSecs);
|
|
|
|
};
|
|
|
|
|
2018-08-30 17:45:08 +00:00
|
|
|
// fetchCodes contacts the backend to receive JSON containing current codes
|
2018-03-23 11:32:03 +00:00
|
|
|
fetchCodes = function(iteration) {
|
|
|
|
var data, successFunc;
|
|
|
|
if (fetchInProgress) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fetchInProgress = true;
|
|
|
|
if (iteration === iterationCurrent) {
|
|
|
|
successFunc = updateCodes;
|
|
|
|
} else {
|
|
|
|
successFunc = updatePreFetch;
|
|
|
|
}
|
|
|
|
if (iteration === iterationCurrent && preFetch !== null) {
|
|
|
|
data = preFetch;
|
|
|
|
preFetch = null;
|
|
|
|
successFunc(data);
|
|
|
|
return;
|
|
|
|
}
|
2017-06-14 18:40:12 +00:00
|
|
|
return $.ajax({
|
2018-08-30 17:45:08 +00:00
|
|
|
url: `codes.json?it=${iteration}`,
|
2018-03-23 11:32:03 +00:00
|
|
|
success: successFunc,
|
2017-06-14 21:15:15 +00:00
|
|
|
dataType: 'json',
|
|
|
|
error: function() {
|
2018-03-23 11:32:03 +00:00
|
|
|
fetchInProgress = false;
|
|
|
|
createAlert('danger', 'Oops.', 'Server could not be contacted. Maybe you (or the server) are offline? Reload to try again.', 0);
|
|
|
|
return serverConnectionError = true;
|
2017-06-14 21:15:15 +00:00
|
|
|
},
|
|
|
|
statusCode: {
|
|
|
|
401: function() {
|
|
|
|
return window.location.reload();
|
|
|
|
},
|
|
|
|
500: function() {
|
2018-03-23 11:32:03 +00:00
|
|
|
fetchInProgress = false;
|
|
|
|
createAlert('danger', 'Oops.', 'The server responded with an internal error. Reload to try again.', 0);
|
|
|
|
return serverConnectionError = true;
|
2017-06-14 21:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-14 18:40:12 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-08-30 17:45:08 +00:00
|
|
|
// filterChange is called when changing the filter field and matches the
|
|
|
|
// titles of all shown entries. Those not matching the given regular expression
|
|
|
|
// will be hidden. The filterChange function is also called after a successful
|
|
|
|
// refresh of the shown codes to re-apply
|
2017-06-14 18:40:12 +00:00
|
|
|
filterChange = function() {
|
|
|
|
var filter;
|
|
|
|
filter = $('#filter').val().toLowerCase();
|
|
|
|
return $('.otp-item').each(function(idx, el) {
|
|
|
|
if ($(el).find('.title').text().toLowerCase().match(filter) === null) {
|
|
|
|
return $(el).hide();
|
|
|
|
} else {
|
|
|
|
return $(el).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-08-30 17:45:08 +00:00
|
|
|
// initializeApplication initializes some basic events and starts the first
|
|
|
|
// polling for codes
|
2017-06-14 18:40:12 +00:00
|
|
|
initializeApplication = function() {
|
|
|
|
$('#keylist').empty();
|
|
|
|
$('#filter').bind('keyup', filterChange);
|
|
|
|
tick(500, refreshTimerProgress);
|
2018-03-23 11:32:03 +00:00
|
|
|
return fetchCodes(iterationCurrent);
|
2017-06-14 18:40:12 +00:00
|
|
|
};
|
|
|
|
|
2018-08-30 17:45:08 +00:00
|
|
|
// refreshTimerProgress updates the top progressbar to display the
|
|
|
|
// remaining time until the one-time-passwords changes
|
2017-06-14 18:40:12 +00:00
|
|
|
refreshTimerProgress = function() {
|
|
|
|
var secondsLeft;
|
|
|
|
secondsLeft = timeLeft();
|
2018-08-30 17:45:08 +00:00
|
|
|
$('#timer').css('width', `${secondsLeft / 30 * 100}%`);
|
2018-03-23 11:32:03 +00:00
|
|
|
if (secondsLeft < 10 && preFetch === null && !serverConnectionError) {
|
2018-08-30 17:45:08 +00:00
|
|
|
// Do a pre-fetch to provide a seamless experience
|
2018-03-23 11:32:03 +00:00
|
|
|
return fetchCodes(iterationNext);
|
|
|
|
}
|
2017-06-14 18:40:12 +00:00
|
|
|
};
|
|
|
|
|
2018-08-30 17:45:08 +00:00
|
|
|
// tick is a convenience wrapper to swap parameters of setInterval
|
2017-06-14 18:40:12 +00:00
|
|
|
tick = function(delay, fkt) {
|
|
|
|
return window.setInterval(fkt, delay);
|
|
|
|
};
|
|
|
|
|
2018-08-30 17:45:08 +00:00
|
|
|
// timeLeft calculates the remaining time until codes get invalid
|
2017-06-14 18:40:12 +00:00
|
|
|
timeLeft = function() {
|
|
|
|
var now;
|
|
|
|
now = new Date().getTime();
|
|
|
|
return (currentTimeout - now) / 1000;
|
|
|
|
};
|
|
|
|
|
2018-08-30 17:45:08 +00:00
|
|
|
// updateCodes is being called when the backend delivered codes. The codes
|
|
|
|
// are then rendered and the clipboard methods are re-bound. Afterwards the
|
|
|
|
// next fetchCodes call is timed to that moment when the codes are getting
|
|
|
|
// invalid
|
2017-06-14 18:40:12 +00:00
|
|
|
updateCodes = function(data) {
|
|
|
|
var i, len, ref, token;
|
|
|
|
currentTimeout = new Date(data.next_wrap).getTime();
|
|
|
|
if (clipboard) {
|
|
|
|
clipboard.destroy();
|
|
|
|
}
|
2018-03-23 18:16:56 +00:00
|
|
|
$('#initLoader').hide();
|
2017-06-14 18:40:12 +00:00
|
|
|
$('#keylist').empty();
|
|
|
|
ref = data.tokens;
|
|
|
|
for (i = 0, len = ref.length; i < len; i++) {
|
|
|
|
token = ref[i];
|
|
|
|
createOTPItem(token);
|
|
|
|
}
|
|
|
|
clipboard = new Clipboard('.otp-item', {
|
|
|
|
text: function(trigger) {
|
2018-08-30 17:45:08 +00:00
|
|
|
return $(trigger).find('.badge').text().replace(/ /g, '');
|
2017-06-14 18:40:12 +00:00
|
|
|
}
|
|
|
|
});
|
2018-03-23 18:06:48 +00:00
|
|
|
clipboard.on('success', function(e) {
|
2018-03-23 19:23:47 +00:00
|
|
|
createAlert('success', 'Success:', 'Code copied to clipboard', 1000);
|
2018-03-23 18:06:48 +00:00
|
|
|
return e.blur();
|
|
|
|
});
|
|
|
|
clipboard.on('error', function(e) {
|
2018-03-23 19:23:47 +00:00
|
|
|
return createAlert('danger', 'Oops.', 'Copy to clipboard failed', 2000);
|
2018-03-23 18:06:48 +00:00
|
|
|
});
|
2017-06-14 18:40:12 +00:00
|
|
|
filterChange();
|
2018-03-23 11:32:03 +00:00
|
|
|
delay(timeLeft() * 1000, function() {
|
|
|
|
return fetchCodes(iterationCurrent);
|
|
|
|
});
|
|
|
|
return fetchInProgress = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
updatePreFetch = function(data) {
|
|
|
|
preFetch = data;
|
|
|
|
return fetchInProgress = false;
|
2017-06-14 18:40:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}).call(this);
|