Password::verifyRequest()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5.2596

Importance

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