2018-02-10 08:22:14 +01:00
|
|
|
<template>
|
2019-07-03 13:18:07 +02:00
|
|
|
<form class="mk-signin" :class="{ signing, totpLogin }" @submit.prevent="onSubmit">
|
2018-06-16 00:40:07 +02:00
|
|
|
<div class="avatar" :style="{ backgroundImage: user ? `url('${ user.avatarUrl }')` : null }" v-show="withAvatar"></div>
|
2019-07-03 13:18:07 +02:00
|
|
|
<div class="normal-signin" v-if="!totpLogin">
|
|
|
|
<ui-input v-model="username" type="text" pattern="^[a-zA-Z0-9_]+$" spellcheck="false" autofocus required @input="onUsernameChange">
|
|
|
|
<span>{{ $t('username') }}</span>
|
|
|
|
<template #prefix>@</template>
|
|
|
|
<template #suffix>@{{ host }}</template>
|
|
|
|
</ui-input>
|
2019-07-06 18:38:36 +02:00
|
|
|
<ui-input v-model="password" type="password" :with-password-toggle="true" v-if="!user || user && !user.usePasswordLessLogin" required>
|
2019-07-03 13:18:07 +02:00
|
|
|
<span>{{ $t('password') }}</span>
|
|
|
|
<template #prefix><fa icon="lock"/></template>
|
|
|
|
</ui-input>
|
|
|
|
<ui-button type="submit" :disabled="signing">{{ signing ? $t('signing-in') : $t('@.signin') }}</ui-button>
|
|
|
|
<p v-if="meta && meta.enableTwitterIntegration" style="margin: 8px 0;"><a :href="`${apiUrl}/signin/twitter`"><fa :icon="['fab', 'twitter']"/> {{ $t('signin-with-twitter') }}</a></p>
|
|
|
|
<p v-if="meta && meta.enableGithubIntegration" style="margin: 8px 0;"><a :href="`${apiUrl}/signin/github`"><fa :icon="['fab', 'github']"/> {{ $t('signin-with-github') }}</a></p>
|
|
|
|
<p v-if="meta && meta.enableDiscordIntegration" style="margin: 8px 0;"><a :href="`${apiUrl}/signin/discord`"><fa :icon="['fab', 'discord']"/> {{ $t('signin-with-discord') /* TODO: Make these layouts better */ }}</a></p>
|
|
|
|
</div>
|
|
|
|
<div class="2fa-signin" v-if="totpLogin" :class="{ securityKeys: user && user.securityKeys }">
|
|
|
|
<div v-if="user && user.securityKeys" class="twofa-group tap-group">
|
|
|
|
<p>{{ $t('tap-key') }}</p>
|
|
|
|
<ui-button @click="queryKey" v-if="!queryingKey">
|
|
|
|
{{ $t('@.error.retry') }}
|
|
|
|
</ui-button>
|
|
|
|
</div>
|
|
|
|
<div class="or-hr" v-if="user && user.securityKeys">
|
|
|
|
<p class="or-msg">{{ $t('or') }}</p>
|
|
|
|
</div>
|
|
|
|
<div class="twofa-group totp-group">
|
|
|
|
<p style="margin-bottom:0;">{{ $t('enter-2fa-code') }}</p>
|
2019-07-06 18:38:36 +02:00
|
|
|
<ui-input v-model="password" type="password" :with-password-toggle="true" v-if="user && user.usePasswordLessLogin" required>
|
|
|
|
<span>{{ $t('password') }}</span>
|
|
|
|
<template #prefix><fa icon="lock"/></template>
|
|
|
|
</ui-input>
|
2019-07-03 13:18:07 +02:00
|
|
|
<ui-input v-model="token" type="text" pattern="^[0-9]{6}$" autocomplete="off" spellcheck="false" required>
|
|
|
|
<span>{{ $t('@.2fa') }}</span>
|
|
|
|
<template #prefix><fa icon="gavel"/></template>
|
|
|
|
</ui-input>
|
|
|
|
<ui-button type="submit" :disabled="signing">{{ signing ? $t('signing-in') : $t('@.signin') }}</ui-button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-02-10 08:22:14 +01:00
|
|
|
</form>
|
|
|
|
</template>
|
|
|
|
|
2018-02-11 04:08:43 +01:00
|
|
|
<script lang="ts">
|
2018-02-10 08:22:14 +01:00
|
|
|
import Vue from 'vue';
|
2018-11-08 19:44:35 +01:00
|
|
|
import i18n from '../../../i18n';
|
2018-08-19 15:32:25 +02:00
|
|
|
import { apiUrl, host } from '../../../config';
|
2018-11-11 04:35:30 +01:00
|
|
|
import { toUnicode } from 'punycode';
|
2019-07-03 13:18:07 +02:00
|
|
|
import { hexifyAB } from '../../scripts/2fa';
|
2018-02-10 08:22:14 +01:00
|
|
|
|
|
|
|
export default Vue.extend({
|
2018-11-08 19:44:35 +01:00
|
|
|
i18n: i18n('common/views/components/signin.vue'),
|
2018-12-18 16:40:29 +01:00
|
|
|
|
2018-06-16 00:40:07 +02:00
|
|
|
props: {
|
|
|
|
withAvatar: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: true
|
|
|
|
}
|
|
|
|
},
|
2018-12-18 16:40:29 +01:00
|
|
|
|
2018-02-10 08:22:14 +01:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
signing: false,
|
2018-02-11 04:08:43 +01:00
|
|
|
user: null,
|
|
|
|
username: '',
|
|
|
|
password: '',
|
2018-03-26 09:18:40 +02:00
|
|
|
token: '',
|
|
|
|
apiUrl,
|
2018-11-16 18:13:01 +01:00
|
|
|
host: toUnicode(host),
|
2019-07-03 13:18:07 +02:00
|
|
|
meta: null,
|
|
|
|
totpLogin: false,
|
|
|
|
credential: null,
|
|
|
|
challengeData: null,
|
|
|
|
queryingKey: false,
|
2018-02-10 08:22:14 +01:00
|
|
|
};
|
|
|
|
},
|
2018-12-18 16:40:29 +01:00
|
|
|
|
2018-11-16 18:13:01 +01:00
|
|
|
created() {
|
|
|
|
this.$root.getMeta().then(meta => {
|
|
|
|
this.meta = meta;
|
|
|
|
});
|
|
|
|
},
|
2018-12-18 16:40:29 +01:00
|
|
|
|
2018-02-10 08:22:14 +01:00
|
|
|
methods: {
|
|
|
|
onUsernameChange() {
|
2018-11-09 00:13:34 +01:00
|
|
|
this.$root.api('users/show', {
|
2018-02-10 08:22:14 +01:00
|
|
|
username: this.username
|
|
|
|
}).then(user => {
|
|
|
|
this.user = user;
|
2018-06-16 00:31:35 +02:00
|
|
|
}, () => {
|
|
|
|
this.user = null;
|
2018-02-10 08:22:14 +01:00
|
|
|
});
|
|
|
|
},
|
2018-12-18 16:40:29 +01:00
|
|
|
|
2019-07-03 13:18:07 +02:00
|
|
|
queryKey() {
|
|
|
|
this.queryingKey = true;
|
|
|
|
return navigator.credentials.get({
|
|
|
|
publicKey: {
|
|
|
|
challenge: Buffer.from(
|
|
|
|
this.challengeData.challenge
|
|
|
|
.replace(/\-/g, '+')
|
|
|
|
.replace(/_/g, '/'),
|
|
|
|
'base64'
|
|
|
|
),
|
|
|
|
allowCredentials: this.challengeData.securityKeys.map(key => ({
|
|
|
|
id: Buffer.from(key.id, 'hex'),
|
|
|
|
type: 'public-key',
|
2020-01-26 21:38:40 +01:00
|
|
|
transports: ['usb', 'nfc', 'ble', 'internal']
|
2019-07-03 13:18:07 +02:00
|
|
|
})),
|
|
|
|
timeout: 60 * 1000
|
|
|
|
}
|
2019-07-05 00:48:12 +02:00
|
|
|
}).catch(() => {
|
2019-07-03 13:18:07 +02:00
|
|
|
this.queryingKey = false;
|
|
|
|
return Promise.reject(null);
|
|
|
|
}).then(credential => {
|
|
|
|
this.queryingKey = false;
|
|
|
|
this.signing = true;
|
|
|
|
return this.$root.api('signin', {
|
|
|
|
username: this.username,
|
|
|
|
password: this.password,
|
|
|
|
signature: hexifyAB(credential.response.signature),
|
|
|
|
authenticatorData: hexifyAB(credential.response.authenticatorData),
|
|
|
|
clientDataJSON: hexifyAB(credential.response.clientDataJSON),
|
|
|
|
credentialId: credential.id,
|
|
|
|
challengeId: this.challengeData.challengeId
|
|
|
|
});
|
2019-01-18 08:46:56 +01:00
|
|
|
}).then(res => {
|
2018-11-28 08:19:02 +01:00
|
|
|
localStorage.setItem('i', res.i);
|
2018-02-10 08:22:14 +01:00
|
|
|
location.reload();
|
2019-07-03 13:18:07 +02:00
|
|
|
}).catch(err => {
|
2019-07-05 00:48:12 +02:00
|
|
|
if (err === null) return;
|
2019-04-16 06:05:10 +02:00
|
|
|
this.$root.dialog({
|
|
|
|
type: 'error',
|
|
|
|
text: this.$t('login-failed')
|
|
|
|
});
|
2018-02-10 08:22:14 +01:00
|
|
|
this.signing = false;
|
|
|
|
});
|
2019-07-03 13:18:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onSubmit() {
|
|
|
|
this.signing = true;
|
|
|
|
|
|
|
|
if (!this.totpLogin && this.user && this.user.twoFactorEnabled) {
|
|
|
|
if (window.PublicKeyCredential && this.user.securityKeys) {
|
2019-07-05 00:48:12 +02:00
|
|
|
this.$root.api('signin', {
|
2019-07-03 13:18:07 +02:00
|
|
|
username: this.username,
|
|
|
|
password: this.password
|
|
|
|
}).then(res => {
|
|
|
|
this.totpLogin = true;
|
|
|
|
this.signing = false;
|
|
|
|
this.challengeData = res;
|
|
|
|
return this.queryKey();
|
2019-07-05 00:48:12 +02:00
|
|
|
}).catch(() => {
|
|
|
|
this.$root.dialog({
|
|
|
|
type: 'error',
|
|
|
|
text: this.$t('login-failed')
|
|
|
|
});
|
|
|
|
this.challengeData = null;
|
|
|
|
this.totpLogin = false;
|
|
|
|
this.signing = false;
|
2019-07-03 13:18:07 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.totpLogin = true;
|
|
|
|
this.signing = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.$root.api('signin', {
|
|
|
|
username: this.username,
|
|
|
|
password: this.password,
|
|
|
|
token: this.user && this.user.twoFactorEnabled ? this.token : undefined
|
|
|
|
}).then(res => {
|
|
|
|
localStorage.setItem('i', res.i);
|
|
|
|
location.reload();
|
|
|
|
}).catch(() => {
|
|
|
|
this.$root.dialog({
|
|
|
|
type: 'error',
|
|
|
|
text: this.$t('login-failed')
|
|
|
|
});
|
|
|
|
this.signing = false;
|
|
|
|
});
|
|
|
|
}
|
2018-02-10 08:22:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="stylus" scoped>
|
2018-02-11 04:42:02 +01:00
|
|
|
.mk-signin
|
2018-06-16 00:31:35 +02:00
|
|
|
color #555
|
|
|
|
|
2019-07-03 13:18:07 +02:00
|
|
|
.or-hr,
|
|
|
|
.or-hr .or-msg,
|
|
|
|
.twofa-group,
|
|
|
|
.twofa-group p
|
|
|
|
color var(--text)
|
|
|
|
|
|
|
|
.tap-group > button
|
|
|
|
margin-bottom 1em
|
|
|
|
|
|
|
|
.securityKeys .or-hr
|
|
|
|
&
|
|
|
|
position relative
|
|
|
|
|
|
|
|
.or-msg
|
|
|
|
&:before
|
|
|
|
right 100%
|
|
|
|
margin-right 0.125em
|
|
|
|
|
|
|
|
&:after
|
|
|
|
left 100%
|
|
|
|
margin-left 0.125em
|
|
|
|
|
|
|
|
&:before, &:after
|
|
|
|
content ""
|
|
|
|
position absolute
|
|
|
|
top 50%
|
|
|
|
width 100%
|
|
|
|
height 2px
|
|
|
|
background #555
|
|
|
|
|
|
|
|
&
|
|
|
|
position relative
|
|
|
|
margin auto
|
|
|
|
left 0
|
|
|
|
right 0
|
|
|
|
top 0
|
|
|
|
bottom 0
|
|
|
|
font-size 1.5em
|
|
|
|
height 1.5em
|
|
|
|
width 3em
|
|
|
|
text-align center
|
|
|
|
|
2018-02-10 08:22:14 +01:00
|
|
|
&.signing
|
|
|
|
&, *
|
|
|
|
cursor wait !important
|
|
|
|
|
2018-06-16 00:31:35 +02:00
|
|
|
> .avatar
|
2018-09-03 16:23:50 +02:00
|
|
|
margin 0 auto 0 auto
|
2018-06-16 00:31:35 +02:00
|
|
|
width 64px
|
|
|
|
height 64px
|
|
|
|
background #ddd
|
|
|
|
background-position center
|
|
|
|
background-size cover
|
|
|
|
border-radius 100%
|
2018-02-10 08:22:14 +01:00
|
|
|
|
|
|
|
</style>
|