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