|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\Server\Endpoint\ClientRegistration; |
|
15
|
|
|
|
|
16
|
|
|
use Assert\Assertion; |
|
17
|
|
|
use Interop\Http\Factory\ResponseFactoryInterface; |
|
18
|
|
|
use Interop\Http\Server\RequestHandlerInterface; |
|
19
|
|
|
use Interop\Http\Server\MiddlewareInterface; |
|
20
|
|
|
use OAuth2Framework\Component\Server\Command\Client\CreateClientCommand; |
|
21
|
|
|
use OAuth2Framework\Component\Server\DataTransporter; |
|
22
|
|
|
use OAuth2Framework\Component\Server\Model\Client\Client; |
|
23
|
|
|
use OAuth2Framework\Component\Server\Model\Client\ClientId; |
|
24
|
|
|
use OAuth2Framework\Component\Server\Model\DataBag\DataBag; |
|
25
|
|
|
use OAuth2Framework\Component\Server\Model\InitialAccessToken\InitialAccessToken; |
|
26
|
|
|
use OAuth2Framework\Component\Server\Response\OAuth2Exception; |
|
27
|
|
|
use OAuth2Framework\Component\Server\Response\OAuth2ResponseFactoryManager; |
|
28
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
29
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
30
|
|
|
use Ramsey\Uuid\Uuid; |
|
31
|
|
|
use SimpleBus\Message\Bus\MessageBus; |
|
32
|
|
|
|
|
33
|
|
|
final class ClientRegistrationEndpoint implements MiddlewareInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var MessageBus |
|
37
|
|
|
*/ |
|
38
|
|
|
private $messageBus; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var ResponseFactoryInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $responseFactory; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* ClientRegistrationEndpoint constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param ResponseFactoryInterface $responseFactory |
|
49
|
|
|
* @param MessageBus $messageBus |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(ResponseFactoryInterface $responseFactory, MessageBus $messageBus) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->responseFactory = $responseFactory; |
|
54
|
|
|
$this->messageBus = $messageBus; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $requestHandler = null): ResponseInterface |
|
61
|
|
|
{ |
|
62
|
|
|
$this->checkRequest($request); |
|
63
|
|
|
$data = new DataTransporter(); |
|
64
|
|
|
$initialAccessToken = $request->getAttribute('initial_access_token'); |
|
65
|
|
|
|
|
66
|
|
|
try { |
|
67
|
|
|
if (null !== $initialAccessToken) { |
|
68
|
|
|
Assertion::isInstanceOf($initialAccessToken, InitialAccessToken::class, 'Initial Access Token is missing or invalid.'); |
|
69
|
|
|
$userAccountId = $initialAccessToken->getUserAccountId(); |
|
70
|
|
|
} else { |
|
71
|
|
|
$userAccountId = null; |
|
72
|
|
|
} |
|
73
|
|
|
$commandParameters = DataBag::createFromArray($request->getParsedBody() ?? []); |
|
74
|
|
|
// Allow custom client id generators |
|
75
|
|
|
$clientId = ClientId::create(Uuid::uuid4()->toString()); |
|
76
|
|
|
$command = CreateClientCommand::create($clientId, $userAccountId, $commandParameters, $data); |
|
77
|
|
|
$this->messageBus->handle($command); |
|
78
|
|
|
} catch (\InvalidArgumentException $e) { |
|
79
|
|
|
throw new OAuth2Exception(400, ['error' => OAuth2ResponseFactoryManager::ERROR_INVALID_REQUEST, 'error_description' => $e->getMessage()]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this->createResponse($data->getData()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param ServerRequestInterface $request |
|
87
|
|
|
* |
|
88
|
|
|
* @throws OAuth2Exception |
|
89
|
|
|
*/ |
|
90
|
|
|
private function checkRequest(ServerRequestInterface $request) |
|
91
|
|
|
{ |
|
92
|
|
|
if ('POST' !== $request->getMethod()) { |
|
93
|
|
|
throw new OAuth2Exception( |
|
94
|
|
|
405, |
|
95
|
|
|
[ |
|
96
|
|
|
'error' => OAuth2ResponseFactoryManager::ERROR_INVALID_REQUEST, |
|
97
|
|
|
'error_description' => 'Unsupported method.', |
|
98
|
|
|
] |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param Client $client |
|
105
|
|
|
* |
|
106
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
107
|
|
|
*/ |
|
108
|
|
|
private function createResponse(Client $client): ResponseInterface |
|
109
|
|
|
{ |
|
110
|
|
|
$response = $this->responseFactory->createResponse(201); |
|
111
|
|
|
foreach (['Content-Type' => 'application/json', 'Cache-Control' => 'no-store', 'Pragma' => 'no-cache'] as $k => $v) { |
|
112
|
|
|
$response = $response->withHeader($k, $v); |
|
113
|
|
|
} |
|
114
|
|
|
$response->getBody()->write(json_encode($client->all())); |
|
115
|
|
|
|
|
116
|
|
|
return $response; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|