Completed
Pull Request — master (#788)
by
unknown
33:41
created

AuthCodeGrant::completeAuthorizationRequest()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 62
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 62
rs 8.6652
cc 5
eloc 38
nc 9
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author      Alex Bilbie <[email protected]>
4
 * @copyright   Copyright (c) Alex Bilbie
5
 * @license     http://mit-license.org/
6
 *
7
 * @link        https://github.com/thephpleague/oauth2-server
8
 */
9
10
namespace League\OAuth2\Server\Grant;
11
12
use League\OAuth2\Server\Entities\ClientEntityInterface;
13
use League\OAuth2\Server\Entities\ScopeEntityInterface;
14
use League\OAuth2\Server\Entities\UserEntityInterface;
15
use League\OAuth2\Server\Exception\OAuthServerException;
16
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
17
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
18
use League\OAuth2\Server\RequestEvent;
19
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
20
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
21
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
24
class AuthCodeGrant extends AbstractAuthorizeGrant
25
{
26
    /**
27
     * @var \DateInterval
28
     */
29
    private $authCodeTTL;
30
31
    /**
32
     * @var bool
33
     */
34
    private $enableCodeExchangeProof = false;
35
36
    /**
37
     * @param AuthCodeRepositoryInterface     $authCodeRepository
38
     * @param RefreshTokenRepositoryInterface $refreshTokenRepository
39
     * @param \DateInterval                   $authCodeTTL
40
     */
41
    public function __construct(
42
        AuthCodeRepositoryInterface $authCodeRepository,
43
        RefreshTokenRepositoryInterface $refreshTokenRepository,
44
        \DateInterval $authCodeTTL
45
    ) {
46
        $this->setAuthCodeRepository($authCodeRepository);
47
        $this->setRefreshTokenRepository($refreshTokenRepository);
48
        $this->authCodeTTL = $authCodeTTL;
49
        $this->refreshTokenTTL = new \DateInterval('P1M');
50
    }
51
52
    public function enableCodeExchangeProof()
53
    {
54
        $this->enableCodeExchangeProof = true;
55
    }
56
57
    /**
58
     * Respond to an access token request.
59
     *
60
     * @param ServerRequestInterface $request
61
     * @param ResponseTypeInterface  $responseType
62
     * @param \DateInterval          $accessTokenTTL
63
     *
64
     * @throws OAuthServerException
65
     *
66
     * @return ResponseTypeInterface
67
     */
68
    public function respondToAccessTokenRequest(
69
        ServerRequestInterface $request,
70
        ResponseTypeInterface $responseType,
71
        \DateInterval $accessTokenTTL
72
    ) {
73
        // Validate request
74
        $client = $this->validateClient($request);
75
        $encryptedAuthCode = $this->getRequestParameter('code', $request, null);
76
77
        if ($encryptedAuthCode === null) {
78
            throw OAuthServerException::invalidRequest('code');
79
        }
80
81
        // Validate the authorization code
82
        try {
83
            $authCodePayload = json_decode($this->decrypt($encryptedAuthCode));
84
            if (time() > $authCodePayload->expire_time) {
85
                throw OAuthServerException::invalidRequest('code', 'Authorization code has expired');
86
            }
87
88
            if ($this->authCodeRepository->isAuthCodeRevoked($authCodePayload->auth_code_id) === true) {
89
                throw OAuthServerException::invalidRequest('code', 'Authorization code has been revoked');
90
            }
91
92
            if ($authCodePayload->client_id !== $client->getIdentifier()) {
93
                throw OAuthServerException::invalidRequest('code', 'Authorization code was not issued to this client');
94
            }
95
96
            // The redirect URI is required in this request
97
            $redirectUri = $this->getRequestParameter('redirect_uri', $request, null);
98
            if (empty($authCodePayload->redirect_uri) === false && $redirectUri === null) {
99
                throw OAuthServerException::invalidRequest('redirect_uri');
100
            }
101
102
            if ($authCodePayload->redirect_uri !== $redirectUri) {
103
                throw OAuthServerException::invalidRequest('redirect_uri', 'Invalid redirect URI');
104
            }
105
106
            $scopes = [];
107
            foreach ($authCodePayload->scopes as $scopeId) {
108
                $scope = $this->scopeRepository->getScopeEntityByIdentifier($scopeId);
109
110
                if ($scope instanceof ScopeEntityInterface === false) {
111
                    // @codeCoverageIgnoreStart
112
                    throw OAuthServerException::invalidScope($scopeId);
113
                    // @codeCoverageIgnoreEnd
114
                }
115
116
                $scopes[] = $scope;
117
            }
118
119
            // Finalize the requested scopes
120
            $scopes = $this->scopeRepository->finalizeScopes(
121
                $scopes,
122
                $this->getIdentifier(),
123
                $client,
124
                $authCodePayload->user_id
125
            );
126
        } catch (\LogicException  $e) {
127
            throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code');
128
        }
129
130
        // Validate code challenge
131
        if ($this->enableCodeExchangeProof === true) {
132
            $codeVerifier = $this->getRequestParameter('code_verifier', $request, null);
133
            if ($codeVerifier === null) {
134
                throw OAuthServerException::invalidRequest('code_verifier');
135
            }
136
137
            switch ($authCodePayload->code_challenge_method) {
138
                case 'plain':
139
                    if (hash_equals($codeVerifier, $authCodePayload->code_challenge) === false) {
140
                        throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.');
141
                    }
142
143
                    break;
144
                case 'S256':
145
                    if (
146
                        hash_equals(
147
                            urlencode(base64_encode(hash('sha256', $codeVerifier))),
148
                            $authCodePayload->code_challenge
149
                        ) === false
150
                    ) {
151
                        throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.');
152
                    }
153
                    // @codeCoverageIgnoreStart
154
                    break;
155
                default:
156
                    throw OAuthServerException::serverError(
157
                        sprintf(
158
                            'Unsupported code challenge method `%s`',
159
                            $authCodePayload->code_challenge_method
160
                        )
161
                    );
162
                // @codeCoverageIgnoreEnd
163
            }
164
        }
165
166
        // Issue and persist access + refresh tokens
167
        $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes);
168
        $refreshToken = $this->issueRefreshToken($accessToken);
0 ignored issues
show
Bug introduced by
It seems like $accessToken defined by $this->issueAccessToken(...load->user_id, $scopes) on line 167 can be null; however, League\OAuth2\Server\Gra...nt::issueRefreshToken() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
169
170
        // Inject tokens into response type
171
        $responseType->setAccessToken($accessToken);
0 ignored issues
show
Bug introduced by
It seems like $accessToken defined by $this->issueAccessToken(...load->user_id, $scopes) on line 167 can be null; however, League\OAuth2\Server\Res...rface::setAccessToken() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
172
        $responseType->setRefreshToken($refreshToken);
0 ignored issues
show
Bug introduced by
It seems like $refreshToken defined by $this->issueRefreshToken($accessToken) on line 168 can be null; however, League\OAuth2\Server\Res...face::setRefreshToken() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
173
174
        // Revoke used auth code
175
        $this->authCodeRepository->revokeAuthCode($authCodePayload->auth_code_id);
176
177
        return $responseType;
178
    }
179
180
    /**
181
     * Return the grant identifier that can be used in matching up requests.
182
     *
183
     * @return string
184
     */
185
    public function getIdentifier()
186
    {
187
        return 'authorization_code';
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function canRespondToAuthorizationRequest(ServerRequestInterface $request)
194
    {
195
        return (
196
            array_key_exists('response_type', $request->getQueryParams())
197
            && $request->getQueryParams()['response_type'] === 'code'
198
            && isset($request->getQueryParams()['client_id'])
199
        );
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function validateAuthorizationRequest(ServerRequestInterface $request)
206
    {
207
        $clientId = $this->getQueryStringParameter(
208
            'client_id',
209
            $request,
210
            $this->getServerParameter('PHP_AUTH_USER', $request)
211
        );
212
        if (is_null($clientId)) {
213
            throw OAuthServerException::invalidRequest('client_id');
214
        }
215
216
        $client = $this->clientRepository->getClientEntity(
217
            $clientId,
218
            $this->getIdentifier(),
219
            null,
220
            false
221
        );
222
223
        if ($client instanceof ClientEntityInterface === false) {
224
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
225
            throw OAuthServerException::invalidClient();
226
        }
227
228
        $redirectUri = $this->getQueryStringParameter('redirect_uri', $request);
229
        $this->checkRedirectUri($redirectUri, $client, $request);
230
231
        $scopes = $this->validateScopes(
232
            $this->getQueryStringParameter('scope', $request),
233
            is_array($client->getRedirectUri())
234
                ? $client->getRedirectUri()[0]
235
                : $client->getRedirectUri()
236
        );
237
238
        $stateParameter = $this->getQueryStringParameter('state', $request);
239
240
        $authorizationRequest = new AuthorizationRequest();
241
        $authorizationRequest->setGrantTypeId($this->getIdentifier());
242
        $authorizationRequest->setClient($client);
243
        $authorizationRequest->setRedirectUri($redirectUri);
244
        $authorizationRequest->setState($stateParameter);
245
        $authorizationRequest->setScopes($scopes);
246
247
        if ($this->enableCodeExchangeProof === true) {
248
            $codeChallenge = $this->getQueryStringParameter('code_challenge', $request);
249
            if ($codeChallenge === null) {
250
                throw OAuthServerException::invalidRequest('code_challenge');
251
            }
252
253
            if (preg_match('/^[A-Za-z0-9-._~]{43,128}$/', $codeChallenge) !== 1) {
254
                throw OAuthServerException::invalidRequest(
255
                    'code_challenge',
256
                    'The code_challenge must be between 43 and 128 characters'
257
                );
258
            }
259
260
            $codeChallengeMethod = $this->getQueryStringParameter('code_challenge_method', $request, 'plain');
261
            if (in_array($codeChallengeMethod, ['plain', 'S256']) === false) {
262
                throw OAuthServerException::invalidRequest(
263
                    'code_challenge_method',
264
                    'Code challenge method must be `plain` or `S256`'
265
                );
266
            }
267
268
            $authorizationRequest->setCodeChallenge($codeChallenge);
269
            $authorizationRequest->setCodeChallengeMethod($codeChallengeMethod);
270
        }
271
272
        return $authorizationRequest;
273
    }
274
275
    /**
276
     * {@inheritdoc}
277
     */
278
    public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest)
279
    {
280
        if ($authorizationRequest->getUser() instanceof UserEntityInterface === false) {
281
            throw new \LogicException('An instance of UserEntityInterface should be set on the AuthorizationRequest');
282
        }
283
284
        $finalRedirectUri = ($authorizationRequest->getRedirectUri() === null)
285
            ? is_array($authorizationRequest->getClient()->getRedirectUri())
286
                ? $authorizationRequest->getClient()->getRedirectUri()[0]
287
                : $authorizationRequest->getClient()->getRedirectUri()
288
            : $authorizationRequest->getRedirectUri();
289
290
        // The user approved the client, redirect them back with an auth code
291
        if ($authorizationRequest->isAuthorizationApproved() === true) {
292
            $authCode = $this->issueAuthCode(
293
                $this->authCodeTTL,
294
                $authorizationRequest->getClient(),
295
                $authorizationRequest->getUser()->getIdentifier(),
296
                $authorizationRequest->getRedirectUri(),
297
                $authorizationRequest->getScopes()
298
            );
299
300
            $payload = [
301
                'client_id'             => $authCode->getClient()->getIdentifier(),
302
                'redirect_uri'          => $authCode->getRedirectUri(),
303
                'auth_code_id'          => $authCode->getIdentifier(),
304
                'scopes'                => $authCode->getScopes(),
305
                'user_id'               => $authCode->getUserIdentifier(),
306
                'expire_time'           => (new \DateTime())->add($this->authCodeTTL)->format('U'),
307
                'code_challenge'        => $authorizationRequest->getCodeChallenge(),
308
                'code_challenge_method' => $authorizationRequest->getCodeChallengeMethod(),
309
            ];
310
311
            $response = new RedirectResponse();
312
            $response->setRedirectUri(
313
                $this->makeRedirectUri(
314
                    $finalRedirectUri,
315
                    [
316
                        'code'  => $this->encrypt(
317
                            json_encode(
318
                                $payload
319
                            )
320
                        ),
321
                        'state' => $authorizationRequest->getState(),
322
                    ]
323
                )
324
            );
325
326
            return $response;
327
        }
328
329
        // The user denied the client, redirect them back with an error
330
        throw OAuthServerException::accessDenied(
331
            'The user denied the request',
332
            $this->makeRedirectUri(
333
                $finalRedirectUri,
334
                [
335
                    'state' => $authorizationRequest->getState(),
336
                ]
337
            )
338
        );
339
    }
340
}
341