1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Oauth\Models; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
9
|
|
|
use Silber\Bouncer\Database\Concerns\Authorizable; |
10
|
|
|
use Silber\Bouncer\Database\Concerns\HasAbilities; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
12
|
|
|
|
13
|
|
|
class AccessToken extends Model |
14
|
|
|
{ |
15
|
|
|
use Authorizable; |
16
|
|
|
use HasAbilities; |
17
|
|
|
use ValidatingTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
protected $fillable = [ |
23
|
|
|
'identifier', |
24
|
|
|
'user_id', |
25
|
|
|
'user_type', |
26
|
|
|
'client_id', |
27
|
|
|
'name', |
28
|
|
|
'is_revoked', |
29
|
|
|
'expires_at', |
30
|
|
|
'abilities', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected $casts = [ |
37
|
|
|
'identifier' => 'string', |
38
|
|
|
'user_id' => 'integer', |
39
|
|
|
'user_type' => 'string', |
40
|
|
|
'client_id' => 'integer', |
41
|
|
|
'name' => 'string', |
42
|
|
|
'is_revoked' => 'boolean', |
43
|
|
|
'expires_at' => 'date', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
protected $observables = [ |
50
|
|
|
'validating', |
51
|
|
|
'validated', |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The default rules that the model will validate against. |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $rules = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Whether the model should throw a |
63
|
|
|
* ValidationException if it fails validation. |
64
|
|
|
* |
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
protected $throwValidationExceptions = true; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create a new Eloquent model instance. |
71
|
|
|
* |
72
|
|
|
* @param array $attributes |
73
|
|
|
*/ |
74
|
|
|
public function __construct(array $attributes = []) |
75
|
|
|
{ |
76
|
|
|
$this->setTable(config('rinvex.oauth.tables.access_tokens')); |
77
|
|
|
$this->mergeRules([ |
78
|
|
|
'identifier' => 'required|string|strip_tags|max:100', |
79
|
|
|
'user_id' => 'required|integer', |
80
|
|
|
'user_type' => 'required|string|strip_tags|max:150', |
81
|
|
|
'client_id' => 'required|integer|exists:'.config('rinvex.oauth.tables.clients').',id', |
82
|
|
|
'name' => 'nullable|string|strip_tags|max:150', |
83
|
|
|
'is_revoked' => 'sometimes|boolean', |
84
|
|
|
'expires_at' => 'nullable|date', |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
parent::__construct($attributes); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the client that the token belongs to. |
92
|
|
|
* |
93
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
94
|
|
|
*/ |
95
|
|
|
public function client() |
96
|
|
|
{ |
97
|
|
|
return $this->belongsTo(config('rinvex.oauth.models.client')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the user that the token belongs to. |
102
|
|
|
* |
103
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
104
|
|
|
*/ |
105
|
|
|
public function user(): MorphTo |
106
|
|
|
{ |
107
|
|
|
return $this->morphTo('user', 'user_type', 'user_id', 'id'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get all of the refresh tokens that belong to the access token. |
112
|
|
|
* |
113
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
114
|
|
|
*/ |
115
|
|
|
public function refreshTokens() |
116
|
|
|
{ |
117
|
|
|
return $this->hasMany(config('rinvex.oauth.models.refresh_token'), 'access_token_identifier', 'identifier'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Revoke the token instance. |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function revoke() |
126
|
|
|
{ |
127
|
|
|
$this->refreshTokens()->update(['is_revoked' => true]); |
|
|
|
|
128
|
|
|
|
129
|
|
|
return $this->forceFill(['is_revoked' => true])->save(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Determine if the token is a transient JWT token. |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function transient() |
138
|
|
|
{ |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.