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