Passed
Push — master ( caab97...76a5ed )
by Rutger
12:41
created

Oauth2PasswordGrant::validateUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 12
rs 10
ccs 0
cts 8
cp 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\components\server\grants;
4
5
use League\OAuth2\Server\Entities\ClientEntityInterface;
6
use League\OAuth2\Server\Exception\OAuthServerException;
7
use League\OAuth2\Server\Grant\PasswordGrant;
8
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use rhertogh\Yii2Oauth2Server\interfaces\components\server\grants\Oauth2PasswordGrantInterface;
11
use rhertogh\Yii2Oauth2Server\interfaces\components\user\Oauth2PasswordGrantUserComponentInterface;
12
use Yii;
13
14
class Oauth2PasswordGrant extends PasswordGrant implements Oauth2PasswordGrantInterface
15
{
16
    protected $validatedUser = null;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
17
18
    /**
19
     * @inheritDoc
20
     */
21
    protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client)
22
    {
23
        $this->validatedUser = parent::validateUser($request, $client);
24
        if (Yii::$app->user instanceof Oauth2PasswordGrantUserComponentInterface) {
25
            if (!Yii::$app->user->beforeOauth2PasswordGrantLogin($this->validatedUser, $client, $this)) {
26
                Yii::info('Login rejected by `beforeOauthPasswordGrantLogin()`.', 'oauth2');
27
                $this->validatedUser = null;
28
                throw OAuthServerException::invalidCredentials();
29
            }
30
        }
31
32
        return $this->validatedUser;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function respondToAccessTokenRequest(
39
        ServerRequestInterface $request,
40
        ResponseTypeInterface $responseType,
41
        \DateInterval $accessTokenTTL
42
    )
43
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
44
        $responseType = parent::respondToAccessTokenRequest($request, $responseType, $accessTokenTTL);
45
        if (Yii::$app->user instanceof Oauth2PasswordGrantUserComponentInterface) {
46
            Yii::$app->user->afterOauth2PasswordGrantLogin($this->validatedUser, $this);
47
        }
48
        return $responseType;
49
    }
50
}
51