2019-12-21 23:25:19 +01:00
|
|
|
<template>
|
|
|
|
<div>
|
2020-02-06 17:03:08 +01:00
|
|
|
<figure class="image is-64x64" :class="{ 'no-icon': !internal_icon }" style="display: inline-block">
|
2020-02-04 17:06:11 +01:00
|
|
|
<img :src="'storage/icons/' + internal_icon" v-if="internal_icon">
|
2019-12-21 23:25:19 +01:00
|
|
|
</figure>
|
2020-02-04 17:06:11 +01:00
|
|
|
<p class="is-size-4 has-text-grey-light has-ellipsis">{{ internal_service }}</p>
|
|
|
|
<p class="is-size-6 has-text-grey has-ellipsis">{{ internal_account }}</p>
|
2020-02-27 16:58:04 +01:00
|
|
|
<p id="otp" class="is-size-1 has-text-white" :title="$t('commons.copy_to_clipboard')" v-clipboard="() => otp.replace(/ /g, '')" v-clipboard:success="clipboardSuccessHandler">{{ displayedOtp }}</p>
|
2020-02-05 23:45:53 +01:00
|
|
|
<ul class="dots" v-if="type === 'totp'">
|
2020-01-22 15:27:33 +01:00
|
|
|
<li v-for="n in 30"></li>
|
|
|
|
</ul>
|
2020-02-05 23:45:53 +01:00
|
|
|
<ul v-else-if="type === 'hotp'">
|
2020-01-27 22:18:45 +01:00
|
|
|
<li>counter: {{ counter }}</li>
|
2020-01-24 22:37:48 +01:00
|
|
|
</ul>
|
2019-12-21 23:25:19 +01:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
2020-01-23 17:07:05 +01:00
|
|
|
id: null,
|
2020-02-04 17:06:11 +01:00
|
|
|
internal_service: '',
|
|
|
|
internal_account: '',
|
|
|
|
internal_uri: '',
|
2020-02-06 12:24:18 +01:00
|
|
|
next_uri: '',
|
2020-02-05 23:45:53 +01:00
|
|
|
internal_icon: '',
|
|
|
|
type: '',
|
2020-01-24 22:37:48 +01:00
|
|
|
otp : '',
|
2020-01-22 15:27:33 +01:00
|
|
|
timerID: null,
|
|
|
|
position: null,
|
2020-01-24 22:37:48 +01:00
|
|
|
counter: null,
|
2019-12-21 23:25:19 +01:00
|
|
|
}
|
|
|
|
},
|
2020-01-22 15:27:33 +01:00
|
|
|
|
2020-02-04 17:06:11 +01:00
|
|
|
props: {
|
|
|
|
service: '',
|
|
|
|
account: '',
|
|
|
|
uri : '',
|
2020-02-05 23:45:53 +01:00
|
|
|
icon: ''
|
2020-02-04 17:06:11 +01:00
|
|
|
},
|
2020-02-27 16:58:04 +01:00
|
|
|
|
|
|
|
computed: {
|
|
|
|
displayedOtp() {
|
|
|
|
return Boolean(Number(appSettings.showTokenAsDot)) ? this.otp.replace(/[0-9]/g, '●') : this.otp
|
|
|
|
}
|
|
|
|
},
|
2020-02-04 17:06:11 +01:00
|
|
|
|
|
|
|
mounted: function() {
|
2020-02-05 23:45:53 +01:00
|
|
|
this.showAccount()
|
2020-02-04 17:06:11 +01:00
|
|
|
},
|
|
|
|
|
2020-01-22 15:27:33 +01:00
|
|
|
methods: {
|
|
|
|
|
2020-02-05 23:45:53 +01:00
|
|
|
async showAccount(id) {
|
2020-01-23 17:07:05 +01:00
|
|
|
|
2020-02-05 23:45:53 +01:00
|
|
|
// 2 possible cases :
|
|
|
|
// - ID is provided so we fetch the account data from db but without the uri.
|
|
|
|
// This prevent the uri (a sensitive data) to transit via http request unnecessarily. In this
|
2020-02-06 12:24:18 +01:00
|
|
|
// case this.type is sent by the backend.
|
|
|
|
// - the URI prop has been set via the create form, we need to preview some OTP before storing the account.
|
2020-02-05 23:45:53 +01:00
|
|
|
// So this.type is set on client side from the provided URI
|
|
|
|
|
2020-01-23 17:07:05 +01:00
|
|
|
this.id = id
|
2020-01-22 17:32:41 +01:00
|
|
|
|
2020-02-05 23:45:53 +01:00
|
|
|
if( this.id || this.uri ) {
|
|
|
|
if( this.id ) {
|
2020-01-22 17:32:41 +01:00
|
|
|
|
2020-02-05 23:45:53 +01:00
|
|
|
const { data } = await this.axios.get('api/twofaccounts/' + this.id)
|
2020-01-22 17:32:41 +01:00
|
|
|
|
2020-02-05 23:45:53 +01:00
|
|
|
this.internal_service = data.service
|
|
|
|
this.internal_account = data.account
|
|
|
|
this.internal_icon = data.icon
|
|
|
|
this.type = data.type
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
this.internal_service = this.service
|
|
|
|
this.internal_account = this.account
|
|
|
|
this.internal_icon = this.icon
|
|
|
|
this.internal_uri = this.uri
|
|
|
|
this.type = this.internal_uri.slice(0, 15 ) === "otpauth://totp/" ? 'totp' : 'hotp';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.type === 'totp' ? await this.getTOTP() : await this.getHOTP()
|
|
|
|
this.$parent.isActive = true
|
|
|
|
}
|
2020-01-22 17:32:41 +01:00
|
|
|
},
|
|
|
|
|
2020-01-24 22:37:48 +01:00
|
|
|
getTOTP: function() {
|
2020-01-22 17:32:41 +01:00
|
|
|
|
2020-02-04 17:06:11 +01:00
|
|
|
this.axios.post('api/twofaccounts/otp', {data: this.id ? this.id : this.internal_uri }).then(response => {
|
2020-01-24 22:37:48 +01:00
|
|
|
let spacePosition = Math.ceil(response.data.otp.length / 2);
|
2020-01-22 15:27:33 +01:00
|
|
|
|
2020-01-24 22:37:48 +01:00
|
|
|
this.otp = response.data.otp.substr(0, spacePosition) + " " + response.data.otp.substr(spacePosition);
|
2020-01-22 15:27:33 +01:00
|
|
|
this.position = response.data.position;
|
|
|
|
|
|
|
|
let dots = this.$el.querySelector('.dots');
|
|
|
|
|
|
|
|
// clear active dots
|
|
|
|
while (dots.querySelector('[data-is-active]')) {
|
|
|
|
dots.querySelector('[data-is-active]').removeAttribute('data-is-active');
|
|
|
|
}
|
|
|
|
|
|
|
|
// set dot at given position as the active one
|
|
|
|
let active = dots.querySelector('li:nth-child(' + (this.position + 1 ) + ')');
|
|
|
|
active.setAttribute('data-is-active', true);
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
this.timerID = setInterval(function() {
|
|
|
|
|
|
|
|
let sibling = active.nextSibling;
|
|
|
|
|
|
|
|
if(active.nextSibling === null) {
|
2020-01-24 22:37:48 +01:00
|
|
|
console.log('no more sibling to activate, we refresh the OTP')
|
2020-01-22 15:27:33 +01:00
|
|
|
self.stopLoop()
|
2020-01-24 22:37:48 +01:00
|
|
|
self.getTOTP();
|
2020-01-22 15:27:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
active.removeAttribute('data-is-active');
|
|
|
|
sibling.setAttribute('data-is-active', true);
|
|
|
|
active = sibling
|
|
|
|
}
|
|
|
|
|
|
|
|
}, 1000);
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-01-24 22:37:48 +01:00
|
|
|
getHOTP: function() {
|
|
|
|
|
2020-02-05 23:45:53 +01:00
|
|
|
this.axios.post('api/twofaccounts/otp', {data: this.id ? this.id : this.internal_uri }).then(response => {
|
2020-01-24 22:37:48 +01:00
|
|
|
let spacePosition = Math.ceil(response.data.otp.length / 2);
|
|
|
|
|
2020-02-06 12:24:18 +01:00
|
|
|
this.otp = response.data.otp.substr(0, spacePosition) + " " + response.data.otp.substr(spacePosition)
|
|
|
|
this.counter = response.data.counter
|
|
|
|
this.next_uri = response.data.nextUri
|
|
|
|
|
2020-01-24 22:37:48 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-01-22 15:27:33 +01:00
|
|
|
clearOTP: function() {
|
|
|
|
this.stopLoop()
|
2020-01-24 22:37:48 +01:00
|
|
|
this.id = this.timerID = this.position = this.counter = null
|
2020-02-05 23:45:53 +01:00
|
|
|
this.internal_service = this.internal_account = this.internal_icon = this.internal_uri = ''
|
2020-01-24 22:37:48 +01:00
|
|
|
this.otp = '... ...'
|
2020-02-06 12:24:18 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
this.$el.querySelector('[data-is-active]').removeAttribute('data-is-active');
|
|
|
|
this.$el.querySelector('.dots li:first-child').setAttribute('data-is-active', true);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
// we do not throw anything
|
|
|
|
}
|
2020-01-22 15:27:33 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
stopLoop: function() {
|
2020-02-06 12:24:18 +01:00
|
|
|
if( this.type === 'totp' ) {
|
|
|
|
clearInterval(this.timerID)
|
|
|
|
}
|
2020-01-22 15:27:33 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
clipboardSuccessHandler ({ value, event }) {
|
|
|
|
console.log('success', value)
|
|
|
|
},
|
|
|
|
|
|
|
|
clipboardErrorHandler ({ value, event }) {
|
|
|
|
console.log('error', value)
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeDestroy () {
|
|
|
|
this.stopLoop()
|
|
|
|
}
|
2019-12-21 23:25:19 +01:00
|
|
|
}
|
|
|
|
</script>
|