|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodexShaper\OAuth2\Server\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
|
|
7
|
|
|
class Token extends Model |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The database table used by the model. |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $table = 'oauth_access_tokens'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The "type" of the primary key ID. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $keyType = 'string'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Indicates if the IDs are auto-incrementing. |
|
25
|
|
|
* |
|
26
|
|
|
* @var bool |
|
27
|
|
|
*/ |
|
28
|
|
|
public $incrementing = false; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The guarded attributes on the model. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $guarded = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The attributes that should be cast to native types. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $casts = [ |
|
43
|
|
|
'scopes' => 'array', |
|
44
|
|
|
'revoked' => 'bool', |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The attributes that should be mutated to dates. |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $dates = [ |
|
53
|
|
|
'expires_at', |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Indicates if the model should be timestamped. |
|
58
|
|
|
* |
|
59
|
|
|
* @var bool |
|
60
|
|
|
*/ |
|
61
|
|
|
public $timestamps = false; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the client that the token belongs to. |
|
65
|
|
|
* |
|
66
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
67
|
|
|
*/ |
|
68
|
|
|
public function client() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->belongsTo(Passport::clientModel()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the user that the token belongs to. |
|
75
|
|
|
* |
|
76
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
77
|
|
|
*/ |
|
78
|
|
|
public function user() |
|
79
|
|
|
{ |
|
80
|
|
|
$provider = config('auth.guards.api.provider'); |
|
81
|
|
|
|
|
82
|
|
|
return $this->belongsTo(config('auth.providers.'.$provider.'.model')); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Determine if the token has a given scope. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $scope |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function can($scope) |
|
93
|
|
|
{ |
|
94
|
|
|
if (in_array('*', $this->scopes)) { |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$scopes = Passport::$withInheritedScopes |
|
99
|
|
|
? $this->resolveInheritedScopes($scope) |
|
100
|
|
|
: [$scope]; |
|
101
|
|
|
|
|
102
|
|
|
foreach ($scopes as $scope) { |
|
103
|
|
|
if (array_key_exists($scope, array_flip($this->scopes))) { |
|
104
|
|
|
return true; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Resolve all possible scopes. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $scope |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function resolveInheritedScopes($scope) |
|
119
|
|
|
{ |
|
120
|
|
|
$parts = explode(':', $scope); |
|
121
|
|
|
|
|
122
|
|
|
$scopes = []; |
|
123
|
|
|
|
|
124
|
|
|
for ($i = 0; $i <= count($parts); $i++) { |
|
|
|
|
|
|
125
|
|
|
$scopes[] = implode(':', array_slice($parts, 0, $i)); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $scopes; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Determine if the token is missing a given scope. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $scope |
|
135
|
|
|
* |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
|
|
public function cant($scope) |
|
139
|
|
|
{ |
|
140
|
|
|
return !$this->can($scope); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Revoke the token instance. |
|
145
|
|
|
* |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
public function revoke() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->forceFill(['revoked' => true])->save(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Determine if the token is a transient JWT token. |
|
155
|
|
|
* |
|
156
|
|
|
* @return bool |
|
157
|
|
|
*/ |
|
158
|
|
|
public function transient() |
|
159
|
|
|
{ |
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: