2020-04-16 23:02:35 +02:00
|
|
|
// Generated by CoffeeScript 1.12.7
|
2018-02-20 18:36:34 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
LemonLDAP::NG TOTP registration script
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function() {
|
2018-04-15 18:21:42 +02:00
|
|
|
var displayError, getKey, setMsg, token, verify;
|
2018-02-20 18:36:34 +01:00
|
|
|
|
|
|
|
setMsg = function(msg, level) {
|
|
|
|
$('#msg').html(window.translate(msg));
|
2018-04-10 11:06:06 +02:00
|
|
|
$('#color').removeClass('message-positive message-warning message-danger alert-success alert-warning alert-danger');
|
2018-02-20 18:36:34 +01:00
|
|
|
$('#color').addClass("message-" + level);
|
|
|
|
if (level === 'positive') {
|
|
|
|
level = 'success';
|
|
|
|
}
|
|
|
|
return $('#color').addClass("alert-" + level);
|
|
|
|
};
|
|
|
|
|
|
|
|
displayError = function(j, status, err) {
|
|
|
|
var res;
|
|
|
|
console.log('Error', err);
|
|
|
|
res = JSON.parse(j.responseText);
|
|
|
|
if (res && res.error) {
|
|
|
|
res = res.error.replace(/.* /, '');
|
|
|
|
console.log('Returned error', res);
|
|
|
|
return setMsg(res, 'warning');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
token = '';
|
|
|
|
|
|
|
|
getKey = function(reset) {
|
2018-04-10 11:06:06 +02:00
|
|
|
setMsg('yourTotpKey', 'warning');
|
2018-02-20 18:36:34 +01:00
|
|
|
return $.ajax({
|
|
|
|
type: "POST",
|
2018-03-15 07:04:52 +01:00
|
|
|
url: portal + "/2fregisters/totp/getkey",
|
2018-02-20 22:58:20 +01:00
|
|
|
dataType: 'json',
|
2018-02-20 18:36:34 +01:00
|
|
|
data: {
|
|
|
|
newkey: reset
|
|
|
|
},
|
|
|
|
error: displayError,
|
|
|
|
success: function(data) {
|
|
|
|
var qr, s;
|
2018-03-18 14:26:45 +01:00
|
|
|
if (data.error) {
|
2018-05-07 19:26:39 +02:00
|
|
|
if (data.error.match(/totpExistingKey/)) {
|
|
|
|
$("#divToHide").hide();
|
|
|
|
}
|
2018-03-18 14:26:45 +01:00
|
|
|
return setMsg(data.error, 'warning');
|
|
|
|
}
|
2018-03-15 07:04:52 +01:00
|
|
|
if (!(data.portal && data.user && data.secret)) {
|
|
|
|
return setMsg('PE24', 'danger');
|
|
|
|
}
|
2018-05-07 19:26:39 +02:00
|
|
|
$("#divToHide").show();
|
2018-02-20 18:36:34 +01:00
|
|
|
s = "otpauth://totp/" + (escape(data.portal)) + ":" + (escape(data.user)) + "?secret=" + data.secret + "&issuer=" + (escape(data.portal));
|
2018-02-21 22:07:12 +01:00
|
|
|
if (data.digits !== 6) {
|
|
|
|
s += "&digits=" + data.digits;
|
|
|
|
}
|
|
|
|
if (data.interval !== 30) {
|
|
|
|
s += "&period=" + data.interval;
|
|
|
|
}
|
2018-02-20 18:36:34 +01:00
|
|
|
qr = new QRious({
|
|
|
|
element: document.getElementById('qr'),
|
|
|
|
value: s,
|
|
|
|
size: 150
|
|
|
|
});
|
|
|
|
$('#serialized').text(s);
|
|
|
|
if (data.newkey) {
|
|
|
|
setMsg('yourNewTotpKey', 'warning');
|
|
|
|
} else {
|
2018-02-20 22:58:20 +01:00
|
|
|
setMsg('yourTotpKey', 'success');
|
2018-02-20 18:36:34 +01:00
|
|
|
}
|
|
|
|
return token = data.token;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-15 18:21:42 +02:00
|
|
|
verify = function() {
|
2018-02-20 22:58:20 +01:00
|
|
|
var val;
|
|
|
|
val = $('#code').val();
|
|
|
|
if (!val) {
|
2018-03-09 22:32:42 +01:00
|
|
|
return setMsg('fillTheForm', 'warning');
|
2018-02-20 22:58:20 +01:00
|
|
|
} else {
|
|
|
|
return $.ajax({
|
|
|
|
type: "POST",
|
2018-04-15 18:21:42 +02:00
|
|
|
url: portal + "/2fregisters/totp/verify",
|
2018-02-20 22:58:20 +01:00
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
token: token,
|
2018-03-29 23:11:46 +02:00
|
|
|
code: val,
|
|
|
|
TOTPName: $('#TOTPName').val()
|
2018-02-20 22:58:20 +01:00
|
|
|
},
|
|
|
|
error: displayError,
|
|
|
|
success: function(data) {
|
|
|
|
if (data.error) {
|
2018-09-02 11:23:01 +02:00
|
|
|
if (data.error.match(/bad(Code|Name)/)) {
|
|
|
|
return setMsg(data.error, 'warning');
|
2018-02-20 22:58:20 +01:00
|
|
|
} else {
|
|
|
|
return setMsg(data.error, 'danger');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return setMsg('yourKeyIsRegistered', 'success');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-20 18:36:34 +01:00
|
|
|
$(document).ready(function() {
|
2018-02-20 22:58:20 +01:00
|
|
|
getKey(0);
|
|
|
|
$('#changekey').on('click', function() {
|
|
|
|
return getKey(1);
|
|
|
|
});
|
2018-04-15 18:21:42 +02:00
|
|
|
return $('#verify').on('click', function() {
|
|
|
|
return verify();
|
2018-02-20 22:58:20 +01:00
|
|
|
});
|
2018-02-20 18:36:34 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
}).call(this);
|