forked from thecodingmachine/graphqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityInputFieldMiddleware.php
More file actions
104 lines (86 loc) · 4.17 KB
/
SecurityInputFieldMiddleware.php
File metadata and controls
104 lines (86 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite\Middlewares;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use TheCodingMachine\GraphQLite\Annotations\Security;
use TheCodingMachine\GraphQLite\InputField;
use TheCodingMachine\GraphQLite\InputFieldDescriptor;
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;
use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface;
use TheCodingMachine\GraphQLite\Security\AuthorizationServiceInterface;
use Throwable;
use function array_combine;
use function array_keys;
use function array_slice;
use function count;
/**
* A field input middleware that reads "Security" Symfony annotations.
* it is the equivalent to the SecurityFieldMiddleware.
*/
class SecurityInputFieldMiddleware implements InputFieldMiddlewareInterface
{
public function __construct(
private readonly ExpressionLanguage $language,
private readonly AuthenticationServiceInterface $authenticationService,
private readonly AuthorizationServiceInterface $authorizationService,
) {
}
public function process(InputFieldDescriptor $inputFieldDescriptor, InputFieldHandlerInterface $inputFieldHandler): InputField|null
{
$annotations = $inputFieldDescriptor->getMiddlewareAnnotations();
/** @var Security[] $securityAnnotations */
$securityAnnotations = $annotations->getAnnotationsByType(Security::class);
if (empty($securityAnnotations)) {
return $inputFieldHandler->handle($inputFieldDescriptor);
}
$resolver = $inputFieldDescriptor->getResolver();
$originalResolver = $inputFieldDescriptor->getOriginalResolver();
$parameters = $inputFieldDescriptor->getParameters();
// InputField::fromFieldDescriptor prepends a SourceParameter to the parameters list when
// `injectSource` is true, but that injection happens AFTER this middleware runs. See the
// sibling SecurityFieldMiddleware for the detailed comment.
$injectSource = $inputFieldDescriptor->isInjectSource();
$inputFieldDescriptor = $inputFieldDescriptor->withResolver(function (object|null $source, ...$args) use ($originalResolver, $securityAnnotations, $resolver, $parameters, $inputFieldDescriptor, $injectSource) {
$variables = $this->getVariables(
$args,
$parameters,
$injectSource ? $source : $originalResolver->executionSource($source),
$injectSource,
);
foreach ($securityAnnotations as $annotation) {
try {
$authorized = $this->language->evaluate($annotation->getExpression(), $variables);
} catch (Throwable $e) {
throw BadExpressionInSecurityException::wrapException($e, $inputFieldDescriptor);
}
if (! $authorized) {
throw new MissingAuthorizationException($annotation->getMessage(), $annotation->getStatusCode());
}
}
return $resolver($source, ...$args);
});
return $inputFieldHandler->handle($inputFieldDescriptor);
}
/**
* @param array<int|string, mixed> $args
* @param array<string, ParameterInterface> $parameters
*
* @return array<string, mixed>
*/
private function getVariables(array $args, array $parameters, object|null $source, bool $injectSource = false): array
{
$variables = [
// If a user is not logged, we provide an empty user object to make usage easier
'user' => $this->authenticationService->getUser(),
'authorizationService' => $this->authorizationService, // Used by the is_granted expression language function.
'authenticationService' => $this->authenticationService, // Used by the is_logged expression language function.
'this' => $source,
];
if ($injectSource && count($args) > count($parameters)) {
$args = array_slice($args, 1);
}
$argsName = array_keys($parameters);
$argsByName = $argsName ? array_combine($argsName, $args) : [];
return $variables + $argsByName;
}
}