|
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\Server\TokenEndpoint; |
|
15
|
|
|
|
|
16
|
|
|
use Interop\Http\Server\RequestHandlerInterface; |
|
17
|
|
|
use Interop\Http\Server\MiddlewareInterface; |
|
18
|
|
|
use OAuth2Framework\Component\Server\Core\Client\ClientRepository; |
|
19
|
|
|
use OAuth2Framework\Component\Server\TokenEndpoint\AuthenticationMethod\AuthenticationMethodManager; |
|
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
21
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class ClientAuthenticationMiddleware implements MiddlewareInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var AuthenticationMethodManager |
|
27
|
|
|
*/ |
|
28
|
|
|
private $authenticationMethodManager; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ClientRepository |
|
32
|
|
|
*/ |
|
33
|
|
|
private $clientRepository; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* ClientAuthenticationMiddleware constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param ClientRepository $clientRepository |
|
39
|
|
|
* @param AuthenticationMethodManager $authenticationMethodManager |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(ClientRepository $clientRepository, AuthenticationMethodManager $authenticationMethodManager) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->clientRepository = $clientRepository; |
|
44
|
|
|
$this->authenticationMethodManager = $authenticationMethodManager; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
51
|
|
|
{ |
|
52
|
|
|
$clientId = $this->authenticationMethodManager->findClientIdAndCredentials($request, $authentication_method, $client_credentials); |
|
53
|
|
|
if (null !== $clientId) { |
|
54
|
|
|
$client = $this->clientRepository->find($clientId); |
|
55
|
|
|
if ($this->authenticationMethodManager->isClientAuthenticated($request, $client, $authentication_method, $client_credentials)) { |
|
|
|
|
|
|
56
|
|
|
$request = $request->withAttribute('client', $client); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $handler->handle($request); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: