2020-01-29 20:37:25 +01:00
|
|
|
<template>
|
2023-06-16 01:12:32 +02:00
|
|
|
<FormSection :first="first">
|
|
|
|
<template #label>{{ i18n.ts["2fa"] }}</template>
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2024-03-07 03:06:45 +01:00
|
|
|
<div v-if="me" class="_gaps_s">
|
2023-06-16 01:12:32 +02:00
|
|
|
<MkFolder>
|
|
|
|
<template #icon
|
2023-06-16 04:32:27 +02:00
|
|
|
><i
|
2023-10-17 03:57:20 +02:00
|
|
|
:class="icon('ph-shield-check')"
|
2023-06-16 04:32:27 +02:00
|
|
|
style="margin-right: 0.5rem"
|
|
|
|
></i
|
2023-06-16 01:12:32 +02:00
|
|
|
></template>
|
|
|
|
<template #label>{{ i18n.ts.totp }}</template>
|
|
|
|
<template #caption>{{ i18n.ts.totpDescription }}</template>
|
2024-03-07 03:06:45 +01:00
|
|
|
<div v-if="me.twoFactorEnabled" class="_gaps_s">
|
2023-06-16 01:12:32 +02:00
|
|
|
<div v-text="i18n.ts._2fa.alreadyRegistered" />
|
2024-02-29 13:19:26 +01:00
|
|
|
<MkButton @click="unregisterTOTP"
|
2023-06-16 04:32:27 +02:00
|
|
|
><i
|
2023-10-17 03:57:20 +02:00
|
|
|
:class="icon('ph-shield-slash')"
|
2023-06-16 04:32:27 +02:00
|
|
|
style="margin-right: 0.5rem"
|
|
|
|
></i
|
2023-06-16 01:42:24 +02:00
|
|
|
>{{ i18n.ts.unregister }}</MkButton
|
|
|
|
>
|
2023-04-08 02:01:42 +02:00
|
|
|
</div>
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-06-16 01:12:32 +02:00
|
|
|
<MkButton
|
2024-03-07 03:06:45 +01:00
|
|
|
v-else-if="!twoFactorData && !me.twoFactorEnabled"
|
2023-06-16 01:12:32 +02:00
|
|
|
@click="registerTOTP"
|
|
|
|
>{{ i18n.ts._2fa.registerTOTP }}</MkButton
|
|
|
|
>
|
|
|
|
</MkFolder>
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-06-16 01:12:32 +02:00
|
|
|
<MkFolder>
|
2023-06-16 04:32:27 +02:00
|
|
|
<template #icon
|
2023-10-17 03:57:20 +02:00
|
|
|
><i :class="icon('ph-key')" style="margin-right: 0.5rem"></i
|
2023-06-16 04:32:27 +02:00
|
|
|
></template>
|
2023-06-16 01:12:32 +02:00
|
|
|
<template #label>{{ i18n.ts.securityKeyAndPasskey }}</template>
|
|
|
|
<div class="_gaps_s">
|
|
|
|
<MkInfo>
|
|
|
|
{{ i18n.ts._2fa.securityKeyInfo }}<br />
|
|
|
|
<br />
|
|
|
|
{{ i18n.ts._2fa.chromePasskeyNotSupported }}
|
|
|
|
</MkInfo>
|
|
|
|
|
|
|
|
<MkInfo v-if="!supportsCredentials" warn>
|
|
|
|
{{ i18n.ts._2fa.securityKeyNotSupported }}
|
|
|
|
</MkInfo>
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-06-16 01:12:32 +02:00
|
|
|
<template v-else>
|
2023-06-16 01:42:24 +02:00
|
|
|
<MkButton primary @click="addSecurityKey"
|
2023-06-16 04:32:27 +02:00
|
|
|
><i
|
2023-10-17 03:57:20 +02:00
|
|
|
:class="icon('ph-key')"
|
2023-06-16 04:32:27 +02:00
|
|
|
style="margin-right: 0.5rem"
|
|
|
|
></i
|
2023-06-16 01:42:24 +02:00
|
|
|
>{{ i18n.ts._2fa.registerSecurityKey }}</MkButton
|
|
|
|
>
|
2023-06-16 01:12:32 +02:00
|
|
|
<MkFolder
|
2024-03-07 03:06:45 +01:00
|
|
|
v-for="key in me.securityKeysList"
|
2023-06-16 01:12:32 +02:00
|
|
|
:key="key.id"
|
2023-04-08 02:01:42 +02:00
|
|
|
>
|
2023-06-28 08:01:21 +02:00
|
|
|
<h3>{{ key.name }}</h3>
|
2023-06-28 08:37:24 +02:00
|
|
|
<p>
|
|
|
|
{{ `${i18n.ts.lastUsedDate}: ${key.lastUsed}` }}
|
|
|
|
</p>
|
2023-07-12 08:23:44 +02:00
|
|
|
<div class="_flexList">
|
2023-06-16 01:12:32 +02:00
|
|
|
<MkButton @click="renameKey(key)"
|
2023-10-17 03:57:20 +02:00
|
|
|
><i :class="icon('ph-pencil-line')"></i>
|
2023-06-16 01:12:32 +02:00
|
|
|
{{ i18n.ts.rename }}</MkButton
|
|
|
|
>
|
|
|
|
<MkButton danger @click="unregisterKey(key)"
|
2023-10-17 03:57:20 +02:00
|
|
|
><i :class="icon('ph-trash')"></i>
|
2023-06-16 01:12:32 +02:00
|
|
|
{{ i18n.ts.unregister }}</MkButton
|
|
|
|
>
|
|
|
|
</div>
|
|
|
|
</MkFolder>
|
|
|
|
</template>
|
|
|
|
</div>
|
|
|
|
</MkFolder>
|
|
|
|
|
|
|
|
<MkSwitch
|
|
|
|
:disabled="
|
2024-03-07 03:06:45 +01:00
|
|
|
!me.twoFactorEnabled || me.securityKeysList.length === 0
|
2023-06-16 01:12:32 +02:00
|
|
|
"
|
2023-09-02 01:27:33 +02:00
|
|
|
:model-value="usePasswordLessLogin"
|
2023-06-16 01:12:32 +02:00
|
|
|
@update:modelValue="(v) => updatePasswordLessLogin(v)"
|
|
|
|
>
|
|
|
|
<template #label>{{ i18n.ts.passwordLessLogin }}</template>
|
|
|
|
<template #caption>{{
|
|
|
|
i18n.ts.passwordLessLoginDescription
|
|
|
|
}}</template>
|
|
|
|
</MkSwitch>
|
2023-04-08 02:01:42 +02:00
|
|
|
</div>
|
2023-06-16 01:12:32 +02:00
|
|
|
</FormSection>
|
2020-01-29 20:37:25 +01:00
|
|
|
</template>
|
|
|
|
|
2022-05-05 15:41:10 +02:00
|
|
|
<script lang="ts" setup>
|
2023-09-02 01:27:33 +02:00
|
|
|
import { computed, defineAsyncComponent, ref } from "vue";
|
2023-04-08 02:01:42 +02:00
|
|
|
import { hostname } from "@/config";
|
|
|
|
import { byteify, hexify, stringify } from "@/scripts/2fa";
|
|
|
|
import MkButton from "@/components/MkButton.vue";
|
|
|
|
import MkInfo from "@/components/MkInfo.vue";
|
2023-06-16 01:25:31 +02:00
|
|
|
import MkSwitch from "@/components/form/switch.vue";
|
2023-06-16 01:12:32 +02:00
|
|
|
import FormSection from "@/components/form/section.vue";
|
|
|
|
import MkFolder from "@/components/MkFolder.vue";
|
2023-04-08 02:01:42 +02:00
|
|
|
import * as os from "@/os";
|
2024-03-07 03:06:45 +01:00
|
|
|
import { me } from "@/me";
|
2023-04-08 02:01:42 +02:00
|
|
|
import { i18n } from "@/i18n";
|
2023-10-17 03:57:20 +02:00
|
|
|
import icon from "@/scripts/icon";
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-06-16 01:12:32 +02:00
|
|
|
// メモ: 各エンドポイントはmeUpdatedを発行するため、refreshAccountは不要
|
|
|
|
|
|
|
|
withDefaults(
|
|
|
|
defineProps<{
|
|
|
|
first?: boolean;
|
|
|
|
}>(),
|
|
|
|
{
|
|
|
|
first: false,
|
2023-07-06 03:28:27 +02:00
|
|
|
},
|
2023-06-16 01:12:32 +02:00
|
|
|
);
|
|
|
|
|
2022-05-05 15:41:10 +02:00
|
|
|
const twoFactorData = ref<any>(null);
|
|
|
|
const supportsCredentials = ref(!!navigator.credentials);
|
2024-03-07 03:06:45 +01:00
|
|
|
const usePasswordLessLogin = computed(() => me!.usePasswordLessLogin);
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-06-16 01:12:32 +02:00
|
|
|
async function registerTOTP() {
|
|
|
|
const password = await os.inputText({
|
|
|
|
title: i18n.ts._2fa.registerTOTP,
|
|
|
|
text: i18n.ts.currentPassword,
|
2023-04-08 02:01:42 +02:00
|
|
|
type: "password",
|
2023-06-16 01:12:32 +02:00
|
|
|
autocomplete: "current-password",
|
|
|
|
});
|
|
|
|
if (password.canceled) return;
|
|
|
|
|
|
|
|
const twoFactorData = await os.apiWithDialog("i/2fa/register", {
|
|
|
|
password: password.result,
|
|
|
|
});
|
|
|
|
|
|
|
|
const qrdialog = await new Promise<boolean>((res) => {
|
|
|
|
os.popup(
|
|
|
|
defineAsyncComponent(() => import("./2fa.qrdialog.vue")),
|
|
|
|
{
|
|
|
|
twoFactorData,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ok: () => res(true),
|
|
|
|
cancel: () => res(false),
|
|
|
|
},
|
2023-07-06 03:28:27 +02:00
|
|
|
"closed",
|
2023-06-16 01:12:32 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
if (!qrdialog) return;
|
|
|
|
|
2023-06-25 05:18:49 +02:00
|
|
|
const token = await os.inputText({
|
2023-06-16 01:12:32 +02:00
|
|
|
title: i18n.ts._2fa.step3Title,
|
|
|
|
text: i18n.ts._2fa.step3,
|
|
|
|
autocomplete: "one-time-code",
|
|
|
|
});
|
|
|
|
if (token.canceled) return;
|
|
|
|
|
|
|
|
await os.apiWithDialog("i/2fa/done", {
|
2023-06-25 05:18:49 +02:00
|
|
|
token: token.result,
|
2023-06-16 01:12:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
await os.alert({
|
|
|
|
type: "success",
|
|
|
|
text: i18n.ts._2fa.step4,
|
2022-05-05 15:41:10 +02:00
|
|
|
});
|
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-06-16 01:12:32 +02:00
|
|
|
function unregisterTOTP() {
|
2022-05-05 15:41:10 +02:00
|
|
|
os.inputText({
|
|
|
|
title: i18n.ts.password,
|
2023-04-08 02:01:42 +02:00
|
|
|
type: "password",
|
2023-06-16 01:12:32 +02:00
|
|
|
autocomplete: "current-password",
|
2022-05-05 15:41:10 +02:00
|
|
|
}).then(({ canceled, result: password }) => {
|
|
|
|
if (canceled) return;
|
2023-06-16 01:12:32 +02:00
|
|
|
os.apiWithDialog("i/2fa/unregister", {
|
2023-09-02 01:27:33 +02:00
|
|
|
password,
|
2023-06-16 01:12:32 +02:00
|
|
|
}).catch((error) => {
|
2023-04-08 02:01:42 +02:00
|
|
|
os.alert({
|
|
|
|
type: "error",
|
2023-06-16 01:12:32 +02:00
|
|
|
text: error,
|
2023-04-08 02:01:42 +02:00
|
|
|
});
|
2022-05-05 15:41:10 +02:00
|
|
|
});
|
2023-06-16 01:12:32 +02:00
|
|
|
});
|
2022-05-05 15:41:10 +02:00
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-06-16 01:12:32 +02: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 }),
|
|
|
|
});
|
|
|
|
if (confirm.canceled) return;
|
|
|
|
|
|
|
|
const password = await os.inputText({
|
2022-05-05 15:41:10 +02:00
|
|
|
title: i18n.ts.password,
|
2023-04-08 02:01:42 +02:00
|
|
|
type: "password",
|
2023-06-16 01:12:32 +02:00
|
|
|
autocomplete: "current-password",
|
2022-05-05 15:41:10 +02:00
|
|
|
});
|
2023-06-16 01:12:32 +02:00
|
|
|
if (password.canceled) return;
|
|
|
|
|
|
|
|
await os.apiWithDialog("i/2fa/remove-key", {
|
|
|
|
password: password.result,
|
|
|
|
credentialId: key.id,
|
|
|
|
});
|
|
|
|
os.success();
|
2022-05-05 15:41:10 +02:00
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-06-16 01:12:32 +02: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,
|
2023-04-08 02:01:42 +02:00
|
|
|
type: "password",
|
2023-06-16 01:12:32 +02:00
|
|
|
autocomplete: "current-password",
|
|
|
|
});
|
|
|
|
if (password.canceled) return;
|
|
|
|
|
|
|
|
const challenge: any = await os.apiWithDialog("i/2fa/register-key", {
|
|
|
|
password: password.result,
|
|
|
|
});
|
|
|
|
|
|
|
|
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 webAuthnCreation = navigator.credentials.create({
|
|
|
|
publicKey: {
|
|
|
|
challenge: byteify(challenge.challenge, "base64"),
|
|
|
|
rp: {
|
|
|
|
id: hostname,
|
2023-10-20 02:01:02 +02:00
|
|
|
name: "Firefish",
|
2023-06-16 01:12:32 +02:00
|
|
|
},
|
|
|
|
user: {
|
2024-03-07 03:06:45 +01:00
|
|
|
id: byteify(me!.id, "ascii"),
|
|
|
|
name: me!.username,
|
|
|
|
displayName: me!.name,
|
2023-06-16 01:12:32 +02:00
|
|
|
},
|
|
|
|
pubKeyCredParams: [{ alg: -7, type: "public-key" }],
|
|
|
|
timeout: 60000,
|
|
|
|
attestation: "direct",
|
|
|
|
},
|
|
|
|
}) as Promise<
|
|
|
|
| (PublicKeyCredential & { response: AuthenticatorAttestationResponse })
|
|
|
|
| null
|
|
|
|
>;
|
|
|
|
|
|
|
|
const credential = await os.promiseDialog(
|
|
|
|
webAuthnCreation,
|
|
|
|
null,
|
|
|
|
() => {}, // ユーザーのキャンセルはrejectなのでエラーダイアログを出さない
|
2023-07-06 03:28:27 +02:00
|
|
|
i18n.ts._2fa.tapSecurityKey,
|
2023-06-16 01:12:32 +02:00
|
|
|
);
|
|
|
|
if (!credential) return;
|
|
|
|
|
|
|
|
await os.apiWithDialog("i/2fa/key-done", {
|
|
|
|
password: password.result,
|
|
|
|
name: name.result,
|
|
|
|
challengeId: challenge.challengeId,
|
|
|
|
// we convert each 16 bits to a string to serialise
|
|
|
|
clientDataJSON: stringify(credential.response.clientDataJSON),
|
|
|
|
attestationObject: hexify(credential.response.attestationObject),
|
2022-05-05 15:41:10 +02:00
|
|
|
});
|
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-06-16 01:12:32 +02: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>
|