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\OpenID\ResponseModes\ResponseModeInterface; |
15
|
|
|
use OAuth2\Repositories\ConfigurationRepository; |
16
|
|
|
use OAuth2\Roles\Clients\RegisteredClient; |
17
|
|
|
use OAuth2\Roles\ResourceOwnerInterface; |
18
|
|
|
use OAuth2\OpenID\Storages\AuthorizationCodeStorageInterface; |
19
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
20
|
|
|
|
21
|
|
|
class CodeResponseType implements ResponseTypeInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ConfigurationRepository |
26
|
|
|
*/ |
27
|
|
|
private $configurationRepository; |
28
|
|
|
/** |
29
|
|
|
* @var AuthorizationCodeStorageInterface |
30
|
|
|
*/ |
31
|
|
|
private $authorizationCodeStorage; |
32
|
|
|
|
33
|
|
|
public function __construct(ConfigurationRepository $configurationRepository, AuthorizationCodeStorageInterface $authorizationCodeStorage) |
34
|
|
|
{ |
35
|
|
|
$this->configurationRepository = $configurationRepository; |
36
|
|
|
$this->authorizationCodeStorage = $authorizationCodeStorage; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getResponseType(): string |
40
|
|
|
{ |
41
|
|
|
return 'code'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ServerRequestInterface $request |
46
|
|
|
* @param ResourceOwnerInterface $resourceOwner |
47
|
|
|
* @param RegisteredClient $client |
48
|
|
|
* @param array|null $scope |
49
|
|
|
* @param array|null $extendedResponseTypes |
50
|
|
|
* @return array |
51
|
|
|
* @throws OAuthException |
52
|
|
|
*/ |
53
|
|
|
public function handle(ServerRequestInterface $request, ResourceOwnerInterface $resourceOwner, |
54
|
|
|
RegisteredClient $client, ?array $scope = null, ?array $extendedResponseTypes = null): array |
55
|
|
|
{ |
56
|
|
|
$data = $request->getMethod() === 'GET' ? $request->getQueryParams() : $request->getParsedBody(); |
57
|
|
|
|
58
|
|
|
if (is_array($client->getSupportedGrantTypes()) && !in_array('authorization_code', $client->getSupportedGrantTypes())) { |
59
|
|
|
throw new OAuthException('unauthorized_client', |
60
|
|
|
'Client is not authorized to request an authorization code with this method', |
61
|
|
|
'https://tools.ietf.org/html/rfc6749#section-5.2'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$idToken = |
65
|
|
|
|
66
|
|
|
$requestedScopes = $data['scope'] ?? null; |
67
|
|
|
$requestedScopes = $requestedScopes ? explode(' ', $requestedScopes) : []; |
68
|
|
|
|
69
|
|
|
$scopeRequestedIsIdentical = true; |
70
|
|
|
if ((empty($requestedScopes) && !is_null($scope)) || (is_array($scope) && !empty(array_diff($requestedScopes, $scope)))) { |
71
|
|
|
$scopeRequestedIsIdentical = false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$scope = is_array($scope) ? implode(' ', $scope) : null; |
75
|
|
|
$authorizationCode = $this->authorizationCodeStorage->create( |
76
|
|
|
$client->getIdentifier(), $resourceOwner->getIdentifier(), $idToken, $data['redirect_uri'], $scope, $scopeRequestedIsIdentical); |
|
|
|
|
77
|
|
|
|
78
|
|
|
return [ |
79
|
|
|
'code' => $authorizationCode->getCode() |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param ServerRequestInterface $request |
86
|
|
|
*/ |
87
|
|
|
public function verifyRequest(ServerRequestInterface $request): void |
88
|
|
|
{ |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getDefaultResponseMode(): string |
92
|
|
|
{ |
93
|
|
|
return ResponseModeInterface::RESPONSE_MODE_QUERY; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function isImplicit(): bool |
97
|
|
|
{ |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function requireTLS(): bool |
102
|
|
|
{ |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function generateCode() |
107
|
|
|
{ |
108
|
|
|
return bin2hex(random_bytes(8)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function verifyRequest(ServerRequestInterface $request): void |
112
|
|
|
{ |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function isMultiValuedResponseTypeSupported(): bool |
116
|
|
|
{ |
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getExtendedResponseTypes(): ?array |
121
|
|
|
{ |
122
|
|
|
return null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function isQueryResponseModeSupported(): bool |
126
|
|
|
{ |
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |