Passed
Push — master ( cd0cec...4b3abd )
by Alexandre
06:20
created

handleAccessTokenRequest()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 4
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: GCC-MED
5
 * Date: 12/03/2018
6
 * Time: 15:16
7
 */
8
9
namespace OAuth2\Flows;
10
11
12
use OAuth2\Endpoints\AuthorizationEndpoint;
13
use OAuth2\Endpoints\TokenEndpoint;
14
use OAuth2\Exceptions\OAuthException;
15
use OAuth2\GrantTypes\AbstractGrantType;
16
use OAuth2\ScopePolicy\ScopePolicyManager;
17
use OAuth2\Storages\AccessTokenStorageInterface;
18
use OAuth2\Storages\RefreshTokenStorageInterface;
19
use OAuth2\Storages\ResourceOwnerStorageInterface;
20
21
class ResourceOwnerPasswordCredentialsFlow extends AbstractGrantType implements FlowInterface
22
{
23
    /**
24
     * @var ResourceOwnerStorageInterface
25
     */
26
    private $resourceOwnerStorage;
27
    /**
28
     * @var ScopePolicyManager
29
     */
30
    private $scopePolicyManager;
31
32
    public function __construct(ScopePolicyManager $scopePolicyManager,
33
                                ResourceOwnerStorageInterface $resourceOwnerStorage,
34
                                AccessTokenStorageInterface $accessTokenStorage,
35
                                RefreshTokenStorageInterface $refreshTokenStorage)
36
    {
37
        parent::__construct($accessTokenStorage, $refreshTokenStorage);
38
        $this->resourceOwnerStorage = $resourceOwnerStorage;
39
        $this->scopePolicyManager = $scopePolicyManager;
40
    }
41
42
    public function getResponseTypes(): array
43
    {
44
        return [];
45
    }
46
47
    public function getGrantTypes(): array
48
    {
49
        return ['password'];
50
    }
51
52
    /**
53
     * @param TokenEndpoint $tokenEndpoint
54
     * @param array $requestData
55
     * @return array
56
     * @throws OAuthException
57
     */
58
    public function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array
59
    {
60
        if (empty($requestData['username'])) {
61
            throw new OAuthException('invalid_request',
62
                'The request is missing the required parameter username.',
63
                'https://tools.ietf.org/html/rfc7636#section-4.3');
64
        }
65
        if (empty($requestData['password'])) {
66
            throw new OAuthException('invalid_request',
67
                'The request is missing the required parameter password.',
68
                'https://tools.ietf.org/html/rfc7636#section-4.3');
69
        }
70
71
        $scopes = $this->scopePolicyManager->getScopes($tokenEndpoint->getClient(), $requestData['scope'] ?? null);
72
        $this->scopePolicyManager->verifyScopes($tokenEndpoint->getClient(), $scopes);
73
74
        $resourceOwnerIdentifier = $this->resourceOwnerStorage->validateCredentials(
75
            $requestData['username'], $requestData['password']);
76
77
        if (!$resourceOwnerIdentifier) {
78
            throw new OAuthException('invalid_grant',
79
                'The provider authorization grant is invalid. Resource owner credentials invalid.',
80
                'https://tools.ietf.org/html/rfc7636#section-4.3');
81
        }
82
83
        return $this->issueTokens(implode(' ', $scopes), $tokenEndpoint->getClient()->getIdentifier(), $resourceOwnerIdentifier);
84
    }
85
86
    public function verifyAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData)
87
    {
88
        throw new \BadMethodCallException();
89
    }
90
91
    public function handleAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData): array
92
    {
93
        throw new \BadMethodCallException();
94
    }
95
96
    public function getDefaultResponseMode(): string
97
    {
98
        throw new \BadMethodCallException();
99
    }
100
101
    public function getUnsupportedResponseModes(): array
102
    {
103
        throw new \BadMethodCallException();
104
    }
105
}