mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-06-20 11:47:53 +02:00
Run populateFromUri() from uri setter instead of external call
This commit is contained in:
parent
7bdd286fb2
commit
648c8f8006
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
|
||||||
|
@ -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([
|
||||||
|
Loading…
x
Reference in New Issue
Block a user