2022-07-17 18:16:00 +00:00
<!--
2023-11-04 09:54:52 +00:00
Copyright (C) 2023 Nicola Murino
2022-07-17 18:16:00 +00:00
2023-11-04 09:54:52 +00:00
This WebUI uses the KeenThemes Mega Bundle, a proprietary theme:
2022-07-17 18:16:00 +00:00
2023-11-04 09:54:52 +00:00
https://keenthemes.com/products/templates-mega-bundle
2022-07-17 18:16:00 +00:00
2023-11-04 09:54:52 +00:00
KeenThemes HTML/CSS/JS components are allowed for use only within the
SFTPGo product and restricted to be used in a resealable HTML template
that can compete with KeenThemes products anyhow.
This WebUI is allowed for use only within the SFTPGo product and
therefore cannot be used in derivative works/products without an
explicit grant from the SFTPGo Team (support@sftpgo.com).
2022-07-17 18:16:00 +00:00
-->
2021-09-04 10:11:04 +00:00
{{template "base" .}}
2023-11-04 09:54:52 +00:00
{{- define "page_body"}}
< div class = "card shadow-sm" >
< div class = "card-header bg-light" >
2023-12-10 15:40:13 +00:00
< h3 data-i18n = "2fa.title" class = "card-title text-primary" > Two-factor authentication using Authenticator apps< / h3 >
2021-09-04 10:11:04 +00:00
< / div >
< div class = "card-body" >
2023-11-04 09:54:52 +00:00
< div class = "notice d-flex bg-light-primary rounded border-primary border border-dashed p-6 mb-5" >
< i class = "ki-duotone ki-shield-tick fs-2tx text-primary me-4" >
< span class = "path1" > < / span >
< span class = "path2" > < / span >
< / i >
< div class = "d-flex flex-stack flex-grow-1 flex-wrap flex-md-nowrap" >
< div class = "mb-3 mb-md-0 fw-semibold" >
< h4 class = "text-gray-900 fw-bold" >
{{- if .TOTPConfig.Enabled}}
2023-12-10 15:40:13 +00:00
< span data-i18n = "2fa.msg_enabled" > Two-factor authentication is enabled< / span > {{- if gt (len .TOTPConfigs) 1 }} ({{$.TOTPConfig.ConfigName}}) {{- end}}
2023-11-04 09:54:52 +00:00
{{- else}}
2023-12-10 15:40:13 +00:00
< span data-i18n = "2fa.msg_disabled" > Secure Your Account< / span >
2023-11-04 09:54:52 +00:00
{{- end}}
< / h4 >
< div class = "fs-6 text-gray-800 pe-7" >
2023-12-10 15:40:13 +00:00
< span data-i18n = "2fa.msg_info" >
Two-factor authentication adds an extra layer of security to your account. To log in you'll need
to provide an additional authentication code.
< / span >
2023-11-04 09:54:52 +00:00
< / div >
< / div >
{{- if .TOTPConfig.Enabled}}
2023-11-14 18:38:09 +00:00
< button type = "button" id = "disable_btn" class = "btn btn-danger ms-4 px-6 align-self-center text-nowrap" >
2023-12-10 15:40:13 +00:00
< span data-i18n = "general.disable" class = "indicator-label" >
2023-11-04 09:54:52 +00:00
Disable
< / span >
2023-12-10 15:40:13 +00:00
< span data-i18n = "general.wait" class = "indicator-progress" >
Please wait...
< span class = "spinner-border spinner-border-sm align-middle ms-2" > < / span >
2023-11-04 09:54:52 +00:00
< / span >
< / button >
{{- end}}
2021-09-04 10:11:04 +00:00
< / div >
< / div >
2023-11-04 09:54:52 +00:00
{{- template "errmsg" ""}}
< div class = "form-group row mt-10" >
2023-12-10 15:40:13 +00:00
< label data-i18n = "general.configuration" class = "col-md-3 col-form-label" > Configuration< / label >
2023-11-04 09:54:52 +00:00
< div class = "col-md-9" >
2023-12-10 15:40:13 +00:00
< select id = "id_config" name = "config_name" class = "form-select" data-control = "i18n-select2" data-hide-search = "true" >
< option data-i18n = "general.none" value = "" > None< / option >
2023-11-04 09:54:52 +00:00
{{range .TOTPConfigs}}
< option value = "{{.}}" { { if eq . $ . TOTPConfig . ConfigName } } selected { { end } } > {{.}}< / option >
{{end}}
< / select >
< / div >
2021-09-04 10:11:04 +00:00
< / div >
2023-11-04 09:54:52 +00:00
< div class = "form-group row mt-10" >
2023-12-10 15:40:13 +00:00
< label data-i18n = "2fa.require_for" class = "col-md-3 col-form-label required" >
2023-11-04 09:54:52 +00:00
Require 2FA for
< / label >
< div class = "col-md-9" >
2023-12-10 15:40:13 +00:00
< select id = "id_protocols" name = "multi_factor_protocols" class = "form-select" data-control = "i18n-select2" data-hide-search = "true" data-close-on-select = "false" multiple = "multiple" >
2023-11-04 09:54:52 +00:00
< option > < / option >
2021-09-04 10:11:04 +00:00
{{range $protocol := .Protocols}}
< option value = "{{$protocol}}" { { range $ p : = $.TOTPConfig.Protocols } } { { if eq $ p $ protocol } } selected { { end } } { { end } } > {{$protocol}}
< / option >
{{end}}
< / select >
< / div >
< / div >
2023-11-04 09:54:52 +00:00
< div class = "d-flex justify-content-end mt-15" >
{{- if .TOTPConfig.Enabled }}
2023-11-14 18:38:09 +00:00
< button type = "button" id = "generate_secret_btn" class = "btn btn-light-primary px-10 me-10 d-none" >
2023-12-10 15:40:13 +00:00
< span data-i18n = "2fa.generate" class = "indicator-label" >
2023-11-04 09:54:52 +00:00
Generate new secret
< / span >
2023-12-10 15:40:13 +00:00
< span data-i18n = "general.wait" class = "indicator-progress" >
Please wait...
< span class = "spinner-border spinner-border-sm align-middle ms-2" > < / span >
2023-11-04 09:54:52 +00:00
< / span >
< / button >
{{- end}}
2023-11-14 18:38:09 +00:00
< button type = "button" id = "save_btn" class = "btn btn-primary px-10 d-none" >
2023-11-04 09:54:52 +00:00
< span id = "save_label" class = "indicator-label" > < / span >
2023-12-10 15:40:13 +00:00
< span data-i18n = "general.wait" class = "indicator-progress" >
Please wait...
< span class = "spinner-border spinner-border-sm align-middle ms-2" > < / span >
2023-11-04 09:54:52 +00:00
< / span >
< / button >
2021-09-04 10:11:04 +00:00
< / div >
2023-11-04 09:54:52 +00:00
< / div >
< / div >
2021-09-04 10:11:04 +00:00
2023-11-04 09:54:52 +00:00
{{- if .TOTPConfig.Enabled}}
< div class = "notice d-flex bg-light-primary rounded border-primary border border-dashed p-6 my-10" >
< i class = "ki-duotone ki-shield-tick fs-2tx text-primary me-4" >
< span class = "path1" > < / span >
< span class = "path2" > < / span >
< / i >
< div class = "d-flex flex-stack flex-grow-1 flex-wrap flex-md-nowrap" >
< div class = "mb-3 mb-md-0 fw-semibold" >
2023-12-10 15:40:13 +00:00
< h4 data-i18n = "2fa.recovery_codes" class = "text-gray-900 fw-bold" >
2023-11-04 09:54:52 +00:00
Recovery codes
< / h4 >
< div class = "fs-6 text-gray-800" >
2023-12-10 15:40:13 +00:00
< p data-i18n = "2fa.recovery_codes_msg1" > Recovery codes are a set of one time use codes that can be used in place of the authentication code to login to the web UI. You can use them if you lose access to your phone to login to your account and disable or regenerate two-factor configuration.< / p >
< p data-i18n = "2fa.recovery_codes_msg2" > To keep your account secure, don't share or distribute your recovery codes. We recommend saving them with a secure password manager.< / p >
< p data-i18n = "2fa.recovery_codes_msg3" > If you generate new recovery codes, you automatically invalidate old ones.< / p >
2023-11-04 09:54:52 +00:00
< / div >
< div class = "d-flex justify-content-center mt-10" >
2023-11-14 18:38:09 +00:00
< button type = "button" id = "generate_recovery_code_btn" class = "btn btn-primary px-10 me-10" >
2023-12-10 15:40:13 +00:00
< span data-i18n = "general.generate" class = "indicator-label" >
2023-11-04 09:54:52 +00:00
Generate
< / span >
2023-12-10 15:40:13 +00:00
< span data-i18n = "general.wait" class = "indicator-progress" >
Please wait...
< span class = "spinner-border spinner-border-sm align-middle ms-2" > < / span >
2023-11-04 09:54:52 +00:00
< / span >
< / button >
2023-11-14 18:38:09 +00:00
< button type = "button" id = "view_recovery_code_btn" class = "btn btn-primary px-10" >
2023-12-10 15:40:13 +00:00
< span data-i18n = "general.view" id = "save_label" class = "indicator-label" > View< / span >
< span data-i18n = "general.wait" class = "indicator-progress" >
Please wait...
< span class = "spinner-border spinner-border-sm align-middle ms-2" > < / span >
2023-11-04 09:54:52 +00:00
< / span >
< / button >
2021-09-04 10:11:04 +00:00
< / div >
< / div >
2023-11-04 09:54:52 +00:00
< / div >
< / div >
{{- end}}
{{- end}}
{{- define "additionalnavitems"}}
< div class = "d-flex align-items-center ms-2 ms-lg-3" id = "kt_header_user_menu_toggle" >
< div class = "btn btn-icon btn-active-light-primary w-35px h-35px w-md-40px h-md-40px" data-bs-toggle = "modal" data-bs-target = "#info_modal" >
< i class = "ki-duotone ki-information-2 fs-2" >
< i class = "path1" > < / i >
< i class = "path2" > < / i >
< i class = "path3" > < / i >
< / i >
< / div >
< / div >
{{- end}}
2021-09-04 10:11:04 +00:00
2023-11-04 09:54:52 +00:00
{{- define "modals"}}
< div class = "modal fade" id = "recovery_codes_modal" tabindex = "-1" >
< div class = "modal-dialog" >
< div class = "modal-content" >
< div class = "modal-header" >
< h3 id = "idRecoveryCodesTitle" class = "modal-title" > < / h3 >
2023-12-10 15:40:13 +00:00
< div data-i18n = "[aria-label]general.close" class = "btn btn-icon btn-sm btn-active-color-primary ms-2" data-bs-dismiss = "modal" aria-label = "Close" >
2023-11-04 09:54:52 +00:00
< i class = "ki-duotone ki-cross fs-1" > < span class = "path1" > < / span > < span class = "path2" > < / span > < / i >
< / div >
2021-09-04 10:11:04 +00:00
< / div >
2023-11-04 09:54:52 +00:00
< div class = "modal-body" >
< div id = "idRecoveryCodesList" class = "d-flex flex-column" >
< / div >
< / div >
< div class = "modal-footer" >
2023-12-10 15:40:13 +00:00
< button data-i18n = "general.ok" class = "btn btn-primary" type = "button" data-bs-dismiss = "modal" > OK< / button >
2021-09-04 10:11:04 +00:00
< / div >
< / div >
< / div >
< / div >
2023-11-04 09:54:52 +00:00
< div class = "modal fade" id = "info_modal" tabindex = "-1" >
< div class = "modal-dialog" >
< div class = "modal-content" >
< div class = "modal-header" >
2023-12-10 15:40:13 +00:00
< h3 data-i18n = "2fa.info_title" class = "modal-title" > Learn about two-factor authentication< / h3 >
< div data-i18n = "[aria-label]general.close" class = "btn btn-icon btn-sm btn-active-color-primary ms-2" data-bs-dismiss = "modal" aria-label = "Close" >
2023-11-04 09:54:52 +00:00
< i class = "ki-duotone ki-cross fs-1" > < span class = "path1" > < / span > < span class = "path2" > < / span > < / i >
< / div >
2021-09-04 10:11:04 +00:00
< / div >
2023-11-04 09:54:52 +00:00
< div class = "modal-body fw-semibold fs-6" >
2023-12-10 15:40:13 +00:00
< p data-i18n = "2fa.info1" > SSH protocol (SFTP/SCP/SSH commands) will ask for the passcode if the client uses keyboard interactive authentication.< / p >
< p data-i18n = "2fa.info2" > HTTP protocol means Web UI and REST APIs. Web UI will ask for the passcode using a specific page. For REST API
2023-11-04 09:54:52 +00:00
you have to add the passcode using an HTTP header.< / p >
2023-12-10 15:40:13 +00:00
< p data-i18n = "2fa.info3" > FTP has no standard way to support two factor authentication, if you enable the FTP support, you have to add the
2023-11-04 09:54:52 +00:00
TOTP passcode after the password. For example if your password is "password" and your one time passcode is
"123456" you have to use "password123456" as password.< / p >
2023-12-10 15:40:13 +00:00
< p data-i18n = "2fa.info4" > WebDAV is not supported since each single request must be authenticated and a passcode cannot be reused.< / p >
2023-11-04 09:54:52 +00:00
< / div >
< div class = "modal-footer" >
2023-12-10 15:40:13 +00:00
< button data-i18n = "general.ok" class = "btn btn-primary" type = "button" data-bs-dismiss = "modal" > OK< / button >
2021-09-04 10:11:04 +00:00
< / div >
< / div >
< / div >
< / div >
2023-11-04 09:54:52 +00:00
< div class = "modal fade" id = "qrcode_modal" tabindex = "-1" >
< div class = "modal-dialog modal-dialog-centered modal-lg" >
2021-09-04 10:11:04 +00:00
< div class = "modal-content" >
< div class = "modal-header" >
2023-12-10 15:40:13 +00:00
< h3 data-i18n = "2fa.setup_title" class = "modal-title" > Set up two-factor authentication< / h3 >
< div data-i18n = "[aria-label]general.close" class = "btn btn-icon btn-sm btn-active-color-primary ms-2" data-bs-dismiss = "modal" aria-label = "Close" >
2023-11-04 09:54:52 +00:00
< i class = "ki-duotone ki-cross fs-1" > < span class = "path1" > < / span > < span class = "path2" > < / span > < / i >
< / div >
< / div >
< div class = "modal-body scroll-y pt-10 pb-15 px-lg-17" >
< div class = "text-gray-700 fw-semibold fs-6 mb-10" >
2023-12-10 15:40:13 +00:00
< span data-i18n = "2fa.setup_msg" >
Use your preferred Authenticator App (e.g. Microsoft Authenticator, Google Authenticator, Authy, 1Password etc. ) to scan the QR code. It will generate an authentication code for you to enter below.
< / span >
2023-11-04 09:54:52 +00:00
< / div >
2023-11-18 19:06:31 +00:00
< div id = "id_qr_code_container" class = "pt-5 text-center" >
2023-11-04 09:54:52 +00:00
< / div >
< div class = "notice d-flex bg-light-warning rounded border-warning border border-dashed my-10 p-6" >
< i class = "ki-duotone ki-information fs-2tx text-warning me-4" >
< span class = "path1" > < / span >
< span class = "path2" > < / span >
< span class = "path3" > < / span >
< / i >
< div class = "d-flex flex-stack flex-grow-1" >
< div class = "fw-semibold" >
2023-12-10 15:40:13 +00:00
< div class = "fs-6 text-gray-800" >
< span data-i18n = "2fa.setup_help" > If you have trouble using the QR code, select manual entry on your app, and enter the code:< / span >
2023-11-12 15:28:21 +00:00
< span id = "id_secret" class = "fw-bold text-gray-900 pt-2" > < / span >
2023-11-04 09:54:52 +00:00
< / div >
< / div >
< / div >
< / div >
2023-12-10 15:40:13 +00:00
< div id = "errorModalMsg" class = "d-none rounded border-warning border border-dashed bg-light-warning d-flex align-items-center p-5 mb-10" >
< i class = "ki-duotone ki-information fs-3x text-warning me-5" > < span class = "path1" > < / span > < span class = "path2" > < / span > < span class = "path3" > < / span > < / i >
2023-11-04 09:54:52 +00:00
< div class = "text-gray-700 fw-bold fs-5 d-flex flex-column pe-0 pe-sm-10" >
< span id = "errorModalTxt" > < / span >
< / div >
2023-11-14 18:38:09 +00:00
< button id = "id_dismiss_error_modal_msg" type = "button" class = "position-absolute position-sm-relative m-2 m-sm-0 top-0 end-0 btn btn-icon btn-sm btn-active-light-primary ms-sm-auto" >
2023-11-04 09:54:52 +00:00
< i class = "ki-duotone ki-cross fs-2x text-primary" > < span class = "path1" > < / span > < span class = "path2" > < / span > < / i >
< / button >
< / div >
< div class = "fv-row" >
2023-12-10 15:40:13 +00:00
< input data-i18n = "[placeholder]login.auth_code" type = "text" id = "id_passcode" name = "passcode" class = "form-control form-control-lg form-control-solid" placeholder = "Enter authentication code" spellcheck = "false" / >
2023-11-04 09:54:52 +00:00
< / div >
2021-09-04 10:11:04 +00:00
< / div >
< div class = "modal-footer" >
2023-12-10 15:40:13 +00:00
< button data-i18n = "general.cancel" type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Cancel< / button >
2023-11-14 18:38:09 +00:00
< button type = "button" class = "btn btn-primary ms-6" id = "passcode_btn" >
2023-12-10 15:40:13 +00:00
< span data-i18n = "general.submit" class = "indicator-label" >
2023-11-04 09:54:52 +00:00
Submit
< / span >
2023-12-10 15:40:13 +00:00
< span data-i18n = "general.wait" class = "indicator-progress" >
Please wait...
< span class = "spinner-border spinner-border-sm align-middle ms-2" > < / span >
2023-11-04 09:54:52 +00:00
< / span >
2021-09-04 10:11:04 +00:00
< / button >
< / div >
< / div >
< / div >
< / div >
2023-11-04 09:54:52 +00:00
{{- end}}
2021-09-04 10:11:04 +00:00
2023-11-04 09:54:52 +00:00
{{- define "extra_js"}}
2023-11-15 17:48:16 +00:00
< script type = "text/javascript" { { - if . CSPNonce } } nonce = "{{.CSPNonce}}" { { - end } } >
2023-11-04 09:54:52 +00:00
const qrModal = new bootstrap.Modal('#qrcode_modal');
const recCodesModal = new bootstrap.Modal('#recovery_codes_modal');
2021-09-04 10:11:04 +00:00
2023-11-04 09:54:52 +00:00
function onConfigChanged() {
let selectedConfig = $('#id_config option:selected').val();
if (selectedConfig == ""){
$('#save_btn').addClass("d-none");
$('#generate_secret_btn').addClass("d-none");
} else {
//{{- if .TOTPConfig.Enabled }}
if (selectedConfig == "{{.TOTPConfig.ConfigName}}"){
2023-12-10 15:40:13 +00:00
$('#save_label').text($.t('general.submit'));
2023-11-04 09:54:52 +00:00
} else {
2023-12-10 15:40:13 +00:00
$('#save_label').text($.t('general.enable'));
2021-09-04 10:11:04 +00:00
}
2023-11-04 09:54:52 +00:00
$('#save_btn').removeClass("d-none");
$('#generate_secret_btn').removeClass("d-none");
//{{- else}}
$('#save_btn').removeClass("d-none");
2023-12-10 15:40:13 +00:00
$('#save_label').text($.t('general.enable'));
2023-11-04 09:54:52 +00:00
$('#generate_secret_btn').addClass("d-none");
//{{- end}}
}
2021-09-04 10:11:04 +00:00
}
2023-11-04 09:54:52 +00:00
function generateRecoveryCodes() {
el = document.querySelector('#generate_recovery_code_btn');
el.setAttribute('data-kt-indicator', 'on');
el.disabled = true;
axios.post('{{.RecCodesURL}}', null, {
timeout: 15000,
headers: {
'X-CSRF-TOKEN': '{{.CSRFToken}}'
},
validateStatus: function (status) {
return status == 200;
}
}).then(function (response){
el.removeAttribute('data-kt-indicator');
el.disabled = false;
2023-12-10 15:40:13 +00:00
$('#idRecoveryCodesTitle').text($.t('2fa.new_recovery_codes'));
2023-11-04 09:54:52 +00:00
let recList = $('#idRecoveryCodesList');
recList.empty();
$.each(response.data, function(key, item) {
itemCode = escapeHTML(item);
recList.append(`< li class = "d-flex align-items-center py-2 fw-semibold fs-5 text-gray-800" > < span class = "bullet bullet-dot me-5" > < / span > ${itemCode}< / li > `);
});
recCodesModal.show();
}).catch(function (error){
el.removeAttribute('data-kt-indicator');
el.disabled = false;
ModalAlert.fire({
2023-12-10 15:40:13 +00:00
text: $.t('2fa.recovery_codes_gen_err'),
2023-11-04 09:54:52 +00:00
icon: "warning",
2023-12-10 15:40:13 +00:00
confirmButtonText: $.t('general.ok'),
2023-11-04 09:54:52 +00:00
customClass: {
confirmButton: "btn btn-danger"
}
});
});
2021-09-04 10:11:04 +00:00
}
2023-11-04 09:54:52 +00:00
function viewRecoveryCodes() {
el = document.querySelector('#view_recovery_code_btn');
el.setAttribute('data-kt-indicator', 'on');
el.disabled = true;
axios.get('{{.RecCodesURL}}',{
timeout: 15000,
headers: {
'X-CSRF-TOKEN': '{{.CSRFToken}}'
},
validateStatus: function (status) {
return status == 200;
}
}).then(function (response){
el.removeAttribute('data-kt-indicator');
el.disabled = false;
2023-12-10 15:40:13 +00:00
$('#idRecoveryCodesTitle').text($.t('2fa.recovery_codes'));
2023-11-04 09:54:52 +00:00
let recList = $('#idRecoveryCodesList');
recList.empty();
$.each(response.data, function(key, item) {
itemCode = escapeHTML(item.code);
2023-11-07 17:09:24 +00:00
let txtStyleClass = "";
2023-11-04 09:54:52 +00:00
if (item.used) {
2023-11-07 17:09:24 +00:00
txtStyleClass = "line-through";
2023-11-04 09:54:52 +00:00
}
2023-11-07 17:09:24 +00:00
recList.append(`< li class = "d-flex align-items-center py-2 fw-semibold fs-5 text-gray-800 ${txtStyleClass}" > < span class = "bullet bullet-dot me-5" > < / span > ${itemCode}< / li > `);
2023-11-04 09:54:52 +00:00
});
recCodesModal.show();
}).catch(function (error){
el.removeAttribute('data-kt-indicator');
el.disabled = false;
ModalAlert.fire({
2023-12-10 15:40:13 +00:00
text: $.t('2fa.recovery_codes_get_err'),
2023-11-04 09:54:52 +00:00
icon: "warning",
2023-12-10 15:40:13 +00:00
confirmButtonText: $.t('general.ok'),
2023-11-04 09:54:52 +00:00
customClass: {
confirmButton: "btn btn-danger"
}
});
});
}
function disableConfig() {
ModalAlert.fire({
2023-12-10 15:40:13 +00:00
text: $.t('2fa.disable_question'),
2023-11-04 09:54:52 +00:00
icon: "warning",
2023-12-10 15:40:13 +00:00
confirmButtonText: $.t('general.confirm'),
cancelButtonText: $.t('general.cancel'),
2023-11-04 09:54:52 +00:00
customClass: {
confirmButton: "btn btn-danger",
cancelButton: 'btn btn-secondary'
}
}).then((result) => {
if (result.isConfirmed){
doSaveConfig(document.querySelector('#disable_btn'), null, false, true);
2021-09-04 10:11:04 +00:00
}
});
}
2023-11-04 09:54:52 +00:00
function validatePasscode() {
el = document.querySelector('#passcode_btn');
let errDivEl = $('#errorModalMsg');
let errTxtEl = $('#errorModalTxt');
2023-12-10 15:40:13 +00:00
let errorMessage = '2fa.auth_code_invalid';
2023-11-04 09:54:52 +00:00
let passcode = $('#id_passcode').val();
2021-09-04 10:11:04 +00:00
2023-11-04 09:54:52 +00:00
errDivEl.addClass("d-none");
if (passcode == "") {
2023-12-10 15:40:13 +00:00
setI18NData(errTxtEl, '2fa.auth_code_required');
2023-11-04 09:54:52 +00:00
errDivEl.removeClass("d-none");
return;
}
el.setAttribute('data-kt-indicator', 'on');
el.disabled = true;
axios.post("{{.ValidateTOTPURL}}", {
passcode: passcode,
config_name: $('#id_config option:selected').val(),
secret: $('#id_secret').text()
}, {
headers: {
timeout: 15000,
'X-CSRF-TOKEN': '{{.CSRFToken}}'
},
validateStatus: function (status) {
return status == 200;
}
}).then(function (response){
el.removeAttribute('data-kt-indicator');
el.disabled = false;
if (!response.data.message) {
2023-12-10 15:40:13 +00:00
setI18NData(errTxtEl, errorMessage);
2023-11-04 09:54:52 +00:00
errDivEl.removeClass("d-none");
return;
}
qrModal.hide();
doSaveConfig(null, null, true, false);
}).catch(function (error){
el.removeAttribute('data-kt-indicator');
el.disabled = false;
2023-12-10 15:40:13 +00:00
setI18NData(errTxtEl, errorMessage);
2023-11-04 09:54:52 +00:00
errDivEl.removeClass("d-none");
});
2021-09-04 10:11:04 +00:00
}
2023-11-04 09:54:52 +00:00
function confirmGenerateSecret() {
ModalAlert.fire({
2023-12-10 15:40:13 +00:00
text: $.t('2fa.generate_question'),
2023-11-04 09:54:52 +00:00
icon: "warning",
2023-12-10 15:40:13 +00:00
confirmButtonText: $.t('general.confirm'),
cancelButtonText: $.t('general.cancel'),
2023-11-04 09:54:52 +00:00
customClass: {
confirmButton: "btn btn-danger",
cancelButton: 'btn btn-secondary'
}
}).then((result) => {
if (result.isConfirmed){
el = document.querySelector('#generate_secret_btn');
configName = $('#id_config option:selected').val();
generateSecret(el,configName);
2021-09-04 10:11:04 +00:00
}
});
}
2023-11-04 09:54:52 +00:00
function generateSecret(el, configName) {
if (!el || !configName){
confirmGenerateSecret();
return;
}
let errDivEl = $('#errorMsg');
let errTxtEl = $('#errorTxt');
if ($('#id_protocols').find('option:selected').length == 0){
2023-12-10 15:40:13 +00:00
setI18NData(errTxtEl, '2fa.no_protocol');
2023-11-04 09:54:52 +00:00
errDivEl.removeClass("d-none");
return;
}
2023-12-10 15:40:13 +00:00
let errorMessage = '2fa.auth_secret_gen_err';
2023-11-04 09:54:52 +00:00
$('#id_secret').text("");
errDivEl.addClass("d-none");
el.setAttribute('data-kt-indicator', 'on');
el.disabled = true;
axios.post("{{.GenerateTOTPURL}}", {
config_name: configName
}, {
headers: {
timeout: 15000,
'X-CSRF-TOKEN': '{{.CSRFToken}}'
},
validateStatus: function (status) {
return status == 200;
}
}).then(function (response){
el.removeAttribute('data-kt-indicator');
el.disabled = false;
if (!response.data.secret) {
2023-12-10 15:40:13 +00:00
setI18NData(errTxtEl, errorMessage);
2023-11-04 09:54:52 +00:00
errDivEl.removeClass("d-none");
return;
}
$('#id_secret').text(response.data.secret);
$('#errorModalMsg').addClass("d-none");
$('#id_passcode').val("");
2023-11-18 19:06:31 +00:00
let qrCodeContainer = document.getElementById("id_qr_code_container");
clearChilds(qrCodeContainer);
let qrCodeImg = document.createElement("img");
qrCodeImg.classList.add("mw-150px");
qrCodeImg.src = "{{.MFAURL}}/qrcode?url="+encodeURIComponent(response.data.url);
2023-12-10 15:40:13 +00:00
qrCodeImg.alt = $.t('general.qr_code');
2023-11-18 19:06:31 +00:00
qrCodeContainer.appendChild(qrCodeImg);
2023-11-04 09:54:52 +00:00
qrModal.show();
}).catch(function (error){
el.removeAttribute('data-kt-indicator');
el.disabled = false;
2023-12-10 15:40:13 +00:00
setI18NData(errTxtEl, errorMessage);
2023-11-04 09:54:52 +00:00
errDivEl.removeClass("d-none");
});
2021-09-04 10:11:04 +00:00
}
2023-11-04 09:54:52 +00:00
function doSaveConfig(el, configName, full, disabled) {
if (!el){
el = document.querySelector('#save_btn');
}
if (!configName) {
configName = $('#id_config option:selected').val();
}
let errDivEl = $('#errorMsg');
let errTxtEl = $('#errorTxt');
2023-12-10 15:40:13 +00:00
let errorMessage = '2fa.save_err';
2023-11-04 09:54:52 +00:00
let protocolsArray = [];
$('#id_protocols').find('option:selected').each(function(){
protocolsArray.push($(this).val());
});
if (protocolsArray.length == 0){
2023-12-10 15:40:13 +00:00
setI18NData(errTxtEl, '2fa.no_protocol');
2023-11-04 09:54:52 +00:00
errDivEl.removeClass("d-none");
return;
}
let postData = {
protocols: protocolsArray
}
if (full){
postData.config_name = configName;
postData.secret = {
status: "Plain",
payload: $('#id_secret').text()
}
}
if (disabled){
postData.enabled = false;
} else {
postData.enabled = true;
}
errDivEl.addClass("d-none");
el.setAttribute('data-kt-indicator', 'on');
el.disabled = true;
axios.post("{{.SaveTOTPURL}}", postData, {
headers: {
timeout: 15000,
'X-CSRF-TOKEN': '{{.CSRFToken}}'
},
validateStatus: function (status) {
return status == 200;
}
}).then(function (response){
el.removeAttribute('data-kt-indicator');
el.disabled = false;
if (!response.data.message) {
2023-12-10 15:40:13 +00:00
setI18NData(errTxtEl, errorMessage);
2023-11-04 09:54:52 +00:00
errDivEl.removeClass("d-none");
return;
}
ModalAlert.fire({
2023-12-10 15:40:13 +00:00
text: $.t('general.config_saved'),
2023-11-04 09:54:52 +00:00
icon: "success",
2023-12-10 15:40:13 +00:00
confirmButtonText: $.t('general.ok'),
2023-11-04 09:54:52 +00:00
customClass: {
confirmButton: 'btn btn-primary'
}
}).then((result) => {
if (result.isConfirmed){
location.reload();
}
2021-09-04 10:11:04 +00:00
});
2023-11-04 09:54:52 +00:00
}).catch(function (error) {
el.removeAttribute('data-kt-indicator');
el.disabled = false;
if (error & & error.response) {
2023-12-10 15:40:13 +00:00
switch (error.response.status) {
case 400:
errorMessage = "2fa.save_err_proto";
break;
2021-09-04 10:11:04 +00:00
}
}
2023-12-10 15:40:13 +00:00
setI18NData(errTxtEl, errorMessage);
2023-11-04 09:54:52 +00:00
errDivEl.removeClass("d-none");
});
2021-09-04 10:11:04 +00:00
}
2023-11-04 09:54:52 +00:00
function saveConfig() {
let selectedConfig = $('#id_config option:selected').val();
let saveBtn = document.querySelector('#save_btn');
if (selectedConfig == "{{.TOTPConfig.ConfigName}}"){
doSaveConfig(saveBtn, selectedConfig, false, false);
return;
2021-09-04 10:11:04 +00:00
}
2023-11-04 09:54:52 +00:00
generateSecret(saveBtn, selectedConfig);
2021-09-04 10:11:04 +00:00
}
2023-12-10 15:40:13 +00:00
$(document).on("i18nshow", function(){
2023-11-04 09:54:52 +00:00
onConfigChanged();
2023-11-14 18:38:09 +00:00
var dismissErrorModalBtn = $('#id_dismiss_error_modal_msg');
if (dismissErrorModalBtn){
dismissErrorModalBtn.on("click",function(){
$('#errorModalMsg').addClass("d-none");
});
}
var disableBtn = $('#disable_btn');
if (disableBtn){
disableBtn.on("click", function(){
disableConfig();
});
}
var generateSecretBtn = $('#generate_secret_btn');
if (generateSecretBtn){
generateSecretBtn.on("click", function(){
generateSecret();
});
}
var saveBtn = $('#save_btn');
if (saveBtn){
saveBtn.on("click", function(){
saveConfig();
});
}
var generateRecoveryCodeBtn = $('#generate_recovery_code_btn');
if (generateRecoveryCodeBtn){
generateRecoveryCodeBtn.on("click", function(){
generateRecoveryCodes();
});
}
var viewRecoveryCodesBtn = $('#view_recovery_code_btn');
if (viewRecoveryCodesBtn){
viewRecoveryCodesBtn.on("click", function(){
viewRecoveryCodes();
});
}
var passcodeBtn = $('#passcode_btn');
if (passcodeBtn){
passcodeBtn.on("click", function() {
validatePasscode();
});
}
2023-11-15 17:48:16 +00:00
var configSelect = $('#id_config');
if (configSelect){
configSelect.on("change", function(){
onConfigChanged();
});
}
2021-09-04 10:11:04 +00:00
});
2023-11-04 09:54:52 +00:00
2021-09-04 10:11:04 +00:00
< / script >
2023-11-04 09:54:52 +00:00
{{- end}}