ClientCredentials::getIdentifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\OAuth2\Grant;
4
5
6
use kalanis\OAuth2\Exceptions\UnauthorizedClientException;
7
use kalanis\OAuth2\Storage\AccessTokens\IAccessToken;
8
use kalanis\OAuth2\Storage\Clients\IClient;
9
use kalanis\OAuth2\Storage\ITokenFacade;
10
use kalanis\OAuth2\Storage\RefreshTokens\IRefreshToken;
11
12
13
/**
14
 * ClientCredentials
15
 * @package kalanis\OAuth2\Grant
16
 */
17
class ClientCredentials extends GrantType
18
{
19
    /**
20
     * Get identifier string to this grant type
21
     */
22
    public function getIdentifier(): string
23
    {
24
        return self::CLIENT_CREDENTIALS;
25
    }
26
27
    /**
28
     * Verify request
29
     * @throws UnauthorizedClientException
30
     */
31
    protected function verifyRequest(): void
32
    {
33 1
        if (!$this->input->getParameter(self::CLIENT_SECRET_KEY)) {
34 1
            throw new UnauthorizedClientException;
35
        }
36
    }
37
38
    /**
39
     * Generate access token
40
     * @param IClient $client
41
     * @return array<string, string|int>
42
     */
43
    protected function generateAccessToken(IClient $client): array
44
    {
45 1
        $accessTokenStorage = $this->token->getToken(ITokenFacade::ACCESS_TOKEN);
46 1
        $refreshTokenStorage = $this->token->getToken(ITokenFacade::REFRESH_TOKEN);
47
48
        /** @var IAccessToken $accessToken */
49 1
        $accessToken = $accessTokenStorage->create($client, null, $this->getScope());
50
        /** @var IRefreshToken $refreshToken */
51 1
        $refreshToken = $refreshTokenStorage->create($client, null, $this->getScope());
52
53
        return [
54 1
            'access_token' => $accessToken->getAccessToken(),
55 1
            'token_type' => 'bearer',
56 1
            'expires_in' => $accessTokenStorage->getLifetime(),
57 1
            'refresh_token' => $refreshToken->getRefreshToken(),
58
        ];
59
    }
60
}
61