2024-01-15 07:21:27 +00:00
|
|
|
|
<script lang="ts" setup>
|
2024-06-26 10:42:50 +00:00
|
|
|
|
import type { TotpRequest } from "@halo-dev/api-client";
|
2024-06-25 04:31:44 +00:00
|
|
|
|
import { ucApiClient } from "@halo-dev/api-client";
|
2024-06-26 10:42:50 +00:00
|
|
|
|
import { Toast, VButton, VModal, VSpace } from "@halo-dev/components";
|
2024-01-15 07:21:27 +00:00
|
|
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/vue-query";
|
|
|
|
|
import { useQRCode } from "@vueuse/integrations/useQRCode";
|
2024-06-26 10:42:50 +00:00
|
|
|
|
import { computed, ref } from "vue";
|
2024-01-15 07:21:27 +00:00
|
|
|
|
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
|
(event: "close"): void;
|
|
|
|
|
}>();
|
|
|
|
|
|
2024-05-27 08:56:57 +00:00
|
|
|
|
const modal = ref<InstanceType<typeof VModal> | null>(null);
|
2024-01-15 07:21:27 +00:00
|
|
|
|
|
|
|
|
|
const { data } = useQuery({
|
|
|
|
|
queryKey: ["totp-auth-link"],
|
|
|
|
|
queryFn: async () => {
|
2024-06-25 04:31:44 +00:00
|
|
|
|
const { data } = await ucApiClient.security.twoFactor.getTotpAuthLink();
|
2024-01-15 07:21:27 +00:00
|
|
|
|
return data;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const qrcode = useQRCode(computed(() => data.value?.authLink || ""));
|
|
|
|
|
|
|
|
|
|
const { mutate, isLoading } = useMutation({
|
|
|
|
|
mutationKey: ["configure-totp"],
|
|
|
|
|
mutationFn: async ({ totpRequest }: { totpRequest: TotpRequest }) => {
|
2024-06-25 04:31:44 +00:00
|
|
|
|
await ucApiClient.security.twoFactor.configurerTotp({
|
2024-01-15 07:21:27 +00:00
|
|
|
|
totpRequest: totpRequest,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
onSuccess() {
|
|
|
|
|
Toast.success("配置成功");
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: ["two-factor-settings"] });
|
2024-05-27 08:56:57 +00:00
|
|
|
|
modal.value?.close();
|
2024-01-15 07:21:27 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function onSubmit(data: TotpRequest) {
|
|
|
|
|
mutate({ totpRequest: data });
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<VModal
|
|
|
|
|
ref="modal"
|
|
|
|
|
:width="500"
|
|
|
|
|
:centered="false"
|
|
|
|
|
title="TOTP 配置"
|
|
|
|
|
@close="emit('close')"
|
|
|
|
|
>
|
|
|
|
|
<div>
|
|
|
|
|
<div class="mb-4 space-y-3 border-b border-gray-100 pb-4 text-gray-900">
|
|
|
|
|
<div class="text-sm font-semibold">使用验证器应用扫描下方二维码:</div>
|
|
|
|
|
<img :src="qrcode" class="rounded-base border border-gray-100" />
|
|
|
|
|
<details>
|
|
|
|
|
<summary class="cursor-pointer select-none text-sm text-gray-800">
|
|
|
|
|
如果无法扫描二维码,点击查看代替步骤
|
|
|
|
|
</summary>
|
|
|
|
|
<div class="mt-3 rounded-base border border-gray-100 p-2">
|
|
|
|
|
<span class="text-sm text-gray-600">
|
|
|
|
|
使用以下代码手动配置验证器应用:
|
|
|
|
|
</span>
|
|
|
|
|
<div class="mt-2">
|
|
|
|
|
<code
|
|
|
|
|
class="select-all rounded bg-gray-200 p-1 text-xs text-gray-900"
|
|
|
|
|
>
|
|
|
|
|
{{ data?.rawSecret }}
|
|
|
|
|
</code>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</details>
|
|
|
|
|
</div>
|
|
|
|
|
<FormKit id="totp-form" type="form" name="totp-form" @submit="onSubmit">
|
|
|
|
|
<FormKit
|
|
|
|
|
type="number"
|
|
|
|
|
name="code"
|
|
|
|
|
label="验证码"
|
|
|
|
|
validation="required"
|
|
|
|
|
help="从验证器应用获得的 6 位验证码"
|
|
|
|
|
></FormKit>
|
|
|
|
|
<FormKit
|
|
|
|
|
type="password"
|
|
|
|
|
label="验证密码"
|
|
|
|
|
validation="required"
|
|
|
|
|
name="password"
|
|
|
|
|
help="当前账号的登录密码"
|
|
|
|
|
autocomplete="current-password"
|
|
|
|
|
></FormKit>
|
|
|
|
|
<FormKit
|
|
|
|
|
:model-value="data?.rawSecret"
|
|
|
|
|
type="hidden"
|
|
|
|
|
name="secret"
|
|
|
|
|
></FormKit>
|
|
|
|
|
</FormKit>
|
|
|
|
|
</div>
|
|
|
|
|
<template #footer>
|
|
|
|
|
<VSpace>
|
|
|
|
|
<VButton
|
|
|
|
|
:loading="isLoading"
|
|
|
|
|
type="secondary"
|
|
|
|
|
@click="$formkit.submit('totp-form')"
|
|
|
|
|
>
|
|
|
|
|
完成
|
|
|
|
|
</VButton>
|
2024-05-27 08:56:57 +00:00
|
|
|
|
<VButton @click="modal?.close()">关闭</VButton>
|
2024-01-15 07:21:27 +00:00
|
|
|
|
</VSpace>
|
|
|
|
|
</template>
|
|
|
|
|
</VModal>
|
|
|
|
|
</template>
|