-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathSecurityFieldMiddleware.php
126 lines (105 loc) · 4.84 KB
/
SecurityFieldMiddleware.php
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite\Middlewares;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\OutputType;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use TheCodingMachine\GraphQLite\Annotations\FailWith;
use TheCodingMachine\GraphQLite\Annotations\Security;
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;
use TheCodingMachine\GraphQLite\QueryFieldDescriptor;
use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface;
use TheCodingMachine\GraphQLite\Security\AuthorizationServiceInterface;
use Throwable;
use function array_combine;
use function array_keys;
use function assert;
/**
* A field middleware that reads "Security" Symfony annotations.
*/
class SecurityFieldMiddleware implements FieldMiddlewareInterface
{
public function __construct(
private readonly ExpressionLanguage $language,
private readonly AuthenticationServiceInterface $authenticationService,
private readonly AuthorizationServiceInterface $authorizationService,
) {
}
public function process(
QueryFieldDescriptor $queryFieldDescriptor,
FieldHandlerInterface $fieldHandler,
): FieldDefinition|null
{
$annotations = $queryFieldDescriptor->getMiddlewareAnnotations();
/** @var Security[] $securityAnnotations */
$securityAnnotations = $annotations->getAnnotationsByType(Security::class);
if (empty($securityAnnotations)) {
return $fieldHandler->handle($queryFieldDescriptor);
}
$failWith = $annotations->getAnnotationByType(FailWith::class);
// If the failWith value is null and the return type is non nullable, we must set it to nullable.
$makeReturnTypeNullable = false;
$type = $queryFieldDescriptor->getType();
if ($type instanceof NonNull) {
if ($failWith !== null && $failWith->getValue() === null) {
$makeReturnTypeNullable = true;
} else {
foreach ($securityAnnotations as $annotation) {
if (! $annotation->isFailWithSet() || $annotation->getFailWith() !== null) {
continue;
}
$makeReturnTypeNullable = true;
}
}
if ($makeReturnTypeNullable) {
$type = $type->getWrappedType();
assert($type instanceof OutputType);
$queryFieldDescriptor = $queryFieldDescriptor->withType($type);
}
}
$resolver = $queryFieldDescriptor->getResolver();
$originalResolver = $queryFieldDescriptor->getOriginalResolver();
$parameters = $queryFieldDescriptor->getParameters();
$queryFieldDescriptor = $queryFieldDescriptor->withResolver(function (object|null $source, ...$args) use ($originalResolver, $securityAnnotations, $resolver, $failWith, $parameters, $queryFieldDescriptor) {
$variables = $this->getVariables($args, $parameters, $originalResolver->executionSource($source));
foreach ($securityAnnotations as $annotation) {
try {
$authorized = $this->language->evaluate($annotation->getExpression(), $variables);
} catch (Throwable $e) {
throw BadExpressionInSecurityException::wrapException($e, $queryFieldDescriptor);
}
if (! $authorized) {
if ($annotation->isFailWithSet()) {
return $annotation->getFailWith();
}
if ($failWith !== null) {
return $failWith->getValue();
}
throw new MissingAuthorizationException($annotation->getMessage(), $annotation->getStatusCode());
}
}
return $resolver($source, ...$args);
});
return $fieldHandler->handle($queryFieldDescriptor);
}
/**
* @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): 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,
];
$argsName = array_keys($parameters);
$argsByName = array_combine($argsName, $args);
return $variables + $argsByName;
}
}