Completed
Push — master ( 592d95...c00565 )
by Abdelrahman
18:11 queued 10s
created

ClientCredentialsGrant   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A respondToAccessTokenRequest() 0 32 2
A validateUser() 0 8 3
A getIdentifier() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Oauth\Grants;
6
7
use DateInterval;
8
use League\OAuth2\Server\RequestEvent;
9
use Psr\Http\Message\ServerRequestInterface;
10
use League\OAuth2\Server\Grant\AbstractGrant;
11
use League\OAuth2\Server\Exception\OAuthServerException;
12
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
13
14
class ClientCredentialsGrant extends AbstractGrant
15
{
16
    /**
17
     * Respond to an access token request.
18
     *
19
     * @param ServerRequestInterface $request
20
     * @param ResponseTypeInterface  $responseType
21
     * @param DateInterval           $accessTokenTTL
22
     *
23
     * @throws OAuthServerException
24
     *
25
     * @return ResponseTypeInterface
26
     */
27
    public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseTypeInterface $responseType, DateInterval $accessTokenTTL)
28
    {
29
        [$clientId] = $this->getClientCredentials($request);
0 ignored issues
show
Bug introduced by
The variable $clientId does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
30
31
        $client = $this->getClientEntityOrFail($clientId, $request);
32
33
        if (! $client->isConfidential()) {
34
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
35
36
            throw OAuthServerException::invalidClient($request);
37
        }
38
39
        // Validate request
40
        $this->validateClient($request);
41
        $this->validateUser($request);
42
43
        $scopes = $this->validateScopes($this->getRequestParameter('scope', $request, $this->defaultScope));
44
45
        // Finalize the requested scopes
46
        $finalizedScopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client);
47
48
        // Issue and persist access token
49
        $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $this->getRequestParameter('user_id', $request), $finalizedScopes);
50
51
        // Send event to emitter
52
        $this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request));
53
54
        // Inject access token into response type
55
        $responseType->setAccessToken($accessToken);
0 ignored issues
show
Bug introduced by
It seems like $accessToken defined by $this->issueAccessToken(...est), $finalizedScopes) on line 49 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...
56
57
        return $responseType;
58
    }
59
60
    /**
61
     * Validate the authorization code user.
62
     *
63
     * @param \Psr\Http\Message\ServerRequestInterface $request
64
     *
65
     * @throws \League\OAuth2\Server\Exception\OAuthServerException
66
     */
67
    protected function validateUser(ServerRequestInterface $request)
68
    {
69
        [$userType, $userId] = explode(':', $this->getRequestParameter('user_id', $request));
0 ignored issues
show
Bug introduced by
The variable $userId does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $userType does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
70
71
        if ($userType !== request()->user()->getMorphClass() || $userId !== request()->user()->getRouteKey()) {
72
            throw OAuthServerException::invalidRequest('user_id', 'This action is not authorized to this user');
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getIdentifier()
80
    {
81
        return 'client_credentials';
82
    }
83
}
84