1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tbclla\Revolut\Auth; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use tbclla\Revolut\Traits\Encryptable; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @property int|null $id |
11
|
|
|
* @property string|null $type |
12
|
|
|
* @property string|null $value |
13
|
|
|
* @property bool|null $is_encrypted |
14
|
|
|
* @property \Carbon\Carbon|null $created_at |
15
|
|
|
* @property \Carbon\Carbon|null $expires_at |
16
|
|
|
*/ |
17
|
|
|
abstract class Token extends Model |
18
|
|
|
{ |
19
|
|
|
use Encryptable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Whether or not to use timestamps |
23
|
|
|
* |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
public $timestamps = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The attributes that are fillable |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $fillable = ['value']; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The attributes that should be cast to native types. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $casts = [ |
41
|
|
|
'is_encrypted' => 'boolean', |
42
|
|
|
'expires_at' => 'datetime', |
43
|
|
|
'created_at' => 'datetime', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The "booting" method of the model. |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
protected static function boot() |
52
|
|
|
{ |
53
|
|
|
parent::boot(); |
54
|
|
|
|
55
|
|
|
static::creating(function($model) { |
56
|
|
|
$model->type = static::getType(); |
57
|
|
|
$model->expires_at = static::getExpiration(); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
static::addGlobalScope('type', function(Builder $builder) { |
61
|
|
|
$builder->whereType(static::getType()); |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the name of the tokens table |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getTable() |
71
|
|
|
{ |
72
|
|
|
return config('revolut.tokens.database.table_name'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check if the token has expired |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function hasExpired() |
81
|
|
|
{ |
82
|
|
|
return $this->expires_at ? $this->expires_at < now() : false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Scope a query to only inlcude active tokens |
87
|
|
|
* |
88
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
89
|
|
|
* @param bool $isActive |
90
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
91
|
|
|
*/ |
92
|
|
|
public function scopeActive($query, bool $isActive = true) |
93
|
|
|
{ |
94
|
|
|
$col = 'expires_at'; |
95
|
|
|
|
96
|
|
|
return $isActive |
|
|
|
|
97
|
|
|
? $query->where($col, '>', now())->orWhereNull($col) |
98
|
|
|
: $query->where($col, '<=', now()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Delete all expired access tokens |
103
|
|
|
* |
104
|
|
|
* @return int The number of deleted tokens |
105
|
|
|
*/ |
106
|
|
|
public static function clearExpired() |
107
|
|
|
{ |
108
|
|
|
return (int) self::active(false)->delete(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|