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