Code Duplication    Length = 170-182 lines in 2 locations

src/Grant/AuthorizationCodeGrant.php 1 location

@@ 18-199 (lines=182) @@
15
use Phisch\OAuth\Server\Token\TokenTypeInterface;
16
use Symfony\Component\HttpFoundation\Request;
17
18
class AuthorizationCodeGrant implements AuthorizationGrantInterface
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 TokenTypeInterface
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 TokenTypeInterface $token
58
     */
59
    public function __construct(
60
        ClientRepositoryInterface $clientRepository,
61
        ScopeRepositoryInterface $scopeRepository,
62
        UserRepositoryInterface $userRepository,
63
        AccessTokenRepositoryInterface $accessTokenRepository,
64
        RefreshTokenRepositoryInterface $refreshTokenRepository,
65
        TokenTypeInterface $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('response_type') === $this->getIdentifier();
82
    }
83
84
    /**
85
     * @param Request $request
86
     * @param ResponseBuilderInterface $responseBuilder
87
     * @return mixed
88
     */
89
    public function handle(Request $request, ResponseBuilderInterface $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 'code';
186
    }
187
188
    public function cancel()
189
    {
190
        // TODO: Implement cancel() method.
191
    }
192
193
    public function validate(Request $request)
194
    {
195
        //throw new AuthorizationServerException('fuck fuck fuck',0, null, 'yeah, something went horribly wrong!');
196
    }
197
198
199
}
200

src/Grant/PasswordCredentialsGrant.php 1 location

@@ 18-187 (lines=170) @@
15
use Phisch\OAuth\Server\Token\TokenType;
16
use Symfony\Component\HttpFoundation\Request;
17
18
class PasswordCredentialsGrant implements Grant
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