|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Alexandre |
|
5
|
|
|
* Date: 11/06/2018 |
|
6
|
|
|
* Time: 21:06 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace OAuth2\Extensions\PKCE\Endpoints\Authorization; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use OAuth2\Endpoints\Authorization\AuthorizationRequestInterface; |
|
13
|
|
|
use OAuth2\Exceptions\InvalidAuthorizationRequest; |
|
14
|
|
|
use OAuth2\Exceptions\InvalidRequestMethod; |
|
15
|
|
|
use OAuth2\Exceptions\OAuthException; |
|
16
|
|
|
use OAuth2\Roles\ClientTypes\PublicClient; |
|
17
|
|
|
use OAuth2\Roles\ResourceOwnerInterface; |
|
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
19
|
|
|
|
|
20
|
|
|
class AuthorizationRequestBuilder extends \OAuth2\Endpoints\Authorization\AuthorizationRequestBuilder |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param ServerRequestInterface $request |
|
24
|
|
|
* @param ResourceOwnerInterface $resourceOwner |
|
25
|
|
|
* @return AuthorizationRequestInterface |
|
26
|
|
|
* @throws InvalidRequestMethod |
|
27
|
|
|
* @throws OAuthException |
|
28
|
|
|
* @throws InvalidAuthorizationRequest |
|
29
|
|
|
*/ |
|
30
|
|
|
public function build(ServerRequestInterface $request, ResourceOwnerInterface $resourceOwner): AuthorizationRequestInterface |
|
31
|
|
|
{ |
|
32
|
|
|
$authorizationRequest = parent::build($request, $resourceOwner); |
|
33
|
|
|
|
|
34
|
|
|
try { |
|
35
|
|
|
$codeChallenge = $authorizationRequest->getData()['code_challenge'] ?? null; |
|
36
|
|
|
|
|
37
|
|
|
$codeChallengeMethod = 'plain'; |
|
38
|
|
|
if(!empty($authorizationRequest->getData()['code_challenge_method'])) { |
|
39
|
|
|
$codeChallengeMethod = $authorizationRequest->getData()['code_challenge_method']; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (empty($codeChallenge)) { |
|
43
|
|
|
if ($authorizationRequest->getClient() instanceof PublicClient) { |
|
44
|
|
|
throw new OAuthException('invalid_request', |
|
45
|
|
|
'The request is missing the required parameter code_challenge for public clients.', |
|
46
|
|
|
'https://tools.ietf.org/html/rfc7636#section-4.4'); |
|
47
|
|
|
} |
|
48
|
|
|
return $authorizationRequest; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (!in_array($codeChallengeMethod, ['plain', 'S256'])) { |
|
52
|
|
|
throw new OAuthException('invalid_request', |
|
53
|
|
|
'The request includes the invalid parameter code_challenge_method. Supported : plain, S256.', |
|
54
|
|
|
'https://tools.ietf.org/html/rfc7636#section-4'); |
|
55
|
|
|
} |
|
56
|
|
|
} catch (OAuthException $e) { |
|
57
|
|
|
throw new InvalidAuthorizationRequest($e, $authorizationRequest->getRedirectUri(), |
|
58
|
|
|
$authorizationRequest->getResponseMode(), $authorizationRequest->getState()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return new AuthorizationRequest($authorizationRequest, $codeChallenge, $codeChallengeMethod); |
|
62
|
|
|
} |
|
63
|
|
|
} |