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-12-21 03:36:45 +01:00
|
|
|
import { defineAsyncComponent, computed } 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';
|
2023-09-19 09:37:43 +02:00
|
|
|
import * as os from '@/os.js';
|
2024-01-04 07:30:40 +01:00
|
|
|
import { signinRequired } from '@/account.js';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { i18n } from '@/i18n.js';
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2024-01-04 07:30:40 +01:00
|
|
|
const $i = signinRequired();
|
|
|
|
|
2023-02-20 08:40:24 +01:00
|
|
|
// メモ: 各エンドポイントはmeUpdatedを発行するため、refreshAccountは不要
|
|
|
|
|
|
|
|
withDefaults(defineProps<{
|
|
|
|
first?: boolean;
|
|
|
|
}>(), {
|
|
|
|
first: false,
|
|
|
|
});
|
|
|
|
|
2024-01-04 07:30:40 +01: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-09-22 07:12:33 +02:00
|
|
|
const auth = await os.authenticateDialog();
|
|
|
|
if (auth.canceled) return;
|
2023-02-20 08:40:24 +01:00
|
|
|
|
|
|
|
const twoFactorData = await os.apiWithDialog('i/2fa/register', {
|
2023-09-22 07:12:33 +02:00
|
|
|
password: auth.result.password,
|
|
|
|
token: auth.result.token,
|
2023-02-20 08:40:24 +01:00
|
|
|
});
|
|
|
|
|
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-22 07:12:33 +02:00
|
|
|
async function unregisterTOTP(): Promise<void> {
|
|
|
|
const auth = await os.authenticateDialog();
|
|
|
|
if (auth.canceled) return;
|
|
|
|
|
|
|
|
os.apiWithDialog('i/2fa/unregister', {
|
|
|
|
password: auth.result.password,
|
|
|
|
token: auth.result.token,
|
|
|
|
}).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,
|
2024-01-20 00:11:59 +01:00
|
|
|
text: i18n.tsx._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-09-22 07:12:33 +02:00
|
|
|
const auth = await os.authenticateDialog();
|
|
|
|
if (auth.canceled) return;
|
2023-02-20 08:40:24 +01:00
|
|
|
|
|
|
|
await os.apiWithDialog('i/2fa/remove-key', {
|
2023-09-22 07:12:33 +02:00
|
|
|
password: auth.result.password,
|
|
|
|
token: auth.result.token,
|
2023-02-20 08:40:24 +01:00
|
|
|
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() {
|
2023-09-22 07:12:33 +02:00
|
|
|
const auth = await os.authenticateDialog();
|
|
|
|
if (auth.canceled) return;
|
2023-02-20 08:40:24 +01:00
|
|
|
|
2023-09-08 07:05:03 +02:00
|
|
|
const registrationOptions = parseCreationOptionsFromJSON({
|
|
|
|
publicKey: await os.apiWithDialog('i/2fa/register-key', {
|
2023-09-22 07:12:33 +02:00
|
|
|
password: auth.result.password,
|
|
|
|
token: auth.result.token,
|
2023-09-08 07:05:03 +02:00
|
|
|
}),
|
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;
|
|
|
|
|
2023-09-22 07:12:33 +02:00
|
|
|
const auth2 = await os.authenticateDialog();
|
|
|
|
if (auth2.canceled) return;
|
|
|
|
|
2023-02-20 08:40:24 +01:00
|
|
|
await os.apiWithDialog('i/2fa/key-done', {
|
2023-09-22 07:12:33 +02:00
|
|
|
password: auth.result.password,
|
|
|
|
token: auth.result.token,
|
2023-02-20 08:40:24 +01:00
|
|
|
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>
|