PasswordCredentialsGrant   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 170
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 170
loc 170
c 0
b 0
f 0
wmc 11
lcom 1
cbo 8
ccs 0
cts 82
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 1
A supports() 4 4 1
A handle() 16 16 1
B validateClient() 34 34 4
A validateScopes() 9 9 1
A validateUser() 19 19 2
A getIdentifier() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Phisch\OAuth\Server\Grant;
4
5
use Phisch\OAuth\Server\Entity\ClientEntityInterface;
6
use Phisch\OAuth\Server\Entity\ScopeEntityInterface;
7
use Phisch\OAuth\Server\Entity\UserEntityInterface;
8
use Phisch\OAuth\Server\Exception\AuthorizationServerException;
9
use Phisch\OAuth\Server\Repository\AccessTokenRepositoryInterface;
10
use Phisch\OAuth\Server\Repository\ClientRepositoryInterface;
11
use Phisch\OAuth\Server\Repository\RefreshTokenRepositoryInterface;
12
use Phisch\OAuth\Server\Repository\ScopeRepositoryInterface;
13
use Phisch\OAuth\Server\Repository\UserRepositoryInterface;
14
use Phisch\OAuth\Server\Response\ResponseBuilder;
15
use Phisch\OAuth\Server\Token\TokenType;
16
use Symfony\Component\HttpFoundation\Request;
17
18 View Code Duplication
class PasswordCredentialsGrant implements Grant
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
{
20
21
    /**
22
     * @var ClientRepositoryInterface
23
     */
24
    private $clientRepository;
25
26
    /**
27
     * @var ScopeRepositoryInterface
28
     */
29
    private $scopeRepository;
30
31
    /**
32
     * @var UserRepositoryInterface
33
     */
34
    private $userRepository;
35
36
    /**
37
     * @var AccessTokenRepositoryInterface
38
     */
39
    private $accessTokenRepository;
40
41
    /**
42
     * @var RefreshTokenRepositoryInterface
43
     */
44
    private $refreshTokenRepository;
45
46
    /**
47
     * @var TokenType
48
     */
49
    private $token;
50
51
    /**
52
     * @param ClientRepositoryInterface $clientRepository
53
     * @param ScopeRepositoryInterface $scopeRepository
54
     * @param UserRepositoryInterface $userRepository
55
     * @param AccessTokenRepositoryInterface $accessTokenRepository
56
     * @param RefreshTokenRepositoryInterface $refreshTokenRepository
57
     * @param TokenType $token
58
     */
59
    public function __construct(
60
        ClientRepositoryInterface $clientRepository,
61
        ScopeRepositoryInterface $scopeRepository,
62
        UserRepositoryInterface $userRepository,
63
        AccessTokenRepositoryInterface $accessTokenRepository,
64
        RefreshTokenRepositoryInterface $refreshTokenRepository,
65
        TokenType $token
66
    ) {
67
        $this->clientRepository = $clientRepository;
68
        $this->scopeRepository = $scopeRepository;
69
        $this->userRepository = $userRepository;
70
        $this->accessTokenRepository = $accessTokenRepository;
71
        $this->refreshTokenRepository = $refreshTokenRepository;
72
        $this->token = $token;
73
    }
74
75
    /**
76
     * @param Request $request
77
     * @return bool
78
     */
79
    public function supports(Request $request)
80
    {
81
        return $request->get('grant_type') === $this->getIdentifier();
82
    }
83
84
    /**
85
     * @param Request $request
86
     * @param ResponseBuilder $responseBuilder
87
     * @return mixed
88
     */
89
    public function handle(Request $request, ResponseBuilder $responseBuilder)
90
    {
91
        $client = $this->validateClient($request);
92
        $scopes = $this->validateScopes($request);
93
        $user = $this->validateUser($request);
94
95
        // check if scopes fit for client
96
97
        $expiryDateTime = (new \DateTime())->add(new \DateInterval('PT1H'));
98
        $accessToken = $this->accessTokenRepository->createToken($client, $user, $scopes, $expiryDateTime);
99
100
        $expiryDateTime = (new \DateTime())->add(new \DateInterval('P1M'));
101
        $refreshToken = $this->refreshTokenRepository->createToken($accessToken, $expiryDateTime);
102
103
        return $responseBuilder->success($this->token, $accessToken, $refreshToken, $scopes);
104
    }
105
106
    /**
107
     * @param Request $request
108
     * @return ClientEntityInterface
109
     * @throws AuthorizationServerException
110
     */
111
    private function validateClient(Request $request)
112
    {
113
        $clientId = $request->get('client_id');
114
        $clientSecret = $request->get('client_secret');
115
116
        $client = $this->clientRepository->getClient($clientId);
117
118
        if ($client instanceof ClientEntityInterface === false) {
119
            throw new AuthorizationServerException('The requested client is unknown.', null, null, 'invalid_client');
120
        }
121
122
        if ($client->getSecret() !== $clientSecret) {
123
            throw new AuthorizationServerException(
124
                'The given client_secret does not match the client.',
125
                null,
126
                null,
127
                'invalid_client'
128
            );
129
        }
130
131
        if (!in_array($this->getIdentifier(), $client->getGrantTypes())) {
132
            throw new AuthorizationServerException(
133
                'The authenticated client is not authorized to use this authorization grant type.',
134
                null,
135
                null,
136
                'unauthorized_client'
137
            );
138
        }
139
140
        // TODO: check if validating the uri is necessary here, should be irrelevant for password credentials grant
141
        // TODO: might be necessary if this will be used for multiple grants though
142
143
        return $client;
144
    }
145
146
    /**
147
     * @param Request $request
148
     * @return ScopeEntityInterface[]
149
     */
150
    private function validateScopes(Request $request)
151
    {
152
        $scopeParameter = $request->get('scope');
153
        $scopeIdentifiers = explode(' ', $scopeParameter);
154
155
        $scopes = $this->scopeRepository->getScopes($scopeIdentifiers);
156
157
        return $scopes;
158
    }
159
160
    private function validateUser(Request $request)
161
    {
162
        $username = $request->get('username');
163
        $password = $request->get('password');
164
165
        $user = $this->userRepository->getUser($username, $password);
166
167
        if ($user instanceof UserEntityInterface === false) {
168
            // TODO: check how to correctly handle invalid user credentials, the rfc doesn't really guide that case
169
            throw new AuthorizationServerException(
170
                'The given user credentials are invalid.',
171
                null,
172
                null,
173
                'invalid_user_credentials'
174
            );
175
        }
176
177
        return $user;
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    private function getIdentifier()
184
    {
185
        return 'password';
186
    }
187
}
188