2019-12-21 23:25:19 +01:00
|
|
|
<template>
|
|
|
|
<div>
|
2020-01-08 17:03:41 +01:00
|
|
|
<figure class="image is-64x64" style="display: inline-block" v-if="icon">
|
2020-01-08 23:22:51 +01:00
|
|
|
<img :src="'storage/icons/' + icon">
|
2019-12-21 23:25:19 +01:00
|
|
|
</figure>
|
2020-01-06 21:45:14 +01:00
|
|
|
<p class="is-size-4 has-text-grey-light">{{ service }}</p>
|
|
|
|
<p class="is-size-6 has-text-grey">{{ account }}</p>
|
2020-01-24 22:37:48 +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">{{ otp }}</p>
|
|
|
|
<ul class="dots" v-if="type === 'totp'">
|
2020-01-22 15:27:33 +01:00
|
|
|
<li v-for="n in 30"></li>
|
|
|
|
</ul>
|
2020-01-24 22:37:48 +01:00
|
|
|
<ul v-else-if="type === 'hotp'">
|
|
|
|
<li>(counter = {{ counter }})</li>
|
|
|
|
</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-01-22 17:32:41 +01:00
|
|
|
service: '',
|
|
|
|
account: '',
|
|
|
|
icon: '',
|
2020-01-24 22:37:48 +01:00
|
|
|
type : '',
|
|
|
|
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
|
|
|
|
|
|
|
methods: {
|
|
|
|
|
2020-01-23 17:07:05 +01:00
|
|
|
getAccount: function(id) {
|
|
|
|
|
|
|
|
this.id = id
|
2020-01-22 17:32:41 +01:00
|
|
|
|
2020-01-23 17:07:05 +01:00
|
|
|
axios.get('api/twofaccounts/' + this.id)
|
2020-01-23 20:51:55 +01:00
|
|
|
.then(async (response) => {
|
2020-01-22 17:32:41 +01:00
|
|
|
|
|
|
|
this.service = response.data.service
|
|
|
|
this.account = response.data.account
|
|
|
|
this.icon = response.data.icon
|
2020-01-24 22:37:48 +01:00
|
|
|
this.type = response.data.type
|
2020-01-22 17:32:41 +01:00
|
|
|
|
2020-01-24 22:37:48 +01:00
|
|
|
this.type === 'totp' ? await this.getTOTP() : await this.getHOTP()
|
2020-01-23 20:51:55 +01:00
|
|
|
this.$parent.isActive = true
|
2020-01-22 17:32:41 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
this.$router.push({ name: 'genericError', params: { err: error.response } });
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-01-24 22:37:48 +01:00
|
|
|
getTOTP: function() {
|
2020-01-22 17:32:41 +01:00
|
|
|
|
2020-01-24 22:37:48 +01:00
|
|
|
axios.get('api/twofaccounts/' + this.id + '/otp').then(response => {
|
|
|
|
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() {
|
|
|
|
|
|
|
|
axios.get('api/twofaccounts/' + this.id + '/otp').then(response => {
|
|
|
|
let spacePosition = Math.ceil(response.data.otp.length / 2);
|
|
|
|
|
|
|
|
this.otp = response.data.otp.substr(0, spacePosition) + " " + response.data.otp.substr(spacePosition);
|
|
|
|
this.counter = response.data.counter;
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
this.service = this.account = this.icon = this.type = ''
|
|
|
|
this.otp = '... ...'
|
2020-01-22 15:27:33 +01:00
|
|
|
this.$el.querySelector('[data-is-active]').removeAttribute('data-is-active');
|
|
|
|
this.$el.querySelector('.dots li:first-child').setAttribute('data-is-active', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
stopLoop: function() {
|
|
|
|
clearInterval(this.timerID)
|
|
|
|
},
|
|
|
|
|
|
|
|
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>
|