|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Limoncello\Application\Authorization; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2020 [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use Limoncello\Application\Exceptions\AuthorizationException; |
|
22
|
|
|
use Limoncello\Auth\Authorization\PolicyDecision\PolicyDecisionPoint; |
|
23
|
|
|
use Limoncello\Auth\Authorization\PolicyEnforcement\PolicyEnforcementPoint; |
|
24
|
|
|
use Limoncello\Auth\Authorization\PolicyEnforcement\Request; |
|
25
|
|
|
use Limoncello\Auth\Authorization\PolicyInformation\PolicyInformationPoint; |
|
26
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyEnforcement\PolicyEnforcementPointInterface; |
|
27
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyEnforcement\RequestInterface; |
|
28
|
|
|
use Limoncello\Contracts\Authorization\AuthorizationManagerInterface; |
|
29
|
|
|
use Psr\Container\ContainerInterface; |
|
30
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
31
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
32
|
|
|
use function assert; |
|
33
|
|
|
use function is_array; |
|
34
|
|
|
use function is_int; |
|
35
|
|
|
use function is_string; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @package Limoncello\Application |
|
39
|
|
|
*/ |
|
40
|
|
|
class AuthorizationManager implements AuthorizationManagerInterface, LoggerAwareInterface |
|
41
|
|
|
{ |
|
42
|
|
|
use LoggerAwareTrait; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var ContainerInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
private $container; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
2 |
|
private $policiesData; |
|
52
|
|
|
|
|
53
|
2 |
|
/** |
|
54
|
|
|
* @param ContainerInterface $container |
|
55
|
|
|
* @param array $policiesData |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(ContainerInterface $container, array $policiesData) |
|
58
|
|
|
{ |
|
59
|
1 |
|
$this->setContainer($container)->setPoliciesData($policiesData); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritdoc |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function isAllowed( |
|
66
|
1 |
|
string $action, |
|
67
|
|
|
string $resourceType = null, |
|
68
|
1 |
|
string $resourceIdentity = null, |
|
69
|
|
|
array $extraParams = [] |
|
70
|
|
|
): bool { |
|
71
|
|
|
$request = $this->createRequest($action, $resourceType, $resourceIdentity, $extraParams); |
|
72
|
|
|
$result = $this->createPolicyEnforcementPoint($this->getContainer())->authorize($request); |
|
73
|
|
|
|
|
74
|
1 |
|
return $result; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritdoc |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function authorize( |
|
81
|
1 |
|
string $action, |
|
82
|
|
|
string $resourceType = null, |
|
83
|
|
|
string $resourceIdentity = null, |
|
84
|
|
|
array $extraParams = [] |
|
85
|
|
|
): void { |
|
86
|
|
|
if ($this->isAllowed($action, $resourceType, $resourceIdentity, $extraParams) !== true) { |
|
87
|
|
|
throw new AuthorizationException($action, $resourceType, $resourceIdentity, $extraParams); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
1 |
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param ContainerInterface $container |
|
93
|
1 |
|
* |
|
94
|
|
|
* @return PolicyEnforcementPointInterface |
|
95
|
|
|
*/ |
|
96
|
1 |
|
protected function createPolicyEnforcementPoint(ContainerInterface $container): PolicyEnforcementPointInterface |
|
97
|
1 |
|
{ |
|
98
|
1 |
|
$contextDefinitions = [ |
|
99
|
|
|
ContextProperties::CTX_CONTAINER => $container, |
|
100
|
1 |
|
]; |
|
101
|
1 |
|
|
|
102
|
1 |
|
$pip = new PolicyInformationPoint($contextDefinitions); |
|
103
|
1 |
|
$pdp = new PolicyDecisionPoint($this->getPoliciesData()); |
|
104
|
|
|
$pep = new PolicyEnforcementPoint($pip, $pdp); |
|
105
|
|
|
|
|
106
|
1 |
|
if ($this->logger !== null) { |
|
107
|
|
|
$pip->setLogger($this->logger); |
|
108
|
|
|
$pdp->setLogger($this->logger); |
|
109
|
|
|
$pep->setLogger($this->logger); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
return $pep; |
|
113
|
|
|
} |
|
114
|
1 |
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return ContainerInterface |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function getContainer(): ContainerInterface |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->container; |
|
121
|
|
|
} |
|
122
|
2 |
|
|
|
123
|
|
|
/** |
|
124
|
2 |
|
* @param ContainerInterface $container |
|
125
|
|
|
* |
|
126
|
2 |
|
* @return self |
|
127
|
|
|
*/ |
|
128
|
|
|
private function setContainer(ContainerInterface $container): self |
|
129
|
|
|
{ |
|
130
|
|
|
$this->container = $container; |
|
131
|
|
|
|
|
132
|
1 |
|
return $this; |
|
133
|
|
|
} |
|
134
|
1 |
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function getPoliciesData(): array |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->policiesData; |
|
141
|
|
|
} |
|
142
|
2 |
|
|
|
143
|
|
|
/** |
|
144
|
2 |
|
* @param array $policiesData |
|
145
|
|
|
* |
|
146
|
2 |
|
* @return self |
|
147
|
|
|
*/ |
|
148
|
|
|
private function setPoliciesData(array $policiesData): self |
|
149
|
|
|
{ |
|
150
|
|
|
$this->policiesData = $policiesData; |
|
151
|
|
|
|
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $action |
|
157
|
1 |
|
* @param string|null $type |
|
158
|
|
|
* @param string|null $identity |
|
159
|
|
|
* @param array $extraParams |
|
160
|
|
|
* |
|
161
|
|
|
* @return RequestInterface |
|
162
|
|
|
*/ |
|
163
|
1 |
|
private function createRequest( |
|
164
|
1 |
|
string $action, |
|
165
|
1 |
|
string $type = null, |
|
166
|
1 |
|
string $identity = null, |
|
167
|
1 |
|
array $extraParams = [] |
|
168
|
1 |
|
): RequestInterface { |
|
169
|
|
|
assert($identity === null || is_string($identity) || is_array($identity) || is_int($identity)); |
|
170
|
|
|
return new Request([ |
|
171
|
|
|
RequestProperties::REQ_ACTION => $action, |
|
172
|
|
|
RequestProperties::REQ_RESOURCE_TYPE => $type, |
|
173
|
|
|
RequestProperties::REQ_RESOURCE_IDENTITY => $identity, |
|
174
|
|
|
] + $extraParams); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|