-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathRequestRulesManager.php
More file actions
106 lines (89 loc) · 3.44 KB
/
Copy pathRequestRulesManager.php
File metadata and controls
106 lines (89 loc) · 3.44 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
105
106
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\oidc\Server\RequestRules;
use LogicException;
use Psr\Http\Message\ServerRequestInterface;
use SimpleSAML\Module\oidc\Server\RequestRules\Interfaces\RequestRuleInterface;
use SimpleSAML\Module\oidc\Server\RequestRules\Interfaces\ResultBagInterface;
use SimpleSAML\Module\oidc\Server\ResponseModes\QueryResponseMode;
use SimpleSAML\Module\oidc\Server\ResponseModes\ResponseModeInterface;
use SimpleSAML\Module\oidc\Services\LoggerService;
use SimpleSAML\OpenID\Codebooks\HttpMethodsEnum;
use function sprintf;
class RequestRulesManager
{
/** @var \SimpleSAML\Module\oidc\Server\RequestRules\Interfaces\RequestRuleInterface[] $rules */
private array $rules = [];
/** @var \SimpleSAML\Module\oidc\Server\RequestRules\Interfaces\ResultBagInterface $resultBag */
protected ResultBagInterface $resultBag;
/** @var array $data Which will be available during each check */
protected array $data = [];
/**
* RequestRulesManager constructor.
* @param \SimpleSAML\Module\oidc\Server\RequestRules\Interfaces\RequestRuleInterface[] $rules
*/
public function __construct(array $rules = [], protected LoggerService $loggerService = new LoggerService())
{
foreach ($rules as $rule) {
$this->add($rule);
}
$this->resultBag = new ResultBag();
}
public function add(RequestRuleInterface $rule): void
{
$this->rules[$rule->getKey()] = $rule;
}
/**
* @param class-string[] $ruleKeysToExecute
* @param ResponseModeInterface $responseMode Response mode which will be
* used in rules execution, as some rules might need to adjust their
* behaviour based on response mode used in request.
* @param HttpMethodsEnum[] $allowedServerRequestMethods Indicate allowed HTTP methods used for request
* @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
*/
public function check(
ServerRequestInterface $request,
array $ruleKeysToExecute,
ResponseModeInterface $responseMode = new QueryResponseMode(),
array $allowedServerRequestMethods = [HttpMethodsEnum::GET],
): ResultBagInterface {
foreach ($ruleKeysToExecute as $ruleKey) {
if (! isset($this->rules[$ruleKey])) {
throw new LogicException(sprintf('Rule for key %s not defined.', $ruleKey));
}
$result = $this->rules[$ruleKey]->checkRule(
$request,
$this->resultBag,
$this->loggerService,
$this->data,
$responseMode,
$allowedServerRequestMethods,
);
if ($result !== null) {
$this->resultBag->add($result);
}
}
return $this->resultBag;
}
/**
* Predefine (add) the existing result, so it can be used by other checkers during check.
*/
public function predefineResult(Result $result): void
{
$this->resultBag->add($result);
}
/**
* Predefine existing ResultBag so that it can be used by other checkers during check.
*/
public function predefineResultBag(ResultBagInterface $resultBag): void
{
$this->resultBag = $resultBag;
}
/**
* Set data which will be available in each check, using key value pair
*/
public function setData(string $key, string $value): void
{
$this->data[$key] = $value;
}
}