respondToAccessTokenRequest()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.024

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
nc 2
nop 3
dl 0
loc 28
rs 9.9332
c 1
b 0
f 0
ccs 9
cts 11
cp 0.8182
crap 2.024
1
<?php
2
3
/**
4
 * OAuth 2.0 Client credentials grant.
5
 *
6
 * @author      Alex Bilbie <[email protected]>
7
 * @copyright   Copyright (c) Alex Bilbie
8
 * @license     http://mit-license.org/
9
 *
10
 * @link        https://github.com/thephpleague/oauth2-server
11
 */
12
13
declare(strict_types=1);
14
15
namespace League\OAuth2\Server\Grant;
16
17
use DateInterval;
18
use League\OAuth2\Server\Exception\OAuthServerException;
19
use League\OAuth2\Server\RequestAccessTokenEvent;
20
use League\OAuth2\Server\RequestEvent;
21
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
24
/**
25
 * Client credentials grant class.
26
 */
27
class ClientCredentialsGrant extends AbstractGrant
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32 4
    public function respondToAccessTokenRequest(
33
        ServerRequestInterface $request,
34
        ResponseTypeInterface $responseType,
35
        DateInterval $accessTokenTTL
36
    ): ResponseTypeInterface {
37 4
        $client = $this->validateClient($request);
38
39 3
        if (!$client->isConfidential()) {
40
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
41
42
            throw OAuthServerException::invalidClient($request);
43
        }
44
45 3
        $scopes = $this->validateScopes($this->getRequestParameter('scope', $request, $this->defaultScope));
46
47
        // Finalize the requested scopes
48 3
        $finalizedScopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client);
49
50
        // Issue and persist access token
51 3
        $accessToken = $this->issueAccessToken($accessTokenTTL, $client, null, $finalizedScopes);
52
53
        // Send event to emitter
54 3
        $this->getEmitter()->emit(new RequestAccessTokenEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request, $accessToken));
55
56
        // Inject access token into response type
57 3
        $responseType->setAccessToken($accessToken);
58
59 3
        return $responseType;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 6
    public function getIdentifier(): string
66
    {
67 6
        return 'client_credentials';
68
    }
69
}
70