Return account even if the uri stored in db is still encrypted (or invalid)

This commit is contained in:
Bubka 2020-11-22 11:00:55 +01:00
parent ed1ac10f59
commit 742107a270
2 changed files with 39 additions and 12 deletions

View File

@ -0,0 +1,14 @@
<?php
namespace App\Exceptions;
use Exception;
/**
* Class NotImplementedException.
*
* @codeCoverageIgnore
*/
class InvalidOtpParameterException extends Exception
{
}

View File

@ -57,7 +57,7 @@ class TwoFAccount extends Model implements Sortable
*
* @var OTPHP/TOTP || OTPHP/HOTP
*/
protected $otp, $timestamp;
protected $otp, $timestamp, $badUri;
/**
@ -70,7 +70,12 @@ protected static function boot()
parent::boot();
static::retrieved(function ($model) {
$model->populateFromUri($model->uri);
try {
$model->populateFromUri($model->uri);
}
catch( \App\Exceptions\InvalidOtpParameterException $e ) {
$model->badUri = true;
}
});
static::saving(function ($model) {
@ -217,9 +222,7 @@ private function populateFromUri($uri)
}
catch (\Exception $e) {
throw \Illuminate\Validation\ValidationException::withMessages([
'qrcode' => __('errors.no_valid_otp')
]);
throw new \App\Exceptions\InvalidOtpParameterException;
}
}
@ -364,7 +367,7 @@ public function setAccountAttribute($value)
*/
public function getIsConsistentAttribute()
{
return $this->uri === '*encrypted*' || $this->account === '*encrypted*' ? false : true;
return $this->uri === '*encrypted*' || $this->account === '*encrypted*' || $this->badUri ? false : true;
}
@ -398,7 +401,12 @@ public function getTokenAttribute() : string
*/
public function getOtpTypeAttribute()
{
return get_class($this->otp) === 'OTPHP\TOTP' ? 'totp' : 'hotp';
if( isset($this->otp) ) {
return get_class($this->otp) === 'OTPHP\TOTP' ? 'totp' : 'hotp';
}
else {
return null;
}
}
@ -410,7 +418,7 @@ public function getOtpTypeAttribute()
*/
public function getSecretAttribute()
{
return $this->otp->getSecret();
return isset($this->otp) ? $this->otp->getSecret() : null;
}
@ -422,7 +430,7 @@ public function getSecretAttribute()
*/
public function getAlgorithmAttribute()
{
return $this->otp->getDigest(); // default is SHA1
return isset($this->otp) ? $this->otp->getDigest() : null; // default is SHA1
}
@ -434,7 +442,7 @@ public function getAlgorithmAttribute()
*/
public function getDigitsAttribute()
{
return $this->otp->getDigits(); // Default is 6
return isset($this->otp) ? $this->otp->getDigits() : null; // Default is 6
}
@ -458,7 +466,7 @@ public function getTotpPeriodAttribute()
*/
public function getHotpCounterAttribute()
{
return $this->otpType === 'hotp' ? $this->otp->getCounter() : null; // Default is 0
return isset($this->otp) && $this->otpType === 'hotp' ? $this->otp->getCounter() : null; // Default is 0
}
@ -482,7 +490,12 @@ public function setHotpCounterAttribute($value)
*/
public function getImageLinkAttribute()
{
return $this->otp->hasParameter('image') ? $this->otp->getParameter('image') : null;
if( isset($this->otp) ) {
return $this->otp->hasParameter('image') ? $this->otp->getParameter('image') : null;
}
else {
return false;
}
}
}