1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\Server\Model\Scope; |
15
|
|
|
|
16
|
|
|
use Assert\Assertion; |
17
|
|
|
use OAuth2Framework\Component\Server\Model\Client\Client; |
18
|
|
|
|
19
|
|
|
final class ScopePolicyManager |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ScopePolicyInterface[] |
23
|
|
|
*/ |
24
|
|
|
private $scopePolicies = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $defaultScopePolicy = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ScopePolicyInterface $scopePolicy |
33
|
|
|
* @param bool $isDefault |
34
|
|
|
* |
35
|
|
|
* @return ScopePolicyManager |
36
|
|
|
*/ |
37
|
|
|
public function add(ScopePolicyInterface $scopePolicy, bool $isDefault = false): ScopePolicyManager |
38
|
|
|
{ |
39
|
|
|
$name = $scopePolicy->name(); |
40
|
|
|
$this->scopePolicies[$name] = $scopePolicy; |
41
|
|
|
|
42
|
|
|
if (true === $isDefault || 1 === count($this->scopePolicies)) { |
43
|
|
|
$this->defaultScopePolicy = $name; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $scopePolicy |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function has(string $scopePolicy): bool |
55
|
|
|
{ |
56
|
|
|
return array_key_exists($scopePolicy, $this->scopePolicies); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $scopePolicyName |
61
|
|
|
* |
62
|
|
|
* @return ScopePolicyInterface |
63
|
|
|
*/ |
64
|
|
|
public function get(string $scopePolicyName): ScopePolicyInterface |
65
|
|
|
{ |
66
|
|
|
Assertion::keyExists($this->scopePolicies, $scopePolicyName, sprintf('The scope policy with name \'%s\' is not supported', $scopePolicyName)); |
67
|
|
|
|
68
|
|
|
return $this->scopePolicies[$scopePolicyName]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return ScopePolicyInterface|null |
73
|
|
|
*/ |
74
|
|
|
public function default(): ?ScopePolicyInterface |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
if (null === $this->defaultScopePolicy) { |
|
|
|
|
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->scopePolicies[$this->defaultScopePolicy]; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $scope |
85
|
|
|
* @param Client $client |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function check(array $scope, Client $client): array |
90
|
|
|
{ |
91
|
|
|
if (empty($scope)) { |
92
|
|
|
$policy = $this->getForClient($client); |
93
|
|
|
|
94
|
|
|
if (null !== $policy) { |
95
|
|
|
return $policy->checkScopePolicy($scope, $client); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $scope; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Client $client |
104
|
|
|
* |
105
|
|
|
* @return ScopePolicyInterface|null |
106
|
|
|
*/ |
107
|
|
|
private function getForClient(Client $client): ?ScopePolicyInterface |
108
|
|
|
{ |
109
|
|
|
if ($client->has('scope_policy') && $this->has($client->get('scope_policy'))) { |
110
|
|
|
$policyName = $client->get('scope_policy'); |
111
|
|
|
|
112
|
|
|
return $this->get($policyName); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->default(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|