2023-07-27 07:31:52 +02:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
<template>
|
2023-02-20 08:40:24 +01:00
|
|
|
<FormSection :first="first">
|
|
|
|
<template #label>{{ i18n.ts['2fa'] }}</template>
|
|
|
|
|
|
|
|
<div v-if="$i" class="_gaps_s">
|
2023-08-28 11:25:31 +02:00
|
|
|
<MkInfo v-if="$i.twoFactorEnabled && $i.twoFactorBackupCodesStock === 'partial'" warn>
|
|
|
|
{{ i18n.ts._2fa.backupCodeUsedWarning }}
|
|
|
|
</MkInfo>
|
|
|
|
<MkInfo v-if="$i.twoFactorEnabled && $i.twoFactorBackupCodesStock === 'none'" warn>
|
|
|
|
{{ i18n.ts._2fa.backupCodesExhaustedWarning }}
|
|
|
|
</MkInfo>
|
|
|
|
|
|
|
|
<MkFolder :defaultOpen="true">
|
2023-02-20 08:40:24 +01:00
|
|
|
<template #icon><i class="ti ti-shield-lock"></i></template>
|
|
|
|
<template #label>{{ i18n.ts.totp }}</template>
|
|
|
|
<template #caption>{{ i18n.ts.totpDescription }}</template>
|
2023-09-04 06:33:38 +02:00
|
|
|
<template #suffix><i v-if="$i.twoFactorEnabled" class="ti ti-check" style="color: var(--success)"></i></template>
|
2023-08-28 11:25:31 +02:00
|
|
|
|
2023-02-20 08:40:24 +01:00
|
|
|
<div v-if="$i.twoFactorEnabled" class="_gaps_s">
|
|
|
|
<div v-text="i18n.ts._2fa.alreadyRegistered"/>
|
|
|
|
<template v-if="$i.securityKeysList.length > 0">
|
|
|
|
<MkButton @click="renewTOTP">{{ i18n.ts._2fa.renewTOTP }}</MkButton>
|
|
|
|
<MkInfo>{{ i18n.ts._2fa.whyTOTPOnlyRenew }}</MkInfo>
|
|
|
|
</template>
|
2023-08-28 11:25:31 +02:00
|
|
|
<MkButton v-else danger @click="unregisterTOTP">{{ i18n.ts.unregister }}</MkButton>
|
2023-02-20 08:40:24 +01:00
|
|
|
</div>
|
|
|
|
|
2023-08-28 11:25:31 +02:00
|
|
|
<MkButton v-else-if="!$i.twoFactorEnabled" primary gradate @click="registerTOTP">{{ i18n.ts._2fa.registerTOTP }}</MkButton>
|
2023-02-20 08:40:24 +01:00
|
|
|
</MkFolder>
|
|
|
|
|
|
|
|
<MkFolder>
|
|
|
|
<template #icon><i class="ti ti-key"></i></template>
|
|
|
|
<template #label>{{ i18n.ts.securityKeyAndPasskey }}</template>
|
|
|
|
<div class="_gaps_s">
|
|
|
|
<MkInfo>
|
2023-09-08 07:05:03 +02:00
|
|
|
{{ i18n.ts._2fa.securityKeyInfo }}
|
2023-02-20 08:40:24 +01:00
|
|
|
</MkInfo>
|
|
|
|
|
2023-09-08 07:05:03 +02:00
|
|
|
<MkInfo v-if="!webAuthnSupported()" warn>
|
2023-02-20 08:40:24 +01:00
|
|
|
{{ i18n.ts._2fa.securityKeyNotSupported }}
|
|
|
|
</MkInfo>
|
|
|
|
|
2023-09-08 07:05:03 +02:00
|
|
|
<MkInfo v-else-if="webAuthnSupported() && !$i.twoFactorEnabled" warn>
|
2023-02-20 08:40:24 +01:00
|
|
|
{{ i18n.ts._2fa.registerTOTPBeforeKey }}
|
|
|
|
</MkInfo>
|
|
|
|
|
|
|
|
<template v-else>
|
|
|
|
<MkButton primary @click="addSecurityKey">{{ i18n.ts._2fa.registerSecurityKey }}</MkButton>
|
|
|
|
<MkFolder v-for="key in $i.securityKeysList" :key="key.id">
|
|
|
|
<template #label>{{ key.name }}</template>
|
|
|
|
<template #suffix><I18n :src="i18n.ts.lastUsedAt"><template #t><MkTime :time="key.lastUsed"/></template></I18n></template>
|
|
|
|
<div class="_buttons">
|
|
|
|
<MkButton @click="renameKey(key)"><i class="ti ti-forms"></i> {{ i18n.ts.rename }}</MkButton>
|
|
|
|
<MkButton danger @click="unregisterKey(key)"><i class="ti ti-trash"></i> {{ i18n.ts.unregister }}</MkButton>
|
|
|
|
</div>
|
|
|
|
</MkFolder>
|
|
|
|
</template>
|
2021-11-28 12:07:37 +01:00
|
|
|
</div>
|
2023-02-20 08:40:24 +01:00
|
|
|
</MkFolder>
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-05-19 13:52:15 +02:00
|
|
|
<MkSwitch :disabled="!$i.twoFactorEnabled || $i.securityKeysList.length === 0" :modelValue="usePasswordLessLogin" @update:modelValue="v => updatePasswordLessLogin(v)">
|
2023-02-20 08:40:24 +01:00
|
|
|
<template #label>{{ i18n.ts.passwordLessLogin }}</template>
|
|
|
|
<template #caption>{{ i18n.ts.passwordLessLoginDescription }}</template>
|
|
|
|
</MkSwitch>
|
2020-01-29 20:37:25 +01:00
|
|
|
</div>
|
2023-02-20 08:40:24 +01:00
|
|
|
</FormSection>
|
2020-01-29 20:37:25 +01:00
|
|
|
</template>
|
|
|
|
|
2022-05-05 15:41:10 +02:00
|
|
|
<script lang="ts" setup>
|
2023-02-20 08:40:24 +01:00
|
|
|
import { ref, defineAsyncComponent } from 'vue';
|
2023-09-08 07:05:03 +02:00
|
|
|
import { supported as webAuthnSupported, create as webAuthnCreate, parseCreationOptionsFromJSON } from '@github/webauthn-json/browser-ponyfill';
|
2022-09-06 11:21:49 +02:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
|
|
|
import MkInfo from '@/components/MkInfo.vue';
|
2023-01-07 06:59:54 +01:00
|
|
|
import MkSwitch from '@/components/MkSwitch.vue';
|
2023-02-20 08:40:24 +01:00
|
|
|
import FormSection from '@/components/form/section.vue';
|
|
|
|
import MkFolder from '@/components/MkFolder.vue';
|
2021-11-11 18:02:25 +01:00
|
|
|
import * as os from '@/os';
|
2022-05-05 15:41:10 +02:00
|
|
|
import { $i } from '@/account';
|
|
|
|
import { i18n } from '@/i18n';
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-20 08:40:24 +01:00
|
|
|
// メモ: 各エンドポイントはmeUpdatedを発行するため、refreshAccountは不要
|
|
|
|
|
|
|
|
withDefaults(defineProps<{
|
|
|
|
first?: boolean;
|
|
|
|
}>(), {
|
|
|
|
first: false,
|
|
|
|
});
|
|
|
|
|
2023-09-08 07:05:03 +02:00
|
|
|
const usePasswordLessLogin = $computed(() => $i?.usePasswordLessLogin ?? false);
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-09-08 07:05:03 +02:00
|
|
|
async function registerTOTP(): Promise<void> {
|
2023-02-20 08:40:24 +01:00
|
|
|
const password = await os.inputText({
|
|
|
|
title: i18n.ts._2fa.registerTOTP,
|
|
|
|
text: i18n.ts._2fa.passwordToTOTP,
|
2022-12-20 03:00:05 +01:00
|
|
|
type: 'password',
|
2023-02-20 08:40:24 +01:00
|
|
|
autocomplete: 'current-password',
|
|
|
|
});
|
|
|
|
if (password.canceled) return;
|
|
|
|
|
|
|
|
const twoFactorData = await os.apiWithDialog('i/2fa/register', {
|
|
|
|
password: password.result,
|
|
|
|
});
|
|
|
|
|
2023-08-28 11:25:31 +02:00
|
|
|
os.popup(defineAsyncComponent(() => import('./2fa.qrdialog.vue')), {
|
|
|
|
twoFactorData,
|
|
|
|
}, {}, 'closed');
|
2022-05-05 15:41:10 +02:00
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-09-08 07:05:03 +02:00
|
|
|
function unregisterTOTP(): void {
|
2022-05-05 15:41:10 +02:00
|
|
|
os.inputText({
|
|
|
|
title: i18n.ts.password,
|
2022-12-20 03:00:05 +01:00
|
|
|
type: 'password',
|
2023-02-20 08:40:24 +01:00
|
|
|
autocomplete: 'current-password',
|
2022-05-05 15:41:10 +02:00
|
|
|
}).then(({ canceled, result: password }) => {
|
|
|
|
if (canceled) return;
|
2023-02-20 08:40:24 +01:00
|
|
|
os.apiWithDialog('i/2fa/unregister', {
|
2022-12-20 03:00:05 +01:00
|
|
|
password: password,
|
2023-02-20 08:40:24 +01:00
|
|
|
}).catch(error => {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
text: error,
|
|
|
|
});
|
2022-05-05 15:41:10 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-09-08 07:05:03 +02:00
|
|
|
function renewTOTP(): void {
|
2023-02-20 08:40:24 +01:00
|
|
|
os.confirm({
|
|
|
|
type: 'question',
|
|
|
|
title: i18n.ts._2fa.renewTOTP,
|
|
|
|
text: i18n.ts._2fa.renewTOTPConfirm,
|
|
|
|
okText: i18n.ts._2fa.renewTOTPOk,
|
|
|
|
cancelText: i18n.ts._2fa.renewTOTPCancel,
|
|
|
|
}).then(({ canceled }) => {
|
|
|
|
if (canceled) return;
|
|
|
|
registerTOTP();
|
2022-05-05 15:41:10 +02:00
|
|
|
});
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-20 08:40:24 +01:00
|
|
|
async function unregisterKey(key) {
|
|
|
|
const confirm = await os.confirm({
|
|
|
|
type: 'question',
|
|
|
|
title: i18n.ts._2fa.removeKey,
|
|
|
|
text: i18n.t('_2fa.removeKeyConfirm', { name: key.name }),
|
2022-06-10 07:36:55 +02:00
|
|
|
});
|
2023-02-20 08:40:24 +01:00
|
|
|
if (confirm.canceled) return;
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-20 08:40:24 +01:00
|
|
|
const password = await os.inputText({
|
2022-05-05 15:41:10 +02:00
|
|
|
title: i18n.ts.password,
|
2022-12-20 03:00:05 +01:00
|
|
|
type: 'password',
|
2023-02-20 08:40:24 +01:00
|
|
|
autocomplete: 'current-password',
|
|
|
|
});
|
|
|
|
if (password.canceled) return;
|
|
|
|
|
|
|
|
await os.apiWithDialog('i/2fa/remove-key', {
|
|
|
|
password: password.result,
|
|
|
|
credentialId: key.id,
|
2022-05-05 15:41:10 +02:00
|
|
|
});
|
2023-02-20 08:40:24 +01:00
|
|
|
os.success();
|
2022-05-05 15:41:10 +02:00
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-20 08:40:24 +01:00
|
|
|
async function renameKey(key) {
|
|
|
|
const name = await os.inputText({
|
|
|
|
title: i18n.ts.rename,
|
|
|
|
default: key.name,
|
|
|
|
type: 'text',
|
|
|
|
minLength: 1,
|
|
|
|
maxLength: 30,
|
|
|
|
});
|
|
|
|
if (name.canceled) return;
|
|
|
|
|
|
|
|
await os.apiWithDialog('i/2fa/update-key', {
|
|
|
|
name: name.result,
|
|
|
|
credentialId: key.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addSecurityKey() {
|
|
|
|
const password = await os.inputText({
|
2022-05-05 15:41:10 +02:00
|
|
|
title: i18n.ts.password,
|
2022-12-20 03:00:05 +01:00
|
|
|
type: 'password',
|
2023-02-20 08:40:24 +01:00
|
|
|
autocomplete: 'current-password',
|
|
|
|
});
|
|
|
|
if (password.canceled) return;
|
|
|
|
|
2023-09-08 07:05:03 +02:00
|
|
|
const registrationOptions = parseCreationOptionsFromJSON({
|
|
|
|
publicKey: await os.apiWithDialog('i/2fa/register-key', {
|
|
|
|
password: password.result,
|
|
|
|
}),
|
2023-02-20 08:40:24 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const name = await os.inputText({
|
|
|
|
title: i18n.ts._2fa.registerSecurityKey,
|
|
|
|
text: i18n.ts._2fa.securityKeyName,
|
|
|
|
type: 'text',
|
|
|
|
minLength: 1,
|
|
|
|
maxLength: 30,
|
|
|
|
});
|
|
|
|
if (name.canceled) return;
|
|
|
|
|
|
|
|
const credential = await os.promiseDialog(
|
2023-09-08 07:05:03 +02:00
|
|
|
webAuthnCreate(registrationOptions),
|
2023-02-20 08:40:24 +01:00
|
|
|
null,
|
|
|
|
() => {}, // ユーザーのキャンセルはrejectなのでエラーダイアログを出さない
|
|
|
|
i18n.ts._2fa.tapSecurityKey,
|
|
|
|
);
|
|
|
|
if (!credential) return;
|
|
|
|
|
|
|
|
await os.apiWithDialog('i/2fa/key-done', {
|
|
|
|
password: password.result,
|
|
|
|
name: name.result,
|
2023-09-08 07:05:03 +02:00
|
|
|
credential: credential.toJSON(),
|
2022-05-05 15:41:10 +02:00
|
|
|
});
|
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-02-20 08:40:24 +01:00
|
|
|
async function updatePasswordLessLogin(value: boolean) {
|
|
|
|
await os.apiWithDialog('i/2fa/password-less', {
|
|
|
|
value,
|
2022-05-05 15:41:10 +02:00
|
|
|
});
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
</script>
|