2020-01-30 04:37:25 +09:00
|
|
|
<template>
|
2021-11-28 20:07:37 +09:00
|
|
|
<div>
|
|
|
|
<MkButton v-if="!data && !$i.twoFactorEnabled" @click="register">{{ $ts._2fa.registerDevice }}</MkButton>
|
|
|
|
<template v-if="$i.twoFactorEnabled">
|
|
|
|
<p>{{ $ts._2fa.alreadyRegistered }}</p>
|
|
|
|
<MkButton @click="unregister">{{ $ts.unregister }}</MkButton>
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2021-11-28 20:07:37 +09:00
|
|
|
<template v-if="supportsCredentials">
|
|
|
|
<hr class="totp-method-sep">
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2021-11-28 20:07:37 +09:00
|
|
|
<h2 class="heading">{{ $ts.securityKey }}</h2>
|
|
|
|
<p>{{ $ts._2fa.securityKeyInfo }}</p>
|
|
|
|
<div class="key-list">
|
|
|
|
<div v-for="key in $i.securityKeysList" class="key">
|
|
|
|
<h3>{{ key.name }}</h3>
|
|
|
|
<div class="last-used">{{ $ts.lastUsed }}<MkTime :time="key.lastUsed"/></div>
|
|
|
|
<MkButton @click="unregisterKey(key)">{{ $ts.unregister }}</MkButton>
|
2020-01-30 04:37:25 +09:00
|
|
|
</div>
|
2021-11-28 20:07:37 +09:00
|
|
|
</div>
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2021-11-28 20:07:37 +09:00
|
|
|
<MkSwitch v-if="$i.securityKeysList.length > 0" v-model="usePasswordLessLogin" @update:modelValue="updatePasswordLessLogin">{{ $ts.passwordLessLogin }}</MkSwitch>
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2021-11-28 20:07:37 +09:00
|
|
|
<MkInfo v-if="registration && registration.error" warn>{{ $ts.error }} {{ registration.error }}</MkInfo>
|
|
|
|
<MkButton v-if="!registration || registration.error" @click="addSecurityKey">{{ $ts._2fa.registerKey }}</MkButton>
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2021-11-28 20:07:37 +09:00
|
|
|
<ol v-if="registration && !registration.error">
|
|
|
|
<li v-if="registration.stage >= 0">
|
|
|
|
{{ $ts.tapSecurityKey }}
|
|
|
|
<i v-if="registration.saving && registration.stage == 0" class="fas fa-spinner fa-pulse fa-fw"></i>
|
2020-01-30 04:37:25 +09:00
|
|
|
</li>
|
2021-11-28 20:07:37 +09:00
|
|
|
<li v-if="registration.stage >= 1">
|
|
|
|
<MkForm :disabled="registration.stage != 1 || registration.saving">
|
|
|
|
<MkInput v-model="keyName" :max="30">
|
|
|
|
<template #label>{{ $ts.securityKeyName }}</template>
|
|
|
|
</MkInput>
|
|
|
|
<MkButton :disabled="keyName.length == 0" @click="registerKey">{{ $ts.registerSecurityKey }}</MkButton>
|
|
|
|
<i v-if="registration.saving && registration.stage == 1" class="fas fa-spinner fa-pulse fa-fw"></i>
|
|
|
|
</MkForm>
|
2020-01-30 04:37:25 +09:00
|
|
|
</li>
|
|
|
|
</ol>
|
2021-11-28 20:07:37 +09:00
|
|
|
</template>
|
|
|
|
</template>
|
|
|
|
<div v-if="data && !$i.twoFactorEnabled">
|
|
|
|
<ol style="margin: 0; padding: 0 0 0 1em;">
|
|
|
|
<li>
|
|
|
|
<I18n :src="$ts._2fa.step1" tag="span">
|
|
|
|
<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>
|
|
|
|
<li>{{ $ts._2fa.step2 }}<br><img :src="data.qr"></li>
|
|
|
|
<li>{{ $ts._2fa.step3 }}<br>
|
|
|
|
<MkInput v-model="token" type="text" pattern="^[0-9]{6}$" autocomplete="off" spellcheck="false"><template #label>{{ $ts.token }}</template></MkInput>
|
|
|
|
<MkButton primary @click="submit">{{ $ts.done }}</MkButton>
|
|
|
|
</li>
|
|
|
|
</ol>
|
|
|
|
<MkInfo>{{ $ts._2fa.step4 }}</MkInfo>
|
2020-01-30 04:37:25 +09:00
|
|
|
</div>
|
2021-11-28 20:07:37 +09:00
|
|
|
</div>
|
2020-01-30 04:37:25 +09:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2020-10-17 20:12:00 +09:00
|
|
|
import { defineComponent } from 'vue';
|
2021-11-12 02:02:25 +09:00
|
|
|
import { hostname } from '@/config';
|
|
|
|
import { byteify, hexify, stringify } from '@/scripts/2fa';
|
|
|
|
import MkButton from '@/components/ui/button.vue';
|
|
|
|
import MkInfo from '@/components/ui/info.vue';
|
|
|
|
import MkInput from '@/components/form/input.vue';
|
|
|
|
import MkSwitch from '@/components/form/switch.vue';
|
|
|
|
import * as os from '@/os';
|
|
|
|
import * as symbols from '@/symbols';
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2020-10-17 20:12:00 +09:00
|
|
|
export default defineComponent({
|
2020-01-30 04:37:25 +09:00
|
|
|
components: {
|
2020-01-30 18:31:08 +09:00
|
|
|
MkButton, MkInfo, MkInput, MkSwitch
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
data: null,
|
|
|
|
supportsCredentials: !!navigator.credentials,
|
2020-12-19 10:55:52 +09:00
|
|
|
usePasswordLessLogin: this.$i.usePasswordLessLogin,
|
2020-01-30 04:37:25 +09:00
|
|
|
registration: null,
|
|
|
|
keyName: '',
|
|
|
|
token: null,
|
|
|
|
};
|
|
|
|
},
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
methods: {
|
|
|
|
register() {
|
2021-11-18 18:45:58 +09:00
|
|
|
os.inputText({
|
2020-12-26 10:47:36 +09:00
|
|
|
title: this.$ts.password,
|
2021-11-18 18:45:58 +09:00
|
|
|
type: 'password'
|
2020-01-30 04:37:25 +09:00
|
|
|
}).then(({ canceled, result: password }) => {
|
|
|
|
if (canceled) return;
|
2020-10-17 20:12:00 +09:00
|
|
|
os.api('i/2fa/register', {
|
2020-01-30 04:37:25 +09:00
|
|
|
password: password
|
|
|
|
}).then(data => {
|
|
|
|
this.data = data;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
unregister() {
|
2021-11-18 18:45:58 +09:00
|
|
|
os.inputText({
|
2020-12-26 10:47:36 +09:00
|
|
|
title: this.$ts.password,
|
2021-11-18 18:45:58 +09:00
|
|
|
type: 'password'
|
2020-01-30 04:37:25 +09:00
|
|
|
}).then(({ canceled, result: password }) => {
|
|
|
|
if (canceled) return;
|
2020-10-17 20:12:00 +09:00
|
|
|
os.api('i/2fa/unregister', {
|
2020-01-30 04:37:25 +09:00
|
|
|
password: password
|
|
|
|
}).then(() => {
|
|
|
|
this.usePasswordLessLogin = false;
|
|
|
|
this.updatePasswordLessLogin();
|
|
|
|
}).then(() => {
|
2020-10-17 20:12:00 +09:00
|
|
|
os.success();
|
2020-12-19 10:55:52 +09:00
|
|
|
this.$i.twoFactorEnabled = false;
|
2020-01-30 04:37:25 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
submit() {
|
2020-10-17 20:12:00 +09:00
|
|
|
os.api('i/2fa/done', {
|
2020-01-30 04:37:25 +09:00
|
|
|
token: this.token
|
|
|
|
}).then(() => {
|
2020-10-17 20:12:00 +09:00
|
|
|
os.success();
|
2020-12-19 10:55:52 +09:00
|
|
|
this.$i.twoFactorEnabled = true;
|
2020-01-30 04:37:25 +09:00
|
|
|
}).catch(e => {
|
2021-11-18 18:45:58 +09:00
|
|
|
os.alert({
|
2020-01-30 04:37:25 +09:00
|
|
|
type: 'error',
|
2020-10-17 20:12:00 +09:00
|
|
|
text: e
|
2020-01-30 04:37:25 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
registerKey() {
|
|
|
|
this.registration.saving = true;
|
2020-10-17 20:12:00 +09:00
|
|
|
os.api('i/2fa/key-done', {
|
2020-01-30 04:37:25 +09:00
|
|
|
password: this.registration.password,
|
|
|
|
name: this.keyName,
|
|
|
|
challengeId: this.registration.challengeId,
|
|
|
|
// we convert each 16 bits to a string to serialise
|
2020-05-09 08:20:22 +09:00
|
|
|
clientDataJSON: stringify(this.registration.credential.response.clientDataJSON),
|
|
|
|
attestationObject: hexify(this.registration.credential.response.attestationObject)
|
2020-01-30 04:37:25 +09:00
|
|
|
}).then(key => {
|
|
|
|
this.registration = null;
|
|
|
|
key.lastUsed = new Date();
|
2020-10-17 20:12:00 +09:00
|
|
|
os.success();
|
2020-01-30 04:37:25 +09:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
unregisterKey(key) {
|
2021-11-18 18:45:58 +09:00
|
|
|
os.inputText({
|
2020-12-26 10:47:36 +09:00
|
|
|
title: this.$ts.password,
|
2021-11-18 18:45:58 +09:00
|
|
|
type: 'password'
|
2020-01-30 04:37:25 +09:00
|
|
|
}).then(({ canceled, result: password }) => {
|
|
|
|
if (canceled) return;
|
2020-10-17 20:12:00 +09:00
|
|
|
return os.api('i/2fa/remove-key', {
|
2020-01-30 04:37:25 +09:00
|
|
|
password,
|
|
|
|
credentialId: key.id
|
|
|
|
}).then(() => {
|
|
|
|
this.usePasswordLessLogin = false;
|
|
|
|
this.updatePasswordLessLogin();
|
|
|
|
}).then(() => {
|
2020-10-17 20:12:00 +09:00
|
|
|
os.success();
|
2020-01-30 04:37:25 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
addSecurityKey() {
|
2021-11-18 18:45:58 +09:00
|
|
|
os.inputText({
|
2020-12-26 10:47:36 +09:00
|
|
|
title: this.$ts.password,
|
2021-11-18 18:45:58 +09:00
|
|
|
type: 'password'
|
2020-01-30 04:37:25 +09:00
|
|
|
}).then(({ canceled, result: password }) => {
|
|
|
|
if (canceled) return;
|
2020-10-17 20:12:00 +09:00
|
|
|
os.api('i/2fa/register-key', {
|
2020-01-30 04:37:25 +09:00
|
|
|
password
|
|
|
|
}).then(registration => {
|
|
|
|
this.registration = {
|
|
|
|
password,
|
|
|
|
challengeId: registration.challengeId,
|
|
|
|
stage: 0,
|
|
|
|
publicKeyOptions: {
|
2020-05-09 08:20:22 +09:00
|
|
|
challenge: byteify(registration.challenge, 'base64'),
|
2020-01-30 04:37:25 +09:00
|
|
|
rp: {
|
|
|
|
id: hostname,
|
|
|
|
name: 'Misskey'
|
|
|
|
},
|
|
|
|
user: {
|
2020-12-19 10:55:52 +09:00
|
|
|
id: byteify(this.$i.id, 'ascii'),
|
|
|
|
name: this.$i.username,
|
|
|
|
displayName: this.$i.name,
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
2020-05-06 18:59:58 +09:00
|
|
|
pubKeyCredParams: [{ alg: -7, type: 'public-key' }],
|
2020-01-30 04:37:25 +09:00
|
|
|
timeout: 60000,
|
|
|
|
attestation: 'direct'
|
|
|
|
},
|
|
|
|
saving: true
|
|
|
|
};
|
|
|
|
return navigator.credentials.create({
|
|
|
|
publicKey: this.registration.publicKeyOptions
|
|
|
|
});
|
|
|
|
}).then(credential => {
|
|
|
|
this.registration.credential = credential;
|
|
|
|
this.registration.saving = false;
|
|
|
|
this.registration.stage = 1;
|
|
|
|
}).catch(err => {
|
|
|
|
console.warn('Error while registering?', err);
|
|
|
|
this.registration.error = err.message;
|
|
|
|
this.registration.stage = -1;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
updatePasswordLessLogin() {
|
2020-10-17 20:12:00 +09:00
|
|
|
os.api('i/2fa/password-less', {
|
2020-01-30 04:37:25 +09:00
|
|
|
value: !!this.usePasswordLessLogin
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|