实现安全中心邮箱验证码修改密码并补充测试

This commit is contained in:
xuans committed 2026-09-15 19:00:52 +08:00
1 parent 63f8b1777b
commit 45268ce2b7
10 files changed
+478 -32

No files matched your search

@@ -58,3 +58,15 @@
.credential-actions { width: 100%; }
.credential-actions .el-button { flex: 1; }
}
.password-verification-row {
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
gap: 12px;
width: 100%;
}
.password-verification-row strong {
overflow-wrap: anywhere;
}
@@ -1,4 +1,4 @@
import { computed, onMounted, reactive, ref, watch } from 'vue'
import { computed, onMounted, onUnmounted, reactive, ref, watch } from 'vue'
import { ElMessageBox, ElNotification } from 'element-plus'
@@ -19,9 +19,7 @@ export function usePage() {
if (canReadGlobalPolicy.value) {
options.push({ value: 'security-policy', label: '安全策略' })
}
if (!isSuperAdmin.value) {
options.push({ value: 'password', label: lang.modify_pwd })
}
options.push({ value: 'password', label: lang.modify_pwd })
options.push({ value: 'question', label: '密保问题' })
if (!isSuperAdmin.value) {
options.push({ value: 'recovery', label: '离线恢复码' })
@@ -60,10 +58,16 @@ export function usePage() {
const externalEmails = ref([])
const externalListLoading = ref(false)
const externalDialogVisible = ref(false)
const primaryEmail = computed(() => externalEmails.value.find(item => item.type === 'primary' && item.status === 'verified'))
const passwordSending = ref(false)
const passwordSubmitting = ref(false)
const passwordChallenge = ref('')
const passwordCountdown = ref(0)
let passwordTimer = null
let lastNotificationKey = ''
let lastNotificationAt = 0
const ruleForm = reactive({ current_pwd: '', new_pwd: '', new_pwd2: '' })
const ruleForm = reactive({ current_pwd: '', new_pwd: '', new_pwd2: '', code: '' })
const rules = reactive({
current_pwd: [{ required: true, message: lang.err_required_pwd, trigger: 'blur' }],
new_pwd: [{ required: true, message: lang.err_required_pwd, trigger: 'blur' }],
@@ -255,16 +259,75 @@ export function usePage() {
const logout = () => http.post('/api/v1/logout', {}).then(() => location.reload())
const submit = async () => {
if (!ruleForm.current_pwd || !ruleForm.new_pwd) return
if (passwordSubmitting.value || passwordSending.value) return
if (!ruleForm.new_pwd || (!isSuperAdmin.value && !ruleForm.current_pwd)) {
notify('请填写完整的密码信息')
return
}
const passwordBytes = new TextEncoder().encode(ruleForm.new_pwd).length
if (passwordBytes < 8 || passwordBytes > 12) {
notify('密码长度必须为 8-12 字节')
return
}
if (ruleForm.new_pwd !== ruleForm.new_pwd2) {
notify(lang.err_pwd_diff)
return
}
const res = await http.post('/api/v1/settings/modify_password', { current_password: ruleForm.current_pwd, password: ruleForm.new_pwd })
notify(res.errorNo === 0 ? '密码修改成功' : res.errorMsg, res.errorNo === 0 ? 'success' : 'error')
if (res.errorNo === 0) Object.assign(ruleForm, { current_pwd: '', new_pwd: '', new_pwd2: '' })
if (isSuperAdmin.value && (!primaryEmail.value || !passwordChallenge.value || !/^\d{8}$/.test(ruleForm.code.trim()))) {
notify('请获取并输入主安全邮箱中的 8 位验证码')
return
}
passwordSubmitting.value = true
try {
const payload = isSuperAdmin.value
? { password: ruleForm.new_pwd, challenge_id: passwordChallenge.value, code: ruleForm.code.trim() }
: { current_password: ruleForm.current_pwd, password: ruleForm.new_pwd }
const res = await http.post('/api/v1/settings/modify_password', payload)
if (res.errorNo !== 0) throw new Error(res.errorMsg || '密码修改失败')
Object.assign(ruleForm, { current_pwd: '', new_pwd: '', new_pwd2: '', code: '' })
passwordChallenge.value = ''
notify('密码修改成功,请重新登录;原安全凭据已失效', 'success')
globalStatus.clear()
location.assign('/login')
} catch (error) {
notify(error?.message || '密码修改失败,请重新获取验证码后重试')
} finally {
passwordSubmitting.value = false
}
}
const sendPasswordCode = async () => {
if (passwordSending.value || passwordSubmitting.value || passwordCountdown.value > 0 || !primaryEmail.value) return
passwordSending.value = true
passwordChallenge.value = ''
ruleForm.code = ''
try {
const res = await http.post('/api/v1/security/verification/request', {
purpose: 'password_change', account: globalStatus.userInfos.account,
})
if (res.errorNo !== 0 || !res.data?.id) throw new Error(res.errorMsg || '验证码发送失败')
passwordChallenge.value = res.data.id
passwordCountdown.value = 60
const deadline = Date.now() + 60000
clearInterval(passwordTimer)
passwordTimer = setInterval(() => {
passwordCountdown.value = Math.max(0, Math.ceil((deadline - Date.now()) / 1000))
if (!passwordCountdown.value) clearInterval(passwordTimer)
}, 1000)
notify(`验证码已发送至 ${res.data.email || primaryEmail.value.email}`, 'success')
} catch (error) {
notify(error?.message || '验证码发送失败')
} finally {
passwordSending.value = false
}
}
watch(() => primaryEmail.value?.id, () => {
passwordChallenge.value = ''
ruleForm.code = ''
})
onUnmounted(() => clearInterval(passwordTimer))
onMounted(async () => {
await loadSecurityData()
})
@@ -276,6 +339,7 @@ export function usePage() {
return {
lang, isSuperAdmin, supported, webAuthnEnabled, sectionOptions, selectedSection, selectSection,
globalSecurityPolicyPage,
primaryEmail, passwordSending, passwordSubmitting, passwordCountdown, sendPasswordCode,
registering, credentialsLoading, credentials, securityLoading,
securityQuestion, questionForm, recoveryConfigured, generatedCodes, externalEmails, externalListLoading,
externalDialogVisible, ruleForm, rules, submit, registerCredential, renameCredential, revokeCredential,
@@ -15,28 +15,63 @@
ref="globalSecurityPolicyPage"
/>
<section v-if="selectedSection === 'password' && !isSuperAdmin" class="security-section">
<section v-if="selectedSection === 'password'" class="security-section">
<div class="settings-header">
<h3>{{ lang.modify_pwd }}</h3>
<p class="settings-desc">{{ lang.modify_pwd_desc }}</p>
<p class="settings-desc">{{ isSuperAdmin ? '通过主安全邮箱验证码修改密码,完成后需重新登录。' : lang.modify_pwd_desc }}</p>
</div>
<div class="settings-body">
<el-form :model="ruleForm" :rules="rules" status-icon label-position="top" class="settings-form">
<el-form-item :label="lang.current_pwd" prop="current_pwd"><el-input v-model="ruleForm.current_pwd" type="password" :placeholder="lang.enter_current_pwd" show-password class="premium-input" /></el-form-item>
<el-form-item :label="lang.modify_pwd" prop="new_pwd"><el-input v-model="ruleForm.new_pwd" type="password" :placeholder="lang.enter_new_pwd" show-password class="premium-input" /></el-form-item>
<el-form-item :label="lang.enter_again" prop="new_pwd2"><el-input v-model="ruleForm.new_pwd2" type="password" :placeholder="lang.confirm_new_pwd" show-password class="premium-input" /></el-form-item>
<div class="form-actions"><el-button type="primary" @click="submit" class="submit-btn">{{ lang.submit }}</el-button></div>
<template v-if="isSuperAdmin">
<el-form-item label="主要外部安全邮箱">
<div v-if="primaryEmail" class="password-verification-row">
<strong>{{ primaryEmail.email }}</strong>
<el-button
:loading="passwordSending"
:disabled="passwordCountdown > 0 || passwordSubmitting"
@click="sendPasswordCode"
>
{{ passwordCountdown > 0 ? `${passwordCountdown} 秒后重发` : '获取验证码' }}
</el-button>
</div>
<el-button v-else type="primary" link @click="selectSection('external-email')">
请先绑定并验证主要外部安全邮箱
</el-button>
</el-form-item>
<el-form-item label="邮箱验证码" required>
<el-input
v-model="ruleForm.code"
inputmode="numeric"
autocomplete="one-time-code"
maxlength="8"
placeholder="请输入邮箱中的 8 位验证码"
:disabled="!primaryEmail || passwordSubmitting"
/>
</el-form-item>
</template>
<el-form-item v-else :label="lang.current_pwd" prop="current_pwd">
<el-input v-model="ruleForm.current_pwd" type="password" autocomplete="current-password" :placeholder="lang.enter_current_pwd" show-password class="premium-input" />
</el-form-item>
<el-form-item :label="lang.modify_pwd" prop="new_pwd">
<el-input v-model="ruleForm.new_pwd" type="password" autocomplete="new-password" :placeholder="lang.enter_new_pwd" show-password class="premium-input" />
</el-form-item>
<el-form-item :label="lang.enter_again" prop="new_pwd2">
<el-input v-model="ruleForm.new_pwd2" type="password" autocomplete="new-password" :placeholder="lang.confirm_new_pwd" show-password class="premium-input" />
</el-form-item>
<div class="form-actions">
<el-button type="primary" :loading="passwordSubmitting" :disabled="passwordSending || (isSuperAdmin && !primaryEmail)" @click="submit" class="submit-btn">
{{ lang.submit }}
</el-button>
</div>
</el-form>
</div>
<el-divider class="settings-divider" />
</section>
<el-alert v-if="selectedSection === 'password' && isSuperAdmin" title="超级管理员密码只能通过部署端专用本机通道重置,网页不会提供修改入口。" type="warning" :closable="false" show-icon class="security-alert" />
<section v-if="selectedSection === 'question'" class="security-section">
<div class="settings-header">
<h3>密保问题</h3>
<p v-if="isSuperAdmin" class="settings-desc">此设置不会开放超级管理员网页密码恢复,密码仍须通过部署端本机通道重置。</p>
<p v-if="isSuperAdmin" class="settings-desc">密保不用于超级管理员重置密码,请使用修改密码页面的邮箱验证或部署端本机恢复通道。</p>
<p v-else class="settings-desc">密保问题不能单独重置密码,必须与离线恢复码组合使用。</p>
</div>
<el-form :model="questionForm" label-position="top" class="settings-form">
@@ -111,6 +146,7 @@ import ExternalEmailBindingDialog from './ExternalEmailBindingDialog/ExternalEma
const {
lang, isSuperAdmin, supported, webAuthnEnabled, sectionOptions, selectedSection, selectSection,
globalSecurityPolicyPage,
primaryEmail, passwordSending, passwordSubmitting, passwordCountdown, sendPasswordCode,
registering, credentialsLoading, credentials, securityLoading,
securityQuestion, questionForm, recoveryConfigured, generatedCodes, externalEmails, externalListLoading,
externalDialogVisible, ruleForm, rules, submit, registerCredential, renameCredential, revokeCredential,