Model::verifyUserForGrant()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 9
cp 0
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 4
crap 20
1
<?php
2
3
namespace CodexShaper\OAuth2\Server;
4
5
use Carbon\Carbon;
6
use CodexShaper\OAuth2\Server\Entities\Client as ClientEntity;
7
use DateTime;
8
9
class Model
10
{
11
    /**
12
     * @var string
13
     */
14
    protected static $authCodeModel = '\CodexShaper\OAuth2\Server\Models\AuthCode';
15
16
    /**
17
     * @var string
18
     */
19
    protected static $clientModel = '\CodexShaper\OAuth2\Server\Models\Client';
20
21
    /**
22
     * @var string
23
     */
24
    protected static $refreshTokenModel = '\CodexShaper\OAuth2\Server\Models\RefreshToken';
25
26
    /**
27
     * @var string
28
     */
29
    protected static $tokenModel = '\CodexShaper\OAuth2\Server\Models\Token';
30
31
    /**
32
     * @var string
33
     */
34
    protected static $userModel = '\CodexShaper\OAuth2\Server\Models\User';
35
36
    /**
37
     * Create a new model instance.
38
     *
39
     * @param string $model The model name
40
     *
41
     * @return \CodexShaper\OAuth2\Server\Models\AuthCode|\CodexShaper\OAuth2\Server\Models\Client|\CodexShaper\OAuth2\Server\Models\RefreshToken|\CodexShaper\OAuth2\Server\Models\Token|\CodexShaper\OAuth2\Server\Models\User|null
42
     */
43
    public static function instance($model)
44
    {
45
        if (!static::${$model}) {
46
            return null;
47
        }
48
49
        return new static::${$model}();
50
    }
51
52
    /**
53
     * Get a client.
54
     *
55
     * @param string $clientIdentifier The client's identifier
56
     *
57
     * @return \League\OAuth2\Server\Entities\ClientEntityInterface|null
58
     */
59
    public static function getClientEntity($clientIdentifier)
60
    {
61
        $client = static::instance('clientModel');
62
63
        $record = $client->where($client->getKeyName(), $clientIdentifier)->first();
64
65
        if (!$record) {
66
            return;
67
        }
68
69
        return new ClientEntity(
70
            $clientIdentifier,
71
            $record->name,
72
            $record->redirect,
73
            $record->isConfidential()
74
        );
75
    }
76
77
    /**
78
     * store a new auth code to permanent storage.
79
     *
80
     * @param \League\OAuth2\Server\Entities\AuthCodeEntityInterface $accessTokenEntity
0 ignored issues
show
Bug introduced by
There is no parameter named $accessTokenEntity. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
81
     *
82
     * @return void
83
     */
84
    public static function storeAuthCode($authCodeEntity)
85
    {
86
        static::instance('authCodeModel')->create([
87
            'id'         => $authCodeEntity->getIdentifier(),
88
            'user_id'    => $authCodeEntity->getUserIdentifier(),
89
            'client_id'  => $authCodeEntity->getClient()->getIdentifier(),
90
            'scopes'     => $authCodeEntity->getScopes(),
91
            'revoked'    => 0,
92
            'expires_at' => $authCodeEntity->getExpiryDateTime(),
93
        ]);
94
    }
95
96
    /**
97
     * store a new access token to permanent storage.
98
     *
99
     * @param \League\OAuth2\Server\Entities\AccessTokenEntityInterface $accessTokenEntity
100
     *
101
     * @return void
102
     */
103
    public static function storeAccessToken($accessTokenEntity)
104
    {
105
        static::instance('tokenModel')->create([
106
            'id'         => $accessTokenEntity->getIdentifier(),
107
            'user_id'    => $accessTokenEntity->getUserIdentifier(),
108
            'client_id'  => $accessTokenEntity->getClient()->getIdentifier(),
109
            'scopes'     => $accessTokenEntity->getScopes(),
110
            'revoked'    => false,
111
            'created_at' => new DateTime(),
112
            'updated_at' => new DateTime(),
113
            'expires_at' => $accessTokenEntity->getExpiryDateTime(),
114
        ]);
115
    }
116
117
    /**
118
     * store a new access refresh token to permanent storage.
119
     *
120
     * @param \League\OAuth2\Server\Entities\RefreshTokenEntityInterface $refreshTokenEntity
121
     *
122
     * @return void
123
     */
124
    public static function storeRefreshToken($refreshTokenEntity)
125
    {
126
        static::instance('refreshTokenModel')->create([
127
            'id'              => $refreshTokenEntity->getIdentifier(),
128
            'access_token_id' => $refreshTokenEntity->getAccessToken()->getIdentifier(),
129
            'revoked'         => false,
130
            'expires_at'      => $refreshTokenEntity->getExpiryDateTime(),
131
        ]);
132
    }
133
134
    /**
135
     * Validate a client's secret.
136
     *
137
     * @param string      $clientIdentifier The client's identifier
138
     * @param null|string $clientSecret     The client's secret (if sent)
139
     * @param null|string $grantType        The type of grant the client is using (if sent)
140
     *
141
     * @return bool
142
     */
143
    public static function validateClientCredentials($clientIdentifier, $clientSecret, $grantType)
144
    {
145
        $client = static::instance('clientModel');
146
147
        $record = $client->where($client->getKeyName(), $clientIdentifier)->first();
148
149
        if (!$record || !static::handlesGrant($record, $grantType)) {
150
            return false;
151
        }
152
153
        $scopes = is_array($record->scopes) ? $record->scopes : [];
154
        Manager::setScopes($scopes);
155
156
        return !$record->isConfidential() || hash_equals($record->secret, (string) $clientSecret);
157
    }
158
159
    /**
160
     * Determine if the given client can handle the given grant type.
161
     *
162
     * @param \CodexShaper\OAuth2\Server\Models\Client $record
163
     * @param string                                   $grantType
164
     *
165
     * @return bool
166
     */
167
    protected static function handlesGrant($record, $grantType)
168
    {
169
        if (is_array($record->grant_types) && !in_array($grantType, $record->grant_types)) {
170
            return false;
171
        }
172
173
        switch ($grantType) {
174
            case 'authorization_code':
175
                return $record->authorization_code_client;
176
            case 'password':
177
                return $record->password_client;
178
            case 'client_credentials':
179
                return $record->isConfidential();
180
            default:
181
                return true;
182
        }
183
    }
184
185
    /**
186
     * Veryfy user credentials for current grant.
187
     *
188
     * @param string                                               $username
189
     * @param string                                               $password
190
     * @param string                                               $grantType
191
     * @param \League\OAuth2\Server\Entities\ClientEntityInterface $clientEntity
192
     *
193
     * @return \CodexShaper\OAuth2\Server\Models\User|null
194
     */
195
    public static function verifyUserForGrant($username, $password, $grantType, $clientEntity)
0 ignored issues
show
Unused Code introduced by
The parameter $clientEntity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
196
    {
197
        if ($grantType == 'password') {
198
            $user = static::instance('userModel')->where('user_email', $username)->first();
199
200
            if (!$user) {
201
                return null;
202
            }
203
204
            if (!md5($password) === $user->password) {
205
                return null;
206
            }
207
208
            return $user->{$user->getKeyName()};
209
        }
210
211
        return null;
212
    }
213
214
    /**
215
     * Revoke provided token id.
216
     *
217
     * @param string $model
218
     * @param string $grantType
0 ignored issues
show
Bug introduced by
There is no parameter named $grantType. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
219
     *
220
     * @return void
221
     */
222
    public static function revoke($model, $tokenId)
223
    {
224
        static::instance($model)->whereId($tokenId)->update(['revoked' => true]);
225
    }
226
227
    /**
228
     * Determine if the given token id is revoked or not.
229
     *
230
     * @param string $model
231
     * @param string $grantType
0 ignored issues
show
Bug introduced by
There is no parameter named $grantType. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
232
     *
233
     * @return bool
234
     */
235
    public static function isRevoked($model, $tokenId)
236
    {
237
        return static::instance($model)->whereId($tokenId)->whereRevoked(1)->exists();
238
    }
239
240
    /**
241
     * Authorization token.
242
     *
243
     * @param string $model
244
     * @param array  $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
245
     *
246
     * @return \CodexShaper\OAuth2\Server\Models\AuthCode|\CodexShaper\OAuth2\Server\Models\Client|\CodexShaper\OAuth2\Server\Models\RefreshToken|\CodexShaper\OAuth2\Server\Models\Token|\CodexShaper\OAuth2\Server\Models\User|null
247
     */
248
    public static function findToken($model, $authRequest, $user)
249
    {
250
        return static::instance($model)
251
                ->find($authRequest->getClient()->getIdentifier())
252
                ->tokens()
253
                ->whereUserId($user->getKey())
254
                ->whereRevoked(0)
255
                ->where('expires_at', '>', Carbon::now())
256
                ->latest('expires_at')
257
                ->first();
258
    }
259
}
260