2020-09-02 14:48:23 +00:00
< style >
2020-09-02 17:41:06 +00:00
.twofactor #totp-setup,
2020-09-02 14:48:23 +00:00
.twofactor #disable-2fa,
.twofactor #output-2fa {
display: none;
}
.twofactor.loaded .loading-indicator {
display: none;
}
.twofactor.disabled #disable-2fa,
2020-09-02 17:41:06 +00:00
.twofactor.enabled #totp-setup {
2020-09-02 14:48:23 +00:00
display: none;
}
2020-09-02 17:41:06 +00:00
.twofactor.disabled #totp-setup,
2020-09-02 14:48:23 +00:00
.twofactor.enabled #disable-2fa {
display: block;
}
2020-09-02 17:41:06 +00:00
.twofactor #totp-setup-qr img {
2020-09-02 14:48:23 +00:00
display: block;
width: 256px;
max-width: 100%;
height: auto;
}
.twofactor #output-2fa.visible {
display: block;
}
< / style >
2020-09-03 17:07:21 +00:00
< h2 > Two-Factor Authentication< / h2 >
2020-09-02 14:48:23 +00:00
2020-09-27 12:31:23 +00:00
< p > When two-factor authentication is enabled, you will be prompted to enter a six digit code from an
authenticator app (usually on your phone) when you log into this control panel.< / p >
< div class = "panel panel-danger" >
< div class = "panel-heading" >
Enabling two-factor authentication does not protect access to your email
< / div >
< div class = "panel-body" >
Enabling two-factor authentication on this page only limits access to this control panel. Remember that most websites allow you to
reset your password by checking your email, so anyone with access to your email can typically take over
your other accounts. Additionally, if your email address or any alias that forwards to your email
address is a typical domain control validation address (e.g admin@, administrator@, postmaster@, hostmaster@,
webmaster@, abuse@), extra care should be taken to protect the account. < strong > Always use a strong password,
and ensure every administrator account for this control panel does the same.< / strong >
< / div >
< / div >
2020-09-02 14:48:23 +00:00
< div class = "twofactor" >
< div class = "loading-indicator" > Loading...< / div >
2020-09-02 17:41:06 +00:00
< form id = "totp-setup" >
2020-09-27 12:31:23 +00:00
< h3 > Setup Instructions< / h3 >
< div class = "form-group" >
< p > 1. Install < a href = "https://freeotp.github.io/" > FreeOTP< / a > or < a href = "https://www.pcworld.com/article/3225913/what-is-two-factor-authentication-and-which-2fa-apps-are-best.html" > any
other two-factor authentication app< / a > that supports TOTP.< / p >
< / div >
2020-09-12 14:34:06 +00:00
2020-09-02 14:48:23 +00:00
< div class = "form-group" >
2020-09-27 12:31:23 +00:00
< p style = "margin-bottom: 0" > 2. Scan the QR code in the app or directly enter the secret into the app:< / p >
2020-09-02 17:41:06 +00:00
< div id = "totp-setup-qr" > < / div >
2020-09-02 14:48:23 +00:00
< / div >
< div class = "form-group" >
2020-09-27 12:31:23 +00:00
< label for = "otp-label" style = "font-weight: normal" > 3. Optionally, give your device a label so that you can remember what device you set it up on:< / label >
< input type = "text" id = "totp-setup-label" class = "form-control" placeholder = "my phone" / >
< / div >
< div class = "form-group" >
< label for = "otp" style = "font-weight: normal" > 4. Use the app to generate your first six-digit code and enter it here:< / label >
2020-09-02 17:41:06 +00:00
< input type = "text" id = "totp-setup-token" class = "form-control" placeholder = "6-digit code" / >
2020-09-02 14:48:23 +00:00
< / div >
2020-09-02 17:41:06 +00:00
< input type = "hidden" id = "totp-setup-secret" / >
2020-09-02 14:48:23 +00:00
< div class = "form-group" >
2020-09-27 12:31:23 +00:00
< p > When you click Enable Two-Factor Authentication, you will be logged out of the control panel and will have to log in
again, now using your two-factor authentication app.< / p >
< button id = "totp-setup-submit" disabled type = "submit" class = "btn" > Enable Two-Factor Authentication< / button >
2020-09-02 14:48:23 +00:00
< / div >
< / form >
< form id = "disable-2fa" >
< div class = "form-group" >
2020-09-27 12:31:23 +00:00
< p > Two-factor authentication is active for your account< span id = "mfa-device-label" > < / span > .< / p >
2020-09-12 14:34:06 +00:00
< p > You will have to log into the admin panel again after disabling two-factor authentication.< / p >
< / div >
< div class = "form-group" >
2020-09-27 12:31:23 +00:00
< button type = "submit" class = "btn btn-danger" > Disable Two-Factor Authentication< / button >
2020-09-02 14:48:23 +00:00
< / div >
< / form >
< div id = "output-2fa" class = "panel panel-danger" >
< div class = "panel-body" > < / div >
< / div >
< / div >
< script >
var el = {
disableForm: document.getElementById('disable-2fa'),
output: document.getElementById('output-2fa'),
2020-09-02 17:41:06 +00:00
totpSetupForm: document.getElementById('totp-setup'),
totpSetupToken: document.getElementById('totp-setup-token'),
totpSetupSecret: document.getElementById('totp-setup-secret'),
2020-09-27 12:31:23 +00:00
totpSetupLabel: document.getElementById('totp-setup-label'),
2020-09-02 17:41:06 +00:00
totpQr: document.getElementById('totp-setup-qr'),
totpSetupSubmit: document.querySelector('#totp-setup-submit'),
2020-09-02 14:48:23 +00:00
wrapper: document.querySelector('.twofactor')
}
function update_setup_disabled(evt) {
var val = evt.target.value.trim();
if (
typeof val !== 'string' ||
2020-09-02 17:41:06 +00:00
typeof el.totpSetupSecret.value !== 'string' ||
2020-09-02 14:48:23 +00:00
val.length !== 6 ||
2020-09-02 17:41:06 +00:00
el.totpSetupSecret.value.length !== 32 ||
2020-09-02 14:48:23 +00:00
!(/^\+?\d+$/.test(val))
) {
2020-09-02 17:41:06 +00:00
el.totpSetupSubmit.setAttribute('disabled', '');
2020-09-02 14:48:23 +00:00
} else {
2020-09-02 17:41:06 +00:00
el.totpSetupSubmit.removeAttribute('disabled');
2020-09-02 14:48:23 +00:00
}
}
2020-09-27 12:31:23 +00:00
function render_totp_setup(provisioned_totp) {
var img = document.createElement('img');
img.src = "data:image/png;base64," + provisioned_totp.qr_code_base64;
2020-09-02 14:48:23 +00:00
2020-09-27 12:31:23 +00:00
var code = document.createElement('div');
code.innerHTML = `Secret: ${provisioned_totp.secret}`;
2020-09-02 14:48:23 +00:00
2020-09-27 12:31:23 +00:00
el.totpQr.appendChild(img);
el.totpQr.appendChild(code);
2020-09-02 14:48:23 +00:00
2020-09-02 17:41:06 +00:00
el.totpSetupToken.addEventListener('input', update_setup_disabled);
el.totpSetupForm.addEventListener('submit', do_enable_totp);
2020-09-02 14:48:23 +00:00
2020-09-27 12:31:23 +00:00
el.totpSetupSecret.setAttribute('value', provisioned_totp.secret);
2020-09-02 14:48:23 +00:00
el.wrapper.classList.add('disabled');
}
2020-09-27 12:31:23 +00:00
function render_disable(mfa) {
2020-09-02 14:48:23 +00:00
el.disableForm.addEventListener('submit', do_disable);
el.wrapper.classList.add('enabled');
2020-09-27 12:31:23 +00:00
if (mfa.label)
$("#mfa-device-label").text(" on device '" + mfa.label + "'");
2020-09-02 14:48:23 +00:00
}
function hide_error() {
el.output.querySelector('.panel-body').innerHTML = '';
el.output.classList.remove('visible');
}
function render_error(msg) {
el.output.querySelector('.panel-body').innerHTML = msg;
el.output.classList.add('visible');
}
function reset_view() {
el.wrapper.classList.remove('loaded', 'disabled', 'enabled');
el.disableForm.removeEventListener('submit', do_disable);
hide_error();
2020-09-02 17:41:06 +00:00
el.totpSetupForm.reset();
el.totpSetupForm.removeEventListener('submit', do_enable_totp);
2020-09-02 14:48:23 +00:00
2020-09-02 17:41:06 +00:00
el.totpSetupSecret.setAttribute('value', '');
el.totpSetupToken.removeEventListener('input', update_setup_disabled);
2020-09-02 14:48:23 +00:00
2020-09-02 17:41:06 +00:00
el.totpSetupSubmit.setAttribute('disabled', '');
el.totpQr.innerHTML = '';
2020-09-02 14:48:23 +00:00
}
2020-09-27 12:31:23 +00:00
function show_mfa() {
2020-09-02 14:48:23 +00:00
reset_view();
api(
2020-09-03 11:48:03 +00:00
'/mfa/status',
2020-10-29 19:41:34 +00:00
'POST',
2020-09-02 14:48:23 +00:00
{},
function(res) {
el.wrapper.classList.add('loaded');
2020-09-27 12:31:23 +00:00
var has_mfa = false;
res.enabled_mfa.forEach(function(mfa) {
if (mfa.type == "totp") {
render_disable(mfa);
has_mfa = true;
}
});
if (!has_mfa)
render_totp_setup(res.new_mfa.totp);
2020-09-02 14:48:23 +00:00
}
);
}
function do_disable(evt) {
evt.preventDefault();
hide_error();
api(
2020-09-27 12:31:23 +00:00
'/mfa/disable',
2020-09-02 14:48:23 +00:00
'POST',
2020-09-27 12:31:23 +00:00
{ type: 'totp' },
2020-09-12 14:34:06 +00:00
function() {
do_logout();
}
2020-09-02 14:48:23 +00:00
);
return false;
}
2020-09-02 17:41:06 +00:00
function do_enable_totp(evt) {
2020-09-02 14:48:23 +00:00
evt.preventDefault();
hide_error();
api(
2020-09-03 11:48:03 +00:00
'/mfa/totp/enable',
2020-09-02 14:48:23 +00:00
'POST',
{
2020-09-02 17:41:06 +00:00
token: $(el.totpSetupToken).val(),
2020-09-27 12:31:23 +00:00
secret: $(el.totpSetupSecret).val(),
label: $(el.totpSetupLabel).val()
2020-09-02 14:48:23 +00:00
},
2020-09-28 19:04:44 +00:00
function(res) { do_logout(); },
function(res) { render_error(res); }
2020-09-02 14:48:23 +00:00
);
return false;
}
< / script >