|
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\TokenIntrospectionEndpoint; |
|
15
|
|
|
|
|
16
|
|
|
use Interop\Http\Server\RequestHandlerInterface; |
|
17
|
|
|
use Interop\Http\Server\MiddlewareInterface; |
|
18
|
|
|
use OAuth2Framework\Component\Server\Core\ResourceServer\ResourceServerRepository; |
|
19
|
|
|
use OAuth2Framework\Component\Server\Core\Response\OAuth2Exception; |
|
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
21
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class ResourceServerAuthenticationMiddleware implements MiddlewareInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ResourceServerRepository |
|
27
|
|
|
*/ |
|
28
|
|
|
private $resourceServerRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var TokenIntrospectionEndpointAuthenticationMethodManager |
|
32
|
|
|
*/ |
|
33
|
|
|
private $tokenIntrospectionEndpointAuthenticationMethodManager; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* ResourceServerAuthenticationMiddleware constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param ResourceServerRepository $resourceServerRepository |
|
39
|
|
|
* @param TokenIntrospectionEndpointAuthenticationMethodManager $tokenIntrospectionEndpointAuthenticationMethodManager |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(ResourceServerRepository $resourceServerRepository, TokenIntrospectionEndpointAuthenticationMethodManager $tokenIntrospectionEndpointAuthenticationMethodManager) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->resourceServerRepository = $resourceServerRepository; |
|
44
|
|
|
$this->tokenIntrospectionEndpointAuthenticationMethodManager = $tokenIntrospectionEndpointAuthenticationMethodManager; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
51
|
|
|
{ |
|
52
|
|
|
$resourceServerId = $this->tokenIntrospectionEndpointAuthenticationMethodManager->findResourceServerInformationInTheRequest($request, $authenticationMethod, $resourceServerCredentials); |
|
|
|
|
|
|
53
|
|
|
if (null === $resourceServerId) { |
|
54
|
|
|
throw new OAuth2Exception(401, OAuth2Exception::ERROR_INVALID_RESOURCE_SERVER, 'Resource Server authentication failed.'); |
|
55
|
|
|
} |
|
56
|
|
|
$resourceServer = $this->resourceServerRepository->find($resourceServerId); |
|
57
|
|
|
|
|
58
|
|
|
if (null === $resourceServer || false === $this->tokenIntrospectionEndpointAuthenticationMethodManager->isResourceServerAuthenticated($request, $resourceServer, $authenticationMethod, $resourceServerCredentials)) { |
|
59
|
|
|
throw new OAuth2Exception(401, OAuth2Exception::ERROR_INVALID_RESOURCE_SERVER, 'Resource Server authentication failed.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$request = $request->withAttribute('resource_server', $resourceServer); |
|
63
|
|
|
|
|
64
|
|
|
return $handler->handle($request); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.