ClientCredentialsGrantType::grant()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
rs 9.0856
cc 3
eloc 11
nc 3
nop 1
1
<?php
2
/**
3
 * @author Boris Guéry <[email protected]>
4
 */
5
6
namespace Bgy\OAuth2\GrantType;
7
8
use Bgy\OAuth2\ClientAuthenticator;
9
use Bgy\OAuth2\Storage\ClientStorage;
10
use Bgy\OAuth2\TokenRequestAttempt;
11
use Bgy\OAuth2\Utils\GrantTypeUtils;
12
13
class ClientCredentialsGrantType implements GrantType
14
{
15
    private $clientAuthenticator;
16
17
    public function __construct(ClientAuthenticator $clientAuthenticator)
18
    {
19
        $this->clientAuthenticator = $clientAuthenticator;
20
    }
21
22
    public function grant(TokenRequestAttempt $tokenRequestAttempt)
23
    {
24
        GrantTypeUtils::ensureRequestedGrantTypeIsSupported($this, $tokenRequestAttempt);
25
26
        try {
27
            GrantTypeUtils::ensureInputDataAreValid($this, $tokenRequestAttempt);
28
29
        } catch (MissingOrInvalidInputData $e) {
30
31
            return GrantDecision::denied(GrantError::invalidRequest($e->getMessage()));
32
        }
33
34
        if (true === $this->clientAuthenticator->isClientValid(
35
                $tokenRequestAttempt->getInputData()->getClientId(),
36
                $tokenRequestAttempt->getInputData()->getClientSecret()
37
            )
38
        ) {
39
40
            return GrantDecision::allowed();
41
        }
42
43
        return GrantDecision::denied(GrantError::accessDenied());
44
    }
45
46
    public function getRequiredInputData()
47
    {
48
        return [
49
            'client_id',
50
            'client_secret',
51
        ];
52
    }
53
54
    public function getIdentifier()
55
    {
56
        return 'client_credentials';
57
    }
58
}
59