Completed
Pull Request — master (#987)
by
unknown
04:24
created

PasswordGrant   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 90
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A respondToAccessTokenRequest() 0 27 1
A validateUser() 0 26 4
A getIdentifier() 0 4 1
1
<?php
2
/**
3
 * OAuth 2.0 Password grant.
4
 *
5
 * @author      Alex Bilbie <[email protected]>
6
 * @copyright   Copyright (c) Alex Bilbie
7
 * @license     http://mit-license.org/
8
 *
9
 * @link        https://github.com/thephpleague/oauth2-server
10
 */
11
12
namespace League\OAuth2\Server\Grant;
13
14
use DateInterval;
15
use League\OAuth2\Server\Entities\ClientEntityInterface;
16
use League\OAuth2\Server\Entities\UserEntityInterface;
17
use League\OAuth2\Server\Exception\OAuthServerException;
18
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
19
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
20
use League\OAuth2\Server\RequestEvent;
21
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
24
/**
25
 * Password grant class.
26
 */
27
class PasswordGrant extends AbstractGrant
28
{
29
    /**
30
     * @param UserRepositoryInterface         $userRepository
31
     * @param RefreshTokenRepositoryInterface $refreshTokenRepository
32
     */
33
    public function __construct(
34
        UserRepositoryInterface $userRepository,
35
        RefreshTokenRepositoryInterface $refreshTokenRepository
36
    ) {
37
        $this->setUserRepository($userRepository);
38
        $this->setRefreshTokenRepository($refreshTokenRepository);
39
40
        $this->refreshTokenTTL = new DateInterval('P1M');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function respondToAccessTokenRequest(
47
        ServerRequestInterface $request,
48
        ResponseTypeInterface $responseType,
49
        DateInterval $accessTokenTTL
50
    ) {
51
        // Validate request
52
        $client = $this->validateClient($request);
53
        $scopes = $this->validateScopes($this->getRequestParameter('scope', $request, $this->defaultScope));
54
        $user = $this->validateUser($request, $client);
55
56
        // Finalize the requested scopes
57
        $finalizedScopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client, $user->getIdentifier());
58
59
        // Issue and persist new tokens
60
        $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $finalizedScopes);
61
        $refreshToken = $this->issueRefreshToken($accessToken);
62
63
        // Send events to emitter
64
        $this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request));
65
        $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request));
66
67
        // Inject tokens into response
68
        $responseType->setAccessToken($accessToken);
0 ignored issues
show
Bug introduced by
It seems like $accessToken defined by $this->issueAccessToken(...er(), $finalizedScopes) on line 60 can be null; however, League\OAuth2\Server\Res...rface::setAccessToken() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
69
        $responseType->setRefreshToken($refreshToken);
0 ignored issues
show
Bug introduced by
It seems like $refreshToken defined by $this->issueRefreshToken($accessToken) on line 61 can be null; however, League\OAuth2\Server\Res...face::setRefreshToken() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
70
71
        return $responseType;
72
    }
73
74
    /**
75
     * @param ServerRequestInterface $request
76
     * @param ClientEntityInterface  $client
77
     *
78
     * @throws OAuthServerException
79
     *
80
     * @return UserEntityInterface
81
     */
82
    protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client)
83
    {
84
        $username = $this->getRequestParameter('username', $request);
85
        if (is_null($username)) {
86
            throw OAuthServerException::invalidRequest('username');
87
        }
88
89
        $password = $this->getRequestParameter('password', $request);
90
        if (is_null($password)) {
91
            throw OAuthServerException::invalidRequest('password');
92
        }
93
94
        $user = $this->userRepository->getUserEntityByUserCredentials(
95
            $username,
96
            $password,
97
            $this->getIdentifier(),
98
            $client
99
        );
100
        if ($user instanceof UserEntityInterface === false) {
101
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request));
102
103
            throw OAuthServerException::invalidCredentials();
104
        }
105
106
        return $user;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getIdentifier()
113
    {
114
        return 'password';
115
    }
116
}
117