This commit is contained in:
1 parent
04cc5774e0
commit
41593ee762
25 files changed
+1223
-207
No files matched your search
@@ -25,6 +25,7 @@ export function usePage() {
|
||||
options.push({ value: 'recovery', label: '离线恢复码' })
|
||||
}
|
||||
options.push({ value: 'external-email', label: '外部安全邮箱' })
|
||||
options.push({ value: 'totp', label: '手机动态码' })
|
||||
options.push({ value: 'webauthn', label: '安全密钥' })
|
||||
return options
|
||||
})
|
||||
|
||||
@@ -110,6 +110,8 @@
|
||||
<el-empty v-else v-loading="externalListLoading" description="暂无外部安全邮箱" />
|
||||
</section>
|
||||
|
||||
<TOTPSettings v-if="selectedSection === 'totp'" />
|
||||
|
||||
<el-divider v-if="selectedSection === 'webauthn'" class="settings-divider" />
|
||||
<el-alert
|
||||
v-if="selectedSection === 'webauthn' && !webAuthnEnabled"
|
||||
@@ -137,6 +139,7 @@
|
||||
<script setup>
|
||||
import { usePage } from './SecuritySettings.js'
|
||||
import ProfileSettings from './ProfileSettings/ProfileSettings.vue'
|
||||
import TOTPSettings from './TOTPSettings/TOTPSettings.vue'
|
||||
import SettingsSubmenu from '../SettingsSubmenu/SettingsSubmenu.vue'
|
||||
import GlobalSecurityPolicy from './GlobalSecurityPolicy/GlobalSecurityPolicy.vue'
|
||||
import ExternalEmailBindingDialog from './ExternalEmailBindingDialog/ExternalEmailBindingDialog.vue'
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
.totp-settings {
|
||||
max-width: 640px;
|
||||
padding: 20px 0;
|
||||
}
|
||||
|
||||
.totp-settings p {
|
||||
color: var(--pm-text-secondary);
|
||||
line-height: 1.7;
|
||||
}
|
||||
|
||||
.totp-settings .el-alert {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.totp-enrollment img,
|
||||
.totp-enrollment label,
|
||||
.totp-enrollment code {
|
||||
display: block;
|
||||
margin: 12px 0;
|
||||
}
|
||||
|
||||
.totp-enrollment code {
|
||||
padding: 12px;
|
||||
border: 1px solid var(--pm-border-color);
|
||||
border-radius: 8px;
|
||||
overflow-wrap: anywhere;
|
||||
user-select: all;
|
||||
}
|
||||
|
||||
.totp-actions {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
margin-top: 16px;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import { onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
import QRCode from 'qrcode'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { http } from '@/utils/axios'
|
||||
import { isRecentAuthenticationCancelled, requestWithRecentAuthentication } from '@/utils/recentAuthentication'
|
||||
|
||||
export function useTOTPSettings() {
|
||||
const enabled = ref(false)
|
||||
const ready = ref(false)
|
||||
const busy = ref(false)
|
||||
const enrollment = ref(null)
|
||||
const qrCode = ref('')
|
||||
const code = ref('')
|
||||
const errorMessage = ref('')
|
||||
let disposed = false
|
||||
|
||||
const clearEnrollment = () => {
|
||||
enrollment.value = null
|
||||
qrCode.value = ''
|
||||
code.value = ''
|
||||
}
|
||||
|
||||
const load = async () => {
|
||||
const result = await http.get('/api/v1/security/totp', { timeout: 4500 })
|
||||
if (result.errorNo !== 0) throw new Error(result.errorMsg || '无法读取动态码状态')
|
||||
if (disposed) return
|
||||
enabled.value = result.data.enabled
|
||||
ready.value = true
|
||||
}
|
||||
|
||||
const perform = async action => {
|
||||
if (busy.value) return
|
||||
if (['confirm', 'disable'].includes(action) && !/^\d{6}$/.test(code.value)) {
|
||||
ElMessage.warning('请输入手机验证器生成的六位动态码')
|
||||
return
|
||||
}
|
||||
busy.value = true
|
||||
errorMessage.value = ''
|
||||
try {
|
||||
const result = await requestWithRecentAuthentication(() => http.post('/api/v1/security/totp', {
|
||||
action, id: enrollment.value?.id, code: code.value
|
||||
}, { timeout: 4500 }))
|
||||
if (result.errorNo !== 0) throw new Error(result.errorMsg || '动态码设置失败')
|
||||
if (disposed) return
|
||||
if (action === 'begin') {
|
||||
enrollment.value = result.data
|
||||
// 二维码在浏览器本地生成,密钥不会发送给第三方服务。
|
||||
qrCode.value = await QRCode.toDataURL(result.data.uri, { width: 240, margin: 2 })
|
||||
} else {
|
||||
clearEnrollment()
|
||||
await load()
|
||||
if (action !== 'cancel') ElMessage.success(action === 'confirm' ? '手机动态码已启用' : '手机动态码已关闭')
|
||||
}
|
||||
} catch (error) {
|
||||
if (!disposed && !isRecentAuthenticationCancelled(error)) {
|
||||
errorMessage.value = error?.message || '操作失败,请刷新状态后重试'
|
||||
}
|
||||
} finally {
|
||||
busy.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const refresh = async () => {
|
||||
busy.value = true
|
||||
errorMessage.value = ''
|
||||
try { await load() } catch (error) { errorMessage.value = error.message } finally { busy.value = false }
|
||||
}
|
||||
|
||||
onMounted(refresh)
|
||||
onBeforeUnmount(() => {
|
||||
disposed = true
|
||||
clearEnrollment()
|
||||
})
|
||||
return { enabled, ready, busy, enrollment, qrCode, code, errorMessage, perform, refresh }
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<template>
|
||||
<section class="totp-settings" v-loading="busy">
|
||||
<h3>手机动态码(TOTP)</h3>
|
||||
<p>所有账号都可以自行选择是否开启。开启后,输入账号密码即可选择手机动态码或邮箱验证码登录。</p>
|
||||
<el-alert v-if="errorMessage" :title="errorMessage" type="error" :closable="false" show-icon />
|
||||
<el-button v-if="!ready" :disabled="busy" @click="refresh">重新读取状态</el-button>
|
||||
<template v-else>
|
||||
<el-tag :type="enabled ? 'success' : 'info'">{{ enabled ? '已启用' : '未启用' }}</el-tag>
|
||||
<div v-if="enrollment" class="totp-enrollment">
|
||||
<p>使用手机验证器扫描二维码,或手动输入下面的密钥。绑定申请十分钟内有效。</p>
|
||||
<img v-if="qrCode" :src="qrCode" alt="手机验证器绑定二维码" width="240" height="240" />
|
||||
<label>手动绑定密钥</label>
|
||||
<code>{{ enrollment.secret }}</code>
|
||||
<p>算法:SHA256;位数:6;周期:30 秒。请妥善保管密钥,勿分享给他人。</p>
|
||||
<el-form @submit.prevent="perform('confirm')">
|
||||
<el-form-item label="六位手机动态码">
|
||||
<el-input v-model="code" maxlength="6" inputmode="numeric" autocomplete="one-time-code" placeholder="输入手机上当前显示的动态码" />
|
||||
</el-form-item>
|
||||
<div class="totp-actions">
|
||||
<el-button type="primary" :disabled="busy" @click="perform('confirm')">确认启用</el-button>
|
||||
<el-button :disabled="busy" @click="perform('cancel')">取消绑定</el-button>
|
||||
</div>
|
||||
</el-form>
|
||||
</div>
|
||||
<el-form v-else-if="enabled" class="totp-disable" @submit.prevent="perform('disable')">
|
||||
<p>关闭前请输入当前动态码。刚绑定或已用于验证的动态码需要等待下一次更新。</p>
|
||||
<el-form-item label="六位手机动态码">
|
||||
<el-input v-model="code" maxlength="6" inputmode="numeric" autocomplete="one-time-code" placeholder="输入动态码以确认关闭" />
|
||||
</el-form-item>
|
||||
<el-button type="danger" plain :disabled="busy" @click="perform('disable')">关闭手机动态码</el-button>
|
||||
</el-form>
|
||||
<div v-else class="totp-actions">
|
||||
<el-button type="primary" :disabled="busy" @click="perform('begin')">绑定手机验证器</el-button>
|
||||
</div>
|
||||
</template>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { useTOTPSettings } from './TOTPSettings.js'
|
||||
|
||||
const { enabled, ready, busy, enrollment, qrCode, code, errorMessage, perform, refresh } = useTOTPSettings()
|
||||
</script>
|
||||
|
||||
<style scoped src="./TOTPSettings.css"></style>
|
||||
Reference in new issue
Block a user