1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015 François Kooman <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage\OAuth; |
20
|
|
|
|
21
|
|
|
use InvalidArgumentException; |
22
|
|
|
|
23
|
|
|
class Scope |
24
|
|
|
{ |
25
|
|
|
/** @var array */ |
26
|
|
|
private $scope; |
27
|
|
|
|
28
|
|
|
public function __construct($scope = null) |
29
|
|
|
{ |
30
|
|
|
if (null === $scope) { |
31
|
|
|
$this->scope = []; |
32
|
|
|
} else { |
33
|
|
|
if (!is_string($scope)) { |
34
|
|
|
throw new InvalidArgumentException('argument must be string'); |
35
|
|
|
} |
36
|
|
|
if (0 === strlen($scope)) { |
37
|
|
|
$this->scope = []; |
38
|
|
|
} else { |
39
|
|
|
$scopeTokens = explode(' ', $scope); |
40
|
|
|
foreach ($scopeTokens as $token) { |
41
|
|
|
$this->validateScopeToken($token); |
42
|
|
|
} |
43
|
|
|
sort($scopeTokens, SORT_STRING); |
44
|
|
|
$this->scope = array_values(array_unique($scopeTokens, SORT_STRING)); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function __toString() |
50
|
|
|
{ |
51
|
|
|
return $this->toString(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function hasScopeToken($scopeToken) |
55
|
|
|
{ |
56
|
|
|
$this->validateScopeToken($scopeToken); |
57
|
|
|
|
58
|
|
|
return in_array($scopeToken, $this->scope); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check if all scope tokens from the provided scope are in this object's |
63
|
|
|
* scope tokens. |
64
|
|
|
* |
65
|
|
|
* @param Scope $scope the scope object to check |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function hasScope(Scope $scope) |
70
|
|
|
{ |
71
|
|
|
foreach ($scope->toArray() as $scopeToken) { |
72
|
|
|
if (!$this->hasScopeToken($scopeToken)) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function toArray() |
81
|
|
|
{ |
82
|
|
|
return $this->scope; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function toString() |
86
|
|
|
{ |
87
|
|
|
return implode(' ', $this->scope); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function validateScopeToken($scopeToken) |
91
|
|
|
{ |
92
|
|
|
if (!is_string($scopeToken) || 0 >= strlen($scopeToken)) { |
93
|
|
|
throw new InvalidArgumentException('scope token must be a non-empty string'); |
94
|
|
|
} |
95
|
|
|
if (1 !== preg_match('/^(?:\x21|[\x23-\x5B]|[\x5D-\x7E])+$/', $scopeToken)) { |
96
|
|
|
throw new InvalidArgumentException('invalid characters in scope token'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|