2023-07-27 05:31:52 +00:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
<template>
|
2023-02-20 07:40:24 +00:00
|
|
|
<FormSection :first="first">
|
|
|
|
<template #label>{{ i18n.ts['2fa'] }}</template>
|
|
|
|
|
|
|
|
<div v-if="$i" class="_gaps_s">
|
2023-08-28 09:25:31 +00: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-09-30 19:53:52 +00:00
|
|
|
<template #icon><i class="ph-shield ph-bold ph-lg-lock"></i></template>
|
2023-02-20 07:40:24 +00:00
|
|
|
<template #label>{{ i18n.ts.totp }}</template>
|
|
|
|
<template #caption>{{ i18n.ts.totpDescription }}</template>
|
2023-09-30 19:53:52 +00:00
|
|
|
<template #suffix><i v-if="$i.twoFactorEnabled" class="ph-check ph-bold ph-lg" style="color: var(--success)"></i></template>
|
2023-08-28 09:25:31 +00:00
|
|
|
|
2023-02-20 07:40:24 +00: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 09:25:31 +00:00
|
|
|
<MkButton v-else danger @click="unregisterTOTP">{{ i18n.ts.unregister }}</MkButton>
|
2023-02-20 07:40:24 +00:00
|
|
|
</div>
|
|
|
|
|
2023-08-28 09:25:31 +00:00
|
|
|
<MkButton v-else-if="!$i.twoFactorEnabled" primary gradate @click="registerTOTP">{{ i18n.ts._2fa.registerTOTP }}</MkButton>
|
2023-02-20 07:40:24 +00:00
|
|
|
</MkFolder>
|
|
|
|
|
|
|
|
<MkFolder>
|
2023-09-30 19:53:52 +00:00
|
|
|
<template #icon><i class="ph-key ph-bold ph-lg"></i></template>
|
2023-02-20 07:40:24 +00:00
|
|
|
<template #label>{{ i18n.ts.securityKeyAndPasskey }}</template>
|
|
|
|
<div class="_gaps_s">
|
|
|
|
<MkInfo>
|
2023-09-08 05:05:03 +00:00
|
|
|
{{ i18n.ts._2fa.securityKeyInfo }}
|
2023-02-20 07:40:24 +00:00
|
|
|
</MkInfo>
|
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
<MkInfo v-if="!webAuthnSupported()" warn>
|
2023-02-20 07:40:24 +00:00
|
|
|
{{ i18n.ts._2fa.securityKeyNotSupported }}
|
|
|
|
</MkInfo>
|
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
<MkInfo v-else-if="webAuthnSupported() && !$i.twoFactorEnabled" warn>
|
2023-02-20 07:40:24 +00: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">
|
2023-09-30 19:53:52 +00:00
|
|
|
<MkButton @click="renameKey(key)"><i class="ph-textbox ph-bold ph-lg"></i> {{ i18n.ts.rename }}</MkButton>
|
|
|
|
<MkButton danger @click="unregisterKey(key)"><i class="ph-trash ph-bold ph-lg"></i> {{ i18n.ts.unregister }}</MkButton>
|
2023-02-20 07:40:24 +00:00
|
|
|
</div>
|
|
|
|
</MkFolder>
|
|
|
|
</template>
|
2021-11-28 11:07:37 +00:00
|
|
|
</div>
|
2023-02-20 07:40:24 +00:00
|
|
|
</MkFolder>
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2023-05-19 11:52:15 +00:00
|
|
|
<MkSwitch :disabled="!$i.twoFactorEnabled || $i.securityKeysList.length === 0" :modelValue="usePasswordLessLogin" @update:modelValue="v => updatePasswordLessLogin(v)">
|
2023-02-20 07:40:24 +00:00
|
|
|
<template #label>{{ i18n.ts.passwordLessLogin }}</template>
|
|
|
|
<template #caption>{{ i18n.ts.passwordLessLoginDescription }}</template>
|
|
|
|
</MkSwitch>
|
2020-01-29 19:37:25 +00:00
|
|
|
</div>
|
2023-02-20 07:40:24 +00:00
|
|
|
</FormSection>
|
2020-01-29 19:37:25 +00:00
|
|
|
</template>
|
|
|
|
|
2022-05-05 13:41:10 +00:00
|
|
|
<script lang="ts" setup>
|
2023-12-21 02:36:45 +00:00
|
|
|
import { defineAsyncComponent, computed } from 'vue';
|
2023-09-08 05:05:03 +00:00
|
|
|
import { supported as webAuthnSupported, create as webAuthnCreate, parseCreationOptionsFromJSON } from '@github/webauthn-json/browser-ponyfill';
|
2022-09-06 09:21:49 +00:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
|
|
|
import MkInfo from '@/components/MkInfo.vue';
|
2023-01-07 05:59:54 +00:00
|
|
|
import MkSwitch from '@/components/MkSwitch.vue';
|
2023-02-20 07:40:24 +00:00
|
|
|
import FormSection from '@/components/form/section.vue';
|
|
|
|
import MkFolder from '@/components/MkFolder.vue';
|
2023-09-19 07:37:43 +00:00
|
|
|
import * as os from '@/os.js';
|
|
|
|
import { $i } from '@/account.js';
|
|
|
|
import { i18n } from '@/i18n.js';
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2023-02-20 07:40:24 +00:00
|
|
|
// メモ: 各エンドポイントはmeUpdatedを発行するため、refreshAccountは不要
|
|
|
|
|
|
|
|
withDefaults(defineProps<{
|
|
|
|
first?: boolean;
|
|
|
|
}>(), {
|
|
|
|
first: false,
|
|
|
|
});
|
|
|
|
|
2023-12-07 05:42:09 +00:00
|
|
|
const usePasswordLessLogin = computed(() => $i?.usePasswordLessLogin ?? false);
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
async function registerTOTP(): Promise<void> {
|
2023-09-22 05:12:33 +00:00
|
|
|
const auth = await os.authenticateDialog();
|
|
|
|
if (auth.canceled) return;
|
2023-02-20 07:40:24 +00:00
|
|
|
|
|
|
|
const twoFactorData = await os.apiWithDialog('i/2fa/register', {
|
2023-09-22 05:12:33 +00:00
|
|
|
password: auth.result.password,
|
|
|
|
token: auth.result.token,
|
2023-02-20 07:40:24 +00:00
|
|
|
});
|
|
|
|
|
2023-08-28 09:25:31 +00:00
|
|
|
os.popup(defineAsyncComponent(() => import('./2fa.qrdialog.vue')), {
|
|
|
|
twoFactorData,
|
|
|
|
}, {}, 'closed');
|
2022-05-05 13:41:10 +00:00
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2023-09-22 05:12:33 +00: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 13:41:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
function renewTOTP(): void {
|
2023-02-20 07:40:24 +00: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 13:41:10 +00:00
|
|
|
});
|
|
|
|
}
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2023-02-20 07:40:24 +00: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 05:36:55 +00:00
|
|
|
});
|
2023-02-20 07:40:24 +00:00
|
|
|
if (confirm.canceled) return;
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2023-09-22 05:12:33 +00:00
|
|
|
const auth = await os.authenticateDialog();
|
|
|
|
if (auth.canceled) return;
|
2023-02-20 07:40:24 +00:00
|
|
|
|
|
|
|
await os.apiWithDialog('i/2fa/remove-key', {
|
2023-09-22 05:12:33 +00:00
|
|
|
password: auth.result.password,
|
|
|
|
token: auth.result.token,
|
2023-02-20 07:40:24 +00:00
|
|
|
credentialId: key.id,
|
2022-05-05 13:41:10 +00:00
|
|
|
});
|
2023-02-20 07:40:24 +00:00
|
|
|
os.success();
|
2022-05-05 13:41:10 +00:00
|
|
|
}
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2023-02-20 07:40:24 +00: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 05:12:33 +00:00
|
|
|
const auth = await os.authenticateDialog();
|
|
|
|
if (auth.canceled) return;
|
2023-02-20 07:40:24 +00:00
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
const registrationOptions = parseCreationOptionsFromJSON({
|
|
|
|
publicKey: await os.apiWithDialog('i/2fa/register-key', {
|
2023-09-22 05:12:33 +00:00
|
|
|
password: auth.result.password,
|
|
|
|
token: auth.result.token,
|
2023-09-08 05:05:03 +00:00
|
|
|
}),
|
2023-02-20 07:40:24 +00: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 05:05:03 +00:00
|
|
|
webAuthnCreate(registrationOptions),
|
2023-02-20 07:40:24 +00:00
|
|
|
null,
|
|
|
|
() => {}, // ユーザーのキャンセルはrejectなのでエラーダイアログを出さない
|
|
|
|
i18n.ts._2fa.tapSecurityKey,
|
|
|
|
);
|
|
|
|
if (!credential) return;
|
|
|
|
|
2023-09-22 05:12:33 +00:00
|
|
|
const auth2 = await os.authenticateDialog();
|
|
|
|
if (auth2.canceled) return;
|
|
|
|
|
2023-02-20 07:40:24 +00:00
|
|
|
await os.apiWithDialog('i/2fa/key-done', {
|
2023-09-22 05:12:33 +00:00
|
|
|
password: auth.result.password,
|
|
|
|
token: auth.result.token,
|
2023-02-20 07:40:24 +00:00
|
|
|
name: name.result,
|
2023-09-08 05:05:03 +00:00
|
|
|
credential: credential.toJSON(),
|
2022-05-05 13:41:10 +00:00
|
|
|
});
|
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2023-02-20 07:40:24 +00:00
|
|
|
async function updatePasswordLessLogin(value: boolean) {
|
|
|
|
await os.apiWithDialog('i/2fa/password-less', {
|
|
|
|
value,
|
2022-05-05 13:41:10 +00:00
|
|
|
});
|
|
|
|
}
|
2020-01-29 19:37:25 +00:00
|
|
|
</script>
|