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

Oauth2PasswordGrant   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 35
rs 10
ccs 0
cts 13
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateUser() 0 12 3
A respondToAccessTokenRequest() 0 11 2
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