2FAuth/app/TwoFAccount.php

219 lines
4.5 KiB
PHP
Raw Normal View History

2019-05-20 07:37:41 +02:00
<?php
namespace App;
2020-11-05 22:54:06 +01:00
use Exception;
// use App\Services\SettingServiceInterface;
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;
2020-01-08 17:03:41 +01:00
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Crypt;
2021-10-15 23:46:21 +02:00
use Illuminate\Support\Facades\Log;
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;
2020-01-10 13:43:36 +01:00
/**
* model's array form.
*
* @var array
*/
protected $fillable = [];
/**
* 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
*/
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
*/
protected $hidden = [];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [];
2020-11-06 15:51:52 +01:00
/**
* Override The "booting" method of the model
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::deleted(function ($model) {
2021-10-15 23:46:21 +02:00
Log::info(sprintf('TwoFAccount #%d deleted', $model->id));
Storage::delete('public/icons/' . $model->icon);
});
}
2020-03-25 21:58:05 +01: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
*/
// public function increaseHotpCounter() : void
// {
// if( $this->otpType === 'hotp' ) {
// $this->counter = $this->counter + 1;
// $this->refreshUri();
// }
// }
2020-11-18 01:13:00 +01:00
/**
* Get is_deciphered attribute
2020-03-08 20:16:56 +01:00
*
* @return bool
*
*/
// 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-24 12:56:38 +01:00
/**
* Get legacy_uri attribute
*
* @param string $value
* @return string
*/
public function getLegacyUriAttribute($value)
2020-01-24 12:56:38 +01:00
{
return $this->decryptOrReturn($value);
2020-01-24 12:56:38 +01:00
}
/**
* Set legacy_uri attribute
*
* @param string $value
* @return void
*/
public function setLegacyUriAttribute($value)
{
2020-11-18 01:13:00 +01:00
// Encrypt if needed
$this->attributes['legacy_uri'] = $this->encryptOrReturn($value);
}
/**
2020-11-18 01:13:00 +01:00
* Get account attribute
*
* @param string $value
* @return string
*/
public function getAccountAttribute($value)
{
return $this->decryptOrReturn($value);
}
/**
* Set account attribute
*
* @param string $value
* @return void
*/
public function setAccountAttribute($value)
{
2020-11-18 01:13:00 +01:00
// Encrypt when needed
$this->attributes['account'] = $this->encryptOrReturn($value);
}
/**
* Get secret attribute
*
* @param string $value
* @return string
*/
public function getSecretAttribute($value)
{
return $this->decryptOrReturn($value);
}
/**
* Set secret attribute
*
* @param string $value
* @return void
*/
public function setSecretAttribute($value)
{
// Encrypt when needed
$this->attributes['secret'] = $this->encryptOrReturn($value);
}
/**
* Returns an acceptable value
*/
private function decryptOrReturn($value)
{
$settingService = resolve('App\Services\SettingServiceInterface');
// Decipher when needed
if ( $settingService->get('useEncryption') )
{
try {
return Crypt::decryptString($value);
}
2021-10-15 23:46:21 +02:00
catch (Exception $ex) {
return __('errors.indecipherable');
}
}
else {
return $value;
}
}
/**
* Encrypt a value
*/
private function encryptOrReturn($value)
{
$settingService = resolve('App\Services\SettingServiceInterface');
// should be replaced by laravel 8 attribute encryption casting
return $settingService->get('useEncryption') ? Crypt::encryptString($value) : $value;
}
}