2FAuth/app/TwoFAccount.php

156 lines
2.8 KiB
PHP
Raw Normal View History

2019-05-20 07:37:41 +02:00
<?php
namespace App;
2020-01-24 12:56:38 +01:00
use OTPHP\HOTP;
use OTPHP\Factory;
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\Contracts\Encryption\DecryptException;
2019-05-20 07:37:41 +02:00
class TwoFAccount extends Model
2019-05-20 07:37:41 +02:00
{
2020-01-10 13:43:36 +01:00
/**
* model's array form.
*
* @var array
*/
protected $fillable = ['service', 'account', 'uri', 'icon'];
/**
* 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
*/
protected $appends = ['type', 'counter'];
/**
* 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-01-08 17:03:41 +01:00
/**
* Null empty icon resource has gone
*
* @param string $value
* @return string
2020-03-08 20:16:56 +01:00
*
* @codeCoverageIgnore
2020-01-08 17:03:41 +01:00
*/
public function getIconAttribute($value)
{
2020-01-10 13:43:36 +01:00
if (\App::environment('testing') == false) {
2020-01-27 21:44:29 +01:00
if( !Storage::exists('public/icons/' . $value) ) {
2020-01-08 17:03:41 +01:00
2020-01-10 13:43:36 +01:00
return '';
}
2020-01-08 17:03:41 +01:00
}
return $value;
}
/**
* Prevent setting a missing icon
*
* @param string $value
* @return string
2020-03-08 20:16:56 +01:00
*
* @codeCoverageIgnore
*/
public function setIconAttribute($value)
{
2020-01-27 21:44:29 +01:00
if( !Storage::exists('public/icons/' . $value) && \App::environment('testing') == false ) {
$this->attributes['icon'] = '';
}
else {
$this->attributes['icon'] = $value;
}
}
2020-01-24 12:56:38 +01:00
/**
* Get the account type.
*
* @return string
*/
public function getTypeAttribute()
{
return substr( $this->uri, 0, 15 ) === "otpauth://totp/" ? 'totp' : 'hotp';
}
/**
* Get the account counter in case of HOTP.
*
* @return integer
*/
public function getCounterAttribute()
{
if( $this->type === 'hotp' ) {
$otp = Factory::loadFromProvisioningUri($this->uri);
return $otp->getCounter();
}
return null;
}
/**
* Set the user's first name.
*
* @param string $value
* @return void
*/
// public function setUriAttribute($value)
// {
// $this->attributes['uri'] = encrypt($value);
// }
/**
* Get the user's first name.
*
* @param string $value
* @return string
*/
// public function getUriAttribute($value)
// {
// try {
// return decrypt($value);
// } catch (DecryptException $e) {
// return null;
// }
// }
2019-05-20 07:37:41 +02:00
}