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 @@ public function decode(Request $request)
// return the OTP object
$twofaccount = new TwoFAccount;
$twofaccount->populateFromUri($uri);
$twofaccount->uri = $uri;
return response()->json($twofaccount->makeVisible(['uri', 'secret', 'algorithm']), 200);
}

View File

@ -57,7 +57,7 @@ public function store(Request $request)
$twofaccount = new TwoFAccount;
if( $request->uri ) {
$twofaccount->populateFromUri($request->uri);
$twofaccount->uri = $request->uri;
}
else {
$twofaccount->populate($request->all());
@ -136,7 +136,7 @@ public function generateOTP(Request $request)
// The request data contain an uri
$twofaccount = new TwoFAccount;
$twofaccount->populateFromUri($request->otp['uri']);
$twofaccount->uri = $request->otp['uri'];
}
else {

View File

@ -69,7 +69,7 @@ protected static function boot()
parent::boot();
static::retrieved(function ($model) {
$model->populateFromUri();
$model->populateFromUri($model->uri);
});
static::saving(function ($model) {
@ -176,13 +176,14 @@ public function getUriAttribute($value)
/**
* Set encrypted uri
* Set uri attribute
*
* @param string $value
* @return void
*/
public function setUriAttribute($value)
{
$this->populateFromUri($value);
$this->attributes['uri'] = Options::get('useEncryption') ? Crypt::encryptString($value) : $value;
}
@ -235,43 +236,20 @@ public function getIsConsistentAttribute()
/**
* 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 {
$this->otp = Factory::loadFromProvisioningUri($uri);
// Account and service values are already recorded in the db so we set them
// only when the uri used is a foreign uri, otherwise it would override
// the db values
if( $foreignUri ) {
// Account and Service values should be already recorded in the db so we set them
// only when db has no value
if( !$this->service ) { $this->service = $this->otp->getIssuer(); }
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) {
throw \Illuminate\Validation\ValidationException::withMessages([