|
@@ -2,55 +2,62 @@
|
|
|
|
|
|
namespace App\Models;
|
|
namespace App\Models;
|
|
|
|
|
|
-use Exception;
|
|
|
|
-use App\Services\LogoService;
|
|
|
|
-use App\Facades\Settings;
|
|
|
|
-use App\Models\Dto\TotpDto;
|
|
|
|
-use App\Models\Dto\HotpDto;
|
|
|
|
use App\Events\TwoFAccountDeleted;
|
|
use App\Events\TwoFAccountDeleted;
|
|
-use App\Exceptions\InvalidSecretException;
|
|
|
|
use App\Exceptions\InvalidOtpParameterException;
|
|
use App\Exceptions\InvalidOtpParameterException;
|
|
-use App\Exceptions\UnsupportedOtpTypeException;
|
|
|
|
|
|
+use App\Exceptions\InvalidSecretException;
|
|
use App\Exceptions\UndecipherableException;
|
|
use App\Exceptions\UndecipherableException;
|
|
-use Illuminate\Validation\ValidationException;
|
|
|
|
-use Spatie\EloquentSortable\Sortable;
|
|
|
|
-use Spatie\EloquentSortable\SortableTrait;
|
|
|
|
-use OTPHP\TOTP;
|
|
|
|
-use OTPHP\HOTP;
|
|
|
|
-use OTPHP\Factory;
|
|
|
|
-use SteamTotp\SteamTotp;
|
|
|
|
|
|
+use App\Exceptions\UnsupportedOtpTypeException;
|
|
|
|
+use App\Facades\Settings;
|
|
|
|
+use App\Helpers\Helpers;
|
|
|
|
+use App\Models\Dto\HotpDto;
|
|
|
|
+use App\Models\Dto\TotpDto;
|
|
|
|
+use App\Services\LogoService;
|
|
|
|
+use Exception;
|
|
|
|
+use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
-use Illuminate\Support\Str;
|
|
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Arr;
|
|
|
|
+use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
+use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Storage;
|
|
-use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
|
+use Illuminate\Validation\ValidationException;
|
|
|
|
+use OTPHP\Factory;
|
|
|
|
+use OTPHP\HOTP;
|
|
|
|
+use OTPHP\TOTP;
|
|
use ParagonIE\ConstantTime\Base32;
|
|
use ParagonIE\ConstantTime\Base32;
|
|
-use Illuminate\Support\Facades\App;
|
|
|
|
-use Illuminate\Support\Facades\Http;
|
|
|
|
-use App\Helpers\Helpers;
|
|
|
|
|
|
+use Spatie\EloquentSortable\Sortable;
|
|
|
|
+use Spatie\EloquentSortable\SortableTrait;
|
|
|
|
+use SteamTotp\SteamTotp;
|
|
|
|
|
|
class TwoFAccount extends Model implements Sortable
|
|
class TwoFAccount extends Model implements Sortable
|
|
{
|
|
{
|
|
-
|
|
|
|
use SortableTrait, HasFactory;
|
|
use SortableTrait, HasFactory;
|
|
|
|
|
|
- const TOTP = 'totp';
|
|
|
|
- const HOTP = 'hotp';
|
|
|
|
|
|
+ const TOTP = 'totp';
|
|
|
|
+
|
|
|
|
+ const HOTP = 'hotp';
|
|
|
|
+
|
|
const STEAM_TOTP = 'steamtotp';
|
|
const STEAM_TOTP = 'steamtotp';
|
|
|
|
|
|
- const SHA1 = 'sha1';
|
|
|
|
- const MD5 = 'md5';
|
|
|
|
- const SHA256 = 'sha256';
|
|
|
|
- const SHA512 = 'sha512';
|
|
|
|
-
|
|
|
|
|
|
+ const SHA1 = 'sha1';
|
|
|
|
+
|
|
|
|
+ const MD5 = 'md5';
|
|
|
|
+
|
|
|
|
+ const SHA256 = 'sha256';
|
|
|
|
+
|
|
|
|
+ const SHA512 = 'sha512';
|
|
|
|
+
|
|
const DEFAULT_PERIOD = 30;
|
|
const DEFAULT_PERIOD = 30;
|
|
|
|
+
|
|
const DEFAULT_COUNTER = 0;
|
|
const DEFAULT_COUNTER = 0;
|
|
|
|
+
|
|
const DEFAULT_DIGITS = 6;
|
|
const DEFAULT_DIGITS = 6;
|
|
|
|
+
|
|
const DEFAULT_ALGORITHM = self::SHA1;
|
|
const DEFAULT_ALGORITHM = self::SHA1;
|
|
|
|
|
|
const DUPLICATE_ID = -1;
|
|
const DUPLICATE_ID = -1;
|
|
|
|
+
|
|
const FAKE_ID = -2;
|
|
const FAKE_ID = -2;
|
|
|
|
|
|
private const IMAGELINK_STORAGE_PATH = 'imagesLink/';
|
|
private const IMAGELINK_STORAGE_PATH = 'imagesLink/';
|
|
@@ -80,7 +87,6 @@ class TwoFAccount extends Model implements Sortable
|
|
// 'icon'
|
|
// 'icon'
|
|
];
|
|
];
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* The table associated with the model.
|
|
* The table associated with the model.
|
|
*
|
|
*
|
|
@@ -88,26 +94,23 @@ class TwoFAccount extends Model implements Sortable
|
|
*/
|
|
*/
|
|
protected $table = 'twofaccounts';
|
|
protected $table = 'twofaccounts';
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* The accessors to append to the model's array form.
|
|
* The accessors to append to the model's array form.
|
|
*
|
|
*
|
|
* @var array
|
|
* @var array
|
|
*/
|
|
*/
|
|
public $appends = [];
|
|
public $appends = [];
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
- * The model's default values for attributes.
|
|
|
|
- *
|
|
|
|
- * @var array
|
|
|
|
- */
|
|
|
|
|
|
+ * The model's default values for attributes.
|
|
|
|
+ *
|
|
|
|
+ * @var array
|
|
|
|
+ */
|
|
protected $attributes = [
|
|
protected $attributes = [
|
|
- 'digits' => 6,
|
|
|
|
|
|
+ 'digits' => 6,
|
|
'algorithm' => self::SHA1,
|
|
'algorithm' => self::SHA1,
|
|
];
|
|
];
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* The attributes that should be hidden for arrays.
|
|
* The attributes that should be hidden for arrays.
|
|
*
|
|
*
|
|
@@ -115,7 +118,6 @@ class TwoFAccount extends Model implements Sortable
|
|
*/
|
|
*/
|
|
protected $hidden = [];
|
|
protected $hidden = [];
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* The attributes that should be cast.
|
|
* The attributes that should be cast.
|
|
*
|
|
*
|
|
@@ -123,7 +125,6 @@ class TwoFAccount extends Model implements Sortable
|
|
*/
|
|
*/
|
|
protected $casts = [];
|
|
protected $casts = [];
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* The event map for the model.
|
|
* The event map for the model.
|
|
*
|
|
*
|
|
@@ -133,7 +134,6 @@ class TwoFAccount extends Model implements Sortable
|
|
'deleted' => TwoFAccountDeleted::class,
|
|
'deleted' => TwoFAccountDeleted::class,
|
|
];
|
|
];
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Override The "booting" method of the model
|
|
* Override The "booting" method of the model
|
|
*
|
|
*
|
|
@@ -144,9 +144,15 @@ class TwoFAccount extends Model implements Sortable
|
|
parent::boot();
|
|
parent::boot();
|
|
|
|
|
|
static::saving(function (TwoFAccount $twofaccount) {
|
|
static::saving(function (TwoFAccount $twofaccount) {
|
|
- if (!$twofaccount->legacy_uri) $twofaccount->legacy_uri = $twofaccount->getURI();
|
|
|
|
- if ($twofaccount->otp_type == TwoFAccount::TOTP && !$twofaccount->period) $twofaccount->period = TwoFAccount::DEFAULT_PERIOD;
|
|
|
|
- if ($twofaccount->otp_type == TwoFAccount::HOTP && !$twofaccount->counter) $twofaccount->counter = TwoFAccount::DEFAULT_COUNTER;
|
|
|
|
|
|
+ if (! $twofaccount->legacy_uri) {
|
|
|
|
+ $twofaccount->legacy_uri = $twofaccount->getURI();
|
|
|
|
+ }
|
|
|
|
+ if ($twofaccount->otp_type == TwoFAccount::TOTP && ! $twofaccount->period) {
|
|
|
|
+ $twofaccount->period = TwoFAccount::DEFAULT_PERIOD;
|
|
|
|
+ }
|
|
|
|
+ if ($twofaccount->otp_type == TwoFAccount::HOTP && ! $twofaccount->counter) {
|
|
|
|
+ $twofaccount->counter = TwoFAccount::DEFAULT_COUNTER;
|
|
|
|
+ }
|
|
});
|
|
});
|
|
|
|
|
|
// static::deleted(function ($model) {
|
|
// static::deleted(function ($model) {
|
|
@@ -154,18 +160,16 @@ class TwoFAccount extends Model implements Sortable
|
|
// });
|
|
// });
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Settings for @spatie/eloquent-sortable package
|
|
* Settings for @spatie/eloquent-sortable package
|
|
*
|
|
*
|
|
* @var array
|
|
* @var array
|
|
*/
|
|
*/
|
|
public $sortable = [
|
|
public $sortable = [
|
|
- 'order_column_name' => 'order_column',
|
|
|
|
|
|
+ 'order_column_name' => 'order_column',
|
|
'sort_when_creating' => true,
|
|
'sort_when_creating' => true,
|
|
];
|
|
];
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* The OTP generator.
|
|
* The OTP generator.
|
|
* Instanciated as null to keep the model light
|
|
* Instanciated as null to keep the model light
|
|
@@ -174,7 +178,6 @@ class TwoFAccount extends Model implements Sortable
|
|
*/
|
|
*/
|
|
protected $generator = null;
|
|
protected $generator = null;
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Get legacy_uri attribute
|
|
* Get legacy_uri attribute
|
|
*
|
|
*
|
|
@@ -183,9 +186,9 @@ class TwoFAccount extends Model implements Sortable
|
|
*/
|
|
*/
|
|
public function getLegacyUriAttribute($value)
|
|
public function getLegacyUriAttribute($value)
|
|
{
|
|
{
|
|
-
|
|
|
|
return $this->decryptOrReturn($value);
|
|
return $this->decryptOrReturn($value);
|
|
}
|
|
}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Set legacy_uri attribute
|
|
* Set legacy_uri attribute
|
|
*
|
|
*
|
|
@@ -198,7 +201,6 @@ class TwoFAccount extends Model implements Sortable
|
|
$this->attributes['legacy_uri'] = $this->encryptOrReturn($value);
|
|
$this->attributes['legacy_uri'] = $this->encryptOrReturn($value);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Get account attribute
|
|
* Get account attribute
|
|
*
|
|
*
|
|
@@ -207,13 +209,13 @@ class TwoFAccount extends Model implements Sortable
|
|
*/
|
|
*/
|
|
public function getAccountAttribute($value)
|
|
public function getAccountAttribute($value)
|
|
{
|
|
{
|
|
-
|
|
|
|
return $this->decryptOrReturn($value);
|
|
return $this->decryptOrReturn($value);
|
|
}
|
|
}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Set account attribute
|
|
* Set account attribute
|
|
*
|
|
*
|
|
- * @param string $value
|
|
|
|
|
|
+ * @param string $value
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
public function setAccountAttribute($value)
|
|
public function setAccountAttribute($value)
|
|
@@ -222,7 +224,6 @@ class TwoFAccount extends Model implements Sortable
|
|
$this->attributes['account'] = $this->encryptOrReturn($value);
|
|
$this->attributes['account'] = $this->encryptOrReturn($value);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Get secret attribute
|
|
* Get secret attribute
|
|
*
|
|
*
|
|
@@ -231,13 +232,13 @@ class TwoFAccount extends Model implements Sortable
|
|
*/
|
|
*/
|
|
public function getSecretAttribute($value)
|
|
public function getSecretAttribute($value)
|
|
{
|
|
{
|
|
-
|
|
|
|
return $this->decryptOrReturn($value);
|
|
return $this->decryptOrReturn($value);
|
|
}
|
|
}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Set secret attribute
|
|
* Set secret attribute
|
|
*
|
|
*
|
|
- * @param string $value
|
|
|
|
|
|
+ * @param string $value
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
public function setSecretAttribute($value)
|
|
public function setSecretAttribute($value)
|
|
@@ -246,47 +247,43 @@ class TwoFAccount extends Model implements Sortable
|
|
$this->attributes['secret'] = $this->encryptOrReturn($value);
|
|
$this->attributes['secret'] = $this->encryptOrReturn($value);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Set digits attribute
|
|
* Set digits attribute
|
|
*
|
|
*
|
|
- * @param string $value
|
|
|
|
|
|
+ * @param string $value
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
public function setDigitsAttribute($value)
|
|
public function setDigitsAttribute($value)
|
|
{
|
|
{
|
|
- $this->attributes['digits'] = !$value ? 6 : $value;
|
|
|
|
|
|
+ $this->attributes['digits'] = ! $value ? 6 : $value;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Set algorithm attribute
|
|
* Set algorithm attribute
|
|
*
|
|
*
|
|
- * @param string $value
|
|
|
|
|
|
+ * @param string $value
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
public function setAlgorithmAttribute($value)
|
|
public function setAlgorithmAttribute($value)
|
|
{
|
|
{
|
|
- $this->attributes['algorithm'] = !$value ? self::SHA1 : strtolower($value);
|
|
|
|
|
|
+ $this->attributes['algorithm'] = ! $value ? self::SHA1 : strtolower($value);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Set period attribute
|
|
* Set period attribute
|
|
*
|
|
*
|
|
- * @param string $value
|
|
|
|
|
|
+ * @param string $value
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
public function setPeriodAttribute($value)
|
|
public function setPeriodAttribute($value)
|
|
{
|
|
{
|
|
- $this->attributes['period'] = !$value && $this->otp_type === self::TOTP ? self::DEFAULT_PERIOD : $value;
|
|
|
|
|
|
+ $this->attributes['period'] = ! $value && $this->otp_type === self::TOTP ? self::DEFAULT_PERIOD : $value;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Set counter attribute
|
|
* Set counter attribute
|
|
*
|
|
*
|
|
- * @param string $value
|
|
|
|
|
|
+ * @param string $value
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
public function setCounterAttribute($value)
|
|
public function setCounterAttribute($value)
|
|
@@ -294,19 +291,19 @@ class TwoFAccount extends Model implements Sortable
|
|
$this->attributes['counter'] = blank($value) && $this->otp_type === self::HOTP ? self::DEFAULT_COUNTER : $value;
|
|
$this->attributes['counter'] = blank($value) && $this->otp_type === self::HOTP ? self::DEFAULT_COUNTER : $value;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Returns a One-Time Password with its parameters
|
|
* Returns a One-Time Password with its parameters
|
|
- *
|
|
|
|
|
|
+ *
|
|
|
|
+ * @return TotpDto|HotpDto
|
|
|
|
+ *
|
|
* @throws InvalidSecretException The secret is not a valid base32 encoded string
|
|
* @throws InvalidSecretException The secret is not a valid base32 encoded string
|
|
* @throws UndecipherableException The secret cannot be deciphered
|
|
* @throws UndecipherableException The secret cannot be deciphered
|
|
* @throws UnsupportedOtpTypeException The defined OTP type is not supported
|
|
* @throws UnsupportedOtpTypeException The defined OTP type is not supported
|
|
* @throws InvalidOtpParameterException One OTP parameter is invalid
|
|
* @throws InvalidOtpParameterException One OTP parameter is invalid
|
|
- * @return TotpDto|HotpDto
|
|
|
|
*/
|
|
*/
|
|
public function getOTP()
|
|
public function getOTP()
|
|
{
|
|
{
|
|
- Log::info(sprintf('OTP requested for TwoFAccount (%s)', $this->id ? 'id:'.$this->id: 'preview'));
|
|
|
|
|
|
+ Log::info(sprintf('OTP requested for TwoFAccount (%s)', $this->id ? 'id:' . $this->id : 'preview'));
|
|
|
|
|
|
// Early exit if the model has an undecipherable secret
|
|
// Early exit if the model has an undecipherable secret
|
|
if (strtolower($this->secret) === __('errors.indecipherable')) {
|
|
if (strtolower($this->secret) === __('errors.indecipherable')) {
|
|
@@ -316,38 +313,33 @@ class TwoFAccount extends Model implements Sortable
|
|
}
|
|
}
|
|
|
|
|
|
$this->initGenerator();
|
|
$this->initGenerator();
|
|
-
|
|
|
|
- try {
|
|
|
|
- if ( $this->otp_type === self::HOTP ) {
|
|
|
|
|
|
|
|
- $OtpDto = new HotpDto();
|
|
|
|
- $OtpDto->otp_type = $this->otp_type;
|
|
|
|
- $counter = $this->generator->getParameter('counter');
|
|
|
|
- $OtpDto->password = $this->generator->at($counter);
|
|
|
|
- $OtpDto->counter = $this->counter = $counter + 1;
|
|
|
|
|
|
+ try {
|
|
|
|
+ if ($this->otp_type === self::HOTP) {
|
|
|
|
+ $OtpDto = new HotpDto();
|
|
|
|
+ $OtpDto->otp_type = $this->otp_type;
|
|
|
|
+ $counter = $this->generator->getParameter('counter');
|
|
|
|
+ $OtpDto->password = $this->generator->at($counter);
|
|
|
|
+ $OtpDto->counter = $this->counter = $counter + 1;
|
|
|
|
|
|
// The updated HOTP counter must be saved to db for persisted account only
|
|
// The updated HOTP counter must be saved to db for persisted account only
|
|
if ($this->id) {
|
|
if ($this->id) {
|
|
$this->save();
|
|
$this->save();
|
|
}
|
|
}
|
|
- }
|
|
|
|
- else {
|
|
|
|
-
|
|
|
|
- $OtpDto = new TotpDto();
|
|
|
|
- $OtpDto->otp_type = $this->otp_type;
|
|
|
|
- $OtpDto->generated_at = time();
|
|
|
|
- $OtpDto->password = $this->otp_type === self::TOTP
|
|
|
|
|
|
+ } else {
|
|
|
|
+ $OtpDto = new TotpDto();
|
|
|
|
+ $OtpDto->otp_type = $this->otp_type;
|
|
|
|
+ $OtpDto->generated_at = time();
|
|
|
|
+ $OtpDto->password = $this->otp_type === self::TOTP
|
|
? $this->generator->at($OtpDto->generated_at)
|
|
? $this->generator->at($OtpDto->generated_at)
|
|
: SteamTotp::getAuthCode(base64_encode(Base32::decodeUpper($this->secret)));
|
|
: SteamTotp::getAuthCode(base64_encode(Base32::decodeUpper($this->secret)));
|
|
- $OtpDto->period = $this->period;
|
|
|
|
|
|
+ $OtpDto->period = $this->period;
|
|
}
|
|
}
|
|
|
|
|
|
- Log::info(sprintf('New OTP generated for TwoFAccount (%s)', $this->id ? 'id:'.$this->id: 'preview'));
|
|
|
|
-
|
|
|
|
- return $OtpDto;
|
|
|
|
|
|
+ Log::info(sprintf('New OTP generated for TwoFAccount (%s)', $this->id ? 'id:' . $this->id : 'preview'));
|
|
|
|
|
|
- }
|
|
|
|
- catch (\Exception|\Throwable $ex) {
|
|
|
|
|
|
+ return $OtpDto;
|
|
|
|
+ } catch (\Exception|\Throwable $ex) {
|
|
Log::error('An error occured, OTP generation aborted');
|
|
Log::error('An error occured, OTP generation aborted');
|
|
// Currently a secret issue is the only possible exception thrown by OTPHP for this stack
|
|
// Currently a secret issue is the only possible exception thrown by OTPHP for this stack
|
|
// so it is Ok to send the corresponding 2FAuth exception.
|
|
// so it is Ok to send the corresponding 2FAuth exception.
|
|
@@ -356,52 +348,50 @@ class TwoFAccount extends Model implements Sortable
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Fill the model using an array of OTP parameters.
|
|
* Fill the model using an array of OTP parameters.
|
|
* Missing parameters will be set with default values
|
|
* Missing parameters will be set with default values
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @return $this
|
|
* @return $this
|
|
*/
|
|
*/
|
|
public function fillWithOtpParameters(array $parameters, bool $skipIconFetching = false)
|
|
public function fillWithOtpParameters(array $parameters, bool $skipIconFetching = false)
|
|
{
|
|
{
|
|
- $this->otp_type = strtolower(Arr::get($parameters, 'otp_type'));
|
|
|
|
- $this->account = Arr::get($parameters, 'account');
|
|
|
|
- $this->service = Arr::get($parameters, 'service');
|
|
|
|
- $this->icon = Arr::get($parameters, 'icon');
|
|
|
|
- $this->secret = Arr::get($parameters, 'secret');
|
|
|
|
- $this->algorithm = strtolower(Arr::get($parameters, 'algorithm', self::SHA1));
|
|
|
|
- $this->digits = Arr::get($parameters, 'digits', self::DEFAULT_DIGITS);
|
|
|
|
- $this->period = Arr::get($parameters, 'period', $this->otp_type == self::TOTP ? self::DEFAULT_PERIOD : null);
|
|
|
|
- $this->counter = Arr::get($parameters, 'counter', $this->otp_type == self::HOTP ? self::DEFAULT_COUNTER : null);
|
|
|
|
|
|
+ $this->otp_type = strtolower(Arr::get($parameters, 'otp_type'));
|
|
|
|
+ $this->account = Arr::get($parameters, 'account');
|
|
|
|
+ $this->service = Arr::get($parameters, 'service');
|
|
|
|
+ $this->icon = Arr::get($parameters, 'icon');
|
|
|
|
+ $this->secret = Arr::get($parameters, 'secret');
|
|
|
|
+ $this->algorithm = strtolower(Arr::get($parameters, 'algorithm', self::SHA1));
|
|
|
|
+ $this->digits = Arr::get($parameters, 'digits', self::DEFAULT_DIGITS);
|
|
|
|
+ $this->period = Arr::get($parameters, 'period', $this->otp_type == self::TOTP ? self::DEFAULT_PERIOD : null);
|
|
|
|
+ $this->counter = Arr::get($parameters, 'counter', $this->otp_type == self::HOTP ? self::DEFAULT_COUNTER : null);
|
|
|
|
|
|
$this->initGenerator();
|
|
$this->initGenerator();
|
|
|
|
|
|
// The generator could have been initialized without a secret, in that case it generates one on the fly.
|
|
// The generator could have been initialized without a secret, in that case it generates one on the fly.
|
|
// The secret attribute has thus to be updated
|
|
// The secret attribute has thus to be updated
|
|
$this->secret = $this->secret ?: $this->generator->getSecret();
|
|
$this->secret = $this->secret ?: $this->generator->getSecret();
|
|
-
|
|
|
|
|
|
+
|
|
if ($this->otp_type === self::STEAM_TOTP || strtolower($this->service) === 'steam') {
|
|
if ($this->otp_type === self::STEAM_TOTP || strtolower($this->service) === 'steam') {
|
|
$this->enforceAsSteam();
|
|
$this->enforceAsSteam();
|
|
}
|
|
}
|
|
|
|
|
|
- if (!$this->icon && $skipIconFetching) {
|
|
|
|
|
|
+ if (! $this->icon && $skipIconFetching) {
|
|
$this->icon = $this->getDefaultIcon();
|
|
$this->icon = $this->getDefaultIcon();
|
|
}
|
|
}
|
|
|
|
|
|
- if (!$this->icon && Settings::get('getOfficialIcons') && !$skipIconFetching) {
|
|
|
|
|
|
+ if (! $this->icon && Settings::get('getOfficialIcons') && ! $skipIconFetching) {
|
|
$this->icon = $this->getDefaultIcon();
|
|
$this->icon = $this->getDefaultIcon();
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
|
|
Log::info(sprintf('TwoFAccount filled with OTP parameters'));
|
|
Log::info(sprintf('TwoFAccount filled with OTP parameters'));
|
|
|
|
|
|
return $this;
|
|
return $this;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Fill the model by parsing an otpauth URI
|
|
* Fill the model by parsing an otpauth URI
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @return $this
|
|
* @return $this
|
|
*/
|
|
*/
|
|
public function fillWithURI(string $uri, bool $isSteamTotp = false, bool $skipIconFetching = false)
|
|
public function fillWithURI(string $uri, bool $isSteamTotp = false, bool $skipIconFetching = false)
|
|
@@ -409,33 +399,32 @@ class TwoFAccount extends Model implements Sortable
|
|
// First we instanciate the OTP generator
|
|
// First we instanciate the OTP generator
|
|
try {
|
|
try {
|
|
$this->generator = Factory::loadFromProvisioningUri($uri);
|
|
$this->generator = Factory::loadFromProvisioningUri($uri);
|
|
- }
|
|
|
|
- catch (\Assert\AssertionFailedException|\Assert\InvalidArgumentException|\Exception|\Throwable $ex) {
|
|
|
|
|
|
+ } catch (\Assert\AssertionFailedException|\Assert\InvalidArgumentException|\Exception|\Throwable $ex) {
|
|
throw ValidationException::withMessages([
|
|
throw ValidationException::withMessages([
|
|
- 'uri' => __('validation.custom.uri.regex', ['attribute' => 'uri'])
|
|
|
|
|
|
+ 'uri' => __('validation.custom.uri.regex', ['attribute' => 'uri']),
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
// As loadFromProvisioningUri() accept URI without label (nor account nor service) we check
|
|
// As loadFromProvisioningUri() accept URI without label (nor account nor service) we check
|
|
// that the account is set
|
|
// that the account is set
|
|
- if ( ! $this->generator->getLabel() ) {
|
|
|
|
|
|
+ if (! $this->generator->getLabel()) {
|
|
Log::error('URI passed to fillWithURI() must contain a label');
|
|
Log::error('URI passed to fillWithURI() must contain a label');
|
|
|
|
|
|
throw ValidationException::withMessages([
|
|
throw ValidationException::withMessages([
|
|
- 'label' => __('validation.custom.label.required')
|
|
|
|
|
|
+ 'label' => __('validation.custom.label.required'),
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
- $this->otp_type = $this->getGeneratorOtpType();
|
|
|
|
- $this->account = $this->generator->getLabel();
|
|
|
|
- $this->secret = $this->generator->getSecret();
|
|
|
|
- $this->service = $this->generator->getIssuer();
|
|
|
|
- $this->algorithm = $this->generator->getDigest();
|
|
|
|
- $this->digits = $this->generator->getDigits();
|
|
|
|
- $this->period = $this->generator->hasParameter('period') ? $this->generator->getParameter('period') : null;
|
|
|
|
- $this->counter = $this->generator->hasParameter('counter') ? $this->generator->getParameter('counter') : null;
|
|
|
|
- $this->legacy_uri = $uri;
|
|
|
|
-
|
|
|
|
|
|
+ $this->otp_type = $this->getGeneratorOtpType();
|
|
|
|
+ $this->account = $this->generator->getLabel();
|
|
|
|
+ $this->secret = $this->generator->getSecret();
|
|
|
|
+ $this->service = $this->generator->getIssuer();
|
|
|
|
+ $this->algorithm = $this->generator->getDigest();
|
|
|
|
+ $this->digits = $this->generator->getDigits();
|
|
|
|
+ $this->period = $this->generator->hasParameter('period') ? $this->generator->getParameter('period') : null;
|
|
|
|
+ $this->counter = $this->generator->hasParameter('counter') ? $this->generator->getParameter('counter') : null;
|
|
|
|
+ $this->legacy_uri = $uri;
|
|
|
|
+
|
|
if ($isSteamTotp || strtolower($this->service) === 'steam') {
|
|
if ($isSteamTotp || strtolower($this->service) === 'steam') {
|
|
$this->enforceAsSteam();
|
|
$this->enforceAsSteam();
|
|
}
|
|
}
|
|
@@ -443,16 +432,15 @@ class TwoFAccount extends Model implements Sortable
|
|
$this->icon = $this->storeImageAsIcon($this->generator->getParameter('image'));
|
|
$this->icon = $this->storeImageAsIcon($this->generator->getParameter('image'));
|
|
}
|
|
}
|
|
|
|
|
|
- if (!$this->icon && Settings::get('getOfficialIcons') && !$skipIconFetching) {
|
|
|
|
|
|
+ if (! $this->icon && Settings::get('getOfficialIcons') && ! $skipIconFetching) {
|
|
$this->icon = $this->getDefaultIcon();
|
|
$this->icon = $this->getDefaultIcon();
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
|
|
Log::info(sprintf('TwoFAccount filled with an URI'));
|
|
Log::info(sprintf('TwoFAccount filled with an URI'));
|
|
|
|
|
|
return $this;
|
|
return $this;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Sets model attributes to STEAM values
|
|
* Sets model attributes to STEAM values
|
|
*/
|
|
*/
|
|
@@ -462,14 +450,13 @@ class TwoFAccount extends Model implements Sortable
|
|
$this->digits = 5;
|
|
$this->digits = 5;
|
|
$this->algorithm = self::SHA1;
|
|
$this->algorithm = self::SHA1;
|
|
$this->period = 30;
|
|
$this->period = 30;
|
|
-
|
|
|
|
|
|
+
|
|
Log::info(sprintf('TwoFAccount configured as Steam account'));
|
|
Log::info(sprintf('TwoFAccount configured as Steam account'));
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Returns the OTP type of the instanciated OTP generator
|
|
* Returns the OTP type of the instanciated OTP generator
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @return mixed
|
|
* @return mixed
|
|
*/
|
|
*/
|
|
private function getGeneratorOtpType()
|
|
private function getGeneratorOtpType()
|
|
@@ -477,7 +464,6 @@ class TwoFAccount extends Model implements Sortable
|
|
return Arr::get($this->generatorClassMap, get_class($this->generator));
|
|
return Arr::get($this->generatorClassMap, get_class($this->generator));
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Returns an otpauth URI built with model attribute values
|
|
* Returns an otpauth URI built with model attribute values
|
|
*/
|
|
*/
|
|
@@ -488,9 +474,9 @@ class TwoFAccount extends Model implements Sortable
|
|
return $this->generator->getProvisioningUri();
|
|
return $this->generator->getProvisioningUri();
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Instanciates the OTP generator with model attribute values
|
|
* Instanciates the OTP generator with model attribute values
|
|
|
|
+ *
|
|
* @throws UnsupportedOtpTypeException The defined OTP type is not supported
|
|
* @throws UnsupportedOtpTypeException The defined OTP type is not supported
|
|
* @throws InvalidOtpParameterException One OTP parameter is invalid
|
|
* @throws InvalidOtpParameterException One OTP parameter is invalid
|
|
*/
|
|
*/
|
|
@@ -519,77 +505,76 @@ class TwoFAccount extends Model implements Sortable
|
|
$this->digits ?: self::DEFAULT_DIGITS
|
|
$this->digits ?: self::DEFAULT_DIGITS
|
|
);
|
|
);
|
|
break;
|
|
break;
|
|
-
|
|
|
|
|
|
+
|
|
default:
|
|
default:
|
|
throw new UnsupportedOtpTypeException();
|
|
throw new UnsupportedOtpTypeException();
|
|
}
|
|
}
|
|
|
|
|
|
- if ($this->service) $this->generator->setIssuer($this->service);
|
|
|
|
- if ($this->account) $this->generator->setLabel($this->account);
|
|
|
|
- }
|
|
|
|
- catch (UnsupportedOtpTypeException $exception) {
|
|
|
|
|
|
+ if ($this->service) {
|
|
|
|
+ $this->generator->setIssuer($this->service);
|
|
|
|
+ }
|
|
|
|
+ if ($this->account) {
|
|
|
|
+ $this->generator->setLabel($this->account);
|
|
|
|
+ }
|
|
|
|
+ } catch (UnsupportedOtpTypeException $exception) {
|
|
Log::error(sprintf('%s is not an OTP type supported by the current generator', $this->otp_type));
|
|
Log::error(sprintf('%s is not an OTP type supported by the current generator', $this->otp_type));
|
|
throw $exception;
|
|
throw $exception;
|
|
- }
|
|
|
|
- catch (\Exception|\Throwable $exception) {
|
|
|
|
|
|
+ } catch (\Exception|\Throwable $exception) {
|
|
throw new InvalidOtpParameterException($exception->getMessage());
|
|
throw new InvalidOtpParameterException($exception->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* Gets the image resource pointed by the image url and store it as an icon
|
|
* Gets the image resource pointed by the image url and store it as an icon
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @return string|null The filename of the stored icon or null if the operation fails
|
|
* @return string|null The filename of the stored icon or null if the operation fails
|
|
*/
|
|
*/
|
|
private function storeImageAsIcon(string $url)
|
|
private function storeImageAsIcon(string $url)
|
|
{
|
|
{
|
|
try {
|
|
try {
|
|
- $path_parts = pathinfo($url);
|
|
|
|
- $newFilename = Helpers::getUniqueFilename($path_parts['extension']); //Str::random(40).'.'.$path_parts['extension'];
|
|
|
|
- $imageFile = self::IMAGELINK_STORAGE_PATH . $newFilename;
|
|
|
|
|
|
+ $path_parts = pathinfo($url);
|
|
|
|
+ $newFilename = Helpers::getUniqueFilename($path_parts['extension']);
|
|
|
|
+ $imageFile = self::IMAGELINK_STORAGE_PATH . $newFilename;
|
|
|
|
|
|
try {
|
|
try {
|
|
$response = Http::retry(3, 100)->get($url);
|
|
$response = Http::retry(3, 100)->get($url);
|
|
-
|
|
|
|
|
|
+
|
|
if ($response->successful()) {
|
|
if ($response->successful()) {
|
|
Storage::disk('imagesLink')->put($newFilename, $response->body());
|
|
Storage::disk('imagesLink')->put($newFilename, $response->body());
|
|
}
|
|
}
|
|
- }
|
|
|
|
- catch (\Exception $exception) {
|
|
|
|
|
|
+ } catch (\Exception $exception) {
|
|
Log::error(sprintf('Cannot fetch imageLink at "%s"', $url));
|
|
Log::error(sprintf('Cannot fetch imageLink at "%s"', $url));
|
|
}
|
|
}
|
|
|
|
|
|
- if ( in_array(Storage::mimeType($imageFile), ['image/png', 'image/jpeg', 'image/webp', 'image/bmp'])
|
|
|
|
- && getimagesize(storage_path() . '/app/' . $imageFile) )
|
|
|
|
- {
|
|
|
|
|
|
+ if (in_array(Storage::mimeType($imageFile), ['image/png', 'image/jpeg', 'image/webp', 'image/bmp'])
|
|
|
|
+ && getimagesize(storage_path() . '/app/' . $imageFile)) {
|
|
// Should be a valid image, we move it to the icons disk
|
|
// Should be a valid image, we move it to the icons disk
|
|
if (Storage::disk('icons')->put($newFilename, Storage::disk('imagesLink')->get($newFilename))) {
|
|
if (Storage::disk('icons')->put($newFilename, Storage::disk('imagesLink')->get($newFilename))) {
|
|
Storage::disk('imagesLink')->delete($newFilename);
|
|
Storage::disk('imagesLink')->delete($newFilename);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
Log::info(sprintf('Icon file %s stored', $newFilename));
|
|
Log::info(sprintf('Icon file %s stored', $newFilename));
|
|
- }
|
|
|
|
- else {
|
|
|
|
|
|
+ } else {
|
|
// @codeCoverageIgnoreStart
|
|
// @codeCoverageIgnoreStart
|
|
Storage::disk('imagesLink')->delete($newFilename);
|
|
Storage::disk('imagesLink')->delete($newFilename);
|
|
throw new \Exception('Unsupported mimeType or missing image on storage');
|
|
throw new \Exception('Unsupported mimeType or missing image on storage');
|
|
// @codeCoverageIgnoreEnd
|
|
// @codeCoverageIgnoreEnd
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
return $newFilename;
|
|
return $newFilename;
|
|
}
|
|
}
|
|
// @codeCoverageIgnoreStart
|
|
// @codeCoverageIgnoreStart
|
|
catch (\Exception|\Throwable $ex) {
|
|
catch (\Exception|\Throwable $ex) {
|
|
Log::error(sprintf('Icon storage failed: %s', $ex->getMessage()));
|
|
Log::error(sprintf('Icon storage failed: %s', $ex->getMessage()));
|
|
|
|
+
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
// @codeCoverageIgnoreEnd
|
|
// @codeCoverageIgnoreEnd
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Fetch a logo in the tfa directory and store it as a new stand alone icon
|
|
* Fetch a logo in the tfa directory and store it as a new stand alone icon
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @return string|null The icon
|
|
* @return string|null The icon
|
|
*/
|
|
*/
|
|
private function getDefaultIcon()
|
|
private function getDefaultIcon()
|
|
@@ -599,28 +584,23 @@ class TwoFAccount extends Model implements Sortable
|
|
return Settings::get('getOfficialIcons') ? $logoService->getIcon($this->service) : null;
|
|
return Settings::get('getOfficialIcons') ? $logoService->getIcon($this->service) : null;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Returns an acceptable value
|
|
* Returns an acceptable value
|
|
*/
|
|
*/
|
|
private function decryptOrReturn(mixed $value) : mixed
|
|
private function decryptOrReturn(mixed $value) : mixed
|
|
{
|
|
{
|
|
// Decipher when needed
|
|
// Decipher when needed
|
|
- if ( Settings::get('useEncryption') && $value )
|
|
|
|
- {
|
|
|
|
|
|
+ if (Settings::get('useEncryption') && $value) {
|
|
try {
|
|
try {
|
|
return Crypt::decryptString($value);
|
|
return Crypt::decryptString($value);
|
|
- }
|
|
|
|
- catch (Exception $ex) {
|
|
|
|
|
|
+ } catch (Exception $ex) {
|
|
return __('errors.indecipherable');
|
|
return __('errors.indecipherable');
|
|
}
|
|
}
|
|
- }
|
|
|
|
- else {
|
|
|
|
|
|
+ } else {
|
|
return $value;
|
|
return $value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Encrypt a value
|
|
* Encrypt a value
|
|
*/
|
|
*/
|
|
@@ -629,5 +609,4 @@ class TwoFAccount extends Model implements Sortable
|
|
// should be replaced by laravel 8 attribute encryption casting
|
|
// should be replaced by laravel 8 attribute encryption casting
|
|
return Settings::get('useEncryption') ? Crypt::encryptString($value) : $value;
|
|
return Settings::get('useEncryption') ? Crypt::encryptString($value) : $value;
|
|
}
|
|
}
|
|
-
|
|
|
|
-}
|
|
|
|
|
|
+}
|