2019-05-20 07:37:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2020-11-05 22:54:06 +01:00
|
|
|
use Exception;
|
2020-11-12 00:09:33 +01:00
|
|
|
use OTPHP\TOTP;
|
2020-01-24 12:56:38 +01:00
|
|
|
use OTPHP\HOTP;
|
|
|
|
use OTPHP\Factory;
|
2020-10-31 01:16:15 +01:00
|
|
|
use App\Classes\Options;
|
2020-11-17 21:27:44 +01:00
|
|
|
use ParagonIE\ConstantTime\Base32;
|
2020-03-25 21:58:05 +01:00
|
|
|
use Spatie\EloquentSortable\Sortable;
|
|
|
|
use Spatie\EloquentSortable\SortableTrait;
|
2019-05-20 07:37:41 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-09-17 23:55:09 +02:00
|
|
|
use Illuminate\Support\Facades\App;
|
2020-01-08 17:03:41 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2020-10-31 01:16:15 +01:00
|
|
|
use Illuminate\Support\Facades\Crypt;
|
2019-05-20 07:37:41 +02:00
|
|
|
|
2020-03-25 21:58:05 +01:00
|
|
|
class TwoFAccount extends Model implements Sortable
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2020-03-25 21:58:05 +01:00
|
|
|
|
|
|
|
use SortableTrait;
|
|
|
|
|
2021-09-17 23:55:09 +02:00
|
|
|
/**
|
|
|
|
* A human understandable value to return when attribute decryption fails
|
|
|
|
*/
|
|
|
|
private const INDECIPHERABLE = '*indecipherable*';
|
|
|
|
|
2020-03-25 21:58:05 +01:00
|
|
|
|
2020-01-10 13:43:36 +01:00
|
|
|
/**
|
|
|
|
* model's array form.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-09-17 23:55:09 +02:00
|
|
|
protected $fillable = [];
|
2019-05-24 14:44:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The table associated with the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'twofaccounts';
|
2020-01-08 17:03:41 +01:00
|
|
|
|
|
|
|
|
2020-01-24 12:56:38 +01:00
|
|
|
/**
|
|
|
|
* The accessors to append to the model's array form.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-09-17 23:55:09 +02:00
|
|
|
public $appends = [];
|
2020-01-24 12:56:38 +01:00
|
|
|
|
|
|
|
|
2020-11-06 15:51:52 +01:00
|
|
|
/**
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-09-17 23:55:09 +02:00
|
|
|
protected $hidden = [];
|
2020-11-12 00:09:33 +01:00
|
|
|
|
|
|
|
|
2020-11-23 12:54:06 +01:00
|
|
|
/**
|
|
|
|
* The attributes that should be cast.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-09-17 23:55:09 +02:00
|
|
|
protected $casts = [];
|
2020-11-06 15:51:52 +01:00
|
|
|
|
|
|
|
|
2020-01-31 23:05:06 +01:00
|
|
|
/**
|
|
|
|
* Override The "booting" method of the model
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::deleted(function ($model) {
|
|
|
|
Storage::delete('public/icons/' . $model->icon);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-25 21:58:05 +01:00
|
|
|
/**
|
2021-09-17 23:55:09 +02:00
|
|
|
* Settings for @spatie/eloquent-sortable package
|
2020-03-25 21:58:05 +01:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $sortable = [
|
|
|
|
'order_column_name' => 'order_column',
|
|
|
|
'sort_when_creating' => true,
|
|
|
|
];
|
|
|
|
|
|
|
|
|
2020-11-18 01:13:00 +01:00
|
|
|
/**
|
|
|
|
* Increment the hotp counter by 1
|
|
|
|
* @return void
|
|
|
|
*/
|
2021-09-17 23:55:09 +02:00
|
|
|
// public function increaseHotpCounter() : void
|
|
|
|
// {
|
|
|
|
// if( $this->otpType === 'hotp' ) {
|
|
|
|
// $this->counter = $this->counter + 1;
|
|
|
|
// $this->refreshUri();
|
|
|
|
// }
|
|
|
|
// }
|
2020-11-18 01:13:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-17 23:55:09 +02:00
|
|
|
* Get is_deciphered attribute
|
2020-03-08 20:16:56 +01:00
|
|
|
*
|
2021-09-17 23:55:09 +02:00
|
|
|
* @return bool
|
2020-01-27 13:52:47 +01:00
|
|
|
*
|
|
|
|
*/
|
2021-09-17 23:55:09 +02:00
|
|
|
// public function getIsDecipheredAttribute()
|
|
|
|
// {
|
|
|
|
// $this->attributes['is_deciphered'] = $this->legacy_uri === self::INDECIPHERABLE || $this->account === self::INDECIPHERABLE || $this->secret === self::INDECIPHERABLE ? false : true;
|
|
|
|
// // $this->attributes['is_deciphered'] = 'toto';
|
|
|
|
// }
|
2020-01-27 13:52:47 +01:00
|
|
|
|
|
|
|
|
2020-01-24 12:56:38 +01:00
|
|
|
/**
|
2021-09-17 23:55:09 +02:00
|
|
|
* Get legacy_uri attribute
|
2020-11-12 00:09:33 +01:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-09-17 23:55:09 +02:00
|
|
|
public function getLegacyUriAttribute($value)
|
2020-01-24 12:56:38 +01:00
|
|
|
{
|
2021-09-17 23:55:09 +02:00
|
|
|
|
|
|
|
return $this->decryptOrReturn($value);
|
2020-01-24 12:56:38 +01:00
|
|
|
}
|
2020-01-21 21:31:28 +01:00
|
|
|
/**
|
2021-09-17 23:55:09 +02:00
|
|
|
* Set legacy_uri attribute
|
2020-01-21 21:31:28 +01:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return void
|
|
|
|
*/
|
2021-09-17 23:55:09 +02:00
|
|
|
public function setLegacyUriAttribute($value)
|
2020-10-31 01:16:15 +01:00
|
|
|
{
|
2020-11-18 01:13:00 +01:00
|
|
|
// Encrypt if needed
|
2021-09-17 23:55:09 +02:00
|
|
|
$this->attributes['legacy_uri'] = $this->encryptOrReturn($value);
|
2020-10-31 01:16:15 +01:00
|
|
|
}
|
2020-01-21 21:31:28 +01:00
|
|
|
|
2020-11-12 00:09:33 +01:00
|
|
|
|
2020-01-21 21:31:28 +01:00
|
|
|
/**
|
2020-11-18 01:13:00 +01:00
|
|
|
* Get account attribute
|
2020-01-21 21:31:28 +01:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-11-12 00:09:33 +01:00
|
|
|
public function getAccountAttribute($value)
|
2020-10-31 01:16:15 +01:00
|
|
|
{
|
2021-09-17 23:55:09 +02:00
|
|
|
|
|
|
|
return $this->decryptOrReturn($value);
|
2020-10-31 01:16:15 +01:00
|
|
|
}
|
|
|
|
/**
|
2021-09-17 23:55:09 +02:00
|
|
|
* Set account attribute
|
2020-10-31 01:16:15 +01:00
|
|
|
*
|
2021-09-17 23:55:09 +02:00
|
|
|
* @param string $value
|
2020-10-31 01:16:15 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setAccountAttribute($value)
|
|
|
|
{
|
2020-11-18 01:13:00 +01:00
|
|
|
// Encrypt when needed
|
2021-09-17 23:55:09 +02:00
|
|
|
$this->attributes['account'] = $this->encryptOrReturn($value);
|
2020-10-31 01:16:15 +01:00
|
|
|
}
|
2020-01-21 21:31:28 +01:00
|
|
|
|
2020-11-12 00:09:33 +01:00
|
|
|
|
2020-10-31 01:16:15 +01:00
|
|
|
/**
|
2021-09-17 23:55:09 +02:00
|
|
|
* Get secret attribute
|
2020-10-31 01:16:15 +01:00
|
|
|
*
|
2021-09-17 23:55:09 +02:00
|
|
|
* @param string $value
|
|
|
|
* @return string
|
2020-10-31 01:16:15 +01:00
|
|
|
*/
|
2021-09-17 23:55:09 +02:00
|
|
|
public function getSecretAttribute($value)
|
2020-10-31 01:16:15 +01:00
|
|
|
{
|
2020-11-12 00:09:33 +01:00
|
|
|
|
2021-09-17 23:55:09 +02:00
|
|
|
return $this->decryptOrReturn($value);
|
2020-11-14 18:55:10 +01:00
|
|
|
}
|
|
|
|
/**
|
2021-09-17 23:55:09 +02:00
|
|
|
* Set secret attribute
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return void
|
2020-11-14 18:55:10 +01:00
|
|
|
*/
|
2021-09-17 23:55:09 +02:00
|
|
|
public function setSecretAttribute($value)
|
2020-11-14 18:55:10 +01:00
|
|
|
{
|
2021-09-17 23:55:09 +02:00
|
|
|
// Encrypt when needed
|
|
|
|
$this->attributes['secret'] = $this->encryptOrReturn($value);
|
2020-11-14 18:55:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-12 00:09:33 +01:00
|
|
|
/**
|
2021-09-17 23:55:09 +02:00
|
|
|
* Returns an acceptable value
|
2020-11-06 21:49:43 +01:00
|
|
|
*/
|
2021-09-17 23:55:09 +02:00
|
|
|
private function decryptOrReturn($value)
|
2020-11-06 21:49:43 +01:00
|
|
|
{
|
2021-09-17 23:55:09 +02:00
|
|
|
// Decipher when needed
|
|
|
|
if ( Options::get('useEncryption') )
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return Crypt::decryptString($value);
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
return self::INDECIPHERABLE;
|
|
|
|
}
|
2020-11-22 11:00:55 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-09-17 23:55:09 +02:00
|
|
|
return $value;
|
2020-11-22 11:00:55 +01:00
|
|
|
}
|
2020-11-12 00:09:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-17 23:55:09 +02:00
|
|
|
* Encrypt a value
|
2020-11-12 00:09:33 +01:00
|
|
|
*/
|
2021-09-17 23:55:09 +02:00
|
|
|
private function encryptOrReturn($value)
|
2020-11-12 00:09:33 +01:00
|
|
|
{
|
2021-09-17 23:55:09 +02:00
|
|
|
// should be replaced by laravel 8 attribute encryption casting
|
|
|
|
return Options::get('useEncryption') ? Crypt::encryptString($value) : $value;
|
2020-11-06 21:49:43 +01:00
|
|
|
}
|
|
|
|
|
2020-11-12 00:09:33 +01:00
|
|
|
}
|