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-11-13 15:45:17 +01:00
|
|
|
<p class="is-size-1 has-text-white is-clickable" :title="$t('commons.copy_to_clipboard')" v-clipboard="() => token.replace(/ /g, '')" v-clipboard:success="clipboardSuccessHandler">{{ displayedToken }}</p>
|
|
|
|
<ul class="dots" v-if="internal_otpType === 'totp'">
|
2020-11-16 09:38:28 +01:00
|
|
|
<li v-for="n in 10"></li>
|
2020-01-22 15:27:33 +01:00
|
|
|
</ul>
|
2020-11-13 15:45:17 +01:00
|
|
|
<ul v-else-if="internal_otpType === 'hotp'">
|
|
|
|
<li>counter: {{ internal_hotpCounter }}</li>
|
2020-01-24 22:37:48 +01:00
|
|
|
</ul>
|
2019-12-21 23:25:19 +01:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
2020-11-13 15:45:17 +01:00
|
|
|
name: 'TokenDisplayer',
|
|
|
|
|
2019-12-21 23:25:19 +01:00
|
|
|
data() {
|
|
|
|
return {
|
2020-01-23 17:07:05 +01:00
|
|
|
id: null,
|
2020-11-13 15:45:17 +01:00
|
|
|
token : '',
|
2020-11-16 09:38:28 +01:00
|
|
|
remainingTimeout: null,
|
|
|
|
firstDotToNextOneTimeout: null,
|
|
|
|
dotToDotInterval: null,
|
2020-01-22 15:27:33 +01:00
|
|
|
position: null,
|
2020-11-16 09:38:28 +01:00
|
|
|
totpTimestamp: null,
|
2020-11-13 15:45:17 +01:00
|
|
|
internal_otpType: '',
|
|
|
|
internal_account: '',
|
|
|
|
internal_service: '',
|
|
|
|
internal_icon: '',
|
|
|
|
internal_hotpCounter: null,
|
2020-11-16 09:38:28 +01:00
|
|
|
lastActiveDot: null,
|
|
|
|
dotToDotCounter: 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: {
|
2020-11-13 15:45:17 +01:00
|
|
|
account : String,
|
|
|
|
algorithm : String,
|
|
|
|
digits : Number,
|
|
|
|
hotpCounter : Number,
|
|
|
|
icon : String,
|
|
|
|
imageLink : String,
|
|
|
|
otpType : String,
|
|
|
|
qrcode : null,
|
|
|
|
secret : String,
|
|
|
|
secretIsBase32Encoded : Number,
|
|
|
|
service : String,
|
|
|
|
totpPeriod : Number,
|
|
|
|
uri : String
|
2020-02-04 17:06:11 +01:00
|
|
|
},
|
2020-02-27 16:58:04 +01:00
|
|
|
|
|
|
|
computed: {
|
2020-11-13 15:45:17 +01:00
|
|
|
displayedToken() {
|
|
|
|
return this.$root.appSettings.showTokenAsDot ? this.token.replace(/[0-9]/g, '●') : this.token
|
2020-02-27 16:58:04 +01:00
|
|
|
}
|
|
|
|
},
|
2020-02-04 17:06:11 +01:00
|
|
|
|
|
|
|
mounted: function() {
|
2020-11-13 15:45:17 +01:00
|
|
|
this.getToken()
|
2020-02-04 17:06:11 +01:00
|
|
|
},
|
|
|
|
|
2020-01-22 15:27:33 +01:00
|
|
|
methods: {
|
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
async getToken(id) {
|
2020-01-23 17:07:05 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
// 3 possible cases :
|
|
|
|
// - Trigger when user ask for a token of an existing account: the ID is provided so we fetch the account data
|
|
|
|
// from db but without the uri.
|
2020-02-05 23:45:53 +01:00
|
|
|
// This prevent the uri (a sensitive data) to transit via http request unnecessarily. In this
|
2020-11-05 22:50:49 +01:00
|
|
|
// case this.otpType is sent by the backend.
|
2020-11-13 15:45:17 +01:00
|
|
|
// - Trigger when user use the Quick Uploader and preview the account: No ID but we have an URI.
|
|
|
|
// - Trigger when user use the Advanced form and preview the account: We should have all OTP parameter
|
|
|
|
// to obtain a token, including Secret and otpType which are required
|
2020-01-22 17:32:41 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
try {
|
|
|
|
this.internal_otpType = this.otpType.toLowerCase()
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
//do nothing
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
this.internal_account = this.account
|
|
|
|
this.internal_service = this.service
|
|
|
|
this.internal_icon = this.icon
|
|
|
|
this.internal_hotpCounter = this.hotpCounter
|
|
|
|
}
|
2020-01-22 17:32:41 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
if( id ) {
|
2020-01-22 17:32:41 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
this.id = id
|
|
|
|
const { data } = await this.axios.get('api/twofaccounts/' + this.id)
|
2020-02-05 23:45:53 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
this.internal_service = data.service
|
|
|
|
this.internal_account = data.account
|
|
|
|
this.internal_icon = data.icon
|
|
|
|
this.internal_otpType = data.otpType
|
|
|
|
}
|
|
|
|
|
|
|
|
// We force the otpType to be based on the uri
|
|
|
|
if( this.uri ) {
|
|
|
|
this.internal_otpType = this.uri.slice(0, 15 ).toLowerCase() === "otpauth://totp/" ? 'totp' : 'hotp';
|
|
|
|
}
|
2020-02-05 23:45:53 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
if( this.id || this.uri || this.secret ) { // minimun required vars to get a token from the backend
|
|
|
|
|
|
|
|
switch(this.internal_otpType) {
|
2020-11-05 22:50:49 +01:00
|
|
|
case 'totp':
|
|
|
|
await this.getTOTP()
|
|
|
|
break;
|
|
|
|
case 'hotp':
|
|
|
|
await this.getHOTP()
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.$router.push({ name: 'genericError', params: { err: this.$t('errors.not_a_supported_otp_type') } });
|
|
|
|
}
|
|
|
|
|
2020-02-05 23:45:53 +01:00
|
|
|
this.$parent.isActive = true
|
|
|
|
}
|
2020-01-22 17:32:41 +01:00
|
|
|
},
|
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2020-01-24 22:37:48 +01:00
|
|
|
getTOTP: function() {
|
2020-01-22 17:32:41 +01:00
|
|
|
|
2020-11-16 09:38:28 +01:00
|
|
|
this.dotToDotCounter = 0
|
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
this.axios.post('/api/twofaccounts/otp', { id: this.id, otp: this.$props }).then(response => {
|
2020-11-16 09:38:28 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
let spacePosition = Math.ceil(response.data.token.length / 2);
|
2020-01-22 15:27:33 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
this.token = response.data.token.substr(0, spacePosition) + " " + response.data.token.substr(spacePosition);
|
2020-11-16 09:38:28 +01:00
|
|
|
this.totpTimestamp = response.data.totpTimestamp; // the timestamp used to generate the token
|
|
|
|
this.position = this.totpTimestamp % response.data.totpPeriod // The position of the totp timestamp in the current period
|
2020-01-22 15:27:33 +01:00
|
|
|
|
2020-11-16 09:38:28 +01:00
|
|
|
// Hide all dots
|
2020-01-22 15:27:33 +01:00
|
|
|
let dots = this.$el.querySelector('.dots');
|
|
|
|
|
|
|
|
while (dots.querySelector('[data-is-active]')) {
|
|
|
|
dots.querySelector('[data-is-active]').removeAttribute('data-is-active');
|
|
|
|
}
|
|
|
|
|
2020-11-16 09:38:28 +01:00
|
|
|
// Activate the dot at the totp position
|
|
|
|
let relativePosition = (this.position * 10) / response.data.totpPeriod
|
|
|
|
let dotNumber = (Math.floor(relativePosition) +1)
|
|
|
|
|
|
|
|
this.lastActiveDot = dots.querySelector('li:nth-child(' + dotNumber + ')');
|
|
|
|
this.lastActiveDot.setAttribute('data-is-active', true);
|
|
|
|
|
|
|
|
// Main timeout which run all over the totpPeriod.
|
|
|
|
|
|
|
|
let remainingTimeBeforeEndOfPeriod = response.data.totpPeriod - this.position
|
|
|
|
let self = this; // because of the setInterval/setTimeout closures
|
|
|
|
|
|
|
|
this.remainingTimeout = setTimeout(function() {
|
|
|
|
self.stopLoop()
|
|
|
|
self.getTOTP();
|
|
|
|
}, remainingTimeBeforeEndOfPeriod*1000);
|
|
|
|
|
|
|
|
// During the remainingTimeout countdown we have to show a next dot every durationBetweenTwoDots seconds
|
|
|
|
// except for the first next dot
|
2020-01-22 15:27:33 +01:00
|
|
|
|
2020-11-16 09:38:28 +01:00
|
|
|
let durationBetweenTwoDots = response.data.totpPeriod / 10 // we have 10 dots
|
|
|
|
let firstDotTimeout = (Math.ceil(this.position / durationBetweenTwoDots) * durationBetweenTwoDots) - this.position
|
2020-01-22 15:27:33 +01:00
|
|
|
|
2020-11-16 09:38:28 +01:00
|
|
|
this.firstDotToNextOneTimeout = setTimeout(function() {
|
2020-01-22 15:27:33 +01:00
|
|
|
|
2020-11-16 09:38:28 +01:00
|
|
|
if( firstDotTimeout > 0 ) {
|
|
|
|
self.activeNextDot()
|
|
|
|
dotNumber += 1
|
|
|
|
}
|
2020-01-22 15:27:33 +01:00
|
|
|
|
2020-11-16 09:38:28 +01:00
|
|
|
self.dotToDotInterval = setInterval(function() {
|
2020-01-22 15:27:33 +01:00
|
|
|
|
2020-11-16 09:38:28 +01:00
|
|
|
self.dotToDotCounter += 1
|
|
|
|
self.activeNextDot()
|
|
|
|
dotNumber += 1
|
|
|
|
|
|
|
|
}, durationBetweenTwoDots*1000)
|
|
|
|
|
|
|
|
}, firstDotTimeout*1000)
|
2020-01-22 15:27:33 +01:00
|
|
|
})
|
2020-11-05 22:54:06 +01:00
|
|
|
.catch(error => {
|
|
|
|
this.$router.push({ name: 'genericError', params: { err: error.response } });
|
|
|
|
});
|
2020-01-22 15:27:33 +01:00
|
|
|
},
|
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2020-01-24 22:37:48 +01:00
|
|
|
getHOTP: function() {
|
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
this.axios.post('/api/twofaccounts/otp', { id: this.id, otp: this.$props }).then(response => {
|
|
|
|
let spacePosition = Math.ceil(response.data.token.length / 2);
|
2020-01-24 22:37:48 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
this.token = response.data.token.substr(0, spacePosition) + " " + response.data.token.substr(spacePosition)
|
2020-02-06 12:24:18 +01:00
|
|
|
|
2020-11-14 18:55:10 +01:00
|
|
|
// returned counter & uri are incremented
|
|
|
|
this.$emit('increment-hotp', { nextHotpCounter: response.data.hotpCounter, nextUri: response.data.uri })
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2020-01-24 22:37:48 +01:00
|
|
|
})
|
2020-11-05 22:54:06 +01:00
|
|
|
.catch(error => {
|
|
|
|
this.$router.push({ name: 'genericError', params: { err: error.response } });
|
|
|
|
});
|
2020-01-24 22:37:48 +01:00
|
|
|
},
|
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2020-01-22 15:27:33 +01:00
|
|
|
clearOTP: function() {
|
|
|
|
this.stopLoop()
|
2020-11-16 09:38:28 +01:00
|
|
|
this.id = this.remainingTimeout = this.dotToDotInterval = this.firstDotToNextOneTimeout = this.position = this.internal_hotpCounter = null
|
2020-11-13 15:45:17 +01:00
|
|
|
this.internal_service = this.internal_account = this.internal_icon = this.internal_otpType = ''
|
|
|
|
this.token = '... ...'
|
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
|
|
|
},
|
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2020-01-22 15:27:33 +01:00
|
|
|
stopLoop: function() {
|
2020-11-13 15:45:17 +01:00
|
|
|
if( this.internal_otpType === 'totp' ) {
|
2020-11-16 09:38:28 +01:00
|
|
|
clearTimeout(this.remainingTimeout)
|
|
|
|
clearTimeout(this.firstDotToNextOneTimeout)
|
|
|
|
clearInterval(this.dotToDotInterval)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
activeNextDot: function() {
|
|
|
|
if(this.lastActiveDot.nextSibling !== null) {
|
|
|
|
|
|
|
|
this.lastActiveDot.removeAttribute('data-is-active')
|
|
|
|
this.lastActiveDot.nextSibling.setAttribute('data-is-active', true)
|
|
|
|
this.lastActiveDot = this.lastActiveDot.nextSibling
|
2020-02-06 12:24:18 +01:00
|
|
|
}
|
2020-01-22 15:27:33 +01:00
|
|
|
},
|
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2020-01-22 15:27:33 +01:00
|
|
|
clipboardSuccessHandler ({ value, event }) {
|
2020-03-23 21:41:59 +01:00
|
|
|
|
2020-10-08 15:38:36 +02:00
|
|
|
if(this.$root.appSettings.kickUserAfter == -1) {
|
|
|
|
this.appLogout()
|
|
|
|
}
|
|
|
|
else if(this.$root.appSettings.closeTokenOnCopy) {
|
2020-03-23 21:41:59 +01:00
|
|
|
this.$parent.isActive = false
|
|
|
|
this.clearOTP()
|
|
|
|
}
|
2020-09-25 23:39:31 +02:00
|
|
|
|
|
|
|
this.$notify({ type: 'is-success', text: this.$t('commons.copied_to_clipboard') })
|
2020-01-22 15:27:33 +01:00
|
|
|
},
|
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2020-01-22 15:27:33 +01:00
|
|
|
clipboardErrorHandler ({ value, event }) {
|
|
|
|
console.log('error', value)
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeDestroy () {
|
|
|
|
this.stopLoop()
|
|
|
|
}
|
2019-12-21 23:25:19 +01:00
|
|
|
}
|
|
|
|
</script>
|