1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Security; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; |
15
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry; |
17
|
|
|
|
18
|
|
|
class EndpointResolver |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var AuthorizationChecker |
22
|
|
|
*/ |
23
|
|
|
protected $authorizationChecker; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $endpointsConfig = []; |
29
|
|
|
|
30
|
22 |
|
public function __construct(AuthorizationChecker $authorizationChecker, array $endpointsConfig = []) |
31
|
|
|
{ |
32
|
22 |
|
$this->authorizationChecker = $authorizationChecker; |
33
|
22 |
|
$this->endpointsConfig = $endpointsConfig['endpoints'] ?? []; |
34
|
22 |
|
} |
35
|
|
|
|
36
|
22 |
|
public function resolveEndpoint(Request $request): ?string |
37
|
|
|
{ |
38
|
22 |
|
if (empty($this->endpointsConfig)) { |
39
|
|
|
return DefinitionRegistry::DEFAULT_ENDPOINT; |
40
|
|
|
} |
41
|
|
|
|
42
|
22 |
|
foreach ($this->endpointsConfig as $endpoint => $config) { |
43
|
22 |
|
if (isset($config['host'])) { |
44
|
|
|
$host = $request->getHost(); |
45
|
|
|
if (preg_match(sprintf('/%s/', $this->cleanExpression($config['host'])), $host)) { |
46
|
|
|
$hostPassed = true; |
47
|
|
|
} else { |
48
|
|
|
$hostPassed = false; |
49
|
|
|
} |
50
|
|
|
} else { |
51
|
22 |
|
$hostPassed = true; |
52
|
|
|
} |
53
|
|
|
|
54
|
22 |
|
if (isset($config['path'])) { |
55
|
|
|
$path = $request->getPathInfo(); |
56
|
|
|
if (preg_match(sprintf('/%s/', $this->cleanExpression($config['path'])), $path)) { |
57
|
|
|
$pathPassed = true; |
58
|
|
|
} else { |
59
|
|
|
$pathPassed = false; |
60
|
|
|
} |
61
|
|
|
} else { |
62
|
22 |
|
$pathPassed = true; |
63
|
|
|
} |
64
|
|
|
|
65
|
22 |
|
if (isset($config['roles'])) { |
66
|
|
|
try { |
67
|
22 |
|
$rolePassed = $this->authorizationChecker->isGranted($config['roles']); |
68
|
|
|
} catch (AuthenticationCredentialsNotFoundException $exception) { |
69
|
22 |
|
$rolePassed = false; |
70
|
|
|
} |
71
|
|
|
} else { |
72
|
|
|
$rolePassed = true; |
73
|
|
|
} |
74
|
|
|
|
75
|
22 |
|
if ($rolePassed && $hostPassed && $pathPassed) { |
76
|
22 |
|
return $endpoint; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function cleanExpression($exp) |
84
|
|
|
{ |
85
|
|
|
return preg_replace('/\//', '\/', $exp); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|