diff --git a/common/auth.py b/common/auth.py index 540fafc405..f26e081c84 100644 --- a/common/auth.py +++ b/common/auth.py @@ -109,33 +109,23 @@ def authenticate_entry(request): if SysConfig().get('enforce_2fa'): # 用户是否配置过2fa if twofa_enabled: - auth_type = twofa_enabled[0].auth_type - phone = twofa_enabled[0].phone verify_mode = 'verify_only' else: - auth_type = 'totp' - phone = '' verify_mode = 'verify_config' # 设置无登录状态session s = SessionStore() s['user'] = authenticated_user.username - s['auth_type'] = auth_type s['verify_mode'] = verify_mode - s['phone'] = phone s.set_expiry(300) s.create() result = {'status': 0, 'msg': 'ok', 'data': s.session_key} else: # 用户是否配置过2fa if twofa_enabled: - auth_type = twofa_enabled[0].auth_type - phone = twofa_enabled[0].phone # 设置无登录状态session s = SessionStore() s['user'] = authenticated_user.username - s['auth_type'] = auth_type s['verify_mode'] = 'verify_only' - s['phone'] = phone s.set_expiry(300) s.create() result = {'status': 0, 'msg': 'ok', 'data': s.session_key} diff --git a/common/templates/2fa.html b/common/templates/2fa.html index 0515e7ce17..b4b22d14a2 100644 --- a/common/templates/2fa.html +++ b/common/templates/2fa.html @@ -27,7 +27,7 @@

启用两步验证

-
-
+
+ +
- +
{% else %} - {% if auth_type == 'totp' %} -
- - - -
-
- -
- {% else %} -
- - -
-
- - -
- {% endif %} +
+ + +
+
+ + + +
+ +
+ + +
{% endif %} @@ -120,6 +120,10 @@

启用两步验证

//keycode==13为回车键 if (event.keyCode === 13) { let otp = $('#otpCode').val(); + if (!otp) { + alert('请输入验证码!') + return + } authOTP(otp); } }); @@ -127,10 +131,20 @@

启用两步验证

$(document).ready(function () { if ('{{ verify_mode }}' === 'verify_config') { - if ('{{ auth_type }}' === 'totp') { - let data = config_2fa(); - $("#qrcode-img").attr("key", data.data.key) - $("#qrcode-img").attr("src", "/user/qrcode/" + data.data.key) + let data = config_2fa(); + $("#qrcode-img").attr("key", data.data.key) + $("#qrcode-img").attr("src", "/user/qrcode/" + data.data.key) + } else if ('{{ verify_mode }}' === 'verify_only') { + let auth_types = {{ auth_types|safe }}; + for (i=0;i < auth_types.length;i++) { + let auth_type; + if (i === 0) { + auth_type = '' + } else { + auth_type = '' + } + $("#auth_type").append(auth_type) + $("#auth_type").trigger('change') } } }) @@ -160,33 +174,31 @@

启用两步验证

}) $("#auth_type").change(function () { + $("#otpCode").val(''); let auth_type = $("#auth_type").val(); if (auth_type === 'totp') { $("#totp-form").show(); $("#sms-form").hide(); - $("#btnCaptcha").hide(); - $("#btnAuth").addClass('btn-block'); + $("#btnCaptcha").hide() } else if (auth_type === 'sms') { $("#totp-form").hide(); $("#sms-form").show(); - $("#btnCaptcha").show(); - $("#btnAuth").removeClass('btn-block'); + $("#btnCaptcha").show() } }) function config_2fa() { // 配置2fa let result; - let phone = $("#phone").val(); - let auth_type = $("#auth_type").val(); $.ajax({ type: "post", url: "/api/v1/user/2fa/", dataType: "json", data: { engineer: '{{ username }}', - auth_type: auth_type?auth_type:'{{ auth_type }}', - phone: phone?phone:'{{ phone }}' + enable: 'true', + auth_type: $("#auth_type").val(), + phone: $("#phone").val() }, async: false, complete: function () { diff --git a/common/templates/base.html b/common/templates/base.html index 94e976fe59..f46c93c663 100644 --- a/common/templates/base.html +++ b/common/templates/base.html @@ -17,6 +17,7 @@ +