Completed
Push — master ( 592d95...c00565 )
by Abdelrahman
18:11 queued 10s
created

Client   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 3
dl 0
loc 238
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A user() 0 4 1
A authCodes() 0 4 1
A accessTokens() 0 4 1
A getValidToken() 0 9 1
A findValidToken() 0 10 1
A getPlainSecretAttribute() 0 4 1
A setSecretAttribute() 0 12 2
A firstParty() 0 4 1
A skipsAuthorization() 0 4 1
A isConfidential() 0 4 1
A revoke() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Oauth\Models;
6
7
use Carbon\Carbon;
8
use Illuminate\Database\Eloquent\Model;
9
use Rinvex\Support\Traits\HasTranslations;
10
use Rinvex\Support\Traits\ValidatingTrait;
11
use Illuminate\Database\Eloquent\Relations\MorphTo;
12
13
class Client extends Model
14
{
15
    use HasTranslations;
16
    use ValidatingTrait;
17
18
    /**
19
     * The attributes excluded from the model's JSON form.
20
     *
21
     * @var array
22
     */
23
    protected $hidden = [
24
        'secret',
25
    ];
26
27
    /**
28
     * The temporary plain-text client secret.
29
     *
30
     * @var string|null
31
     */
32
    protected $plainSecret;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected $fillable = [
38
        'user_id',
39
        'user_type',
40
        'name',
41
        'secret',
42
        'redirect',
43
        'grant_type',
44
        'is_revoked',
45
    ];
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected $casts = [
51
        'user_id' => 'integer',
52
        'user_type' => 'string',
53
        'name' => 'string',
54
        'secret' => 'string',
55
        'redirect' => 'string',
56
        'grant_type' => 'string',
57
        'is_revoked' => 'boolean',
58
    ];
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected $observables = [
64
        'validating',
65
        'validated',
66
    ];
67
68
    /**
69
     * The attributes that are translatable.
70
     *
71
     * @var array
72
     */
73
    public $translatable = [
74
        'name',
75
    ];
76
77
    /**
78
     * The default rules that the model will validate against.
79
     *
80
     * @var array
81
     */
82
    protected $rules = [];
83
84
    /**
85
     * Whether the model should throw a
86
     * ValidationException if it fails validation.
87
     *
88
     * @var bool
89
     */
90
    protected $throwValidationExceptions = true;
91
92
    /**
93
     * Create a new Eloquent model instance.
94
     *
95
     * @param array $attributes
96
     */
97
    public function __construct(array $attributes = [])
98
    {
99
        $this->setTable(config('rinvex.oauth.tables.clients'));
100
        $this->mergeRules([
101
            'user_id' => 'required|integer',
102
            'user_type' => 'required|string|strip_tags|max:150',
103
            'name' => 'required|string|strip_tags|max:150',
104
            'secret' => 'nullable|string|max:100',
105
            'redirect' => 'required|url|max:1500',
106
            'grant_type' => 'required|string|strip_tags|max:100',
107
            'is_revoked' => 'sometimes|boolean',
108
        ]);
109
110
        parent::__construct($attributes);
111
    }
112
113
    /**
114
     * Get the user that the client belongs to.
115
     *
116
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
117
     */
118
    public function user(): MorphTo
119
    {
120
        return $this->morphTo('user', 'user_type', 'user_id', 'id');
121
    }
122
123
    /**
124
     * Get all of the authentication codes for the client.
125
     *
126
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
127
     */
128
    public function authCodes()
129
    {
130
        return $this->hasMany(config('rinvex.oauth.models.auth_code'), 'client_id', 'id');
131
    }
132
133
    /**
134
     * Get all of the tokens that belong to the client.
135
     *
136
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
137
     */
138
    public function accessTokens()
139
    {
140
        return $this->hasMany(config('rinvex.oauth.models.access_token'), 'client_id', 'id');
141
    }
142
143
    /**
144
     * Get a valid token instance for the given user and client.
145
     *
146
     * @param \Illuminate\Database\Eloquent\Model $user
147
     *
148
     * @return \Rinvex\Oauth\Models\AccessToken|null
149
     */
150
    public function getValidToken($user)
151
    {
152
        return $this->accessTokens()
153
                    ->where('user_id', $user->getAuthIdentifier())
154
                    ->where('user_type', $user->getMorphClass())
155
                    ->where('is_revoked', false)
156
                    ->where('expires_at', '>', Carbon::now())
157
                    ->first();
158
    }
159
160
    /**
161
     * Find a valid token for the given user and client.
162
     *
163
     * @param \Illuminate\Database\Eloquent\Model $user
164
     *
165
     * @return \Rinvex\Oauth\Models\AccessToken|null
166
     */
167
    public function findValidToken($user)
168
    {
169
        return $this->accessTokens()
170
                    ->where('user_id', $user->getAuthIdentifier())
171
                    ->where('user_type', $user->getMorphClass())
172
                    ->where('is_revoked', false)
173
                    ->where('expires_at', '>', Carbon::now())
174
                    ->latest('expires_at')
175
                    ->first();
176
    }
177
178
    /**
179
     * The temporary non-hashed client secret.
180
     *
181
     * This is only available once during the request that created the client.
182
     *
183
     * @return string|null
184
     */
185
    public function getPlainSecretAttribute()
186
    {
187
        return $this->plainSecret;
188
    }
189
190
    /**
191
     * Set the value of the secret attribute.
192
     *
193
     * @param string|null $value
194
     *
195
     * @return void
196
     */
197
    public function setSecretAttribute($value)
198
    {
199
        $this->plainSecret = $value;
200
201
        if (is_null($value)) {
202
            $this->attributes['secret'] = $value;
203
204
            return;
205
        }
206
207
        $this->attributes['secret'] = password_hash($value, PASSWORD_BCRYPT);
208
    }
209
210
    /**
211
     * Determine if the client is a "first party" client.
212
     *
213
     * @return bool
214
     */
215
    public function firstParty()
216
    {
217
        return in_array($this->grant_type, ['personal_access', 'password']);
218
    }
219
220
    /**
221
     * Determine if the client should skip the authorization prompt.
222
     *
223
     * @return bool
224
     */
225
    public function skipsAuthorization()
226
    {
227
        return false;
228
    }
229
230
    /**
231
     * Determine if the client is a confidential client.
232
     *
233
     * @return bool
234
     */
235
    public function isConfidential()
236
    {
237
        return ! empty($this->secret);
238
    }
239
240
    /**
241
     * Revoke current client and its tokens.
242
     *
243
     * @return void
244
     */
245
    public function revoke()
246
    {
247
        $this->accessTokens()->update(['is_revoked' => true]);
0 ignored issues
show
Bug introduced by
The method update() does not exist on Illuminate\Database\Eloquent\Relations\HasMany. Did you maybe mean rawUpdate()?

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.

Loading history...
248
        $this->forceFill(['is_revoked' => true])->save();
249
    }
250
}
251