1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 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\Scope\Policy; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Client; |
17
|
|
|
|
18
|
|
|
final class ScopePolicyManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ScopePolicy[] |
22
|
|
|
*/ |
23
|
|
|
private $scopePolicies = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $defaultScopePolicy = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param ScopePolicy $scopePolicy |
32
|
|
|
* @param bool $isDefault |
33
|
|
|
* |
34
|
|
|
* @return ScopePolicyManager |
35
|
|
|
*/ |
36
|
|
|
public function add(ScopePolicy $scopePolicy, bool $isDefault = false): self |
37
|
|
|
{ |
38
|
|
|
$name = $scopePolicy->name(); |
39
|
|
|
$this->scopePolicies[$name] = $scopePolicy; |
40
|
|
|
|
41
|
|
|
if (true === $isDefault || 1 === count($this->scopePolicies)) { |
42
|
|
|
$this->defaultScopePolicy = $name; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $scope |
50
|
|
|
* @param Client $client |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function apply(string $scope, Client $client): string |
55
|
|
|
{ |
56
|
|
|
if (empty($scope)) { |
57
|
|
|
$policy = $this->getForClient($client); |
58
|
|
|
|
59
|
|
|
if (null !== $policy) { |
60
|
|
|
return $policy->applyScopePolicy($scope, $client); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $scope; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string[] |
69
|
|
|
*/ |
70
|
|
|
public function all(): array |
71
|
|
|
{ |
72
|
|
|
return array_keys($this->scopePolicies); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $scopePolicy |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function has(string $scopePolicy): bool |
81
|
|
|
{ |
82
|
|
|
return array_key_exists($scopePolicy, $this->scopePolicies); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $scopePolicyName |
87
|
|
|
* |
88
|
|
|
* @return ScopePolicy |
89
|
|
|
*/ |
90
|
|
|
private function get(string $scopePolicyName): ScopePolicy |
91
|
|
|
{ |
92
|
|
|
if (!$this->has($scopePolicyName)) { |
93
|
|
|
throw new \InvalidArgumentException(sprintf('The scope policy with name "%s" is not supported', $scopePolicyName)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->scopePolicies[$scopePolicyName]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return ScopePolicy|null |
101
|
|
|
*/ |
102
|
|
|
private function default(): ?ScopePolicy |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
if (null === $this->defaultScopePolicy) { |
|
|
|
|
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->scopePolicies[$this->defaultScopePolicy]; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Client $client |
113
|
|
|
* |
114
|
|
|
* @return ScopePolicy|null |
115
|
|
|
*/ |
116
|
|
|
private function getForClient(Client $client): ?ScopePolicy |
117
|
|
|
{ |
118
|
|
|
if ($client->has('scope_policy') && $this->has($client->get('scope_policy'))) { |
119
|
|
|
$policyName = $client->get('scope_policy'); |
120
|
|
|
|
121
|
|
|
return $this->get($policyName); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->default(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|