Completed
Pull Request — master (#1029)
by Andrew
01:51 queued 36s
created

respondToAccessTokenRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 13
cts 13
cp 1
rs 9.376
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
/**
3
 * OAuth 2.0 Client credentials 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\Exception\OAuthServerException;
16
use League\OAuth2\Server\RequestEvent;
17
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
20
/**
21
 * Client credentials grant class.
22
 */
23
class ClientCredentialsGrant extends AbstractGrant
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 5
    public function respondToAccessTokenRequest(
29
        ServerRequestInterface $request,
30
        ResponseTypeInterface $responseType,
31
        DateInterval $accessTokenTTL
32
    ) {
33 5
        list($clientId) = $this->getClientCredentials($request);
34
35 5
        $client = $this->getClientEntityOrFail($clientId, $request);
36
37 4
        if (!$client->isConfidential()) {
38 1
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
39
40 1
            throw OAuthServerException::invalidClient($request);
41
        }
42
43
        // Validate request
44 3
        $this->validateClient($request);
45
46 3
        $scopes = $this->validateScopes($this->getRequestParameter('scope', $request, $this->defaultScope));
47
48
        // Finalize the requested scopes
49 3
        $finalizedScopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client);
50
51
        // Issue and persist access token
52 3
        $accessToken = $this->issueAccessToken($accessTokenTTL, $client, null, $finalizedScopes);
53
54
        // Send event to emitter
55 3
        $this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request));
56
57
        // Inject access token into response type
58 3
        $responseType->setAccessToken($accessToken);
0 ignored issues
show
Bug introduced by
It seems like $accessToken defined by $this->issueAccessToken(...null, $finalizedScopes) on line 52 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...
59
60 3
        return $responseType;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 7
    public function getIdentifier()
67
    {
68 7
        return 'client_credentials';
69
    }
70
}
71