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\Endpoint\Server\Messages\Authorization\ErrorResponse; |
13
|
|
|
use OAuth2\Exceptions\OAuthException; |
14
|
|
|
use OAuth2\Roles\Clients\RegisteredClient; |
15
|
|
|
use OAuth2\Roles\ResourceOwnerInterface; |
16
|
|
|
use OAuth2\Storages\AuthorizationCodeStorageInterface; |
17
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
18
|
|
|
use Psr\Http\Message\UriInterface; |
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
|
|
|
* @return array |
43
|
|
|
* @throws OAuthException |
44
|
|
|
*/ |
45
|
2 |
|
public function handle(ServerRequestInterface $request, ResourceOwnerInterface $resourceOwner, RegisteredClient $client, ?array $scope = null): array |
46
|
|
|
{ |
47
|
2 |
|
if (is_array($client->getSupportedGrantTypes()) && !in_array('authorization_code', $client->getSupportedGrantTypes())) { |
48
|
|
|
throw new OAuthException('unauthorized_client', |
49
|
|
|
'Client is not authorized to request an authorization code with this method', |
50
|
|
|
'https://tools.ietf.org/html/rfc6749#section-5.2'); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
$redirectUri = $request->getQueryParams()['redirect_uri'] ?? $request->getParsedBody()['redirect_uri'] ?? null; |
54
|
|
|
|
55
|
2 |
|
$requestedScopes = $request->getQueryParams()['scope'] ?? $request->getParsedBody()['scope'] ?? null; |
56
|
2 |
|
$requestedScopes = $requestedScopes ? explode(' ', $requestedScopes) : []; |
57
|
|
|
|
58
|
2 |
|
$scopeRequestedIsIdentical = true; |
59
|
2 |
|
if ((empty($requestedScopes) && !is_null($scope)) || (is_array($scope) && !empty(array_diff($requestedScopes, $scope)))) { |
60
|
2 |
|
$scopeRequestedIsIdentical = false; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
$scope = is_array($scope) ? implode(' ', $scope) : null; |
64
|
2 |
|
$authorizationCode = $this->authorizationCodeStorage->create( |
65
|
2 |
|
$client->getIdentifier(), $resourceOwner->getIdentifier(), $redirectUri, $scope, $scopeRequestedIsIdentical); |
66
|
|
|
|
67
|
|
|
return [ |
68
|
2 |
|
'code' => $authorizationCode->getCode() |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
public function getDefaultResponseMode(): string |
73
|
|
|
{ |
74
|
2 |
|
return self::RESPONSE_MODE_QUERY; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
public function isImplicit(): bool |
78
|
|
|
{ |
79
|
2 |
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
public function requireTLS(): bool |
83
|
|
|
{ |
84
|
2 |
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function generateCode() |
88
|
|
|
{ |
89
|
|
|
return bin2hex(random_bytes(8)); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
public function verifyRequest(ServerRequestInterface $request): void |
93
|
|
|
{ |
94
|
|
|
} |
95
|
|
|
} |