Run populateFromUri() from uri setter instead of external call

This commit is contained in:
Bubka 2020-11-17 15:30:50 +01:00
parent 7bdd286fb2
commit 648c8f8006
3 changed files with 12 additions and 34 deletions

View File

@ -69,7 +69,7 @@ class QrCodeController extends Controller
// return the OTP object // return the OTP object
$twofaccount = new TwoFAccount; $twofaccount = new TwoFAccount;
$twofaccount->populateFromUri($uri); $twofaccount->uri = $uri;
return response()->json($twofaccount->makeVisible(['uri', 'secret', 'algorithm']), 200); return response()->json($twofaccount->makeVisible(['uri', 'secret', 'algorithm']), 200);
} }

View File

@ -57,7 +57,7 @@ class TwoFAccountController extends Controller
$twofaccount = new TwoFAccount; $twofaccount = new TwoFAccount;
if( $request->uri ) { if( $request->uri ) {
$twofaccount->populateFromUri($request->uri); $twofaccount->uri = $request->uri;
} }
else { else {
$twofaccount->populate($request->all()); $twofaccount->populate($request->all());
@ -136,7 +136,7 @@ class TwoFAccountController extends Controller
// The request data contain an uri // The request data contain an uri
$twofaccount = new TwoFAccount; $twofaccount = new TwoFAccount;
$twofaccount->populateFromUri($request->otp['uri']); $twofaccount->uri = $request->otp['uri'];
} }
else { else {

View File

@ -69,7 +69,7 @@ class TwoFAccount extends Model implements Sortable
parent::boot(); parent::boot();
static::retrieved(function ($model) { static::retrieved(function ($model) {
$model->populateFromUri(); $model->populateFromUri($model->uri);
}); });
static::saving(function ($model) { static::saving(function ($model) {
@ -176,13 +176,14 @@ class TwoFAccount extends Model implements Sortable
/** /**
* Set encrypted uri * Set uri attribute
* *
* @param string $value * @param string $value
* @return void * @return void
*/ */
public function setUriAttribute($value) public function setUriAttribute($value)
{ {
$this->populateFromUri($value);
$this->attributes['uri'] = Options::get('useEncryption') ? Crypt::encryptString($value) : $value; $this->attributes['uri'] = Options::get('useEncryption') ? Crypt::encryptString($value) : $value;
} }
@ -235,43 +236,20 @@ class TwoFAccount extends Model implements Sortable
/** /**
* Populate some attributes of the model from an uri * Populate the OTP sub-object wih the model URI
* *
* @param $foreignUri an URI to parse
* @return Boolean wether or not the URI provided a valid OTP resource
*/ */
public function populateFromUri(String $foreignUri = null) : bool private function populateFromUri($uri)
{ {
// No uri to parse
if( !$this->uri && !$foreignUri ) {
return false;
}
// The foreign uri is used in first place. This parameter is passed
// when we need a TwoFAccount new object, for example after a qrcode upload
// or for a preview
$uri = $foreignUri ? $foreignUri : $this->uri;
try { try {
$this->otp = Factory::loadFromProvisioningUri($uri); $this->otp = Factory::loadFromProvisioningUri($uri);
// Account and service values are already recorded in the db so we set them // Account and Service values should be already recorded in the db so we set them
// only when the uri used is a foreign uri, otherwise it would override // only when db has no value
// the db values if( !$this->service ) { $this->service = $this->otp->getIssuer(); }
if( $foreignUri ) { if( !$this->account ) { $this->account = $this->otp->getLabel(); }
if(!$this->otp->getIssuer()) {
$this->otp->setIssuer($this->otp->getLabel());
$this->otp->setLabel('');
}
$this->service = $this->otp->getIssuer();
$this->account = $this->otp->getLabel();
$this->uri = $foreignUri;
}
return true;
} }
catch (\Exception $e) { catch (\Exception $e) {
throw \Illuminate\Validation\ValidationException::withMessages([ throw \Illuminate\Validation\ValidationException::withMessages([