Oauth2PasswordGrant   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 15
c 1
b 0
f 0
dl 0
loc 31
ccs 0
cts 17
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validateUser() 0 24 4
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\components\server\grants;
4
5
use League\OAuth2\Server\Entities\ClientEntityInterface;
6
use League\OAuth2\Server\Grant\PasswordGrant;
7
use Psr\Http\Message\ServerRequestInterface;
8
use rhertogh\Yii2Oauth2Server\components\server\grants\traits\Oauth2GrantTrait;
9
use rhertogh\Yii2Oauth2Server\exceptions\Oauth2ServerException;
10
use rhertogh\Yii2Oauth2Server\interfaces\components\server\grants\Oauth2PasswordGrantInterface;
11
use rhertogh\Yii2Oauth2Server\interfaces\models\external\user\Oauth2UserInterface;
12
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface;
13
use Yii;
14
use yii\base\InvalidConfigException;
15
16
class Oauth2PasswordGrant extends PasswordGrant implements Oauth2PasswordGrantInterface
17
{
18
    use Oauth2GrantTrait;
19
20
    /**
21
     * @inheritDoc
22
     */
23
    protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client)
24
    {
25
        if (!($client instanceof Oauth2ClientInterface)) {
26
            throw new InvalidConfigException(get_class($client) . ' must implement ' . Oauth2ClientInterface::class);
27
        }
28
29
        $user = parent::validateUser($request, $client);
30
        if (!($user instanceof Oauth2UserInterface)) {
31
            throw new InvalidConfigException(
32
                'Yii::$app->user->identity (currently ' . get_class($user)
33
                . ') must implement ' . Oauth2UserInterface::class
34
            );
35
        }
36
37
        if ($user->isOauth2ClientAllowed($client, $this->getIdentifier()) !== true) {
38
            throw Oauth2ServerException::accessDenied(
39
                Yii::t('oauth2', 'User {user_id} is not allowed to use client {client_identifier}.', [
40
                    'user_id' => $user->getId(),
41
                    'client_identifier' => $client->getIdentifier(),
42
                ])
43
            );
44
        }
45
46
        return $user;
47
    }
48
}
49