AuthorizationCode::generateAccessToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 1
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\OAuth2\Grant;
4
5
6
use kalanis\OAuth2\Storage\Clients\IClient;
7
use kalanis\OAuth2\Storage\ITokenFacade;
8
use kalanis\OAuth2\Storage;
9
10
11
/**
12
 * AuthorizationCode
13
 * @package kalanis\OAuth2\Grant
14
 */
15 1
class AuthorizationCode extends GrantType
16
{
17
18
    /**
19
     * @var array<string>
20
     */
21
    private array $scope = [];
22
23
    /**
24
     * Get authorization code identifier
25
     */
26
    public function getIdentifier(): string
27
    {
28
        return self::AUTHORIZATION_CODE;
29
    }
30
31
    /**
32
     * Verify request
33
     * @throws Storage\Exceptions\InvalidAuthorizationCodeException
34
     */
35
    protected function verifyRequest(): void
36
    {
37 1
        $code = strval($this->input->getParameter('code'));
38
39 1
        $entity = $this->token->getToken(ITokenFacade::AUTHORIZATION_CODE)->getEntity($code);
40
        $this->scope = $entity->getScope();
41
42
        $this->token->getToken(ITokenFacade::AUTHORIZATION_CODE)->getStorage()->remove($code);
43
    }
44
45
    /**
46
     * @return array<string>
47
     */
48
    protected function getScope(): array
49
    {
50 1
        return $this->scope;
51
    }
52
53
    /**
54
     * Generate access token
55
     * @param IClient $client
56
     * @return array<string, string|int>
57
     */
58
    protected function generateAccessToken(IClient $client): array
59
    {
60 1
        $accessTokenStorage = $this->token->getToken(ITokenFacade::ACCESS_TOKEN);
61 1
        $refreshTokenStorage = $this->token->getToken(ITokenFacade::REFRESH_TOKEN);
62
63
        /** @var Storage\AccessTokens\IAccessToken $accessToken */
64 1
        $accessToken = $accessTokenStorage->create($client, $this->user->getId(), $this->getScope());
65
        /** @var Storage\RefreshTokens\IRefreshToken $refreshToken */
66 1
        $refreshToken = $refreshTokenStorage->create($client, $this->user->getId(), $this->getScope());
67
68
        return [
69 1
            'access_token' => $accessToken->getAccessToken(),
70 1
            'token_type' => 'bearer',
71 1
            'expires_in' => $accessTokenStorage->getLifetime(),
72 1
            'refresh_token' => $refreshToken->getRefreshToken(),
73
        ];
74
    }
75
}
76