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