1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: GCC-MED |
5
|
|
|
* Date: 19/01/2018 |
6
|
|
|
* Time: 15:36 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OAuth2\OpenID\ResponseTypes; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use OAuth2\Config; |
13
|
|
|
use OAuth2\Exceptions\OAuthException; |
14
|
|
|
use OAuth2\Repositories\ConfigurationRepository; |
15
|
|
|
use OAuth2\Roles\Clients\RegisteredClient; |
16
|
|
|
use OAuth2\Roles\ResourceOwnerInterface; |
17
|
|
|
use OAuth2\OpenID\Storages\AuthorizationCodeStorageInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
|
20
|
|
|
class CodeResponseType extends \OAuth2\ResponseTypes\CodeResponseType implements ResponseTypeInterface |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ConfigurationRepository |
25
|
|
|
*/ |
26
|
|
|
private $configurationRepository; |
27
|
|
|
|
28
|
|
|
public function __construct(ConfigurationRepository $configurationRepository, AuthorizationCodeStorageInterface $authorizationCodeStorage) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($authorizationCodeStorage); |
31
|
|
|
$this->configurationRepository = $configurationRepository; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ServerRequestInterface $request |
36
|
|
|
* @param ResourceOwnerInterface $resourceOwner |
37
|
|
|
* @param RegisteredClient $client |
38
|
|
|
* @param array|null $scope |
39
|
|
|
* @param array|null $extendedResponseTypes |
40
|
|
|
* @return array |
41
|
|
|
* @throws OAuthException |
42
|
|
|
*/ |
43
|
|
|
public function handle(ServerRequestInterface $request, ResourceOwnerInterface $resourceOwner, |
44
|
|
|
RegisteredClient $client, ?array $scope = null, ?array $extendedResponseTypes = null): array |
45
|
|
|
{ |
46
|
|
|
$data = $request->getMethod() === 'GET' ? $request->getQueryParams() : $request->getParsedBody(); |
47
|
|
|
|
48
|
|
|
if (is_array($client->getSupportedGrantTypes()) && !in_array('authorization_code', $client->getSupportedGrantTypes())) { |
49
|
|
|
throw new OAuthException('unauthorized_client', |
50
|
|
|
'Client is not authorized to request an authorization code with this method', |
51
|
|
|
'https://tools.ietf.org/html/rfc6749#section-5.2'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$redirectUri = $request->getQueryParams()['redirect_uri'] ?? $request->getParsedBody()['redirect_uri'] ?? null; |
|
|
|
|
55
|
|
|
|
56
|
|
|
$requestedScopes = $request->getQueryParams()['scope'] ?? $request->getParsedBody()['scope'] ?? null; |
57
|
|
|
$requestedScopes = $requestedScopes ? explode(' ', $requestedScopes) : []; |
58
|
|
|
|
59
|
|
|
$scopeRequestedIsIdentical = true; |
60
|
|
|
if ((empty($requestedScopes) && !is_null($scope)) || (is_array($scope) && !empty(array_diff($requestedScopes, $scope)))) { |
61
|
|
|
$scopeRequestedIsIdentical = false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$scope = is_array($scope) ? implode(' ', $scope) : null; |
65
|
|
|
$authorizationCode = $this->authorizationCodeStorage->create( |
66
|
|
|
$client->getIdentifier(), $resourceOwner->getIdentifier(), $data['redirect_uri'], $scope, $scopeRequestedIsIdentical, $idToken); |
|
|
|
|
67
|
|
|
|
68
|
|
|
return [ |
69
|
|
|
'code' => $authorizationCode->getCode() |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param ServerRequestInterface $request |
76
|
|
|
* @throws OAuthException |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
|
|
public function verifyRequest(ServerRequestInterface $request): void |
80
|
|
|
{ |
81
|
|
|
parent::verifyRequest($request); |
82
|
|
|
|
83
|
|
|
$scope = explode(' ', $request->getQueryParams()['scope'] ?? $request->getParsedBody()['scope'] ?? null); |
84
|
|
|
$state = $request->getQueryParams()['state'] ?? $request->getParsedBody()['state'] ?? null; |
85
|
|
|
|
86
|
|
|
if (is_array($scope) && in_array('openid', $scope)) { |
|
|
|
|
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if(!$state && $this->configurationRepository->getConfig(Config::ENFORCE_STATE)) { |
91
|
|
|
throw new OAuthException('invalid_request', |
92
|
|
|
'Missing a required parameter : state', |
93
|
|
|
'http://openid.net/specs/openid-connect-core-1_0.html#AuthorizationEndpoint' |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |