2020-01-29 13:37:25 -06:00
|
|
|
<template>
|
2021-11-28 05:07:37 -06:00
|
|
|
<div>
|
2022-05-05 08:41:10 -05:00
|
|
|
<MkButton v-if="!twoFactorData && !$i.twoFactorEnabled" @click="register">{{ i18n.ts._2fa.registerDevice }}</MkButton>
|
2021-11-28 05:07:37 -06:00
|
|
|
<template v-if="$i.twoFactorEnabled">
|
2022-05-05 08:41:10 -05:00
|
|
|
<p>{{ i18n.ts._2fa.alreadyRegistered }}</p>
|
|
|
|
<MkButton @click="unregister">{{ i18n.ts.unregister }}</MkButton>
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2021-11-28 05:07:37 -06:00
|
|
|
<template v-if="supportsCredentials">
|
|
|
|
<hr class="totp-method-sep">
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2022-05-05 08:41:10 -05:00
|
|
|
<h2 class="heading">{{ i18n.ts.securityKey }}</h2>
|
|
|
|
<p>{{ i18n.ts._2fa.securityKeyInfo }}</p>
|
2021-11-28 05:07:37 -06:00
|
|
|
<div class="key-list">
|
|
|
|
<div v-for="key in $i.securityKeysList" class="key">
|
|
|
|
<h3>{{ key.name }}</h3>
|
2022-05-05 08:41:10 -05:00
|
|
|
<div class="last-used">{{ i18n.ts.lastUsed }}<MkTime :time="key.lastUsed"/></div>
|
|
|
|
<MkButton @click="unregisterKey(key)">{{ i18n.ts.unregister }}</MkButton>
|
2020-01-29 13:37:25 -06:00
|
|
|
</div>
|
2021-11-28 05:07:37 -06:00
|
|
|
</div>
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2022-05-05 08:41:10 -05:00
|
|
|
<MkSwitch v-if="$i.securityKeysList.length > 0" v-model="usePasswordLessLogin" @update:modelValue="updatePasswordLessLogin">{{ i18n.ts.passwordLessLogin }}</MkSwitch>
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2022-05-05 08:41:10 -05:00
|
|
|
<MkInfo v-if="registration && registration.error" warn>{{ i18n.ts.error }} {{ registration.error }}</MkInfo>
|
|
|
|
<MkButton v-if="!registration || registration.error" @click="addSecurityKey">{{ i18n.ts._2fa.registerKey }}</MkButton>
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2021-11-28 05:07:37 -06:00
|
|
|
<ol v-if="registration && !registration.error">
|
|
|
|
<li v-if="registration.stage >= 0">
|
2022-05-05 08:41:10 -05:00
|
|
|
{{ i18n.ts.tapSecurityKey }}
|
2022-12-19 20:00:05 -06:00
|
|
|
<MkLoading v-if="registration.saving && registration.stage == 0" :em="true"/>
|
2020-01-29 13:37:25 -06:00
|
|
|
</li>
|
2021-11-28 05:07:37 -06:00
|
|
|
<li v-if="registration.stage >= 1">
|
|
|
|
<MkForm :disabled="registration.stage != 1 || registration.saving">
|
|
|
|
<MkInput v-model="keyName" :max="30">
|
2022-05-05 08:41:10 -05:00
|
|
|
<template #label>{{ i18n.ts.securityKeyName }}</template>
|
2021-11-28 05:07:37 -06:00
|
|
|
</MkInput>
|
2022-05-05 08:41:10 -05:00
|
|
|
<MkButton :disabled="keyName.length == 0" @click="registerKey">{{ i18n.ts.registerSecurityKey }}</MkButton>
|
2022-12-19 20:00:05 -06:00
|
|
|
<MkLoading v-if="registration.saving && registration.stage == 1" :em="true"/>
|
2021-11-28 05:07:37 -06:00
|
|
|
</MkForm>
|
2020-01-29 13:37:25 -06:00
|
|
|
</li>
|
|
|
|
</ol>
|
2021-11-28 05:07:37 -06:00
|
|
|
</template>
|
|
|
|
</template>
|
2022-05-05 08:41:10 -05:00
|
|
|
<div v-if="twoFactorData && !$i.twoFactorEnabled">
|
2021-11-28 05:07:37 -06:00
|
|
|
<ol style="margin: 0; padding: 0 0 0 1em;">
|
|
|
|
<li>
|
2022-05-05 08:41:10 -05:00
|
|
|
<I18n :src="i18n.ts._2fa.step1" tag="span">
|
2021-11-28 05:07:37 -06:00
|
|
|
<template #a>
|
|
|
|
<a href="https://authy.com/" rel="noopener" target="_blank" class="_link">Authy</a>
|
|
|
|
</template>
|
|
|
|
<template #b>
|
|
|
|
<a href="https://support.google.com/accounts/answer/1066447" rel="noopener" target="_blank" class="_link">Google Authenticator</a>
|
|
|
|
</template>
|
|
|
|
</I18n>
|
|
|
|
</li>
|
2022-05-23 14:55:06 -05:00
|
|
|
<li>{{ i18n.ts._2fa.step2 }}<br><img :src="twoFactorData.qr"><p>{{ $ts._2fa.step2Url }}<br>{{ twoFactorData.url }}</p></li>
|
2022-05-14 01:00:15 -05:00
|
|
|
<li>
|
|
|
|
{{ i18n.ts._2fa.step3 }}<br>
|
2022-07-04 03:35:27 -05:00
|
|
|
<MkInput v-model="token" type="text" pattern="^[0-9]{6}$" autocomplete="off" :spellcheck="false"><template #label>{{ i18n.ts.token }}</template></MkInput>
|
2022-05-05 08:41:10 -05:00
|
|
|
<MkButton primary @click="submit">{{ i18n.ts.done }}</MkButton>
|
2021-11-28 05:07:37 -06:00
|
|
|
</li>
|
|
|
|
</ol>
|
2022-05-05 08:41:10 -05:00
|
|
|
<MkInfo>{{ i18n.ts._2fa.step4 }}</MkInfo>
|
2020-01-29 13:37:25 -06:00
|
|
|
</div>
|
2021-11-28 05:07:37 -06:00
|
|
|
</div>
|
2020-01-29 13:37:25 -06:00
|
|
|
</template>
|
|
|
|
|
2022-05-05 08:41:10 -05:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { ref } from 'vue';
|
2021-11-11 11:02:25 -06:00
|
|
|
import { hostname } from '@/config';
|
|
|
|
import { byteify, hexify, stringify } from '@/scripts/2fa';
|
2022-09-06 04:21:49 -05:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
|
|
|
import MkInfo from '@/components/MkInfo.vue';
|
2021-11-11 11:02:25 -06:00
|
|
|
import MkInput from '@/components/form/input.vue';
|
|
|
|
import MkSwitch from '@/components/form/switch.vue';
|
|
|
|
import * as os from '@/os';
|
2022-05-05 08:41:10 -05:00
|
|
|
import { $i } from '@/account';
|
|
|
|
import { i18n } from '@/i18n';
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2022-05-05 08:41:10 -05:00
|
|
|
const twoFactorData = ref<any>(null);
|
|
|
|
const supportsCredentials = ref(!!navigator.credentials);
|
|
|
|
const usePasswordLessLogin = ref($i!.usePasswordLessLogin);
|
|
|
|
const registration = ref<any>(null);
|
|
|
|
const keyName = ref('');
|
|
|
|
const token = ref(null);
|
2020-11-25 06:31:34 -06:00
|
|
|
|
2022-05-05 08:41:10 -05:00
|
|
|
function register() {
|
|
|
|
os.inputText({
|
|
|
|
title: i18n.ts.password,
|
2022-12-19 20:00:05 -06:00
|
|
|
type: 'password',
|
2022-05-05 08:41:10 -05:00
|
|
|
}).then(({ canceled, result: password }) => {
|
|
|
|
if (canceled) return;
|
|
|
|
os.api('i/2fa/register', {
|
2022-12-19 20:00:05 -06:00
|
|
|
password: password,
|
2022-05-05 08:41:10 -05:00
|
|
|
}).then(data => {
|
|
|
|
twoFactorData.value = data;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-11-25 06:31:34 -06:00
|
|
|
|
2022-05-05 08:41:10 -05:00
|
|
|
function unregister() {
|
|
|
|
os.inputText({
|
|
|
|
title: i18n.ts.password,
|
2022-12-19 20:00:05 -06:00
|
|
|
type: 'password',
|
2022-05-05 08:41:10 -05:00
|
|
|
}).then(({ canceled, result: password }) => {
|
|
|
|
if (canceled) return;
|
|
|
|
os.api('i/2fa/unregister', {
|
2022-12-19 20:00:05 -06:00
|
|
|
password: password,
|
2022-05-05 08:41:10 -05:00
|
|
|
}).then(() => {
|
|
|
|
usePasswordLessLogin.value = false;
|
|
|
|
updatePasswordLessLogin();
|
|
|
|
}).then(() => {
|
|
|
|
os.success();
|
|
|
|
$i!.twoFactorEnabled = false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2022-05-05 08:41:10 -05:00
|
|
|
function submit() {
|
|
|
|
os.api('i/2fa/done', {
|
2022-12-19 20:00:05 -06:00
|
|
|
token: token.value,
|
2022-05-05 08:41:10 -05:00
|
|
|
}).then(() => {
|
|
|
|
os.success();
|
|
|
|
$i!.twoFactorEnabled = true;
|
2022-05-26 08:53:09 -05:00
|
|
|
}).catch(err => {
|
2022-05-05 08:41:10 -05:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-05-26 08:53:09 -05:00
|
|
|
text: err,
|
2022-05-05 08:41:10 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2022-05-05 08:41:10 -05:00
|
|
|
function registerKey() {
|
|
|
|
registration.value.saving = true;
|
|
|
|
os.api('i/2fa/key-done', {
|
|
|
|
password: registration.value.password,
|
|
|
|
name: keyName.value,
|
|
|
|
challengeId: registration.value.challengeId,
|
|
|
|
// we convert each 16 bits to a string to serialise
|
|
|
|
clientDataJSON: stringify(registration.value.credential.response.clientDataJSON),
|
2022-12-19 20:00:05 -06:00
|
|
|
attestationObject: hexify(registration.value.credential.response.attestationObject),
|
2022-05-05 08:41:10 -05:00
|
|
|
}).then(key => {
|
|
|
|
registration.value = null;
|
|
|
|
key.lastUsed = new Date();
|
|
|
|
os.success();
|
2022-06-10 00:36:55 -05:00
|
|
|
});
|
2022-05-05 08:41:10 -05:00
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2022-05-05 08:41:10 -05:00
|
|
|
function unregisterKey(key) {
|
|
|
|
os.inputText({
|
|
|
|
title: i18n.ts.password,
|
2022-12-19 20:00:05 -06:00
|
|
|
type: 'password',
|
2022-05-05 08:41:10 -05:00
|
|
|
}).then(({ canceled, result: password }) => {
|
|
|
|
if (canceled) return;
|
|
|
|
return os.api('i/2fa/remove-key', {
|
|
|
|
password,
|
2022-12-19 20:00:05 -06:00
|
|
|
credentialId: key.id,
|
2022-05-05 08:41:10 -05:00
|
|
|
}).then(() => {
|
|
|
|
usePasswordLessLogin.value = false;
|
|
|
|
updatePasswordLessLogin();
|
|
|
|
}).then(() => {
|
|
|
|
os.success();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2022-05-05 08:41:10 -05:00
|
|
|
function addSecurityKey() {
|
|
|
|
os.inputText({
|
|
|
|
title: i18n.ts.password,
|
2022-12-19 20:00:05 -06:00
|
|
|
type: 'password',
|
2022-05-05 08:41:10 -05:00
|
|
|
}).then(({ canceled, result: password }) => {
|
|
|
|
if (canceled) return;
|
|
|
|
os.api('i/2fa/register-key', {
|
2022-12-19 20:00:05 -06:00
|
|
|
password,
|
2022-05-05 08:41:10 -05:00
|
|
|
}).then(reg => {
|
|
|
|
registration.value = {
|
|
|
|
password,
|
|
|
|
challengeId: reg!.challengeId,
|
|
|
|
stage: 0,
|
|
|
|
publicKeyOptions: {
|
|
|
|
challenge: byteify(reg!.challenge, 'base64'),
|
|
|
|
rp: {
|
|
|
|
id: hostname,
|
2022-12-19 20:00:05 -06:00
|
|
|
name: 'Misskey',
|
2022-05-05 08:41:10 -05:00
|
|
|
},
|
|
|
|
user: {
|
|
|
|
id: byteify($i!.id, 'ascii'),
|
|
|
|
name: $i!.username,
|
|
|
|
displayName: $i!.name,
|
|
|
|
},
|
|
|
|
pubKeyCredParams: [{ alg: -7, type: 'public-key' }],
|
|
|
|
timeout: 60000,
|
2022-12-19 20:00:05 -06:00
|
|
|
attestation: 'direct',
|
2022-05-05 08:41:10 -05:00
|
|
|
},
|
2022-12-19 20:00:05 -06:00
|
|
|
saving: true,
|
2022-05-05 08:41:10 -05:00
|
|
|
};
|
|
|
|
return navigator.credentials.create({
|
2022-12-19 20:00:05 -06:00
|
|
|
publicKey: registration.value.publicKeyOptions,
|
2020-01-29 13:37:25 -06:00
|
|
|
});
|
2022-05-05 08:41:10 -05:00
|
|
|
}).then(credential => {
|
|
|
|
registration.value.credential = credential;
|
|
|
|
registration.value.saving = false;
|
|
|
|
registration.value.stage = 1;
|
|
|
|
}).catch(err => {
|
|
|
|
console.warn('Error while registering?', err);
|
|
|
|
registration.value.error = err.message;
|
|
|
|
registration.value.stage = -1;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-11-25 06:31:34 -06:00
|
|
|
|
2022-05-05 08:41:10 -05:00
|
|
|
async function updatePasswordLessLogin() {
|
|
|
|
await os.api('i/2fa/password-less', {
|
2022-12-19 20:00:05 -06:00
|
|
|
value: !!usePasswordLessLogin.value,
|
2022-05-05 08:41:10 -05:00
|
|
|
});
|
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
</script>
|