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\Client; |
19
|
|
|
use OAuth2Framework\Component\Server\Core\Client\ClientRepository; |
20
|
|
|
use OAuth2Framework\Component\Server\Core\Response\OAuth2Exception; |
21
|
|
|
use OAuth2Framework\Component\Server\TokenEndpoint\AuthenticationMethod\AuthenticationMethod; |
22
|
|
|
use OAuth2Framework\Component\Server\TokenEndpoint\AuthenticationMethod\AuthenticationMethodManager; |
23
|
|
|
use Psr\Http\Message\ResponseInterface; |
24
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
25
|
|
|
|
26
|
|
|
final class ClientAuthenticationMiddleware implements MiddlewareInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var AuthenticationMethodManager |
30
|
|
|
*/ |
31
|
|
|
private $authenticationMethodManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ClientRepository |
35
|
|
|
*/ |
36
|
|
|
private $clientRepository; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* ClientAuthenticationMiddleware constructor. |
40
|
|
|
* |
41
|
|
|
* @param ClientRepository $clientRepository |
42
|
|
|
* @param AuthenticationMethodManager $authenticationMethodManager |
43
|
|
|
*/ |
44
|
|
|
public function __construct(ClientRepository $clientRepository, AuthenticationMethodManager $authenticationMethodManager) |
45
|
|
|
{ |
46
|
|
|
$this->clientRepository = $clientRepository; |
47
|
|
|
$this->authenticationMethodManager = $authenticationMethodManager; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
|
|
$clientId = $this->authenticationMethodManager->findClientIdAndCredentials($request, $authentication_method, $client_credentials); |
57
|
|
|
if (null !== $clientId) { |
58
|
|
|
$client = $this->clientRepository->find($clientId); |
59
|
|
|
$this->checkClient($client); |
60
|
|
|
$this->checkAuthenticationMethod($request, $client, $authentication_method, $client_credentials); |
|
|
|
|
61
|
|
|
$request = $request->withAttribute('client', $client); |
62
|
|
|
$request = $request->withAttribute('client_authentication_method', $authentication_method); |
63
|
|
|
$request = $request->withAttribute('client_credentials', $client_credentials); |
64
|
|
|
} |
65
|
|
|
} catch (\Exception $e) { |
66
|
|
|
throw new OAuth2Exception(401, OAuth2Exception::ERROR_INVALID_CLIENT, $e->getMessage(), [], $e); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $handler->handle($request); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param null|Client $client |
74
|
|
|
*/ |
75
|
|
|
private function checkClient(?Client $client) |
76
|
|
|
{ |
77
|
|
|
if (null === $client || $client->isDeleted()) { |
78
|
|
|
throw new \InvalidArgumentException('Unknown client or client not authenticated.'); |
79
|
|
|
} |
80
|
|
|
if ($client->areClientCredentialsExpired()) { |
81
|
|
|
throw new \InvalidArgumentException('Client credentials expired.'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ServerRequestInterface $request |
87
|
|
|
* @param Client $client |
88
|
|
|
* @param AuthenticationMethod $authenticationMethod |
89
|
|
|
* @param mixed $client_credentials |
90
|
|
|
*/ |
91
|
|
|
private function checkAuthenticationMethod(ServerRequestInterface $request, Client $client, AuthenticationMethod $authenticationMethod, $client_credentials) |
92
|
|
|
{ |
93
|
|
|
if (!$client->has('token_endpoint_auth_method') || !in_array($client->get('token_endpoint_auth_method'), $authenticationMethod->getSupportedMethods())) { |
94
|
|
|
throw new \InvalidArgumentException('Unknown client or client not authenticated.'); |
95
|
|
|
} |
96
|
|
|
if (!$authenticationMethod->isClientAuthenticated($client, $client_credentials, $request)) { |
97
|
|
|
throw new \InvalidArgumentException('Unknown client or client not authenticated.'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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: