From 8253d2810267dbfd53e1c08c58407a57c0841685 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Mon, 16 Nov 2020 09:38:28 +0100 Subject: [PATCH] Fix #11 : Set TOTP loop duration on remaining time instead of remaining dots --- app/TwoFAccount.php | 56 +++++--------- resources/js/components/TokenDisplayer.vue | 85 ++++++++++++++++------ resources/sass/app.scss | 16 ++-- 3 files changed, 84 insertions(+), 73 deletions(-) diff --git a/app/TwoFAccount.php b/app/TwoFAccount.php index c617b45a..b995b097 100644 --- a/app/TwoFAccount.php +++ b/app/TwoFAccount.php @@ -40,7 +40,7 @@ class TwoFAccount extends Model implements Sortable * * @var array */ - protected $appends = ['token', 'isConsistent', 'otpType', 'secret', 'algorithm', 'digits', 'totpPeriod', 'totpPosition', 'hotpCounter', 'imageLink']; + protected $appends = ['token', 'isConsistent', 'otpType', 'secret', 'algorithm', 'digits', 'totpPeriod', 'totpTimestamp', 'hotpCounter', 'imageLink']; /** @@ -56,7 +56,7 @@ class TwoFAccount extends Model implements Sortable * * @var OTPHP/TOTP || OTPHP/HOTP */ - protected $otp; + protected $otp, $timestamp; /** @@ -348,29 +348,6 @@ class TwoFAccount extends Model implements Sortable } - /** - * Calculate where is now() in the totp current period - * @return mixed The position - */ - private function getTotpPosition() - { - // For memo : - // $nextOtpAt = ($PeriodCount+1)*$period - // $remainingTime = $nextOtpAt - time() - if( $this->otpType === 'totp' ) { - - $currentPosition = time(); - $PeriodCount = floor($currentPosition / $this->totpPeriod); //nombre de période de x s depuis T0 (x=30 par défaut) - $currentPeriodStartAt = $PeriodCount * $this->totpPeriod; - $positionInCurrentPeriod = $currentPosition - $currentPeriodStartAt; - - return $positionInCurrentPeriod; - } - - return null; - } - - /** * Update the uri attribute using the OTP object * @return void @@ -382,12 +359,15 @@ class TwoFAccount extends Model implements Sortable /** - * Generate a token which is valid at the current time (now) + * Generate a token which is valid at the current time * @return string The generated token */ public function generateToken() : string { - return $this->otpType === 'totp' ? $this->otp->now() : $this->otp->at($this->otp->getCounter()); + $this->timestamp = time(); + $token = $this->otpType === 'totp' ? $this->otp->at($this->timestamp) : $this->otp->at($this->otp->getCounter()); + + return $token; } @@ -405,6 +385,17 @@ class TwoFAccount extends Model implements Sortable } + /** + * get totpTimestamp attribute + * + * @return int The timestamp + */ + public function getTotpTimestampAttribute() + { + return $this->timestamp; + } + + /** * get token attribute * @@ -416,17 +407,6 @@ class TwoFAccount extends Model implements Sortable } - /** - * get totpPosition attribute - * - * @return int The position - */ - public function getTotpPositionAttribute() - { - return $this->getTotpPosition(); - } - - /** * get OTP Type attribute * diff --git a/resources/js/components/TokenDisplayer.vue b/resources/js/components/TokenDisplayer.vue index 83640f45..d4f78303 100644 --- a/resources/js/components/TokenDisplayer.vue +++ b/resources/js/components/TokenDisplayer.vue @@ -7,7 +7,7 @@

{{ internal_account }}

{{ displayedToken }}