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">
|
2023-01-30 14:28:56 +01:00
|
|
|
<img :src="$root.appConfig.subdirectory + '/storage/icons/' + internal_icon" v-if="internal_icon" :alt="$t('twofaccounts.icon_to_illustrate_the_account')">
|
2019-12-21 23:25:19 +01:00
|
|
|
</figure>
|
2023-02-01 17:21:55 +01:00
|
|
|
<p class="is-size-4 has-ellipsis" :class="$root.showDarkMode ? 'has-text-grey-light' : 'has-text-grey'">{{ internal_service }}</p>
|
|
|
|
<p class="is-size-6 has-ellipsis" :class="$root.showDarkMode ? 'has-text-grey' : 'has-text-grey-light'">{{ internal_account }}</p>
|
2022-10-12 11:30:20 +02:00
|
|
|
<p>
|
2023-02-01 17:21:55 +01:00
|
|
|
<span role="log" ref="otp" tabindex="0" class="otp is-size-1 is-clickable px-3" :class="$root.showDarkMode ? 'has-text-white' : 'has-text-grey-dark'" @click="copyOTP(internal_password)" @keyup.enter="copyOTP(internal_password)" :title="$t('commons.copy_to_clipboard')">
|
2022-10-12 11:30:20 +02:00
|
|
|
{{ displayedOtp }}
|
|
|
|
</span>
|
|
|
|
</p>
|
2022-06-25 17:35:29 +02:00
|
|
|
<ul class="dots" v-show="isTimeBased(internal_otp_type)">
|
2022-07-22 11:32:55 +02:00
|
|
|
<li v-for="n in 10" :key="n"></li>
|
2020-01-22 15:27:33 +01:00
|
|
|
</ul>
|
2022-06-25 17:35:29 +02:00
|
|
|
<ul v-show="isHMacBased(internal_otp_type)">
|
2021-10-09 19:23:24 +02:00
|
|
|
<li>counter: {{ internal_counter }}</li>
|
2020-01-24 22:37:48 +01:00
|
|
|
</ul>
|
2019-12-21 23:25:19 +01:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
2021-10-09 19:23:24 +02:00
|
|
|
name: 'OtpDisplayer',
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2019-12-21 23:25:19 +01:00
|
|
|
data() {
|
|
|
|
return {
|
2021-10-09 19:23:24 +02:00
|
|
|
internal_id: null,
|
|
|
|
internal_otp_type: '',
|
2020-11-13 15:45:17 +01:00
|
|
|
internal_account: '',
|
|
|
|
internal_service: '',
|
|
|
|
internal_icon: '',
|
2021-10-09 19:23:24 +02:00
|
|
|
internal_secret: null,
|
|
|
|
internal_digits: null,
|
|
|
|
internal_algorithm: null,
|
|
|
|
internal_period: null,
|
|
|
|
internal_counter: null,
|
|
|
|
internal_password : '',
|
|
|
|
internal_uri : '',
|
2020-11-16 09:38:28 +01:00
|
|
|
lastActiveDot: null,
|
2021-10-09 19:23:24 +02:00
|
|
|
remainingTimeout: null,
|
|
|
|
firstDotToNextOneTimeout: null,
|
|
|
|
dotToDotInterval: 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: {
|
2021-10-09 19:23:24 +02:00
|
|
|
otp_type : String,
|
2020-11-13 15:45:17 +01:00
|
|
|
account : String,
|
2021-10-09 19:23:24 +02:00
|
|
|
service : String,
|
2020-11-13 15:45:17 +01:00
|
|
|
icon : String,
|
|
|
|
secret : String,
|
2021-10-09 19:23:24 +02:00
|
|
|
digits : Number,
|
|
|
|
algorithm : String,
|
|
|
|
period : null,
|
|
|
|
counter : null,
|
|
|
|
image : String,
|
|
|
|
qrcode : null,
|
2020-11-13 15:45:17 +01:00
|
|
|
uri : String
|
2020-02-04 17:06:11 +01:00
|
|
|
},
|
2020-02-27 16:58:04 +01:00
|
|
|
|
|
|
|
computed: {
|
2023-02-08 16:03:11 +01:00
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
displayedOtp() {
|
2022-07-07 11:47:13 +02:00
|
|
|
let pwd = this.internal_password
|
2023-02-17 17:12:53 +01:00
|
|
|
if (this.$root.userPreferences.formatPassword && pwd.length > 0) {
|
|
|
|
const x = Math.ceil(this.$root.userPreferences.formatPasswordBy < 1 ? pwd.length * this.$root.userPreferences.formatPasswordBy : this.$root.userPreferences.formatPasswordBy)
|
2023-02-08 16:03:11 +01:00
|
|
|
const chunks = pwd.match(new RegExp(`.{1,${x}}`, 'g'));
|
|
|
|
if (chunks) {
|
|
|
|
pwd = chunks.join(' ')
|
|
|
|
}
|
2022-07-07 11:47:13 +02:00
|
|
|
}
|
2023-02-17 17:12:53 +01:00
|
|
|
return this.$root.userPreferences.showOtpAsDot ? pwd.replace(/[0-9]/g, '●') : pwd
|
2022-06-25 17:35:29 +02:00
|
|
|
},
|
2020-02-27 16:58:04 +01:00
|
|
|
},
|
2020-02-04 17:06:11 +01:00
|
|
|
|
|
|
|
mounted: function() {
|
2021-10-09 19:23:24 +02:00
|
|
|
this.show()
|
2020-02-04 17:06:11 +01:00
|
|
|
},
|
|
|
|
|
2022-10-12 11:30:20 +02:00
|
|
|
// created() {
|
|
|
|
// },
|
|
|
|
|
2020-01-22 15:27:33 +01:00
|
|
|
methods: {
|
|
|
|
|
2022-10-12 11:30:20 +02:00
|
|
|
copyOTP (otp) {
|
|
|
|
// see https://web.dev/async-clipboard/ for futur Clipboard API usage.
|
|
|
|
// The API should allow to copy the password on each trip without user interaction.
|
|
|
|
|
|
|
|
// For now too many browsers don't support the clipboard-write permission
|
|
|
|
// (see https://developer.mozilla.org/en-US/docs/Web/API/Permissions#browser_support)
|
|
|
|
|
|
|
|
const rawOTP = otp.replace(/ /g, '')
|
|
|
|
const success = this.$clipboard(rawOTP)
|
|
|
|
|
|
|
|
if (success == true) {
|
2023-02-17 17:12:53 +01:00
|
|
|
if(this.$root.userPreferences.kickUserAfter == -1) {
|
2022-10-12 11:30:20 +02:00
|
|
|
this.appLogout()
|
|
|
|
}
|
2023-02-17 17:12:53 +01:00
|
|
|
else if(this.$root.userPreferences.closeOtpOnCopy) {
|
2022-10-12 11:30:20 +02:00
|
|
|
this.$parent.isActive = false
|
|
|
|
this.clearOTP()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$notify({ type: 'is-success', text: this.$t('commons.copied_to_clipboard') })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-06-25 17:35:29 +02:00
|
|
|
isTimeBased: function(otp_type) {
|
|
|
|
return (otp_type === 'totp' || otp_type === 'steamtotp')
|
|
|
|
},
|
|
|
|
|
|
|
|
isHMacBased: function(otp_type) {
|
|
|
|
return otp_type === 'hotp'
|
|
|
|
},
|
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
async show(id) {
|
2020-01-23 17:07:05 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
// 3 possible cases :
|
2021-10-09 19:23:24 +02:00
|
|
|
// - Trigger when user ask for an otp of an existing account: the ID is provided so we fetch the account data
|
2020-11-13 15:45:17 +01:00
|
|
|
// 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
|
2021-10-09 19:23:24 +02:00
|
|
|
// case this.otp_type 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
|
2021-10-09 19:23:24 +02:00
|
|
|
// to obtain an otp, including Secret and otp_type which are required
|
2020-01-22 17:32:41 +01:00
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
this.internal_otp_type = this.otp_type
|
2020-11-16 14:15:33 +01:00
|
|
|
this.internal_account = this.account
|
2021-10-09 19:23:24 +02:00
|
|
|
this.internal_service = this.service
|
2020-11-16 14:15:33 +01:00
|
|
|
this.internal_icon = this.icon
|
2021-10-09 19:23:24 +02:00
|
|
|
this.internal_secret = this.secret
|
|
|
|
this.internal_digits = this.digits
|
|
|
|
this.internal_algorithm = this.algorithm
|
|
|
|
this.internal_period = this.period
|
|
|
|
this.internal_counter = this.counter
|
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
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
this.internal_id = id
|
2021-11-07 21:57:22 +01:00
|
|
|
const { data } = await this.axios.get('api/v1/twofaccounts/' + this.internal_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
|
2021-10-09 19:23:24 +02:00
|
|
|
this.internal_otp_type = data.otp_type
|
2020-11-16 14:33:56 +01:00
|
|
|
|
2022-06-25 17:35:29 +02:00
|
|
|
if( this.isHMacBased(data.otp_type) && data.counter ) {
|
2021-10-09 19:23:24 +02:00
|
|
|
this.internal_counter = data.counter
|
2020-11-16 14:33:56 +01:00
|
|
|
}
|
2020-11-13 15:45:17 +01:00
|
|
|
}
|
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
// We force the otp_type to be based on the uri
|
2020-11-13 15:45:17 +01:00
|
|
|
if( this.uri ) {
|
2021-10-09 19:23:24 +02:00
|
|
|
this.internal_uri = this.uri
|
|
|
|
this.internal_otp_type = this.uri.slice(0, 15 ).toLowerCase() === "otpauth://totp/" ? 'totp' : 'hotp';
|
2020-11-13 15:45:17 +01:00
|
|
|
}
|
2020-02-05 23:45:53 +01:00
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
if( this.internal_id || this.uri || this.secret ) { // minimun required vars to get an otp from the backend
|
2022-07-22 11:32:55 +02:00
|
|
|
try {
|
|
|
|
if(this.isTimeBased(this.internal_otp_type)) {
|
|
|
|
await this.startTotpLoop()
|
|
|
|
}
|
|
|
|
else if(this.isHMacBased(this.internal_otp_type)) {
|
|
|
|
await this.getHOTP()
|
|
|
|
}
|
|
|
|
else this.$router.push({ name: 'genericError', params: { err: this.$t('errors.not_a_supported_otp_type') } });
|
|
|
|
|
|
|
|
this.$parent.isActive = true
|
2022-10-12 11:30:20 +02:00
|
|
|
this.focusOnOTP()
|
2022-06-25 17:35:29 +02:00
|
|
|
}
|
2022-07-22 11:32:55 +02:00
|
|
|
catch(error) {
|
|
|
|
this.clearOTP()
|
2022-06-25 17:35:29 +02:00
|
|
|
}
|
2020-02-05 23:45:53 +01:00
|
|
|
}
|
2020-01-22 17:32:41 +01:00
|
|
|
},
|
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
getOtp: async function() {
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2022-07-22 11:32:55 +02:00
|
|
|
try {
|
2022-09-15 10:28:33 +02:00
|
|
|
let request, password
|
|
|
|
|
2022-07-22 11:32:55 +02:00
|
|
|
if(this.internal_id) {
|
2022-09-15 10:28:33 +02:00
|
|
|
request = {
|
|
|
|
method: 'get',
|
|
|
|
url: '/api/v1/twofaccounts/' + this.internal_id + '/otp'
|
|
|
|
}
|
2022-07-22 11:32:55 +02:00
|
|
|
}
|
|
|
|
else if(this.internal_uri) {
|
2022-09-15 10:28:33 +02:00
|
|
|
request = {
|
|
|
|
method: 'post',
|
|
|
|
url: '/api/v1/twofaccounts/otp',
|
|
|
|
data: {
|
|
|
|
uri: this.internal_uri
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 11:32:55 +02:00
|
|
|
}
|
|
|
|
else {
|
2022-09-15 10:28:33 +02:00
|
|
|
request = {
|
|
|
|
method: 'post',
|
|
|
|
url: '/api/v1/twofaccounts/otp',
|
|
|
|
data: {
|
|
|
|
service : this.internal_service,
|
|
|
|
account : this.internal_account,
|
|
|
|
icon : this.internal_icon,
|
|
|
|
otp_type : this.internal_otp_type,
|
|
|
|
secret : this.internal_secret,
|
|
|
|
digits : this.internal_digits,
|
|
|
|
algorithm : this.internal_algorithm,
|
|
|
|
period : this.internal_period,
|
|
|
|
counter : this.internal_counter,
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 11:32:55 +02:00
|
|
|
}
|
2022-09-15 10:28:33 +02:00
|
|
|
|
|
|
|
await this.axios(request).then(response => {
|
2023-02-17 17:12:53 +01:00
|
|
|
if(this.$root.userPreferences.copyOtpOnDisplay) {
|
2022-10-12 11:30:20 +02:00
|
|
|
this.copyOTP(response.data.password)
|
2022-09-15 10:28:33 +02:00
|
|
|
}
|
|
|
|
password = response.data
|
|
|
|
})
|
|
|
|
|
|
|
|
return password
|
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
}
|
2022-07-22 11:32:55 +02:00
|
|
|
catch(error) {
|
|
|
|
if (error.response.status === 422) {
|
|
|
|
this.$emit('validation-error', error.response)
|
|
|
|
}
|
|
|
|
throw error
|
2021-10-09 19:23:24 +02:00
|
|
|
}
|
|
|
|
},
|
2020-01-22 15:27:33 +01:00
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
startTotpLoop: async function() {
|
|
|
|
|
|
|
|
let otp = await this.getOtp()
|
|
|
|
|
|
|
|
this.internal_password = otp.password
|
|
|
|
this.internal_otp_type = otp.otp_type
|
2022-09-15 10:28:33 +02:00
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
let generated_at = otp.generated_at
|
|
|
|
let period = otp.period
|
|
|
|
|
|
|
|
let elapsedTimeInCurrentPeriod,
|
|
|
|
remainingTimeBeforeEndOfPeriod,
|
|
|
|
durationBetweenTwoDots,
|
|
|
|
durationFromFirstToNextDot,
|
|
|
|
dots
|
|
|
|
|
|
|
|
// |<----period p----->|
|
|
|
|
// | | |
|
|
|
|
// |------- ··· ------------|--------|----------|---------->
|
|
|
|
// | | | |
|
|
|
|
// unix T0 Tp.start Tgen_at Tp.end
|
|
|
|
// | | |
|
|
|
|
// elapsedTimeInCurrentPeriod--|<------>| |
|
|
|
|
// (in ms) | | |
|
|
|
|
// ● ● ● ● ●|● ◌ ◌ ◌ ◌ |
|
|
|
|
// | | || |
|
|
|
|
// | | |<-------->|--remainingTimeBeforeEndOfPeriod (for remainingTimeout)
|
|
|
|
// durationBetweenTwoDots-->|-|< ||
|
|
|
|
// (for dotToDotInterval) | | >||<---durationFromFirstToNextDot (for firstDotToNextOneTimeout)
|
|
|
|
// |
|
|
|
|
// |
|
|
|
|
// dotIndex
|
|
|
|
|
|
|
|
// The elapsed time from the start of the period that contains the OTP generated_at timestamp and the OTP generated_at timestamp itself
|
|
|
|
elapsedTimeInCurrentPeriod = generated_at % period
|
|
|
|
|
|
|
|
// Switch off all dots
|
|
|
|
dots = this.$el.querySelector('.dots')
|
|
|
|
while (dots.querySelector('[data-is-active]')) {
|
|
|
|
dots.querySelector('[data-is-active]').removeAttribute('data-is-active');
|
|
|
|
}
|
2020-01-22 15:27:33 +01:00
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
// We determine the position of the closest dot next to the generated_at timestamp
|
|
|
|
let relativePosition = (elapsedTimeInCurrentPeriod * 10) / period
|
|
|
|
let dotIndex = (Math.floor(relativePosition) +1)
|
|
|
|
|
|
|
|
// We switch the dot on
|
|
|
|
this.lastActiveDot = dots.querySelector('li:nth-child(' + dotIndex + ')');
|
|
|
|
this.lastActiveDot.setAttribute('data-is-active', true);
|
|
|
|
|
|
|
|
// Main timeout that run until the end of the period
|
|
|
|
remainingTimeBeforeEndOfPeriod = period - elapsedTimeInCurrentPeriod
|
|
|
|
let self = this; // because of the setInterval/setTimeout closures
|
|
|
|
|
|
|
|
this.remainingTimeout = setTimeout(function() {
|
|
|
|
self.stopLoop()
|
|
|
|
self.startTotpLoop();
|
|
|
|
}, remainingTimeBeforeEndOfPeriod*1000);
|
|
|
|
|
|
|
|
// During the remainingTimeout countdown we have to show a next dot every durationBetweenTwoDots seconds
|
|
|
|
// except for the first next dot
|
|
|
|
durationBetweenTwoDots = period / 10 // we have 10 dots
|
|
|
|
durationFromFirstToNextDot = (Math.ceil(elapsedTimeInCurrentPeriod / durationBetweenTwoDots) * durationBetweenTwoDots) - elapsedTimeInCurrentPeriod
|
|
|
|
|
|
|
|
this.firstDotToNextOneTimeout = setTimeout(function() {
|
|
|
|
if( durationFromFirstToNextDot > 0 ) {
|
|
|
|
self.activateNextDot()
|
|
|
|
dotIndex += 1
|
2020-01-22 15:27:33 +01:00
|
|
|
}
|
2021-10-09 19:23:24 +02:00
|
|
|
self.dotToDotInterval = setInterval(function() {
|
|
|
|
self.activateNextDot()
|
|
|
|
dotIndex += 1
|
|
|
|
}, durationBetweenTwoDots*1000)
|
|
|
|
}, durationFromFirstToNextDot*1000)
|
2020-01-22 15:27:33 +01:00
|
|
|
},
|
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
getHOTP: async function() {
|
2020-02-06 12:24:18 +01:00
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
let otp = await this.getOtp()
|
2022-09-14 16:37:58 +02:00
|
|
|
this.internal_password = otp.password
|
|
|
|
this.internal_counter = otp.counter
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
// returned counter & uri are incremented
|
|
|
|
this.$emit('increment-hotp', { nextHotpCounter: otp.counter, nextUri: otp.uri })
|
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() {
|
2021-10-09 19:23:24 +02:00
|
|
|
|
2020-01-22 15:27:33 +01:00
|
|
|
this.stopLoop()
|
2021-10-09 19:23:24 +02:00
|
|
|
this.internal_id = this.remainingTimeout = this.dotToDotInterval = this.firstDotToNextOneTimeout = this.elapsedTimeInCurrentPeriod = this.internal_counter = null
|
2022-06-20 17:50:32 +02:00
|
|
|
this.internal_service = this.internal_account = this.internal_icon = this.internal_otp_type = this.internal_secret = ''
|
2021-10-09 19:23:24 +02:00
|
|
|
this.internal_password = '... ...'
|
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() {
|
2022-06-25 17:35:29 +02:00
|
|
|
if( this.isTimeBased(this.internal_otp_type) ) {
|
2020-11-16 09:38:28 +01:00
|
|
|
clearTimeout(this.remainingTimeout)
|
|
|
|
clearTimeout(this.firstDotToNextOneTimeout)
|
|
|
|
clearInterval(this.dotToDotInterval)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2021-10-09 19:23:24 +02:00
|
|
|
activateNextDot: function() {
|
2020-11-16 09:38:28 +01:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2022-10-12 11:30:20 +02:00
|
|
|
focusOnOTP() {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.$refs.otp.focus()
|
|
|
|
})
|
|
|
|
}
|
2020-01-22 15:27:33 +01:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeDestroy () {
|
|
|
|
this.stopLoop()
|
|
|
|
}
|
2019-12-21 23:25:19 +01:00
|
|
|
}
|
|
|
|
</script>
|