|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2018 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\ClientConfigurationEndpoint; |
|
15
|
|
|
|
|
16
|
|
|
use Http\Message\ResponseFactory; |
|
17
|
|
|
use OAuth2Framework\Component\BearerTokenType\BearerToken; |
|
18
|
|
|
use OAuth2Framework\Component\ClientRule\RuleManager; |
|
19
|
|
|
use OAuth2Framework\Component\Core\Client\Client; |
|
20
|
|
|
use OAuth2Framework\Component\Core\Client\ClientRepository; |
|
21
|
|
|
use OAuth2Framework\Component\Core\Message\OAuth2Error; |
|
22
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
23
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
24
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
25
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
26
|
|
|
|
|
27
|
|
|
final class ClientConfigurationEndpoint implements MiddlewareInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var ClientRepository |
|
31
|
|
|
*/ |
|
32
|
|
|
private $clientRepository; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var BearerToken |
|
36
|
|
|
*/ |
|
37
|
|
|
private $bearerToken; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ResponseFactory |
|
41
|
|
|
*/ |
|
42
|
|
|
private $responseFactory; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var RuleManager |
|
46
|
|
|
*/ |
|
47
|
|
|
private $ruleManager; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct(ClientRepository $clientRepository, BearerToken $bearerToken, ResponseFactory $responseFactory, RuleManager $ruleManager) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->clientRepository = $clientRepository; |
|
52
|
|
|
$this->bearerToken = $bearerToken; |
|
53
|
|
|
$this->responseFactory = $responseFactory; |
|
54
|
|
|
$this->ruleManager = $ruleManager; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface |
|
58
|
|
|
{ |
|
59
|
|
|
$this->checkClient($request); |
|
60
|
|
|
switch ($request->getMethod()) { |
|
61
|
|
|
case 'GET': |
|
62
|
|
|
$get = new ClientConfigurationGetEndpoint($this->responseFactory); |
|
63
|
|
|
|
|
64
|
|
|
return $get->process($request, $next); |
|
65
|
|
|
case 'PUT': |
|
66
|
|
|
$put = new ClientConfigurationPutEndpoint($this->clientRepository, $this->responseFactory, $this->ruleManager); |
|
67
|
|
|
|
|
68
|
|
|
return $put->process($request, $next); |
|
69
|
|
|
case 'DELETE': |
|
70
|
|
|
$delete = new ClientConfigurationDeleteEndpoint($this->clientRepository, $this->responseFactory); |
|
71
|
|
|
|
|
72
|
|
|
return $delete->process($request, $next); |
|
73
|
|
|
default: |
|
74
|
|
|
throw new OAuth2Error(405, OAuth2Error::ERROR_INVALID_REQUEST, 'Unsupported method.'); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function checkClient(ServerRequestInterface $request) |
|
79
|
|
|
{ |
|
80
|
|
|
try { |
|
81
|
|
|
$client = $request->getAttribute('client'); |
|
82
|
|
|
if (!$client instanceof Client) { |
|
83
|
|
|
throw new \RuntimeException('Invalid client or invalid registration access token.'); |
|
84
|
|
|
} |
|
85
|
|
|
if (!$client->has('registration_access_token')) { |
|
86
|
|
|
throw new \RuntimeException('Invalid client or invalid registration access token.'); |
|
87
|
|
|
} |
|
88
|
|
|
$values = []; |
|
89
|
|
|
$token = $this->bearerToken->find($request, $values); |
|
90
|
|
|
if (null === $token) { |
|
91
|
|
|
throw new \RuntimeException('Invalid client or invalid registration access token.'); |
|
92
|
|
|
} |
|
93
|
|
|
if (!\hash_equals($client->get('registration_access_token'), $token)) { |
|
94
|
|
|
throw new \InvalidArgumentException('Invalid client or invalid registration access token.'); |
|
95
|
|
|
} |
|
96
|
|
|
} catch (\InvalidArgumentException $e) { |
|
97
|
|
|
throw OAuth2Error::invalidRequest($e->getMessage(), [], $e); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|